gpt var
1.0.0
GPTVAR促進了與OpenAI API的輕鬆溝通,從而使提示的無縫發送並接收了響應。它可以針對其他模型進行調整,從而簡化了使用GPT生成的內容為變量創建的值。
使用NPM安裝GPTVAR :
npm install gpt-var在幾個簡單的步驟中使用GPTVAR :
導入班級
首先將GPTVAR導入您的打字稿文件:
import GPTVAR from 'gpt-var' ;創建一個實例
使用您的OpenAI API密鑰初始化GPTVAR ,並且可以選擇地使用您的首選模型。有關模型列表,請訪問OpenAI模型概述。
const gptVar = new GPTVAR ( 'your-openai-api-key' , 'model-name' ) ;發送提示
帶有您的消息和所需格式的調用prompt 。格式可以是“任何”,“數組”,“對象”或“ ObjectInarray”。
gptVar . prompt ( 'Your prompt here' , 'any' ) . then ( response => {
console . log ( response ) ;
} ) . catch ( error => {
console . error ( error ) ;
} ) ;處理響應
prompt通過GPT模型的響應返回承諾,然後您可以根據需要對其進行處理。
prompt(message: string, format: string)此方法將消息發送給GPT模型,期望以指定格式進行響應。
messages :GPT模型的輸入字符串。format :預期響應格式。選項:any :返回一個原始字符串,適用於靈活或未指定格式。object :期望一個JSON對象,非常適合結構化數據。array :期望一個JSON數組,非常適合列表或序列。objectInArray :期望有一系列JSON對象,對結構化列表有用。 格式: any
將響應返回為原始字符串,非常適合非結構化數據。
gptVar . prompt ( 'Tell me a joke' , 'any' ) . then ( response => {
console . log ( 'Response:' , response ) ;
} ) ;示例響應:
"Why don't scientists trust atoms? Because they make up everything!"
格式: object
期望JSON對象響應。
gptVar . prompt ( 'Provide details about the Eiffel Tower' , 'object' ) . then ( response => {
console . log ( 'Response:' , response ) ;
} ) ;示例響應:
{
"name" : " Eiffel Tower " ,
"location" : " Paris, France " ,
"height" : " 300 meters "
}格式: array
期望JSON陣列響應。
gptVar . prompt ( 'List three famous scientists' , 'array' ) . then ( response => {
console . log ( 'Response:' , response ) ;
} ) ;示例響應:
[ " Albert Einstein " , " Marie Curie " , " Isaac Newton " ]格式: objectInArray
期望一系列JSON對象。
gptVar . prompt ( 'List major cities with their countries and populations' , 'objectInArray' ) . then ( response => {
console . log ( 'Response:' , response ) ;
} ) ;示例響應:
[
{
"city" : " New York City " ,
"country" : " USA " ,
"population" : " 8.4 million "
},
{
"city" : " Tokyo " ,
"country" : " Japan " ,
"population" : " 9.3 million "
},
{
"city" : " London " ,
"country" : " UK " ,
"population" : " 8.9 million "
}
]為了獲得支持,問題或功能請求,請在我們的GitHub存儲庫上提交問題。