ollama如何将function call插件调用的结果放入上下文中让ai回答?
现在实现了ollama调用大模型返回函数名和参数,但是调完插件获取结果怎么放入上下文让ai来回答?增加一个{role:"tool",message:"插件结果",toolname:"获取时间"}?
网友回复
这个需要你设置好提示语了,在提示语中要告诉ai,虽然你没有这个能力,但是我已经通过第三方插件帮你获取的信息,我们以图片理解为例,通过function call来获取参数,然后调用图片理解的插件,插件返回图片内容后,我们把prompt提示语设置以下:
点击查看全文
通过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 = [{...点击查看剩余70%


