Hansimov commited on
Commit
1828807
1 Parent(s): c32808f

:gem: [Feature] API: Create Conversation Session and return unique ids

Browse files
apis/chat_api.py CHANGED
@@ -44,26 +44,31 @@ class ChatAPIApp:
44
  ]
45
  return self.available_models
46
 
47
- async def create_conversation_session(
48
- self, websocket: WebSocket, conversation_style="precise"
49
- ):
50
- await websocket.accept()
51
- conversation_session = ConversationSession(conversation_style)
52
- conversation_session.open()
53
- while True:
54
- try:
55
- data = await websocket.receive_text()
56
- response = await conversation_session.chat(data)
57
- await websocket.send_text(response)
58
- except Exception as e:
59
- print(e)
60
- break
61
 
62
  def setup_routes(self):
63
- self.router = APIRouter()
64
- self.router.add_api_route("/models", self.get_available_models)
65
- self.router.add_websocket_route("/create", self.create_conversation_session)
66
- self.app.include_router(self.router)
 
 
 
 
 
67
 
68
 
69
  app = ChatAPIApp().app
 
44
  ]
45
  return self.available_models
46
 
47
+ class CreateConversationSessionPostItem(BaseModel):
48
+ model: str = Field(
49
+ default="precise",
50
+ description="(str) `precise`, `balanced`, `creative`, `precise-offline`, `balanced-offline`, `creative-offline`",
51
+ )
52
+
53
+ def create_conversation_session(self, item: CreateConversationSessionPostItem):
54
+ session = ConversationSession(item.model)
55
+ session.open()
56
+ return {
57
+ "conversation_id": session.connector.conversation_id,
58
+ "client_id": session.connector.client_id,
59
+ "sec_access_token": session.connector.sec_access_token,
60
+ }
61
 
62
  def setup_routes(self):
63
+ self.app.get(
64
+ "/models",
65
+ summary="Get available models",
66
+ )(self.get_available_models)
67
+
68
+ self.app.post(
69
+ "/create",
70
+ summary="Create a conversation session",
71
+ )(self.create_conversation_session)
72
 
73
 
74
  app = ChatAPIApp().app
conversations/conversation_session.py CHANGED
@@ -29,16 +29,17 @@ class ConversationSession:
29
  def open(self):
30
  self.create()
31
  self.connect()
32
- self.event_loop = asyncio.get_event_loop()
 
33
 
34
  def close(self):
35
  self.event_loop.close()
36
 
37
- async def chat(self, prompt):
38
  logger.success(f"\n[User]: ", end="")
39
  logger.mesg(f"{prompt}")
40
  logger.success(f"[Bing]:")
41
- return await self.connector.stream_chat(prompt=prompt)
42
 
43
 
44
  if __name__ == "__main__":
 
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)
34
 
35
  def close(self):
36
  self.event_loop.close()
37
 
38
+ def chat(self, prompt):
39
  logger.success(f"\n[User]: ", end="")
40
  logger.mesg(f"{prompt}")
41
  logger.success(f"[Bing]:")
42
+ self.event_loop.run_until_complete(self.connector.stream_chat(prompt=prompt))
43
 
44
 
45
  if __name__ == "__main__":