Spaces:
Paused
Paused
:gem: [Feature] Post request to Conversation Chat API with invocation id
Browse files
apis/chat_api.py
CHANGED
|
@@ -64,6 +64,47 @@ class ChatAPIApp:
|
|
| 64 |
"sec_access_token": creator.sec_access_token,
|
| 65 |
}
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
def setup_routes(self):
|
| 68 |
self.app.get(
|
| 69 |
"/models",
|
|
@@ -75,6 +116,11 @@ class ChatAPIApp:
|
|
| 75 |
summary="Create a conversation session",
|
| 76 |
)(self.create_conversation_session)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
app = ChatAPIApp().app
|
| 80 |
|
|
|
|
| 64 |
"sec_access_token": creator.sec_access_token,
|
| 65 |
}
|
| 66 |
|
| 67 |
+
class ChatPostItem(BaseModel):
|
| 68 |
+
prompt: str = Field(
|
| 69 |
+
default="Hello, who are you?",
|
| 70 |
+
description="(str) Prompt",
|
| 71 |
+
)
|
| 72 |
+
model: str = Field(
|
| 73 |
+
default="precise",
|
| 74 |
+
description="(str) `precise`, `balanced`, `creative`, `precise-offline`, `balanced-offline`, `creative-offline`",
|
| 75 |
+
)
|
| 76 |
+
sec_access_token: str = Field(
|
| 77 |
+
default="",
|
| 78 |
+
description="(str) Sec Access Token",
|
| 79 |
+
)
|
| 80 |
+
client_id: str = Field(
|
| 81 |
+
default="",
|
| 82 |
+
description="(str) Client ID",
|
| 83 |
+
)
|
| 84 |
+
conversation_id: str = Field(
|
| 85 |
+
default="",
|
| 86 |
+
description="(str) Conversation ID",
|
| 87 |
+
)
|
| 88 |
+
invocation_id: int = Field(
|
| 89 |
+
default=0,
|
| 90 |
+
description="(int) Invocation ID",
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
def chat(self, item: ChatPostItem):
|
| 94 |
+
connector = ConversationConnector(
|
| 95 |
+
conversation_style=item.model,
|
| 96 |
+
sec_access_token=item.sec_access_token,
|
| 97 |
+
client_id=item.client_id,
|
| 98 |
+
conversation_id=item.conversation_id,
|
| 99 |
+
invocation_id=item.invocation_id,
|
| 100 |
+
)
|
| 101 |
+
session = ConversationSession(
|
| 102 |
+
conversation_style=item.model,
|
| 103 |
+
connector=connector,
|
| 104 |
+
)
|
| 105 |
+
with session:
|
| 106 |
+
session.chat(prompt=item.prompt)
|
| 107 |
+
|
| 108 |
def setup_routes(self):
|
| 109 |
self.app.get(
|
| 110 |
"/models",
|
|
|
|
| 116 |
summary="Create a conversation session",
|
| 117 |
)(self.create_conversation_session)
|
| 118 |
|
| 119 |
+
self.app.post(
|
| 120 |
+
"/chat",
|
| 121 |
+
summary="Chat in conversation session",
|
| 122 |
+
)(self.chat)
|
| 123 |
+
|
| 124 |
|
| 125 |
app = ChatAPIApp().app
|
| 126 |
|
conversations/conversation_connector.py
CHANGED
|
@@ -15,6 +15,17 @@ http_proxy = "http://localhost:11111" # Replace with yours
|
|
| 15 |
|
| 16 |
|
| 17 |
class ConversationConnector:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def __init__(
|
| 19 |
self,
|
| 20 |
conversation_style: str = "precise",
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
class ConversationConnector:
|
| 18 |
+
"""
|
| 19 |
+
Input params:
|
| 20 |
+
|
| 21 |
+
- `sec_access_token`, `client_id`, `conversation_id`
|
| 22 |
+
- Generated by `ConversationCreator`
|
| 23 |
+
- `invocation_id` (int):
|
| 24 |
+
- For 1st request, this value must be `0`.
|
| 25 |
+
- For all requests after, any integer is valid.
|
| 26 |
+
- To make it simple, use `1` for all requests after the 1st one.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
def __init__(
|
| 30 |
self,
|
| 31 |
conversation_style: str = "precise",
|
conversations/conversation_session.py
CHANGED
|
@@ -33,6 +33,7 @@ class ConversationSession:
|
|
| 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):
|
|
|
|
| 33 |
sec_access_token=self.creator.sec_access_token,
|
| 34 |
client_id=self.creator.client_id,
|
| 35 |
conversation_id=self.creator.conversation_id,
|
| 36 |
+
invocation_id=0,
|
| 37 |
)
|
| 38 |
|
| 39 |
def open(self):
|