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

发布于:2024-06-28 ⋅ 阅读:(12) ⋅ 点赞:(0)

上一节快速使用了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 运行代码

import openai
from openai import OpenAI

openai.api_key = "OPENAI_API_KEY"
openai.api_base= "OPENAI_API_URL"

# 第一步,获取大模型
client = OpenAI(api_key=openai.api_key ,base_url=openai.api_base)

# 第二步,定义业务函数
get_current_weather = {
    "type": "function",
    "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA"
                },
            },
            "required": ["city"],
        },
    }
}
tools = [get_current_weather]

# 第三步,发起交互
messages = [
    {"role": "system", "content": "please input your question"},
    {"role": "user", "content": "how is the weather in Beijing today"}
]

response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=messages,
    tools=tools,
    tool_choice="auto",  
)

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 运行代码

from qwen_agent.llm import get_chat_model

# 第一步,获取大模型
client = get_chat_model({
	'model': 'qwen2',
	'model_server': 'http://127.0.0.1:11434/v1',
	# (Optional) LLM hyper-paramters:
	'generate_cfg': {
		'top_p': 0.8
	}
})


# 第二步,定义业务函数
get_current_weather = {
    "name": "get_current_weather",
    "description": "Get the current weather in a given location",
    "parameters": {
        "type": "object",
        "properties": {
            "city": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA"
            },
        },
        "required": ["city"],
    },
}
tools = [get_current_weather]

# 第三步,发起交互
messages = [
    {'role': 'user', 'content': "What's the weather like in San Francisco?"}
]

# 此处演示流式输出效果
print('此处演示流式输出效果')
res_stream = []
for res_stream in client.chat(
	    messages=messages,
	    functions=tools,
	    stream=True):
    print(res_stream)

# 此处演示输出效果
print('此处演示输出效果')
res = llm.chat(
	    messages=messages,
	    functions=tools,
	    stream=False
)
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 运行代码

from langchain_experimental.llms.ollama_functions import OllamaFunctions
 
# 第一步:获取大模型
client = OllamaFunctions(model='qwen2', base_url='http://localhost:11434', format='json')

# 第二步,定义业务函数
get_current_weather = {
    'name': 'get_current_weather',
    'description': 'Get the current weather in a given location',
    'parameters': {
        'type': 'object',
        'properties': {
            'city': {
                'type': 'string',
                'description': 'The city and state, e.g. San Francisco, CA',
            }
        },
        'required': ['city'],
    }
}
tools = [get_current_weather]
 
# 第三步,通过业务处理函数描述,把业务函数绑定到大模型上
client_with_tool = client.bind_tools(
    tools=tools
)

# 第四步,发起交互
message = "What's the weather like in San Francisco?"
response = client_with_tool.invoke(message)
print(response)

结果如下:

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


网站公告

今日签到

点亮在社区的每一天
去签到