Files
study-agent-service/service/agent_service.py
2026-08-02 22:57:36 +08:00

56 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from langchain_core.messages import AIMessageChunk, HumanMessage
from agent.study import question_agent, knowledge_agent, exam_agent
from schemas.agent import QueryQuestionRequest, QueryKnowledgeRequest, QueryExamRequest
def generate_question_agent(query: QueryQuestionRequest):
try:
user_msg = f"学习内容:\n{query.message}"
# 流式调用Agent
for chunk, metadata in question_agent.stream(
{"messages": [HumanMessage(content=user_msg)]},
stream_mode="messages"
):
if isinstance(chunk, AIMessageChunk):
if isinstance(chunk, AIMessageChunk) and chunk.content:
yield chunk.content
except Exception as e:
print(f"\n[错误]: {str(e)}")
yield "信息检索失败,请重新输入问题提问"
def generate_knowledge_agent(query: QueryKnowledgeRequest):
try:
user_msg = f"学科:{query.subject},模块:{query.module} 名称:{query.name}"
# 流式调用Agent
for chunk, metadata in knowledge_agent.stream(
{"messages": [HumanMessage(content=user_msg)]},
stream_mode="messages"
):
if isinstance(chunk, AIMessageChunk):
if isinstance(chunk, AIMessageChunk) and chunk.content:
yield chunk.content
except Exception as e:
print(f"\n[错误]: {str(e)}")
yield "信息检索失败,请重新输入问题提问"
def generate_exam_agent(query: QueryExamRequest):
try:
user_msg = f"学科:{query.subject},题目总数:{query.total}"
# 流式调用Agent
for chunk, metadata in exam_agent.stream(
{"messages": [HumanMessage(content=user_msg)]},
stream_mode="messages"
):
if isinstance(chunk, AIMessageChunk):
if isinstance(chunk, AIMessageChunk) and chunk.content:
yield chunk.content
except Exception as e:
print(f"\n[错误]: {str(e)}")
yield "信息检索失败,请重新输入问题提问"