赞
踩
上一节快速使用了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 这个概念来比较在上面三种场景中的使用区别。
没有购买,纯看代码
- 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'
)
]
)
参考:【AI基础】大模型部署工具之ollama的安装部署-第一步:下载安装ollama
参考:【AI基础】大模型部署工具之ollama的安装部署-第二步:部署安装大模型
> ollama pull qwen2
> pip install qwen-agent
- 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"}'
}
}]
参考:【AI基础】大模型部署工具之ollama的安装部署-第一步:下载安装ollama
参考:【AI基础】大模型部署工具之ollama的安装部署-第二步:部署安装大模型
> ollama pull qwen2
> pip install -q langchain_experimental
- 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'
}
]
ollama目前尚未支持Function Call功能。
参考官方文档:openai兼容api · ollama/ollama · GitHub
等支持后再试试。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。