您可以透過以下實作來實現 Multiple Functions 的場景。
如以下的例子是撈取對話歷史加上 system prompt後,餵入兩個 functions (summarization & extract_keywords) 讓模型依據對話歷史回傳對應 function 的 arguments。
程式碼區塊 | ||
---|---|---|
| ||
import json response = await chat( conversation=CURRENT_CONVERSATION + [ { "role": "system", "content": "Only use the functions you have been provided with." } ], functions=[ { "name": "summarization", "description": "summarize the conversation", "parameters": { "type": "object", "properties": { "summary": { "type": "string", "description": "the summary of the conversation" } }, "required": ["summary"] } }, { "name": "extract_keywords", "description": "extracting from the conversation", "parameters": { "type": "object", "properties": { "keywords": { "type": "array", "items": { "type": "string", "description": "one keyword extracted from the conversation" } } }, "required": ["keywords"] } } ] ) if 'function_call' in response: if response['function_call']['name'] == 'summarization': print("The summary of the converstaion is ", json.loads(response['function_call']['arguments'])['summary']) elif response['function_call']['name'] == 'extracted_keywords': print("The keywords of the converstaion are ", json.loads(response['function_call']['arguments'])['keywords']) else: print(response['content']) |