Files
dsProject/dsLightRag/Math/T1_Glm_Train.py
2025-08-14 15:45:08 +08:00

58 lines
1.8 KiB
Python

import json
import requests
reasoning_content = "" # 定义完整思考过程
answer_content = "" # 定义完整回复
is_answering = False # 判断是否结束思考过程并开始回复
prompt = """
你是一个严谨的数学描述专家,帮我仔细查看给定的初中数学数学题目,整理出题目内容,答案,解析等内容,要详细,不要遗漏信息。
"""
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":
[
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": "https://hzkc.oss-cn-beijing.aliyuncs.com/HuangHai/24.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)