一个可以给学生改卷子并给出建议的老师对教育辅助平台是很有作用的。我开发了一个可以判题的智能模块,它会分析学生对题目的解答,回答学生的解答是否正确,并向学生给出学习建议。
代码实现
1.接口调用函数
def create_request_param_example(text):
user_id = 'user_test'
task_name = '题目问答'
chat_info = "[{\"type\":\"text\",\"content\":\"你好"+text+"\"}]"
return {'user_id': user_id, 'task_name': task_name, 'chat_info': chat_info}
def do_call(text):
# 构造请求参数
params = create_request_param_example(text)
addXiaopAuthParams(APP_KEY, APP_SECRET, params)
print(params)
# 发送请求
url = 'https://openapi.youdao.com/llmserver/ai/teacher/dialogue/chat'
header = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'text/event-stream'}
response = requests.post(url, data=params, stream=True, headers=header)
# print(response.text)
res=process_response(response.text)
print(res)
return res
-
构造请求参数:
params = create_request_param_example(text)
:调用create_request_param_example
函数,将text
作为参数传入,生成请求参数params
。这个函数根据具体的应用逻辑来构造请求所需的参数。 -
添加身份验证参数:
addXiaopAuthParams(APP_KEY, APP_SECRET, params)
:假设这个函数用于添加身份验证参数到params
中。APP_KEY
和APP_SECRET
是可能在其他地方定义好的认证信息。 -
打印请求参数:
print(params)
:在控制台打印出构造好的请求参数,以便调试和查看。 -
发送请求:
url = 'https://openapi.youdao.com/llmserver/ai/teacher/dialogue/chat'
:设置目标 URL,这是接收 POST 请求的服务端点。header = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'text/event-stream'}
:设置请求头,指定请求的内容类型和接受的响应类型。response = requests.post(url, data=params, stream=True, headers=header)
:使用requests
库发送 POST 请求。data=params
表示将构造好的参数params
作为请求的数据,stream=True
表示响应内容以流的形式返回,headers=header
表示发送请求时使用定义好的请求头。 -
处理响应:
res = process_response(response.text)
:调用process_response
函数处理接收到的响应文本。假设process_response
函数负责解析并处理返回的数据。print(res)
:打印处理后的响应数据,便于调试和查看。 -
返回结果:
return res
:将处理后的响应结果返回给调用者。
2.返回结果处理
接口返回response的是一个事件流字符串,并不直接是回答文本,需要对字符串进行处理提取其中内容。处理函数如下:
def process_response(response_text):
events = response_text.strip().split('\n\n') # Split by double newline to get individual events
text=''
for event in events:
lines = event.strip().split('\n') # Split lines within each event
event_info = {}
for line in lines:
key, value = line.split(':', 1)
event_info[key.strip()] = value.strip()
# Print or process the event information
print(event)
print(f"Event ID: {event_info['id']}")
print(f"Event Type: {event_info['event']}")
# Process data JSON
try:
data = json.loads(event_info['data'])
# print("Data:")
# print(data)
# print(json.dumps(data, indent=2)) # Pretty print JSON data
# print()
if(event_info['event']=="message"):
text=text+data["content"]
except json.JSONDecodeError as e:
print(f"Error decoding JSON data: {e}")
print()
return text
3.提示词设计以及前端调用
在后端notebook.py文件中设计提示词和调用函数。
@note_book.post("/ask_teacher")
async def askTeacher(question:str=Form(...),ans:str=Form(...)):
question=repr(question)
ans=repr(ans)
print(question)
print(ans)
prompt='''
你是一位善于给出建议的老师,而我是学生。
我有一道以markdown格式书写的数学题,并且我给出了解答。
请老师你先独立做完题目,之后,将你的解答和我的解答进行比较,判断我的解答是否正确。
数学题使用+++分隔,我的解答使用---分隔。
+++问题:{question}+++ ---解答:{ans}---
'''.format(question=question,ans=ans)
res=do_call(prompt)
print(res)
return{"data": res,"msg":"ok"}