188 lines
2.9 KiB
Markdown
188 lines
2.9 KiB
Markdown
# Go OpenAI
|
|
[](https://godoc.org/github.com/sashabaranov/go-openai)
|
|
[](https://goreportcard.com/report/github.com/sashabaranov/go-openai)
|
|
|
|
|
|
This library provides Go clients for [OpenAI API](https://platform.openai.com/). We support:
|
|
|
|
* ChatGPT
|
|
* GPT-3
|
|
* DALL·E 2
|
|
* Whisper
|
|
|
|
Installation:
|
|
```
|
|
go get github.com/sashabaranov/go-openai
|
|
```
|
|
|
|
|
|
ChatGPT example usage:
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
gogpt "github.com/sashabaranov/go-gpt3"
|
|
)
|
|
|
|
func main() {
|
|
c := gogpt.NewClient("your token")
|
|
ctx := context.Background()
|
|
|
|
resp, err := c.CreateChatCompletion(
|
|
ctx,
|
|
gogpt.ChatCompletionRequest{
|
|
Model: gogpt.GPT3Dot5Turbo,
|
|
Messages: []gogpt.ChatCompletionMessage{
|
|
{
|
|
Role: "user",
|
|
Content: "Hello!",
|
|
},
|
|
},
|
|
},
|
|
)
|
|
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
fmt.Println(resp.Choices[0].Message.Content)
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Other examples:
|
|
|
|
<details>
|
|
<summary>GPT-3 completion</summary>
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
gogpt "github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
func main() {
|
|
c := gogpt.NewClient("your token")
|
|
ctx := context.Background()
|
|
|
|
req := gogpt.CompletionRequest{
|
|
Model: gogpt.GPT3Ada,
|
|
MaxTokens: 5,
|
|
Prompt: "Lorem ipsum",
|
|
}
|
|
resp, err := c.CreateCompletion(ctx, req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
fmt.Println(resp.Choices[0].Text)
|
|
}
|
|
```
|
|
</details>
|
|
|
|
<details>
|
|
<summary>GPT-3 streaming completion</summary>
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
gogpt "github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
func main() {
|
|
c := gogpt.NewClient("your token")
|
|
ctx := context.Background()
|
|
|
|
req := gogpt.CompletionRequest{
|
|
Model: gogpt.GPT3Ada,
|
|
MaxTokens: 5,
|
|
Prompt: "Lorem ipsum",
|
|
Stream: true,
|
|
}
|
|
stream, err := c.CreateCompletionStream(ctx, req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer stream.Close()
|
|
|
|
for {
|
|
response, err := stream.Recv()
|
|
if errors.Is(err, io.EOF) {
|
|
fmt.Println("Stream finished")
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
fmt.Printf("Stream error: %v\n", err)
|
|
return
|
|
}
|
|
|
|
|
|
fmt.Printf("Stream response: %v\n", response)
|
|
}
|
|
}
|
|
```
|
|
</details>
|
|
|
|
|
|
<details>
|
|
<summary>GPT-3 streaming completion</summary>
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
gogpt "github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
func main() {
|
|
c := gogpt.NewClient("your token")
|
|
ctx := context.Background()
|
|
|
|
req := gogpt.CompletionRequest{
|
|
Model: gogpt.GPT3Ada,
|
|
MaxTokens: 5,
|
|
Prompt: "Lorem ipsum",
|
|
Stream: true,
|
|
}
|
|
stream, err := c.CreateCompletionStream(ctx, req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer stream.Close()
|
|
|
|
for {
|
|
response, err := stream.Recv()
|
|
if errors.Is(err, io.EOF) {
|
|
fmt.Println("Stream finished")
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
fmt.Printf("Stream error: %v\n", err)
|
|
return
|
|
}
|
|
|
|
|
|
fmt.Printf("Stream response: %v\n", response)
|
|
}
|
|
}
|
|
```
|
|
</details>
|