summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/Bing.py
diff options
context:
space:
mode:
authorHeiner Lohaus <heiner@lohaus.eu>2023-10-02 22:43:36 +0200
committerHeiner Lohaus <heiner@lohaus.eu>2023-10-02 22:43:36 +0200
commit252f595b6a1f0bd4068c1f2becd5a932e3345bb7 (patch)
tree0bd7eb92764d6ac806ed14c4ccdc6fc8866138aa /g4f/Provider/Bing.py
parentAdd Phind Provider (diff)
downloadgpt4free-252f595b6a1f0bd4068c1f2becd5a932e3345bb7.tar
gpt4free-252f595b6a1f0bd4068c1f2becd5a932e3345bb7.tar.gz
gpt4free-252f595b6a1f0bd4068c1f2becd5a932e3345bb7.tar.bz2
gpt4free-252f595b6a1f0bd4068c1f2becd5a932e3345bb7.tar.lz
gpt4free-252f595b6a1f0bd4068c1f2becd5a932e3345bb7.tar.xz
gpt4free-252f595b6a1f0bd4068c1f2becd5a932e3345bb7.tar.zst
gpt4free-252f595b6a1f0bd4068c1f2becd5a932e3345bb7.zip
Diffstat (limited to '')
-rw-r--r--g4f/Provider/Bing.py78
1 files changed, 45 insertions, 33 deletions
diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py
index e4e56519..940b3a0b 100644
--- a/g4f/Provider/Bing.py
+++ b/g4f/Provider/Bing.py
@@ -6,8 +6,12 @@ import os
import urllib.parse
from aiohttp import ClientSession, ClientTimeout
from ..typing import AsyncGenerator
-from .base_provider import AsyncGeneratorProvider, get_cookies
+from .base_provider import AsyncGeneratorProvider
+class Tones():
+ creative = "Creative"
+ balanced = "Balanced"
+ precise = "Precise"
class Bing(AsyncGeneratorProvider):
url = "https://bing.com/chat"
@@ -16,12 +20,13 @@ class Bing(AsyncGeneratorProvider):
@staticmethod
def create_async_generator(
- model: str,
- messages: list[dict[str, str]],
- cookies: dict = None, **kwargs) -> AsyncGenerator:
+ model: str,
+ messages: list[dict[str, str]],
+ cookies: dict = None,
+ tone: str = Tones.creative,
+ **kwargs
+ ) -> AsyncGenerator:
- if not cookies:
- cookies = get_cookies(".bing.com")
if len(messages) < 2:
prompt = messages[0]["content"]
context = None
@@ -38,7 +43,7 @@ class Bing(AsyncGeneratorProvider):
'SRCHUSR' : '',
'SRCHHPGUSR' : '',
}
- return stream_generate(prompt, context, cookies)
+ return stream_generate(prompt, tone, context, cookies)
def create_context(messages: list[dict[str, str]]):
context = "".join(f"[{message['role']}](#message)\n{message['content']}\n\n" for message in messages)
@@ -64,6 +69,14 @@ async def create_conversation(session: ClientSession) -> Conversation:
return Conversation(conversationId, clientId, conversationSignature)
+async def retry_conversation(session: ClientSession) -> Conversation:
+ for _ in range(5):
+ try:
+ return await create_conversation(session)
+ except:
+ session.cookie_jar.clear()
+ return await create_conversation(session)
+
async def list_conversations(session: ClientSession) -> list:
url = "https://www.bing.com/turing/conversation/chats"
async with session.get(url) as response:
@@ -162,34 +175,32 @@ class Defaults:
'x-forwarded-for': ip_address,
}
- optionsSets = {
- "optionsSets": [
- 'saharasugg',
- 'enablenewsfc',
- 'clgalileo',
- 'gencontentv3',
- "nlu_direct_response_filter",
- "deepleo",
- "disable_emoji_spoken_text",
- "responsible_ai_policy_235",
- "enablemm",
- "h3precise"
- "dtappid",
- "cricinfo",
- "cricinfov2",
- "dv3sugg",
- "nojbfedge"
- ]
- }
+ optionsSets = [
+ 'saharasugg',
+ 'enablenewsfc',
+ 'clgalileo',
+ 'gencontentv3',
+ "nlu_direct_response_filter",
+ "deepleo",
+ "disable_emoji_spoken_text",
+ "responsible_ai_policy_235",
+ "enablemm",
+ "h3precise"
+ "dtappid",
+ "cricinfo",
+ "cricinfov2",
+ "dv3sugg",
+ "nojbfedge"
+ ]
def format_message(msg: dict) -> str:
return json.dumps(msg, ensure_ascii=False) + Defaults.delimiter
-def create_message(conversation: Conversation, prompt: str, context: str=None) -> str:
+def create_message(conversation: Conversation, prompt: str, tone: str, context: str=None) -> str:
struct = {
'arguments': [
{
- **Defaults.optionsSets,
+ 'optionsSets': Defaults.optionsSets,
'source': 'cib',
'allowedMessageTypes': Defaults.allowedMessageTypes,
'sliceIds': Defaults.sliceIds,
@@ -201,6 +212,7 @@ def create_message(conversation: Conversation, prompt: str, context: str=None) -
'text': prompt,
'messageType': 'Chat'
},
+ 'tone': tone,
'conversationSignature': conversation.conversationSignature,
'participant': {
'id': conversation.clientId
@@ -225,15 +237,16 @@ def create_message(conversation: Conversation, prompt: str, context: str=None) -
async def stream_generate(
prompt: str,
+ tone: str,
context: str=None,
- cookies: dict=None
+ cookies: dict=None,
):
async with ClientSession(
timeout=ClientTimeout(total=900),
cookies=cookies,
headers=Defaults.headers,
) as session:
- conversation = await create_conversation(session)
+ conversation = await retry_conversation(session)
try:
async with session.ws_connect(
'wss://sydney.bing.com/sydney/ChatHub',
@@ -243,7 +256,7 @@ async def stream_generate(
await wss.send_str(format_message({'protocol': 'json', 'version': 1}))
msg = await wss.receive(timeout=900)
- await wss.send_str(create_message(conversation, prompt, context))
+ await wss.send_str(create_message(conversation, prompt, tone, context))
response_txt = ''
returned_text = ''
@@ -281,7 +294,6 @@ async def stream_generate(
result = response['item']['result']
if result.get('error'):
raise Exception(f"{result['value']}: {result['message']}")
- final = True
- break
+ return
finally:
await delete_conversation(session, conversation) \ No newline at end of file