gpt go
v0.1.0
英语| 中文
Openai Docs API参考:https://platform.openai.com/docs/api-reference/introduction
注意:已经支持GPT-4 API,请使用聊天完成API。
# clone the project
git clone https://github.com/hanyuancheung/gpt-go.git
# go to the project directory
cd gpt-go
# set API_KEY as environment variable
export API_KEY={YOUR_API_KEY} chatgpt
# go build example binary
make chatgpt-example
# run example
./chatgpt
API_KEY
https://platform.openai.com/account/api-keys
查看GO文档,以获取有关提供的类型和方法的更详细的文档:https://pkg.go.dev/github.com/hanyuancheung/gpt-go
func main () {
client := gpt . NewClient ( "API_KEY" )
err := client . ChatCompletionStream ( context . Background (), & gpt. ChatCompletionRequest {
Model : gpt . GPT3Dot5Turbo ,
Messages : []gpt. ChatCompletionRequestMessage {
{
Role : "user" ,
Content : "Hello!" ,
},
},
}, func ( response * gpt. ChatCompletionStreamResponse ) {
fmt . Print ( response . Choices [ 0 ]. Delta . Content )
})
if err != nil {
fmt . Printf ( "ChatCompletionStream error: %v n " , err )
return
}
}
func main () {
client := gpt . NewClient ( "API_KEY" )
rsp , err := client . CompletionWithEngine ( context . Background (), & gpt. CompletionRequest {
Model : gpt . TextDavinci003Engine ,
Prompt : [] string { "Hello!" },
})
if err != nil {
fmt . Printf ( "ChatCompletionStream error: %v n " , err )
return
}
fmt . Print ( rsp . Choices [ 0 ]. Text )
}
func main () {
client := gpt . NewClient ( "API_KEY" )
rsp , err := client . Image ( context . Background (), & gpt. ImageRequest {
Prompt : "Chicken" ,
})
if err != nil {
fmt . Printf ( "ChatCompletionStream error: %v n " , err )
return
}
fmt . Print ( rsp . Data [ 0 ]. URL )
}
在为拉动请求做出大量努力之前,请在Github上打开一个问题。提交给PR的代码必须使用gofmt
过滤。
此软件包已根据MIT许可获得许可。有关详细信息,请参见许可证。
如果这个项目对您有所帮助,请给!