已比較的版本

索引鍵

  • 此行已新增。
  • 此行已移除。
  • 格式已變更。

您可以透過以下實作來實現 Multiple Functions 的場景。

如以下的例子是撈取對話歷史加上 system prompt後,餵入兩個 functions (summarization & extract_keywords) 讓模型依據對話歷史回傳對應 function 的 arguments。

...

languagepy

...

透過以下的 plugin 可以協助 summary 一段文章或者取出文章當中的重點

  1. summarization: 透過此 function 可以提供文章的摘要

    image-20241112-080230.pngImage Added
  2. focal_points: 透過此 function 可以列文章的重點

    image-20241112-080149.pngImage Added

Python code

程式碼區塊
languagepy
import json
from pyodide.http import pyfetch

async def main():
    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": "focal_points",
                "description": "extract the focal points of the conversation",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "points": {
                            "type": "array",
                            "items": {
                                "type": "string",
                                "description": "extract the focal points of the conversation"
                            }
                        }
                    },
                    "required": ["points"]
                }
            }
        ]
    )
    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'] == 'focal_points':
            print("The focal points of the converstaion are ", json.loads(response['function_call']['arguments'])['points'])
    else:
        print(response['content'])

await main()

Plugin

程式碼區塊
languagejson
{
    "id": "multi function plugin",
    "schema_version": "v1",
    "name_for_human": "multi function plugin",
    "name_for_model": "multi function plugin",
    "description_for_human": "Testing for multi function plugin",
    "description_for_model": "Always use this plugin",
    "auth": {
      "type": "none"
    },
    "api": {
      "type": "python",
      "python": {
        "source": "import json\nfrom pyodide.http import pyfetch\n\nasync def main():\n    response = await chat(\n        conversation=CURRENT_CONVERSATION + [\n            {\n            \"role\": \"system\",\n            \"content\": \"Only use the functions you have been provided with.\"\n            }\n        ],\n        functions=[\n            {\n                \"name\": \"summarization\",\n                \"description\": \"summarize the conversation\",\n                \"parameters\": {\n                    \"type\": \"object\",\n                    \"properties\": {\n                        \"summary\": {\n                            \"type\": \"string\",\n                            \"description\": \"the summary of the conversation\"\n                        }\n                    },\n                    \"required\": [\"summary\"]\n                }\n            },\n            {\n                \"name\": \"extractfocal_keywordspoints\",\n                \"description\": "extracting from\"extract the focal points of the conversation\",\n                \"parameters\": {\n                    \"type\": \"object\",\n                    \"properties\": {\n                        \"keywordspoints\": {\n                            \"type\": \"array\",\n                            \"items\": {\n                                \"type\": \"string\",\n                                \"description\": \"oneextract the keywordfocal extractedpoints fromof the conversation\"\n                            }\n                        }\n                    },\n                    \"required\": [\"keywordspoints\"]\n                }\n            }\n        ]\n    )\n    if 'function_call' in response:\n        if response['function_call']['name'] == 'summarization':\n            print(\"The summary of the converstaion is \", json.loads(response['function_call']['arguments'])['summary'])\n        elif response['function_call']['name'] == 'extractedfocal_keywordspoints':\n            print(\"The focal keywordspoints of the converstaion are \", json.loads(response['function_call']['arguments'])['keywordspoints'])\n    else:\n        print(response['content'])\n\nawait main()"
      }
    }
  }