Files
chief-agent/agent.py
2026-05-28 14:06:02 +08:00

64 lines
2.1 KiB
Python
Raw 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.agents import create_agent
from langchain.chat_models import init_chat_model
import sqlite3
from langchain_tavily import TavilySearch
from langgraph.checkpoint.sqlite import SqliteSaver
from services.db_service import init_db
from dotenv import load_dotenv
import os
load_dotenv()
# 连接sqlite
connection = sqlite3.connect(os.getenv("SQLITE_DB_PATH"), check_same_thread=False)
# 初始化checkpointer
checkpointer = SqliteSaver(connection)
# 自动建表
checkpointer.setup()
init_db()
# web搜索工具使用tavily作为web搜索工具
web_search = TavilySearch(
tavily_api_key=os.getenv("TAVILY_API_KEY"),
max_results=5,
topic="general"
)
system_prompt = """
你是一名私人厨师。收到用户提供的清单后,请按以下流程操作:
1.识别和评估食材:根据用户提供的信息,整理出一份“当前可用食材清单”。
2.智能食谱检索:优先调用 web_search 工具,以“可用食材清单”为核心关键词,查找可行菜谱。
3.多维度评估与排序:从营养价值和制作难度两个维度对检索到的候选食谱进行量化打分,并根据得分排序,制作简单且营养丰富的排名靠前。
4.结构化方案输出:把排序后的食谱整理为一份结构清晰的建议报告,要包含食材信息(不要过多)、制作步骤、推荐理由,最多输出两份报告。
请严格按照流程,优先调用 web_search 工具搜索食谱,搜索不到的情况下才能自己发挥。
"""
model = init_chat_model(
model=os.getenv("MODEL_NAME"),
model_provider="openai",
base_url=os.getenv("MODEL_BASE_URL"),
api_key=os.getenv("MODEL_API_KEY"),
temperature=1.5,
)
agent = create_agent(
model=model, # 模型
tools=[web_search], # 工具
checkpointer=checkpointer, # 记忆
system_prompt=system_prompt # 系统提示词
)
title_prompt = """
请根据以下对话内容,生成一句不超过 20 字的标题,用于记录本次对话主题。
只返回标题,不要解释。
"""
title_agent = create_agent(
model=model,
system_prompt=title_prompt
)