安全的關(guān)鍵.png)
GraphRAG:基于PolarDB+通義千問(wèn)api+LangChain的知識(shí)圖譜定制實(shí)踐
有了 API 密鑰之后,就可以進(jìn)行測(cè)試?yán)病?/p>
Unsplash API 的功能很多,因?yàn)槲覀冃枰ㄟ^(guò) API 在 Unsplash 里搜索圖片,我看了下文檔,只需要用到 “Search Photos (搜索圖片)” 這個(gè)功能。
這個(gè)文檔可以幫助我們了解如何使用 Unsplash 的 API 搜索圖片。如果讀不懂也沒(méi)關(guān)系,稍后可以喂給 ChatGPT,它是能看懂的。
在創(chuàng)建 GPTs 之前,最好先測(cè)試下 API 是否能獲得成功的響應(yīng)。目前 Postman 是最好的工具,如果沒(méi)有的話可以安裝一下。
如果不知道如何用 Postman 進(jìn)行測(cè)試的話,也可以問(wèn) ChatGPT 。我是這么寫提示詞的:
Prompt: How can I use Postman to test an API based on this documentation:
[Relevant API Document]
提示詞: 如何使用 Postman 測(cè)試基于此文檔的 API? [相關(guān) API 文檔]
中括號(hào)里的內(nèi)容就是喂給 ChatGPT 的 API 文檔。
提交給 ChatGPT 之后,它就生成了一份非常詳細(xì)的操作步驟,我直接跟著做就好。
主要其實(shí)就兩步。1. 把 API Key 填入 Postman。
接著點(diǎn)擊 “Send” 按鈕就可以發(fā)送請(qǐng)求。發(fā)送完請(qǐng)求,就收到了響應(yīng)。并且狀態(tài)碼顯示 “200”,表示成功響應(yīng)。
Schema 就是告訴 GPTs,如何跟 Unsplash 的 API 相互溝通。這個(gè) Schema 也可以讓 ChatGPT 輔助完成。用 Postman 測(cè)試的另一個(gè)目的就是,可以把 Postman 生成的 CURL 請(qǐng)求和響應(yīng)提供給 ChatGPT 做參考。ChatGPT 有了這些信息,生成的 Schema 的準(zhǔn)確度更高。
上圖就是響應(yīng)的主體。不過(guò) Unsplash 的響應(yīng)有點(diǎn)長(zhǎng),直接扔給 ChatGPT 可能會(huì)超過(guò) Token 限制。我只選取了其中最相關(guān)的部分提交,這樣 ChatGPT 寫出來(lái)的 Schema 也會(huì)更簡(jiǎn)潔一些。
CURL 請(qǐng)求在 Postman 的右上角,如下圖所示。
有了這些信息,就可以讓 ChatGPT 幫我寫 Schema 啦。然而如何為 GPTs 寫 Schema 是很新的知識(shí),ChatGPT 還不太了解。為此我找了個(gè)可以輔助造 GPTs 的 GPTs,訪問(wèn)地址是:https://chat.openai.com/g/g-iThwkWDbA。用這個(gè) GPTs 寫出的 Schema 準(zhǔn)確度更高。我寫了這樣一個(gè)提示詞讓它幫我生成 Schema:
Prompt: Below is the curl request and response from the Unsplash API. Can you generate the schema for me?
[CURL Request and Response]
提示詞: 以下是 Unsplash API 的 curl 請(qǐng)求和響應(yīng)。你能為我生成 Schema 嗎?
[CURL 請(qǐng)求和響應(yīng)]
有了 Schema 之后,創(chuàng)建 GPTs 就簡(jiǎn)單多啦??梢圆还芷渌渲茫劝褎偛?GPT 生成的 Schema 填進(jìn)去。
填進(jìn)去以后發(fā)現(xiàn)報(bào)錯(cuò)信息:
Could not find a valid URL in
servers
無(wú)法在
servers
中找到有效 URL
觀察一下 OpenAI 給的參考 Schema 就會(huì)發(fā)現(xiàn)少了 “Servers” 這個(gè)鍵,自己手動(dòng)補(bǔ)充一下就好。
{
"openapi": "3.1.0",
"info": {
"title": "Get weather data",
"description": "Retrieves current weather data for a location.",
"version": "v1.0.0"
},
"servers": [
{
"url": "https://weather.example.com"
}
],
"paths": {
"/location": {
"get": {
"description": "Get temperature for a specific location",
"operationId": "GetCurrentWeather",
"parameters": [
{
"name": "location",
"in": "query",
"description": "The city and state to retrieve the weather for",
"required": true,
"schema": {
"type": "string"
}
}
],
"deprecated": false
}
}
},
"components": {
"schemas": {}
}
}
修改之后,就沒(méi)有報(bào)錯(cuò)信息啦。
完整的 Schema 是這樣的:
{
"openapi": "3.1.0",
"info": {
"title": "Unsplash API",
"version": "1.0.0",
"description": "Schema for the Unsplash API response for searching photos."
},
"servers": [
{
"url": "https://api.unsplash.com"
}
],
"paths": {
"/search/photos": {
"get": {
"summary": "Search photos",
"operationId": "searchPhotos",
"parameters": [
{
"name": "query",
"in": "query",
"required": true,
"description": "The search query term.",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"total": {
"type": "integer",
"description": "Total number of results."
},
"total_pages": {
"type": "integer",
"description": "Total number of pages."
},
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"width": {
"type": "integer",
"description": "Width of the image."
},
"height": {
"type": "integer",
"description": "Height of the image."
},
"color": {
"type": "string",
"description": "Dominant color of the image."
},
"alt_description": {
"type": "string",
"description": "Alternative description of the image."
},
"urls": {
"type": "object",
"properties": {
"raw": {
"type": "string",
"format": "uri",
"description": "URL of the raw image."
},
"full": {
"type": "string",
"format": "uri",
"description": "URL of the full-size image."
},
"regular": {
"type": "string",
"format": "uri",
"description": "URL of the regular-size image."
},
"small": {
"type": "string",
"format": "uri",
"description": "URL of the small-size image."
},
"thumb": {
"type": "string",
"format": "uri",
"description": "URL of the thumbnail image."
},
"small_s3": {
"type": "string",
"format": "uri",
"description": "URL of the small-size image hosted on S3."
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
接著就是在 GPTs 里面填好 API Key。取決于 API Key 的類型,一般來(lái)說(shuō)都是下圖所示的填法。
然后可以完善 GPTs 的配置部分。Instructions 里面可以先說(shuō)明如何利用 API,其它內(nèi)容可以在測(cè)試好 GPTs 的 Actions 之后完善。我寫的 Instructions 內(nèi)容如下:
When users initiate an image search on our platform, the system should employ the ‘searchPhotos’ action to fetch relevant results. The task involves several key steps:
- Implement the ‘searchPhotos’ action to retrieve image search results based on user queries.
- For each image result, extract and display the following details:
- The URL of the image, using ‘urls.regular’ as the source.
- A brief description of the image.
- The dimensions of the image, specifically its width and height.
- The name of the image’s author, sourced from ‘user.name’.
- Configure the display settings to show a default of three random images per search result.
當(dāng)用戶在我們的平臺(tái)上發(fā)起圖片搜索時(shí),系統(tǒng)應(yīng)使用 “searchPhotos “操作來(lái)獲取相關(guān)結(jié)果。這項(xiàng)任務(wù)涉及幾個(gè)關(guān)鍵步驟:
- 執(zhí)行 “searchPhotos “操作,根據(jù)用戶查詢檢索圖片搜索結(jié)果。
- 對(duì)于每個(gè)圖像結(jié)果,提取并顯示以下詳細(xì)信息:
- 圖片的 URL,使用 “urls.regular “作為來(lái)源。
- 圖片的簡(jiǎn)要描述。
- 圖片的尺寸,尤其是寬度和高度。
- 圖片作者的姓名,來(lái)源于 “user.name”。
- 配置顯示設(shè)置,使每個(gè)搜索結(jié)果默認(rèn)顯示三張隨機(jī)圖片。
接著就可以進(jìn)行測(cè)試?yán)?。如下圖所示,測(cè)試成功,大功告成。
創(chuàng)建 AI 應(yīng)用程序放到去年,我連做夢(mèng)都想不出來(lái)。如今,我也可以徒手造一個(gè)出來(lái)啦。創(chuàng)造的過(guò)程本身就是一種幸福。希望本文對(duì)你有所啟發(fā),也能收獲同樣的幸福體驗(yàn)。
文章轉(zhuǎn)自微信公眾號(hào)@Vito的AI力量
GraphRAG:基于PolarDB+通義千問(wèn)api+LangChain的知識(shí)圖譜定制實(shí)踐
使用Node.js、Express和MySQL構(gòu)建REST API
天氣API推薦:精準(zhǔn)獲取氣象數(shù)據(jù)的首選
基于自定義數(shù)據(jù)集的微調(diào):Alpaca與LLaMA模型的訓(xùn)練
OAuth和OpenID Connect圖解指南
有哪些新聞媒體提供Open API?
現(xiàn)在做大模型,還有靠譜且免費(fèi)的API接口嗎?
如何運(yùn)用AI提高自己的工作效率?
區(qū)塊鏈API推薦,快速開發(fā)去中心化應(yīng)用
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)