跳至橫幅的結尾
前往橫幅的開頭

範例 - Multiple Functions

跳至中繼資料的結尾
前往中繼資料的開頭

您正在檢視此頁面的舊版本。請檢視目前版本

比較目前 檢視頁面歷程記錄

« 上一頁 版本 3 下一步 »

您可以透過以下實作來實現 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'])
  • 無標籤