langchain调用通义千文大模型实现联网搜索
【代码】langchain调用通义千文大模型实现联网搜索。
·
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)
结果:

更多推荐



所有评论(0)