langchain创建agent使用阿里云百炼API
langchain创建agent使用阿里云百炼API
·
langchain创建agent使用阿里云百炼API,的示例
from langchain_community.tools import TavilySearchResults
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
import os
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate
model = ChatOpenAI(model="qwen-max",api_key=os.getenv("OPENAI_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
temperature=0.7,
max_tokens=1024)
@tool
def magic_function(input: int) -> int:
"""Applies a magic function to an input."""
print("工具调用================")
return input + 2
tools = [magic_function]
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant"),
("human", "{input}"),
# Placeholders fill up a **list** of messages
("placeholder", "{agent_scratchpad}"),
]
)
# 创建Agent 和 AgentExecutor
agent = create_tool_calling_agent(model, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools,verbose=True)
print(agent_executor.invoke({"input": "what is the value of magic_function(3)?"}))
更多推荐
所有评论(0)