File size: 2,399 Bytes
60bb91e
 
c32808f
 
60bb91e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c32808f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60bb91e
c32808f
 
 
 
60bb91e
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import uvicorn

from fastapi import FastAPI, APIRouter, WebSocket, WebSocketDisconnect
from fastapi.routing import APIRoute
from pydantic import BaseModel, Field
from conversations import ConversationSession


class ChatAPIApp:
    def __init__(self):
        self.app = FastAPI(
            docs_url="/",
            title="Bing Chat API",
            version="1.0",
        )
        self.setup_routes()

    def get_available_models(self):
        self.available_models = [
            {
                "id": "bing-precise",
                "description": "Bing (Precise): Concise and straightforward.",
            },
            {
                "id": "bing-balanced",
                "description": "Bing (Balanced): Informative and friendly.",
            },
            {
                "id": "bing-creative",
                "description": "Bing (Creative): Original and imaginative.",
            },
            {
                "id": "bing-precise-offline",
                "description": "Bing (Precise): (No Internet) Concise and straightforward.",
            },
            {
                "id": "bing-balanced-offline",
                "description": "Bing (Balanced): (No Internet) Informative and friendly.",
            },
            {
                "id": "bing-creative-offline",
                "description": "Bing (Creative): (No Internet) Original and imaginative.",
            },
        ]
        return self.available_models

    async def create_conversation_session(
        self, websocket: WebSocket, conversation_style="precise"
    ):
        await websocket.accept()
        conversation_session = ConversationSession(conversation_style)
        conversation_session.open()
        while True:
            try:
                data = await websocket.receive_text()
                response = await conversation_session.chat(data)
                await websocket.send_text(response)
            except Exception as e:
                print(e)
                break

    def setup_routes(self):
        self.router = APIRouter()
        self.router.add_api_route("/models", self.get_available_models)
        self.router.add_websocket_route("/create", self.create_conversation_session)
        self.app.include_router(self.router)


app = ChatAPIApp().app

if __name__ == "__main__":
    uvicorn.run("__main__:app", host="0.0.0.0", port=22222, reload=True)