对于业务上 LLM 可以通过调用 Function Call 完成对应用户的需求比如发送退款申请,查询订单状态,完成商品上架等。
如何使用Function Call?
首先用户先向模型提问北京天气。
模型是无法知道天气的,通过Function Call调用我们识别天气的函数。
最后模型根据我们给的结果对用户问题进行解答。
定义被调用的函数:
1 2 3 4 5
defget_weather(location): # 这里可以添加实际的天气获取逻辑 # 例如,调用一个天气API并返回结果 # 为了示例,我们假设返回一个固定的天气信息 returnf"The weather in {location} is 24℃."
定义函数描述:
函数描述中详细描述了这个函数的功能以及需要填入的参数类型和属性(比如是否必填)。
之后模型会根据你的描述使用你的函数,并给你提供对应的参数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Get weather of a location, the user should supply a location first", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA", } }, "required": ["location"] }, } }, ]
tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Get weather of a location, the user should supply a location first", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA", } }, "required": ["location"] }, } }, ]
print(f"User>\t How's the weather in Beijing?")
messages = [{"role": "user", "content": "How's the weather in Beijing?"}] message = send_messages(messages)