langchain-tools-example

LangChain使用tools的示例

测试代码如下,一个fib函数和一个查看discord的函数封装成工具,方便LLM使用,流程如下:

  1. 用户提问,给出所有可用工具,调用LLM
  2. 调用合适的工具和输入
  3. 返回工具的结果
  4. 根据工具的结果调用LLM产生最终答案
    优缺点: 工具很灵活,适合封装,缺点,LLM对根据描述能正常筛选工具,但是目前langchain的官方只支持1个参数的工具,如果工具需要的参数如果较多,不是特别好处理。需要设定统一的回复格式,修改源代码。关于为何只支持1个参数,详细查看input_prompt内容。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    import json
    from langchain.chains import LLMChain
    from langchain.agents import Tool, initialize_agent, AgentType
    from langchain.prompts import PromptTemplate
    from chat.paper_chat import PoeChatGPTAPI
    from langchain.memory import ConversationBufferMemory

    def fib(n):
    return 100*n

    def discord(message):
    return f"Discord: I'am glad to grab some apples for you!"

    tools = [
    Tool(
    name="Fibonacci",
    func=lambda n: str(fib(int(n))),
    description="Use when you want to calculate the nth fibonnaci number"
    ),
    Tool(
    name="Discord",
    func=lambda str: discord(str),
    description="Use when you want to send a message using Discord"
    ),
    ]


    memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
    llm=PoeChatGPTAPI(temperature=0, verbose=True)

    agent_chain = initialize_agent(
    tools,
    llm,
    agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,
    memory=memory,
    verbose=True
    )

    response = agent_chain.run(input="Please calculate the 10th fibonacci number")
    print(f"response : {response}")

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[
{
"POE": "poe",
"input_prompt": "System: Assistant is a large language model trained by OpenAI.\n\nAssistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\nAssistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\nOverall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.\nHuman: TOOLS\n------\nAssistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:\n\n> Fibonacci: Use when you want to calculate the nth fibonnaci number\n> Discord: Use when you want to send a message using Discord\n\nRESPONSE FORMAT INSTRUCTIONS\n----------------------------\n\nWhen responding to me, please output a response in one of two formats:\n\n**Option 1:**\nUse this if you want the human to use a tool.\nMarkdown code snippet formatted in the following schema:\n\n```json\n{\n \"action\": string \\ The action to take. Must be one of Fibonacci, Discord\n \"action_input\": string \\ The input to the action\n}\n```\n\n**Option #2:**\nUse this if you want to respond directly to the human. Markdown code snippet formatted in the following schema:\n\n```json\n{\n \"action\": \"Final Answer\",\n \"action_input\": string \\ You should put what you want to return to use here\n}\n```\n\nUSER'S INPUT\n--------------------\nHere is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else):\n\nPlease calculate the 10th fibonacci number",
"input_text": "",
"response": "{\n \"action\": \"Fibonacci\",\n \"action_input\": \"10\"\n}"
}
]
{
"action": "Fibonacci",
"action_input": "10"
}
Observation: 1000
Thought:[
{
"POE": "poe",
"input_prompt": "System: Assistant is a large language model trained by OpenAI.\n\nAssistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\nAssistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\nOverall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.\nHuman: TOOLS\n------\nAssistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:\n\n> Fibonacci: Use when you want to calculate the nth fibonnaci number\n> Discord: Use when you want to send a message using Discord\n\nRESPONSE FORMAT INSTRUCTIONS\n----------------------------\n\nWhen responding to me, please output a response in one of two formats:\n\n**Option 1:**\nUse this if you want the human to use a tool.\nMarkdown code snippet formatted in the following schema:\n\n```json\n{\n \"action\": string \\ The action to take. Must be one of Fibonacci, Discord\n \"action_input\": string \\ The input to the action\n}\n```\n\n**Option #2:**\nUse this if you want to respond directly to the human. Markdown code snippet formatted in the following schema:\n\n```json\n{\n \"action\": \"Final Answer\",\n \"action_input\": string \\ You should put what you want to return to use here\n}\n```\n\nUSER'S INPUT\n--------------------\nHere is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else):\n\nPlease calculate the 10th fibonacci number\nAI: {\n \"action\": \"Fibonacci\",\n \"action_input\": \"10\"\n}\nHuman: TOOL RESPONSE: \n---------------------\n1000\n\nUSER'S INPUT\n--------------------\n\nOkay, so what is the response to my last comment? If using information obtained from the tools you must mention it explicitly without mentioning the tool names - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else.",
"input_text": "",
"response": "{\n \"action\": \"Final Answer\",\n \"action_input\": \"The response to your last comment was: '1000'.\"\n}"
}
]
{
"action": "Final Answer",
"action_input": "The response to your last comment was: '1000'."
}

> Finished chain.
response : The response to your last comment was: '1000'.

Process finished with exit code 0

langchain-tools-example
https://johnson7788.github.io/2023/06/14/langchain-tools-example/
作者
Johnson
发布于
2023年6月14日
许可协议