XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Update Bing.py

Removed unnecessary check that could prematurely stop the AI's response while answering

e2e2f281
Luneye <73485421+Luneye@users.noreply.github.com>
提交于

代码差异

1 个文件 +76 -46
Modified g4f/Provider/Bing.py +76 -46
@@ -8,33 +8,65 @@ import asyncio
8 8 from aiohttp import ClientSession
9 9
10 10 from ..typing import Any, AsyncGenerator, CreateResult, Union
11 from .base_provider import AsyncGeneratorProvider, get_cookies
11 from .base_provider import BaseProvider
12 12
13 class Bing(AsyncGeneratorProvider):
13 class Bing(BaseProvider):
14 14 url = "https://bing.com/chat"
15 needs_auth = True
16 working = True
17 15 supports_gpt_4 = True
16 working=True
18 17 supports_stream=True
19
18
20 19 @staticmethod
21 def create_async_generator(
22 model: str,
23 messages: list[dict[str, str]],
24 cookies: dict = get_cookies(".bing.com"),
25 **kwargs
26 ) -> AsyncGenerator:
27 if len(messages) < 2:
28 prompt = messages[0]["content"]
29 context = None
30
31 else:
32 prompt = messages[-1]["content"]
33 context = create_context(messages[:-1])
34
35 return stream_generate(prompt, context, cookies)
36
37 def create_context(messages: list[dict[str, str]]):
20 def create_completion(
21 model: str,
22 messages: list[dict[str, str]],
23 stream: bool,
24 **kwargs: Any
25 ) -> CreateResult:
26 yield from run(create(messages, **kwargs))
27
28 def create(
29 messages: list[dict[str, str]],
30 cookies: dict = {}
31 ):
32 if len(messages) < 2:
33 prompt = messages[0]["content"]
34 context = None
35
36 else:
37 prompt = messages[-1]["content"]
38 context = convert(messages[:-1])
39
40 if not cookies:
41 cookies = {
42 'MUID': '',
43 'BCP': '',
44 'MUIDB': '',
45 'USRLOC': '',
46 'SRCHD': 'AF=hpcodx',
47 'MMCASM': '',
48 '_UR': '',
49 'ANON': '',
50 'NAP': '',
51 'ABDEF': '',
52 'PPLState': '1',
53 'KievRPSSecAuth': '',
54 '_U': '',
55 'SUID': '',
56 '_EDGE_S': '',
57 'WLS': '',
58 '_HPVN': '',
59 '_SS': '',
60 '_clck': '',
61 'SRCHUSR': '',
62 '_RwBf': '',
63 'SRCHHPGUSR': '',
64 'ipv6': '',
65 }
66
67 return stream_generate(prompt, context, cookies)
68
69 def convert(messages: list[dict[str, str]]):
38 70 context = ""
39 71
40 72 for message in messages:
@@ -159,32 +191,34 @@ class Defaults:
159 191 'x-forwarded-for': ip_address,
160 192 }
161 193
162 optionsSets = [
163 'saharasugg',
164 'enablenewsfc',
165 'clgalileo',
166 'gencontentv3',
167 "nlu_direct_response_filter",
168 "deepleo",
169 "disable_emoji_spoken_text",
170 "responsible_ai_policy_235",
171 "enablemm",
172 "h3precise"
173 "dtappid",
174 "cricinfo",
175 "cricinfov2",
176 "dv3sugg",
177 "nojbfedge"
178 ]
194 optionsSets = {
195 "optionsSets": [
196 'saharasugg',
197 'enablenewsfc',
198 'clgalileo',
199 'gencontentv3',
200 "nlu_direct_response_filter",
201 "deepleo",
202 "disable_emoji_spoken_text",
203 "responsible_ai_policy_235",
204 "enablemm",
205 "h3precise"
206 "dtappid",
207 "cricinfo",
208 "cricinfov2",
209 "dv3sugg",
210 "nojbfedge"
211 ]
212 }
179 213
180 def format_message(message: dict) -> str:
181 return json.dumps(message, ensure_ascii=False) + Defaults.delimiter
214 def format_message(msg: dict) -> str:
215 return json.dumps(msg, ensure_ascii=False) + Defaults.delimiter
182 216
183 217 def create_message(conversation: Conversation, prompt: str, context: str=None) -> str:
184 218 struct = {
185 219 'arguments': [
186 220 {
187 'optionsSets': Defaults.optionsSets,
221 **Defaults.optionsSets,
188 222 'source': 'cib',
189 223 'allowedMessageTypes': Defaults.allowedMessageTypes,
190 224 'sliceIds': Defaults.sliceIds,
@@ -264,10 +298,6 @@ async def stream_generate(
264 298 response_txt += inline_txt + '\n'
265 299 result_text += inline_txt + '\n'
266 300
267 if returned_text.endswith(' '):
268 final = True
269 break
270
271 301 if response_txt.startswith(returned_text):
272 302 new = response_txt[len(returned_text):]
273 303 if new != "\n":