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 )
}
麻省理工学院