openai go
1.0.0
Golang 原生實現,可輕鬆與 OpenAI API 互動。
https://beta.openai.com/docs/api-reference/
您可以使用環境變數來儲存 API 金鑰
export OPENAI_KEY=[YOUR_KEY]
若要初始化引擎,請使用下列命令:
e := openai . New ( os . Getenv ( "OPENAI_KEY" ))
如果您想使用最強大的模型來產生文字輸出,請確保您使用的是「text-davinci-003」。此模型定義為常數openai.ModelTextDavinci003
。
您可以使用捆綁包完成+編輯來根據最後的上下文重新產生回應。
e := openai . New ( os . Getenv ( "OPENAI_KEY" ))
ctx := context . Background ()
completionResp , err := e . Completion ( ctx , & openai. CompletionOptions {
// Choose model, you can see list of available models in models.go file
Model : openai . ModelTextDavinci001 ,
// Number of completion tokens to generate response. By default - 1024
MaxTokens : 1200 ,
// Text to completion
Prompt : [] string { "Write a little bit of Wikipedia. What is that?" },
})
editResp , err := e . Edit ( ctx , & EditOptions {
Model : ModelTextDavinci001 ,
Input : completionResp . Choices [ 0 ],
Instruction : "Please rewrite a bit more and add more information about Wikipedia in different aspects. Please build based on that for 4 topics" ,
})
給出提示後,模型將傳回一個或多個預測完成情況。
注意:預設的完成令牌數量為 1024,如果您想增加或減少此限制,您應該更改MaxTokens
參數。
e := openai . New ( os . Getenv ( "OPENAI_KEY" ))
r , err := e . Completion ( context . Background (), & openai. CompletionOptions {
// Choose model, you can see list of available models in models.go file
Model : openai . ModelTextDavinci001 ,
// Number of completion tokens to generate response. By default - 1024
MaxTokens : 1200 ,
// Text to completion
Prompt : [] string { "Write a little bit of Wikipedia. What is that?" },
})
您將獲得下一個輸出:
{
"id" : " cmpl-6SrcYDLCVT7xyHKVNuSLNuhRvwOJ1 " ,
"object" : " text_completion " ,
"created" : 1672337322 ,
"model" : " text-davinci-001 " ,
"choices" : [
{
"text" : " nn Wikipedia is a free online encyclopedia, created and edited by volunteers. " ,
"index" : 0 ,
"finish_reason" : " stop "
}
],
"usage" : {
"prompt_tokens" : 11 ,
"completion_tokens" : 15 ,
"total_tokens" : 26
}
}
要僅獲取文本,您應該使用以下程式碼:
fmt . Println ( r . Choices [ 0 ]. Text )
所以,完整的程式碼將是:
package main
import (
"context"
"encoding/json"
"log"
"os"
"testing"
"github.com/0x9ef/openai-go"
)
func main () {
e := openai . New ( os . Getenv ( "OPENAI_KEY" ))
r , err := e . Completion ( context . Background (), & openai. CompletionOptions {
// Choose model, you can see list of available models in models.go file
Model : openai . ModelTextDavinci001 ,
// Text to completion
Prompt : [] string { "Write a little bit of Wikipedia. What is that?" },
})
if b , err := json . MarshalIndent ( r , "" , " " ); err != nil {
panic ( err )
} else {
fmt . Println ( string ( b ))
}
// Wikipedia is a free online encyclopedia, created and edited by volunteers.
fmt . Println ( "What is the Wikipedia?" , r . Choices [ 0 ]. Text )
}
列出目前可用的型號,並提供每個型號的基本信息,例如所有者和可用性。
e := openai . New ( os . Getenv ( "OPENAI_KEY" ))
r , err := e . ListModels ( context . Background ())
if err != nil {
log . Fatal ( err )
}
您將獲得下一個輸出:
{
"data" : [
{
"id" : " babbage " ,
"object" : " model " ,
"owned_by" : " openai "
},
{
"id" : " ada " ,
"object" : " model " ,
"owned_by" : " openai "
},
{
"id" : " text-davinci-002 " ,
"object" : " model " ,
"owned_by" : " openai "
},
{
"id" : " davinci " ,
"object" : " model " ,
"owned_by" : " openai "
},
...
]
}
要檢索指定模型而不是所有模型的信息,您可以執行以下操作:
e := openai . New ( os . Getenv ( "OPENAI_KEY" ))
r , err := e . RetrieveModel ( context . Background (), & openai. RetrieveModelOptions {
ID : openai . ModelDavinci ,
})
if err != nil {
log . Fatal ( err )
}
麻省理工學院