为代理定制工具

使用 LangChain 开发 LLM 应用

Jonathan Bennion

AI Engineer & LangChain Contributor

工具格式

from langchain_community.agent_toolkits.load_tools import load_tools

tools = load_tools(["llm-math"], llm=llm)

print(tools[0].name)
Calculator
print(tools[0].description)
当您需要回答有关数学的问题时很有用。
  • 由 LLM/代理用作上下文,以判断何时调用它
使用 LangChain 开发 LLM 应用

工具格式

print(tools[0].return_direct)
False
使用 LangChain 开发 LLM 应用

定义自定义函数

def financial_report(company_name: str, revenue: int, expenses: int) -> str:
    """Generate a financial report for a company that calculates net income."""
    net_income = revenue - expenses

    report = f"Financial Report for {company_name}:\n"
    report += f"Revenue: ${revenue}\n"
    report += f"Expenses: ${expenses}\n"
    report += f"Net Income: ${net_income}\n"
    return report
使用 LangChain 开发 LLM 应用

调用函数

print(financial_report(company_name="LemonadeStand", revenue=100, expenses=50))
Financial Report for LemonadeStand:
Revenue: $100
Expenses: $50
Net Income: $50
使用 LangChain 开发 LLM 应用

从函数到工具

from langchain_core.tools import tool

@tool
def financial_report(company_name: str, revenue: int, expenses: int) -> str: """Generate a financial report for a company that calculates net income.""" net_income = revenue - expenses report = f"Financial Report for {company_name}:\n" report += f"Revenue: ${revenue}\n" report += f"Expenses: ${expenses}\n" report += f"Net Income: ${net_income}\n" return report
使用 LangChain 开发 LLM 应用

检查新工具

print(financial_report.name)

print(financial_report.description)
print(financial_report.return_direct)
print(financial_report.args)
financial_report

为公司生成计算净利润的财务报告。
False
{'company_name': {'title': 'Company Name', 'type': 'string'}, 'revenue': {'title': 'Revenue', 'type': 'integer'}, 'expenses': {'title': 'Expenses', 'type': 'integer'}}
使用 LangChain 开发 LLM 应用

集成自定义工具

from langgraph.prebuilt import create_react_agent

llm = ChatOpenAI(model="gpt-4o-mini", api_key=openai_api_key, temperature=0)
agent = create_react_agent(llm, [financial_report])

messages = agent.invoke({"messages": [("human", "TechStack generated made $10 million with $8 million of costs. Generate a financial report.")]}) print(messages)
使用 LangChain 开发 LLM 应用

集成自定义工具

{'messages': [
    HumanMessage(content='TechStack generated made $10 million dollars with $8 million of...', ...),
    AIMessage(content='', ..., tool_calls=[{'name': 'financial_report',
                                            'args': {'company_name': 'TechStack',
                                                     'revenue': 10000000, 'expenses': 8000000}, ...),
    ToolMessage(content='Financial Report for TechStack:\nRevenue: $10000000\nExpenses...', ...),
    AIMessage(content='Here is the financial report for TechStack...', ...)
]}
使用 LangChain 开发 LLM 应用

工具输出

print(messages['messages'][-1].content)
以下是 TechStack 的财务报告:
- 收入:$10,000,000
- 费用:$8,000,000
- 净利润:$2,000,000
Financial Report for TechStack:
Revenue: $10000000
Expenses: $8000000
Net Income: $2000000
使用 LangChain 开发 LLM 应用

让我们来练习!

使用 LangChain 开发 LLM 应用

Preparing Video For Download...