Spaces:
Runtime error
Runtime error
:gem: [Feature] API: Create Conversation Session and return unique ids
Browse files- apis/chat_api.py +23 -18
- conversations/conversation_session.py +4 -3
apis/chat_api.py
CHANGED
@@ -44,26 +44,31 @@ class ChatAPIApp:
|
|
44 |
]
|
45 |
return self.available_models
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
|
62 |
def setup_routes(self):
|
63 |
-
self.
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
33 |
|
34 |
def close(self):
|
35 |
self.event_loop.close()
|
36 |
|
37 |
-
|
38 |
logger.success(f"\n[User]: ", end="")
|
39 |
logger.mesg(f"{prompt}")
|
40 |
logger.success(f"[Bing]:")
|
41 |
-
|
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__":
|