通过ollma的function call 函数实现
import json import ollama import asyncio # Simulates an API call to get flight times # In a real application, this would fetch data from a live database or API def get_flight_times(departure: str, arrival: str) -> str: flights = { 'NYC-LAX': {'departure': '08:00 AM', 'arrival': '11:30 AM', 'duration': '5h 30m'}, 'LAX-NYC': {'departure': '02:00 PM', 'arrival': '10:30 PM', 'duration': '5h 30m'}, 'LHR-JFK': {'departure': '10:00 AM', 'arrival': '01:00 PM', 'duration': '8h 00m'}, 'JFK-LHR': {'departure': '09:00 PM', 'arrival': '09:00 AM', 'duration': '7h 00m'}, 'CDG-DXB': {'departure': '11:00 AM', 'arrival': '08:00 PM', 'duration': '6h 00m'}, 'DXB-CDG': {'departure': '03:00 AM', 'arrival': '07:30 AM', 'duration': '7h 30m'}, } key = f'{departure}-{arrival}'.upper() return json.dumps(flights.get(key, {'error': 'Flight not found'})) async def run(model: str): client = ollama.AsyncClient() # Initialize conversation with a user query messages = [{'role': 'user', 'content': 'What is the flight time from New York (NYC) to Los Angeles (LAX)?'}] # First API call: Send the query and function description to the model response = await client.chat( model=model, messages=messages, tools=[ { 'type': 'function', 'function': { 'name': 'get_flight_times', 'description': 'Get the flight times between two cities', 'parameters': { 'type': 'object', 'properties': { 'departure': { 'type': 'string', 'description': 'The departure city (airport code)', }, 'arrival': { 'type': 'string', 'description': 'The arrival city (airport code)', }, }, 'required': ['departure', 'arrival'], }, }, }, ], ) # Add the model's response to the conversation history messages.append(response['message']) # Check if the model decided to use the provided function if not response['message'].get('tool_calls'): print("The model didn't use the function. Its response was:") print(response['message']['content']) return # Process function calls made by the model if response['message'].get('tool_calls'): available_functions = { 'get_flight_times': get_flight_times, } for tool in response['message']['tool_calls']: function_to_call = available_functions[tool['function']['name']] function_response = function_to_call(tool['function']['arguments']['departure'], tool['function']['arguments']['arrival']) # Add function response to the conversation messages.append( { 'role': 'tool', 'content': function_response, } ) # Second API call: Get final response from the model final_response = await client.chat(model=model, messages=messages) print(final_response['message']['content']) # Run the async function asyncio.run(run('llama3.1'))
网友回复
阿里通义大模型哪些是支持多模态的api的ai模型?
js如何实现浏览器中离线语音唤醒语音聊天小助手?
浏览器中如何将WebM视频转成mp4视频?
parlant如何改成qwen 的apikey与baseurl?
如何写一个chrome插件实现截屏自动生成步骤图文教程转成pdf或网页?
python如何通过阿里云的api对域名进行解析和ecs主机服务器进行启动停止等操作?
Tesla Robotaxi可以让特斯拉车自动无人驾驶跑滴滴为车主赚钱,国内以后也会这样吗?
有没有可以监控安卓手机上的app打开后偷偷摸摸做了啥的监控软件?
webrtc进行p2p连接发送的文本音视频文件是否是加密的?
如何让一个可爱的三维动物通过three在浏览器中有表情动作的自然说话?