Spaces:
Paused
Paused
:recycle: [Refactor] Build Connector from params
Browse files- apis/chat_api.py +17 -12
- conversations/conversation_connector.py +2 -2
- conversations/conversation_session.py +16 -8
apis/chat_api.py
CHANGED
|
@@ -3,7 +3,11 @@ import uvicorn
|
|
| 3 |
from fastapi import FastAPI, APIRouter, WebSocket, WebSocketDisconnect
|
| 4 |
from fastapi.routing import APIRoute
|
| 5 |
from pydantic import BaseModel, Field
|
| 6 |
-
from conversations import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
class ChatAPIApp:
|
|
@@ -18,27 +22,27 @@ class ChatAPIApp:
|
|
| 18 |
def get_available_models(self):
|
| 19 |
self.available_models = [
|
| 20 |
{
|
| 21 |
-
"id": "
|
| 22 |
"description": "Bing (Precise): Concise and straightforward.",
|
| 23 |
},
|
| 24 |
{
|
| 25 |
-
"id": "
|
| 26 |
"description": "Bing (Balanced): Informative and friendly.",
|
| 27 |
},
|
| 28 |
{
|
| 29 |
-
"id": "
|
| 30 |
"description": "Bing (Creative): Original and imaginative.",
|
| 31 |
},
|
| 32 |
{
|
| 33 |
-
"id": "
|
| 34 |
"description": "Bing (Precise): (No Internet) Concise and straightforward.",
|
| 35 |
},
|
| 36 |
{
|
| 37 |
-
"id": "
|
| 38 |
"description": "Bing (Balanced): (No Internet) Informative and friendly.",
|
| 39 |
},
|
| 40 |
{
|
| 41 |
-
"id": "
|
| 42 |
"description": "Bing (Creative): (No Internet) Original and imaginative.",
|
| 43 |
},
|
| 44 |
]
|
|
@@ -51,12 +55,13 @@ class ChatAPIApp:
|
|
| 51 |
)
|
| 52 |
|
| 53 |
def create_conversation_session(self, item: CreateConversationSessionPostItem):
|
| 54 |
-
|
| 55 |
-
|
| 56 |
return {
|
| 57 |
-
"
|
| 58 |
-
"
|
| 59 |
-
"
|
|
|
|
| 60 |
}
|
| 61 |
|
| 62 |
def setup_routes(self):
|
|
|
|
| 3 |
from fastapi import FastAPI, APIRouter, WebSocket, WebSocketDisconnect
|
| 4 |
from fastapi.routing import APIRoute
|
| 5 |
from pydantic import BaseModel, Field
|
| 6 |
+
from conversations import (
|
| 7 |
+
ConversationConnector,
|
| 8 |
+
ConversationCreator,
|
| 9 |
+
ConversationSession,
|
| 10 |
+
)
|
| 11 |
|
| 12 |
|
| 13 |
class ChatAPIApp:
|
|
|
|
| 22 |
def get_available_models(self):
|
| 23 |
self.available_models = [
|
| 24 |
{
|
| 25 |
+
"id": "precise",
|
| 26 |
"description": "Bing (Precise): Concise and straightforward.",
|
| 27 |
},
|
| 28 |
{
|
| 29 |
+
"id": "balanced",
|
| 30 |
"description": "Bing (Balanced): Informative and friendly.",
|
| 31 |
},
|
| 32 |
{
|
| 33 |
+
"id": "creative",
|
| 34 |
"description": "Bing (Creative): Original and imaginative.",
|
| 35 |
},
|
| 36 |
{
|
| 37 |
+
"id": "precise-offline",
|
| 38 |
"description": "Bing (Precise): (No Internet) Concise and straightforward.",
|
| 39 |
},
|
| 40 |
{
|
| 41 |
+
"id": "balanced-offline",
|
| 42 |
"description": "Bing (Balanced): (No Internet) Informative and friendly.",
|
| 43 |
},
|
| 44 |
{
|
| 45 |
+
"id": "creative-offline",
|
| 46 |
"description": "Bing (Creative): (No Internet) Original and imaginative.",
|
| 47 |
},
|
| 48 |
]
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
def create_conversation_session(self, item: CreateConversationSessionPostItem):
|
| 58 |
+
creator = ConversationCreator()
|
| 59 |
+
creator.create()
|
| 60 |
return {
|
| 61 |
+
"model": item.model,
|
| 62 |
+
"conversation_id": creator.conversation_id,
|
| 63 |
+
"client_id": creator.client_id,
|
| 64 |
+
"sec_access_token": creator.sec_access_token,
|
| 65 |
}
|
| 66 |
|
| 67 |
def setup_routes(self):
|
conversations/conversation_connector.py
CHANGED
|
@@ -40,7 +40,7 @@ class ConversationConnector:
|
|
| 40 |
await self.wss.receive_str()
|
| 41 |
await self.wss_send({"type": 6})
|
| 42 |
|
| 43 |
-
async def
|
| 44 |
self.quotelized_sec_access_token = urllib.parse.quote(self.sec_access_token)
|
| 45 |
self.ws_url = (
|
| 46 |
f"wss://sydney.bing.com/sydney/ChatHub"
|
|
@@ -67,7 +67,7 @@ class ConversationConnector:
|
|
| 67 |
await self.wss_send(self.connect_request_payload)
|
| 68 |
|
| 69 |
async def stream_chat(self, prompt=""):
|
| 70 |
-
await self.
|
| 71 |
await self.send_chathub_request(prompt)
|
| 72 |
message_parser = MessageParser()
|
| 73 |
while not self.wss.closed:
|
|
|
|
| 40 |
await self.wss.receive_str()
|
| 41 |
await self.wss_send({"type": 6})
|
| 42 |
|
| 43 |
+
async def connect(self):
|
| 44 |
self.quotelized_sec_access_token = urllib.parse.quote(self.sec_access_token)
|
| 45 |
self.ws_url = (
|
| 46 |
f"wss://sydney.bing.com/sydney/ChatHub"
|
|
|
|
| 67 |
await self.wss_send(self.connect_request_payload)
|
| 68 |
|
| 69 |
async def stream_chat(self, prompt=""):
|
| 70 |
+
await self.connect()
|
| 71 |
await self.send_chathub_request(prompt)
|
| 72 |
message_parser = MessageParser()
|
| 73 |
while not self.wss.closed:
|
conversations/conversation_session.py
CHANGED
|
@@ -4,8 +4,15 @@ from utils.logger import logger
|
|
| 4 |
|
| 5 |
|
| 6 |
class ConversationSession:
|
| 7 |
-
def __init__(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
self.conversation_style = conversation_style
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def __enter__(self):
|
| 11 |
self.open()
|
|
@@ -19,15 +26,16 @@ class ConversationSession:
|
|
| 19 |
self.creator.create()
|
| 20 |
|
| 21 |
def connect(self):
|
| 22 |
-
self.connector
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def open(self):
|
| 30 |
-
self.create()
|
| 31 |
self.connect()
|
| 32 |
self.event_loop = asyncio.new_event_loop()
|
| 33 |
asyncio.set_event_loop(self.event_loop)
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
class ConversationSession:
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
conversation_style: str = "precise",
|
| 10 |
+
creator=None,
|
| 11 |
+
connector=None,
|
| 12 |
+
):
|
| 13 |
self.conversation_style = conversation_style
|
| 14 |
+
self.creator = creator
|
| 15 |
+
self.connector = connector
|
| 16 |
|
| 17 |
def __enter__(self):
|
| 18 |
self.open()
|
|
|
|
| 26 |
self.creator.create()
|
| 27 |
|
| 28 |
def connect(self):
|
| 29 |
+
if self.connector is None:
|
| 30 |
+
self.create()
|
| 31 |
+
self.connector = ConversationConnector(
|
| 32 |
+
conversation_style=self.conversation_style,
|
| 33 |
+
sec_access_token=self.creator.sec_access_token,
|
| 34 |
+
client_id=self.creator.client_id,
|
| 35 |
+
conversation_id=self.creator.conversation_id,
|
| 36 |
+
)
|
| 37 |
|
| 38 |
def open(self):
|
|
|
|
| 39 |
self.connect()
|
| 40 |
self.event_loop = asyncio.new_event_loop()
|
| 41 |
asyncio.set_event_loop(self.event_loop)
|