前言
ChatGPT正在經(jīng)歷著一次革命性的改變,隨著越來越多的小程序和第三方插件的引入,ChatGPT將變得更加強大、靈活和自由。這些插件不僅能夠讓用戶實現(xiàn)更多更復(fù)雜的AI任務(wù)和目標,還會帶來類似國內(nèi)微信小程序般的瘋狂,為用戶和開發(fā)者帶來更多驚喜和創(chuàng)新。
想象一下,當(dāng)您需要一個特定的功能時,只需輕松安裝一個插件,就能立即體驗到它的好處和便利。無論是語音識別、圖像處理,還是智能推薦等等,插件可以讓您的使用體驗更加高效和順暢。
隨著全球范圍內(nèi)各種第三方插件的接入,ChatGPT將成為一個更加強大、靈活和自由的AI平臺,為用戶和開發(fā)者帶來更多機會和挑戰(zhàn)。而且,通過簡單的幾個步驟,您也可以在ChatGPT中創(chuàng)建一個高度定制化的插件,實現(xiàn)自己的愿景和目標。讓我們一起期待ChatGPT的未來吧!
Introduction 導(dǎo)言
Creating a plugin takes 3 steps:
創(chuàng)建插件需要3個步驟:
- Build an API 構(gòu)建API
- Document the API in the OpenAPI yaml or JSON format 以O(shè)penAPI yaml或JSON格式記錄API
- Create a JSON manifest file that will define relevant metadata for the plugin
創(chuàng)建一個JSON清單文件,該文件將定義插件的相關(guān)元數(shù)據(jù)
The focus of the rest of this section will be creating a todo list plugin by defining the OpenAPI specification along with the manifest file.
本節(jié)其余部分的重點將是通過定義OpenAPI規(guī)范沿著清單文件來創(chuàng)建一個待辦事項列表插件。
Explore example plugins 瀏覽示例插件
Explore example plugins covering multiple use cases and authentication methods.
探索涵蓋多個用例和身份驗證方法的示例插件。
Plugin manifest 插件清單
Every plugin requires a ai-plugin.json
file, which needs to be hosted on the API’s domain. For example, a company called example.com
would make the plugin JSON file accessible via an https://example.com
domain since that is where their API is hosted. When you install the plugin via the ChatGPT UI, on the backend we look for a file located at /.well-known/ai-plugin.json
. The /.well-known
folder is required and must exist on your domain in order for ChatGPT to connect with your plugin. If there is no file found, the plugin cannot be installed. For local development, you can use HTTP but if you are pointing to a remote server, HTTPS is required.
每個插件都需要一個 ai-plugin.json
文件,該文件需要托管在API的域中。例如,一家名為 example.com
的公司將通過 https://example.com
域訪問插件JSON文件,因為這是他們的API托管的地方。當(dāng)您通過ChatGPT UI安裝插件時,在后端我們會查找位于 /.well-known/ai-plugin.json
的文件。 /.well-known
文件夾是必需的,并且必須存在于您的域中,以便ChatGPT與您的插件連接。如果找不到文件,則無法安裝插件。對于本地開發(fā),您可以使用HTTP,但如果您指向遠程服務(wù)器,則需要HTTPS。
The minimal definition of the required ai-plugin.json
file will look like the following:
所需的 ai-plugin.json
文件的最小定義如下所示:
{
"schema_version": "v1",
"name_for_human": "TODO Plugin",
"name_for_model": "todo",
"description_for_human": "Plugin for managing a TODO list. You can add, remove and view your TODOs.",
"description_for_model": "Plugin for managing a TODO list. You can add, remove and view your TODOs.",
"auth": {
"type": "none"
},
"api": {
"type": "openapi",
"url": "http://localhost:3333/openapi.yaml",
"is_user_authenticated": false
},
"logo_url": "http://localhost:3333/logo.png",
"contact_email": "support@example.com",
"legal_info_url": "http://www.example.com/legal"
}
If you want to see all of the possible options for the plugin file, you can refer to the definition below.
如果你想查看插件文件的所有可能選項,你可以參考下面的定義。
FIELD 名稱 | TYPE 類型 | DESCRIPTION / OPTIONS 描述/選項 | REQUIRED是否必須 |
---|---|---|---|
schema_version | String | Manifest schema version 清單架構(gòu)版本 | ? |
name_for_model | String | Name the model will used to target the plugin命名用于定位插件的模型 | ? |
name_for_human | String | Human-readable name, such as the full company name 人類可讀的名稱,如完整的公司名稱 | ? |
description_for_model | String | Description better tailored to the model, such as token context length considerations or keyword usage for improved plugin prompting. 更好地為模型定制描述,例如token上下文長度考慮或關(guān)鍵字使用以改進插件提示。 | ? |
description_for_human | String | Human-readable description of the plugin 人類可讀的插件描述 | ? |
auth | ManifestAuth | Authentication schema 身份驗證模式 | ? |
api | Object | API specification API詳述 | ? |
logo_url | String | URL used to fetch the plugin’s logo 用于獲取插件logo的URL | ? |
contact_email | String | Email contact for safety/moderation reachout, support, and deactivation 通過電子郵件聯(lián)系安全/適度聯(lián)系、支持和停用 | ? |
legal_info_url | String | Redirect URL for users to view plugin information重定向URL以供用戶查看插件信息 | ? |
HttpAuthorizationType | HttpAuthorizationType | “bearer” or “basic” “無記名”或“基本” | ? |
ManifestAuthType | ManifestAuthType | “none”, “user_http”, “service_http”, or “oauth” | |
interface BaseManifestAuth | BaseManifestAuth | type: ManifestAuthType; instructions: string; 類型:ManifestAuthType;說明:string; | |
ManifestNoAuth | ManifestNoAuth | No authentication required: BaseManifestAuth & { type: ‘none’, }無需身份驗證:BaseManifestAuth & { type:‘none’,} | |
ManifestAuth | ManifestAuth | ManifestNoAuth, ManifestServiceHttpAuth, ManifestUserHttpAuth, ManifestOAuthAuth |
The following are examples with different authentication methods:
以下是不同身份驗證方法的示例:
# App-level API keys 應(yīng)用級API密鑰
type ManifestServiceHttpAuth = BaseManifestAuth & {
type: 'service_http';
authorization_type: HttpAuthorizationType;
verification_tokens: {
[service: string]?: string;
};
}
# User-level HTTP authentication 用戶級HTTP認證
type ManifestUserHttpAuth = BaseManifestAuth & {
type: 'user_http';
authorization_type: HttpAuthorizationType;
}
type ManifestOAuthAuth = BaseManifestAuth & {
type: 'oauth';
# OAuth URL where a user is directed to for the OAuth authentication flow to begin. OAuth URL,用戶被定向到該URL以開始OAuth身份驗證流。
client_url: string;
# OAuth scopes required to accomplish operations on the user's behalf. 代表用戶完成操作所需的OAuth范圍。
scope: string;
# Endpoint used to exchange OAuth code with access token. 用于與訪問token交換OAuth代碼的端點。
authorization_url: string;
# When exchanging OAuth code with access token, the expected header 'content-type'. For example: 'content-type: application/json' 當(dāng)與訪問token交換OAuth代碼時,期望的標頭'content-type'。例如:'content-type: application/json'
authorization_content_type: string;
# When registering the OAuth client ID and secrets, the plugin service will surface a unique token. 當(dāng)注冊O(shè)Auth客戶端ID和秘密時,插件服務(wù)將顯示一個唯一的token。
verification_tokens: {
[service: string]?: string;
};
}
There are also some limits to the length of certain field in the manifest file that are subject to change over time:
清單文件中的某些字段的長度也有一些限制,這些限制會隨時間變化:
- 50 character max for
name_for_human
name_for_human
最多50個字符 - 50 character max for
name_for_model
name_for_model
最多50個字符 - 120 character max for
description_for_human
description_for_human
最多120個字符 - 8000 character max just for
description_for_model
(will decrease over time)
僅description_for_model
最多8000個字符(將隨時間減少)
Separately, we also have a 100k character limit (will decrease over time) on the API response body length which is also subject to change.
另外,我們對API響應(yīng)正文長度也有100k字符的限制(會隨著時間的推移而減少),這也會發(fā)生變化。
OpenAPI definition OpenAPI定義
The next step is to build the OpenAPI specification to document the API. The model in ChatGPT does not know anything about your API other than what is defined in the OpenAPI specification and manifest file. This means that if you have an extensive API, you need not expose all functionality to the model and can choose specific endpoints. For example, if you have a social media API, you might want to have the model access content from the site through a GET request but prevent the model from being able to comment on users posts in order to reduce the chance of spam.
下一步是構(gòu)建OpenAPI規(guī)范以記錄API。ChatGPT中的模型除了OpenAPI規(guī)范和清單文件中定義的內(nèi)容外,不知道任何關(guān)于您的API的信息。這意味著,如果您有一個擴展的API,則不需要將所有功能都公開給模型,而是可以選擇特定的端點。例如,如果您有一個社交媒體API,您可能希望讓模型通過GET請求從網(wǎng)站訪問內(nèi)容,但防止模型能夠評論用戶的帖子,以減少垃圾郵件的機會。
The OpenAPI specification is the wrapper that sits on top of your API. A basic OpenAPI specification will look like the following:
OpenAPI規(guī)范是位于API之上的包裝器。基本的OpenAPI規(guī)范如下所示:
openapi: 3.0.1
info:
title: TODO Plugin
description: A plugin that allows the user to create and manage a TODO list using ChatGPT.
version: 'v1'
servers:
- url: http://localhost:3333
paths:
/todos:
get:
operationId: getTodos
summary: Get the list of todos
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/getTodosResponse'
components:
schemas:
getTodosResponse:
type: object
properties:
todos:
type: array
items:
type: string
description: The list of todos.
We start by defining the specification version, the title, description, and version number. When a query is run in ChatGPT, it will look at the description that is defined in the info section to determine if the plugin is relevant for the user query. You can read more about prompting in the writing descriptions section.
我們首先定義規(guī)范版本、標題、描述和版本號。當(dāng)在ChatGPT中運行查詢時,它將查看在info部分中定義的描述,以確定插件是否與用戶查詢相關(guān)。您可以在寫作說明部分閱讀有關(guān)提示的更多信息。
Keep in mind the following limits in your OpenAPI specification, which are subject to change:
請記住OpenAPI規(guī)范中的以下限制,這些限制可能會發(fā)生變化:
- 200 characters max for each API endpoint description/summary field in API specification
API規(guī)范中每個API端點描述/摘要字段最多200個字符 - 200 characters max for each API param description field in API specification
API規(guī)范中每個API參數(shù)描述字段最多200個字符
Since we are running this example locally, we want to set the server to point to your localhost URL. The rest of the OpenAPI specification follows the traditional OpenAPI format, you can learn more about OpenAPI formatting through various online resources. There are also many tools that auto generate OpenAPI specifications based on your underlying API code.
由于我們是在本地運行這個示例,我們希望將服務(wù)器設(shè)置為指向您的localhost URL。OpenAPI規(guī)范的其余部分遵循傳統(tǒng)的OpenAPI格式,您可以通過各種在線資源了解有關(guān)OpenAPI格式的更多信息。還有許多工具可以根據(jù)底層API代碼自動生成OpenAPI規(guī)范。
Running a plugin 運行插件
Once you have created an API, manifest file, and OpenAPI specification for your API, you are now ready to connect the plugin via the ChatGPT UI. There are two different places your plugin might be running, either locally in a development environment or on a remote server.
一旦您為API創(chuàng)建了API、清單文件和OpenAPI規(guī)范,現(xiàn)在就可以通過ChatGPT UI連接插件了。您的插件可能在兩個不同的地方運行,要么在本地開發(fā)環(huán)境中,要么在遠程服務(wù)器上。
If you have a local version of your API running, you can point the plugin interface to your localhost server. To connect the plugin with ChatGPT, navigate to the plugin store and select “Develop your own plugin”. Enter your localhost and port number (e.g localhost:3333
). Note that only auth type none
is currently supported for localhost development.
如果您運行的是本地版本的API,則可以將插件接口指向本地主機服務(wù)器。要將插件與ChatGPT連接,請導(dǎo)航到插件商店并選擇“開發(fā)您自己的插件”。輸入本地主機和端口號(例如 localhost:3333
)。請注意,目前只有授權(quán)類型 none
支持本地主機開發(fā)。
If the plugin is running on a remote server, you will need to first select “Develop your own plugin” to set it up and then “Install an unverified plugin” to install it for yourself. You can simply add the plugin manifest file to the yourdomain.com/.well-known/
path and start testing your API. However, for subsequent changes to your manifest file, you will have to deploy the new changes to your public site which might take a long time. In that case, we suggest setting up a local server to act as a proxy for your API. This allows you to quickly prototype changes to your OpenAPI spec and manifest file.
如果插件運行在遠程服務(wù)器上,您需要首先選擇“開發(fā)您自己的插件”進行設(shè)置,然后選擇“安裝未驗證的插件”為自己安裝。您可以簡單地將插件清單文件添加到 yourdomain.com/.well-known/
路徑并開始測試您的API。但是,對于清單文件的后續(xù)更改,您必須將新更改部署到公共站點,這可能需要很長時間。在這種情況下,我們建議設(shè)置一個本地服務(wù)器作為API的代理。這允許您快速對OpenAPI規(guī)范和清單文件的更改進行原型化。
Setup a local proxy of your public API 設(shè)置公共API的本地代理
The following Python code is an example of how you can set up a simple proxy of your public facing API.
下面的Python代碼是一個示例,說明如何設(shè)置面向公共API的簡單代理。
import requests
import os
import yaml
from flask import Flask, jsonify, Response, request, send_from_directory
from flask_cors import CORS
app = Flask(__name__)
PORT = 3333
# Note: Setting CORS to allow chat.openapi.com is required for ChatGPT to access your plugin
CORS(app, origins=[f"http://localhost:{PORT}", "https://chat.openai.com"])
api_url = 'https://example.com'
@app.route('/.well-known/ai-plugin.json')
def serve_manifest():
return send_from_directory(os.path.dirname(__file__), 'ai-plugin.json')
@app.route('/openapi.yaml')
def serve_openapi_yaml():
with open(os.path.join(os.path.dirname(__file__), 'openapi.yaml'), 'r') as f:
yaml_data = f.read()
yaml_data = yaml.load(yaml_data, Loader=yaml.FullLoader)
return jsonify(yaml_data)
@app.route('/openapi.json')
def serve_openapi_json():
return send_from_directory(os.path.dirname(__file__), 'openapi.json')
@app.route('/<path:path>', methods=['GET', 'POST'])
def wrapper(path):
headers = {
'Content-Type': 'application/json',
}
url = f'{api_url}/{path}'
print(f'Forwarding call: {request.method} {path} -> {url}')
if request.method == 'GET':
response = requests.get(url, headers=headers, params=request.args)
elif request.method == 'POST':
print(request.headers)
response = requests.post(url, headers=headers, params=request.args, json=request.json)
else:
raise NotImplementedError(f'Method {request.method} not implemented in wrapper for {path=}')
return response.content
if __name__ == '__main__':
app.run(port=PORT)
Writing descriptions 書寫描述
When a user makes a query that might be a potential request that goes to a plugin, the model looks through the descriptions of the endpoints in the OpenAPI specification along with the description_for_model
in the manifest file. Just like with prompting other language models, you will want to test out multiple prompts and descriptions to see what works best.
當(dāng)用戶進行可能是去往插件的潛在請求的查詢時,模型會查看OpenAPI規(guī)范中的端點描述沿著清單文件中的 description_for_model
。就像提示其他語言模型一樣,您可能希望測試多個提示和描述,以查看哪些最有效。
The OpenAPI spec itself is a great place to give the model information about the diverse details of your API – what functions are available, with what parameters, etc. Besides using expressive, informative names for each field, the spec can also contain “description” fields for every attribute. These can be used to provide natural language descriptions of what a function does or what information a query field expects, for example. The model will be able to see these, and they will guide it in using the API. If a field is restricted to only certain values, you can also provide an “enum” with descriptive category names.
OpenAPI規(guī)范本身是一個很好的地方,可以為模型提供有關(guān)API的各種細節(jié)的信息-哪些函數(shù)可用,具有哪些參數(shù)等。除了為每個字段使用富有表現(xiàn)力的信息名稱外,規(guī)范還可以為每個屬性包含“描述”字段。例如,這些可以用于提供關(guān)于函數(shù)做什么或查詢字段期望什么信息的自然語言描述。模型將能夠看到這些,它們將指導(dǎo)它使用API。如果字段僅限于某些值,則還可以提供具有描述性類別名稱的“枚舉”。
The description_for_model
attribute gives you the freedom to instruct the model on how to use your plugin generally. Overall, the language model behind ChatGPT is highly capable of understanding natural language and following instructions. Therefore, this is a good place to put in general instructions on what your plugin does and how the model should use it properly. Use natural language, preferably in a concise yet descriptive and objective tone. You can look at some of the examples to have an idea of what this should look like. We suggest starting the description_for_model
with “Plugin for …” and then enumerating all of the functionality that your API provides.description_for_model
屬性讓你可以自由地指導(dǎo)模型如何使用你的插件??偟膩碚f,ChatGPT背后的語言模型非常能夠理解自然語言并遵循指令。因此,這是一個很好的地方,可以放置關(guān)于插件功能以及模型應(yīng)該如何正確使用它的一般說明。使用自然語言,最好使用簡潔、描述性和客觀的語氣。您可以查看一些示例,以了解這應(yīng)該是什么樣子。我們建議以“Plugin for …”開始 description_for_model
,然后枚舉您的API提供的所有功能。
Best practices
Here are some best practices to follow when writing your description_for_model
and descriptions in your OpenAPI specification, as well as when designing your API responses:
以下是在OpenAPI規(guī)范中編寫 description_for_model
和描述以及設(shè)計API響應(yīng)時需要遵循的一些最佳實踐:
- Your descriptions should not attempt to control the mood, personality, or exact responses of ChatGPT. ChatGPT is designed to write appropriate responses to plugins.
您的描述不應(yīng)試圖控制情緒、個性或ChatGPT的確切反應(yīng)。ChatGPT旨在為插件編寫適當(dāng)?shù)捻憫?yīng)。
Bad example:
When the user asks to see their todo list, always respond with “I was able to find your todo list! You have [x] todos: [list the todos here]. I can add more todos if you’d like!”
當(dāng)用戶要求查看他們的待辦事項列表時,總是回答“我能夠找到您的待辦事項列表!您有[x]個待辦事項:[此處列出todos]。如果你喜歡我可以添加更多的todos!”
Good example:
[no instructions needed for this]
[對此不需要說明]
- Your descriptions should not encourage ChatGPT to use the plugin when the user hasn’t asked for your plugin’s particular category of service.
您的描述不應(yīng)該鼓勵ChatGPT在用戶沒有要求您的插件的特定服務(wù)類別時使用該插件。
Bad example:
Whenever the user mentions any type of task or plan, ask if they would like to use the TODOs plugin to add something to their todo list.
每當(dāng)用戶提到任何類型的任務(wù)或計劃時,詢問他們是否愿意使用待辦事項插件將某些內(nèi)容添加到他們的待辦事項列表中。
Good example:
The TODO list can add, remove and view the user’s TODOs.
待辦事項列表可以添加、刪除和查看用戶的待辦事項。
- Your descriptions should not prescribe specific triggers for ChatGPT to use the plugin. ChatGPT is designed to use your plugin automatically when appropriate.
您的描述不應(yīng)規(guī)定ChatGPT使用插件的特定觸發(fā)器。ChatGPT旨在在適當(dāng)?shù)臅r候自動使用您的插件。
Bad example:
When the user mentions a task, respond with “Would you like me to add this to your TODO list? Say ‘yes’ to continue.”
當(dāng)用戶提到一個任務(wù)時,用“你想讓我把這個添加到你的TODO列表中嗎?說“是”以繼續(xù)?!?/p>
Good example:
[no instructions needed for this]
[對此不需要說明]
- Plugin API responses should return raw data instead of natural language responses unless it’s necessary. ChatGPT will provide its own natural language response using the returned data.
插件API響應(yīng)應(yīng)該返回原始數(shù)據(jù),而不是自然語言響應(yīng),除非有必要。ChatGPT將使用返回的數(shù)據(jù)提供自己的自然語言響應(yīng)。
Bad example:
I was able to find your todo list! You have 2 todos: get groceries and walk the dog. I can add more todos if you’d like!
我能找到你的待辦事項清單!你有兩個待辦事項:買點東西和遛狗。我可以添加更多的待辦事項如果你喜歡!
Good example:
{ “todos”: [ “get groceries”, “walk the dog” ] }
{“todos”:[“買雜貨”,“遛狗”] }
Debugging 排除故障
By default, the chat will not show plugin calls and other information that is not surfaced to the user. In order to get a more complete picture of how the model is interacting with your plugin, you can see the request and response by clicking the down arrow on the plugin name after interacting with the plugin.
默認情況下,聊天不會顯示插件調(diào)用和其他未向用戶顯示的信息。為了更完整地了解模型如何與插件交互,您可以在與插件交互后單擊插件名稱上的向下箭頭來查看請求和響應(yīng)。
A model call to the plugin will usually consist of a message from the model containing JSON-like parameters which are sent to the plugin, followed by a response from the plugin, and finally a message from the model utilizing the information returned by the plugin.
對插件的模型調(diào)用通常包括來自模型的消息,該消息包含發(fā)送到插件的類JSON參數(shù),隨后是來自插件的響應(yīng),最后是來自模型的消息,該消息利用插件返回的信息。
If you are developing a localhost plugin, you can also open the developer console by going to “Settings” and toggling “Open plugin devtools”. From there, you can see more verbose logs and “refresh plugin” which re-fetches the Plugin and OpenAPI specification.
如果你正在開發(fā)一個本地主機插件,你也可以打開開發(fā)者控制臺,進入“設(shè)置”并切換“打開插件devtools”。從那里,您可以看到更多詳細的日志和“刷新插件”,它重新獲取插件和OpenAPI規(guī)范。文章來源:http://www.zghlxwxcb.cn/news/detail-440577.html
其它資料下載
如果大家想繼續(xù)了解人工智能相關(guān)學(xué)習(xí)路線和知識體系,歡迎大家翻閱我的另外一篇博客《重磅 | 完備的人工智能AI 學(xué)習(xí)——基礎(chǔ)知識學(xué)習(xí)路線,所有資料免關(guān)注免套路直接網(wǎng)盤下載》
這篇博客參考了Github知名開源平臺,AI技術(shù)平臺以及相關(guān)領(lǐng)域?qū)<遥篋atawhale,ApacheCN,AI有道和黃海廣博士等約有近100G相關(guān)資料,希望能幫助到所有小伙伴們。文章來源地址http://www.zghlxwxcb.cn/news/detail-440577.html
到了這里,關(guān)于OpenAI最新官方ChatGPT聊天插件接口《接入插件快速開始》全網(wǎng)最詳細中英文實用指南和教程,助你零基礎(chǔ)快速輕松掌握全新技術(shù)(二)(附源碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!