51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
import json
|
||
|
||
import requests
|
||
|
||
from Config.Config import GLM_API_KEY, GLM_MODEL_NAME, GLM_BASE_URL
|
||
|
||
url = GLM_BASE_URL
|
||
headers = {
|
||
"Authorization": "Bearer " + GLM_API_KEY,
|
||
"Content-Type": "application/json"
|
||
}
|
||
data = {
|
||
"model": GLM_MODEL_NAME,
|
||
"messages": [
|
||
{
|
||
"role": "user",
|
||
# "content": "做水煮鱼一般用什么鱼"
|
||
"content":
|
||
[
|
||
{
|
||
"type": "text",
|
||
"text": "请帮我解决这个题目,给出详细过程和答案。第4问请注意,F可能在三角形内部,也可能出现在BC的外侧,即F在三角形外两种情况,需要分类讨论作答。"
|
||
},
|
||
{
|
||
"type": "image_url",
|
||
"image_url": {
|
||
"url": "https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/Backup/23.jpg"
|
||
}
|
||
}
|
||
]
|
||
}
|
||
],
|
||
"stream": True # 启用流式调用
|
||
}
|
||
|
||
with requests.post(url, headers=headers, json=data, stream=True) as response:
|
||
for chunk in response.iter_lines():
|
||
if chunk:
|
||
decoded = chunk.decode('utf-8')
|
||
if decoded.startswith('[DONE]'):
|
||
print("完成!")
|
||
break
|
||
try:
|
||
decoded = decoded[5:]
|
||
json_data = json.loads(decoded)
|
||
content = json_data["choices"][0]["delta"]['content']
|
||
if content and len(content) > 0:
|
||
print(content, end="")
|
||
except Exception as e:
|
||
print(e)
|