Spaces:
Sleeping
Sleeping
sanbo
commited on
Commit
·
a4633a8
1
Parent(s):
cc873a6
update sth. at 2024-11-15 19:31:13
Browse files
app.py
CHANGED
@@ -36,49 +36,28 @@ def chat_with_model(messages):
|
|
36 |
return "聊天生成失败,请稍后再试。"
|
37 |
|
38 |
# ---------- chatgpt-4o-mini 模块 ----------
|
39 |
-
def chatgpt_4o_mini(messages):
|
40 |
-
"""
|
41 |
-
调用 gpt-4o-mini API 进行对话,并解析流式响应。
|
42 |
-
"""
|
43 |
-
try:
|
44 |
-
# 构建请求数据
|
45 |
-
data = {
|
46 |
-
"model": "gpt-4o-mini",
|
47 |
-
"messages": [{"role": "system", "content": "你是一个辅助机器人"}] + messages,
|
48 |
-
"stream": True
|
49 |
-
}
|
50 |
-
|
51 |
-
# 发送请求
|
52 |
-
url = 'https://sanbo1200-duck2api.hf.space/completions'
|
53 |
-
headers = {'Content-Type': 'application/json'}
|
54 |
-
|
55 |
-
# 使用 requests 发送 POST 请求
|
56 |
-
response = requests.post(url, headers=headers, json=data, stream=True)
|
57 |
-
|
58 |
-
# 检查响应状态码
|
59 |
-
if response.status_code != 200:
|
60 |
-
return f"请求失败,状态码:{response.status_code}"
|
61 |
-
|
62 |
-
# 处理流式响应
|
63 |
-
full_response = ""
|
64 |
-
for line in response.iter_lines():
|
65 |
-
if line:
|
66 |
-
# 解析每一行的内容
|
67 |
-
try:
|
68 |
-
chunk = json.loads(line.decode('utf-8'))
|
69 |
-
# 解析返回的每个 chunk
|
70 |
-
if 'choices' in chunk and len(chunk['choices']) > 0:
|
71 |
-
content = chunk['choices'][0]['delta'].get('content', '')
|
72 |
-
full_response += content
|
73 |
-
except Exception as e:
|
74 |
-
print(f"Error parsing response chunk: {e}")
|
75 |
-
|
76 |
-
# 返回拼接后的完整对话内容
|
77 |
-
return full_response.strip()
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
# ---------- 图像生成模块 ----------
|
84 |
def image_gen(prompt):
|
|
|
36 |
return "聊天生成失败,请稍后再试。"
|
37 |
|
38 |
# ---------- chatgpt-4o-mini 模块 ----------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
def chatgpt_4o_mini(Query):
|
41 |
+
url = 'https://sanbo1200-duck2api.hf.space/completions'
|
42 |
+
headers = {'Content-Type': 'application/json'}
|
43 |
+
data = {
|
44 |
+
"model": "gpt-4o-mini",
|
45 |
+
"messages": [
|
46 |
+
{"role": "system", "content": "你是一个辅助机器人"},
|
47 |
+
{"role": "user", "content": Query}
|
48 |
+
],
|
49 |
+
"stream": False
|
50 |
+
}
|
51 |
+
|
52 |
+
# 发起 HTTP 请求
|
53 |
+
response = requests.post(url, json=data, headers=headers, stream=True)
|
54 |
+
response.encoding = 'utf-8'
|
55 |
+
if response.status_code!= 200:
|
56 |
+
return "请求失败"
|
57 |
+
else:
|
58 |
+
json_data = response.json()
|
59 |
+
return json_data['choices'][0]['message']['content']
|
60 |
+
|
61 |
|
62 |
# ---------- 图像生成模块 ----------
|
63 |
def image_gen(prompt):
|
req.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
|
4 |
+
def chatgpt_4o_mini(Query):
|
5 |
+
url = 'https://sanbo1200-duck2api.hf.space/completions'
|
6 |
+
headers = {'Content-Type': 'application/json'}
|
7 |
+
data = {
|
8 |
+
"model": "gpt-4o-mini",
|
9 |
+
"messages": [
|
10 |
+
{"role": "system", "content": "你是一个辅助机器人"},
|
11 |
+
{"role": "user", "content": Query}
|
12 |
+
],
|
13 |
+
"stream": False
|
14 |
+
}
|
15 |
+
|
16 |
+
# 发起 HTTP 请求
|
17 |
+
response = requests.post(url, json=data, headers=headers, stream=True)
|
18 |
+
response.encoding = 'utf-8'
|
19 |
+
if response.status_code!= 200:
|
20 |
+
return "请求失败"
|
21 |
+
else:
|
22 |
+
json_data = response.json()
|
23 |
+
return json_data['choices'][0]['message']['content']
|
24 |
+
|
25 |
+
# 获取回答
|
26 |
+
response = chatgpt_4o_mini('你的知识库最后什么日期')
|
27 |
+
|
28 |
+
# 输出回答
|
29 |
+
print(response)
|