当前位置:   article > 正文

OpenAI的Function calling 和 LangChain的Search Agent_langchain function call

langchain function call

OpenAI的Function calling

         openai最近发布的gpt-3.5-turbo-0613 和 gpt-4-0613版本模型增加了function calling的功能,该功能通过定义功能函数,gpt通过分析问题和函数功能描述来决定是否调用函数,并且生成函数对应的入参。函数调用的功能可以弥补gpt的一些缺点,比如实时信息的缺乏、特定领域能力,使得能够进一步利用gpt的逻辑推理能力,可以将问题进行分解处理,解决问题能力更加强大。

gpt的函数调用功能步骤如下:
    1.使用问句和函数定义调用gpt
         2.gpt选择是否调用函数,并输出参数
         3.解析参数 调用函数
         4.将函数返回作为追加信息再次调用gpt

下面是一个通过调用search api的例子

1.定义+描述函数

        下面代码介绍了一个搜索函数,可以通过GoogleSerperAPI实时搜索网络上的信息。

  1. ###定义functions,用于描述函数作用和参数介绍。
  2. functions = [
  3. {
  4. "name": "get_info_from_web",
  5. "description": "get more informations from internet use google search",
  6. "parameters": {
  7. "type": "object",
  8. "properties": {
  9. "query": {
  10. "type": "string",
  11. "description": "all the questions or information you want search from internet",
  12. }
  13. },
  14. "required": ["query"],
  15. },
  16. }
  17. ]
  18. ###函数定义
  19. def get_info_from_web(query):
  20. search = GoogleSerperAPIWrapper(serper_api_key="xxxxx")
  21. return search.run(query)

2.调用gpt,决定是否调用函数以及函数参数

        当用户问句为"今天杭州天气怎么样?"时,gpt做出了进行调用get_info_from_web函数的决定,并且调用的参数为"query": "杭州天气"。

  1. messages = []
  2. messages.append({"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous. "})
  3. messages.append({"role": "user", "content": "今天杭州天气怎么样?"})
  4. chat_response = chat_completion_request(
  5. messages, functions=functions
  6. )
  7. assistant_message = chat_response.json()["choices"][0]["message"]
  8. messages.append(assistant_message)
  9. print(assistant_message)
  10. >>>
  11. {
  12. 'role': 'assistant',
  13. 'content': None,
  14. 'function_call': {
  15. 'name': 'get_info_from_web',
  16. 'arguments': '{\n "query": "杭州天气"\n}'
  17. }
  18. }

3.执行gpt的决定,获得回答问题的中间结果

        调用第2步中gpt输出的参数执行相应的函数,获得中间结果。

  1. assistant_message = chat_response.json()["choices"][0]["message"]
  2. if assistant_message.get("function_call"):
  3. if assistant_message["function_call"]["name"] == "get_info_from_web":
  4. query = json.loads(assistant_message["function_call"]["arguments"])["query"]
  5. results = get_info_from_web(query)
  6. else:
  7. results = f"Error: function {assistant_message['function_call']['name']} does not exist"
  8. print(results)
  9. >>>
  10. 81°F

4.函数结果和原始问题再次询问gpt,获得最终结果

  1. messages.append({"role": "function", "name": assistant_message["function_call"]["name"], "content": results})
  2. second_response = openai.ChatCompletion.create(
  3. model= GPT_MODEL,
  4. messages=messages
  5. )
  6. print(second_response["choices"][0]["message"]["content"])
  7. >>>
  8. 今天杭州的天气是81°F。

LangChain的Search Agent        

        在openai的function calling发布之前,LangChain的Agent就可以实现类似功能。Agent接口是LangChain中一个重要的模块,一些应用程序需要根据用户输入灵活地调用LLM和其他工具。Agent接口为此类应用程序提供了灵活性。Agent可以访问一套工具,并根据用户输入确定要使用哪些工具。Agent可以使用多个工具,并将一个工具的输出用作下一个工具的输入。

        以下是search agent的例子。定义GoogleSerperApi工具作为LLM可用的tool,帮助解决相关问题。

  1. from langchain.utilities import GoogleSerperAPIWrapper
  2. from langchain.llms.openai import OpenAI
  3. from langchain.agents import initialize_agent, Tool
  4. from langchain.agents import AgentType
  5. llm = OpenAI(temperature=0)
  6. search = GoogleSerperAPIWrapper(serper_api_key="xxxxxx")
  7. tools = [
  8. Tool(
  9. name="Intermediate Answer",
  10. func=search.run,
  11. description="useful for when you need to ask with search",
  12. )
  13. ]
  14. self_ask_with_search = initialize_agent(
  15. tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True
  16. )
  17. self_ask_with_search.run(
  18. "今天杭州天气怎么样?"
  19. )
  20. >>>
  21. > Entering new AgentExecutor chain...
  22. Yes.
  23. Follow up: 今天是几号?
  24. Intermediate answer: Sunday, July 16, 2023
  25. Follow up: 杭州今天的天气情况?
  26. Intermediate answer: 88°F
  27. So the final answer is: 88°F
  28. > Finished chain.
  29. 88°F

       agent功能通过设计prompt实现,search agent的prompt设计如下:

  1. """Question: Who lived longer, Muhammad Ali or Alan Turing?
  2. Are follow up questions needed here: Yes.
  3. Follow up: How old was Muhammad Ali when he died?
  4. Intermediate answer: Muhammad Ali was 74 years old when he died.
  5. Follow up: How old was Alan Turing when he died?
  6. Intermediate answer: Alan Turing was 41 years old when he died.
  7. So the final answer is: Muhammad Ali
  8. Question: When was the founder of craigslist born?
  9. Are follow up questions needed here: Yes.
  10. Follow up: Who was the founder of craigslist?
  11. Intermediate answer: Craigslist was founded by Craig Newmark.
  12. Follow up: When was Craig Newmark born?
  13. Intermediate answer: Craig Newmark was born on December 6, 1952.
  14. So the final answer is: December 6, 1952
  15. Question: Who was the maternal grandfather of George Washington?
  16. Are follow up questions needed here: Yes.
  17. Follow up: Who was the mother of George Washington?
  18. Intermediate answer: The mother of George Washington was Mary Ball Washington.
  19. Follow up: Who was the father of Mary Ball Washington?
  20. Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
  21. So the final answer is: Joseph Ball
  22. Question: Are both the directors of Jaws and Casino Royale from the same country?
  23. Are follow up questions needed here: Yes.
  24. Follow up: Who is the director of Jaws?
  25. Intermediate answer: The director of Jaws is Steven Spielberg.
  26. Follow up: Where is Steven Spielberg from?
  27. Intermediate answer: The United States.
  28. Follow up: Who is the director of Casino Royale?
  29. Intermediate answer: The director of Casino Royale is Martin Campbell.
  30. Follow up: Where is Martin Campbell from?
  31. Intermediate answer: New Zealand.
  32. So the final answer is: No
  33. Question: {input}
  34. Are followup questions needed here:{agent_scratchpad}"""

 可以从prompt看出,通过四个例子提出了解决问题的方式,即通过follow up + Intermediate answer 分解问题并解决子问题。follow up是gpt的输出,表示需要search tool搜索的问题, Intermediate answer 则为search tool的答案,循环多次之后得到最终答案。
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/200119
推荐阅读
相关标签
  

闽ICP备14008679号