-
Notifications
You must be signed in to change notification settings - Fork 1
/
KwooVa_api_demo.py
187 lines (162 loc) · 4.91 KB
/
KwooVa_api_demo.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import math
import json
import shutil
from tqdm import tqdm
import requests
import websocket
import json
import time
# 从环境中获取访问密钥
ACCESS_TOKEN = os.getenv('ACCESS_TOKEN')
if not ACCESS_TOKEN:
raise Exception("请设置环境变量 ACCESS_TOKEN")
BASE_URL = 'https://api.tgkwai.com'
# 上传附件
def upload_attachment(file_path):
"""
上传附件到服务器
Args:
file_path (str): 上传图片的本地路径
Raises:
Exception: 上传失败的报错信息
Returns:
str: 图片上传到服务器后的URL
"""
url = f"{BASE_URL}/api/v1/attachments/upload"
headers = {
'X-Token': f'Bearer {ACCESS_TOKEN}'
}
files = {
'type': (None, 'image'),
'file': open(file_path, 'rb')
}
response = requests.post(url, headers=headers, files=files)
response_data = response.json()
if response_data['code'] == 200:
return response_data['data']
else:
raise Exception(f"上传附件失败: {response_data['msg']}")
# 创建WebSocket连接
def create_websocket_connection(session_id=None):
"""
创建WebSocket连接
Args:
session_id (str): 会话ID,可选
Raises:
Exception: 创建连接失败的报错信息
Returns:
tuple: WebSocket对象和会话ID
"""
ws_url = f"wss://api.tgkwai.com/api/v1/qamodel/session?x-token={ACCESS_TOKEN}"
if session_id:
ws_url += f"&session_id={session_id}"
# 由于存在跨域限制,因此建立连接时不能携带Origin头
ws = websocket.create_connection(ws_url, suppress_origin=True)
result = ws.recv()
response_data = json.loads(result)
if response_data['code'] == 200:
return ws, response_data['data']
else:
raise Exception(f"创建WebSocket连接失败: {response_data['msg']}")
# 发送消息
def send_message(session_id, messages, stream=True):
"""
发送消息到WebSocket连接
Args:
session_id (str): 会话ID
messages (list): 消息列表
stream (bool): 是否流式输出
"""
ws, session_id = create_websocket_connection(session_id)
payload = {
"session_id": session_id,
"messages": messages,
"stream": stream
}
ws.send(json.dumps(payload))
response_content = ""
while True:
result = ws.recv()
response_data = json.loads(result)
if response_data['data']['is_finished']:
break
print(response_data)
response_content += response_data['data']['content']
ws.close()
print("大模型回复:", response_content)
# 将服务器的回复添加到历史记录中
messages.append({
"role": "assistant",
"type": "text",
"content": response_content
})
return session_id
# 删除附件
def delete_attachment(file_path):
"""
删除服务器上的附件
Args:
file_path (str): 附件路径
Raises:
Exception: 删除失败的报错信息
"""
url = f"{BASE_URL}/api/v1/attachments/delete"
headers = {
'X-Token': f'Bearer {ACCESS_TOKEN}'
}
params = {
'file_path': file_path
}
response = requests.delete(url, headers=headers, params=params)
response_data = response.json()
if response_data['code'] != 200:
raise Exception(f"删除附件失败: {response_data['msg']}")
def test():
try:
# 上传附件
file_path = 'D:\\Development\\tgkwai\\test_image.jpg'
attachment_url = upload_attachment(file_path)
print(f"附件上传成功: {attachment_url}")
# 初始化会话ID和历史记录
session_id = None
history = []
# 发送第一条消息
messages = [
{
"role": "user",
"type": "multimodal",
"content": "这张图片里的作物得了什么疾病?",
"attachments": [
{ "type": "image", "url": attachment_url }
]
}
]
history.extend(messages)
session_id = send_message(session_id, history)
# 进行多轮对话
additional_messages = [
{
"role": "user",
"type": "text",
"content": "上述病症主要感染作物有哪些?"
},
{
"role": "user",
"type": "text",
"content": "该如何防治这种病害?"
}
]
for message in additional_messages:
history.append(message)
session_id = send_message(session_id, history)
# 删除附件
delete_attachment(attachment_url)
print("附件删除成功")
print("\n完整的对话历史:", json.dumps(history, ensure_ascii=False))
except Exception as e:
print(f"发生错误: {e}")
def main():
test()
if __name__ == '__main__':
main()