from langchain.tools import Tool
from langchain.utilities import SerpAPIWrapper
from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
import os

# 去serpapi官网注册有免费额度https://serpapi.com/
os.environ["SERPAPI_API_KEY"] = ""

# 创建 SerpAPI 包装器
search = SerpAPIWrapper()

# 创建工具
tools = [
    Tool(
        name="Search",
        func=search.run,
        description="Useful for when you need to answer questions about current events. You should ask targeted questions."
    )
]

# 初始化大模型
llm = ChatOpenAI(
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    model="qwen-turbo",
    temperature=0,
    max_retries=5,
    api_key=""  # 请填入有效的 API 密钥
)

# 初始化代理
agent = initialize_agent(tools, llm, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

# 示例查询
question = "今天几号,昨天有什么大事发生"
result = agent.run(question)
print(result)

结果:

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐