当前位置:   article > 正文

【AI提升】AI利器Tool Call/Function Call(二):OpenAI/qwen-agent/LangChain/Ollama_langchain qwen agent

langchain qwen agent

上一节快速使用了Tool Call 【AI提升】AI利器Tool Call/Function Call(一) ,使用的是LangChain+Ollama,这一节说说为什么使用这个组合,以及其余的使用场景。

首先大家都知道,在目前AI的世界里,各大模型都还是跟着OpenAI在跑,API也尽量跟OpenAI保持一致。所以这里大致会有三个场景:1、OpenAI,2、其余模型自身的封装-这里选择qwen-agent,3、通用封装框架-这里选择LangChain和Ollama。这一节通过 Tool Call/Function Call 这个概念来比较在上面三种场景中的使用区别。

一、OpenAI

没有购买,纯看代码

1.1 运行代码

  1. import openai
  2. from openai import OpenAI
  3. openai.api_key = "OPENAI_API_KEY"
  4. openai.api_base= "OPENAI_API_URL"
  5. # 第一步,获取大模型
  6. client = OpenAI(api_key=openai.api_key ,base_url=openai.api_base)
  7. # 第二步,定义业务函数
  8. get_current_weather = {
  9. "type": "function",
  10. "function": {
  11. "name": "get_current_weather",
  12. "description": "Get the current weather in a given location",
  13. "parameters": {
  14. "type": "object",
  15. "properties": {
  16. "city": {
  17. "type": "string",
  18. "description": "The city and state, e.g. San Francisco, CA"
  19. },
  20. },
  21. "required": ["city"],
  22. },
  23. }
  24. }
  25. tools = [get_current_weather]
  26. # 第三步,发起交互
  27. messages = [
  28. {"role": "system", "content": "please input your question"},
  29. {"role": "user", "content": "how is the weather in Beijing today"}
  30. ]
  31. response = client.chat.completions.create(
  32. model="gpt-3.5-turbo",
  33. messages=messages,
  34. tools=tools,
  35. tool_choice="auto",
  36. )
  37. print(response.choices[0].message)

结果如下:

ChatCompletionMessage(
    content=None, 
    role='assistant', 
    function_call=None, 
    tool_calls=[
        ChatCompletionMessageToolCall(
            id='call_OqKaG4mk8Z5oEVKCYsd5rSCO', 
            function=Function(
                arguments='{"city": "San Francisco, CA"}', 
                name='get_current_weather'
            ), 
            type='function'
        )
    ]

二、qwen-agent

2.1 安装ollama

参考:【AI基础】大模型部署工具之ollama的安装部署-第一步:下载安装ollama

2.2 部署大模型

参考:【AI基础】大模型部署工具之ollama的安装部署-第二步:部署安装大模型

> ollama pull qwen2

2.3 安装qwen-agent

> pip install qwen-agent

2.4 运行代码

  1. from qwen_agent.llm import get_chat_model
  2. # 第一步,获取大模型
  3. client = get_chat_model({
  4. 'model': 'qwen2',
  5. 'model_server': 'http://127.0.0.1:11434/v1',
  6. # (Optional) LLM hyper-paramters:
  7. 'generate_cfg': {
  8. 'top_p': 0.8
  9. }
  10. })
  11. # 第二步,定义业务函数
  12. get_current_weather = {
  13. "name": "get_current_weather",
  14. "description": "Get the current weather in a given location",
  15. "parameters": {
  16. "type": "object",
  17. "properties": {
  18. "city": {
  19. "type": "string",
  20. "description": "The city and state, e.g. San Francisco, CA"
  21. },
  22. },
  23. "required": ["city"],
  24. },
  25. }
  26. tools = [get_current_weather]
  27. # 第三步,发起交互
  28. messages = [
  29. {'role': 'user', 'content': "What's the weather like in San Francisco?"}
  30. ]
  31. # 此处演示流式输出效果
  32. print('此处演示流式输出效果')
  33. res_stream = []
  34. for res_stream in client.chat(
  35. messages=messages,
  36. functions=tools,
  37. stream=True):
  38. print(res_stream)
  39. # 此处演示输出效果
  40. print('此处演示输出效果')
  41. res = llm.chat(
  42. messages=messages,
  43. functions=tools,
  44. stream=False
  45. )
  46. print(res)

结果如下:

[{
    'role': 'assistant', 
    'content': '', 
    'function_call': {
        'name': 'get_current_weather', 
        'arguments': '{"city": "San Francisco, CA"}'
    }
}]

三、LangChain

 3.1 安装ollama

参考:【AI基础】大模型部署工具之ollama的安装部署-第一步:下载安装ollama

3.2 部署大模型

参考:【AI基础】大模型部署工具之ollama的安装部署-第二步:部署安装大模型

> ollama pull qwen2

3.3 安装LangChain

> pip install -q langchain_experimental

3.4 运行代码

  1. from langchain_experimental.llms.ollama_functions import OllamaFunctions
  2. # 第一步:获取大模型
  3. client = OllamaFunctions(model='qwen2', base_url='http://localhost:11434', format='json')
  4. # 第二步,定义业务函数
  5. get_current_weather = {
  6. 'name': 'get_current_weather',
  7. 'description': 'Get the current weather in a given location',
  8. 'parameters': {
  9. 'type': 'object',
  10. 'properties': {
  11. 'city': {
  12. 'type': 'string',
  13. 'description': 'The city and state, e.g. San Francisco, CA',
  14. }
  15. },
  16. 'required': ['city'],
  17. }
  18. }
  19. tools = [get_current_weather]
  20. # 第三步,通过业务处理函数描述,把业务函数绑定到大模型上
  21. client_with_tool = client.bind_tools(
  22. tools=tools
  23. )
  24. # 第四步,发起交互
  25. message = "What's the weather like in San Francisco?"
  26. response = client_with_tool.invoke(message)
  27. print(response)

结果如下:

content='' 
id='run-73971df7-2017-4320-b3b5-4f9c478a1815-0' 
tool_calls=[
    {
        'name': 'get_current_weather', 
        'args': {
            'city': 'San Francisco'
        }, 
        'id': 'call_bfa6d6c7e80740d2af1445a8d315ee1d'
    }
]

四、Ollama

ollama目前尚未支持Function Call功能。

参考官方文档:openai兼容api · ollama/ollama · GitHub 

等支持后再试试。 

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

闽ICP备14008679号