DengBiao 53f13dd698 | 1 year ago | |
---|---|---|
.. | ||
.gitignore | 1 year ago | |
.golangci.yml | 1 year ago | |
LICENSE | 1 year ago | |
Makefile | 1 year ago | |
README.md | 1 year ago | |
answers.go | 1 year ago | |
api.go | 1 year ago | |
chat.go | 1 year ago | |
common.go | 1 year ago | |
completion.go | 1 year ago | |
config.go | 1 year ago | |
edits.go | 1 year ago | |
embeddings.go | 1 year ago | |
engines.go | 1 year ago | |
error.go | 1 year ago | |
files.go | 1 year ago | |
image.go | 1 year ago | |
models.go | 1 year ago | |
moderation.go | 1 year ago | |
stream.go | 1 year ago |
OpenAI ChatGPT and GPT-3 API client for Go
Installation:
go get github.com/sashabaranov/go-gpt3
Example usage:
package main
import (
"context"
"fmt"
gogpt "github.com/sashabaranov/go-gpt3"
)
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)
}
Streaming response example:
package main
import (
"errors"
"context"
"fmt"
"io"
gogpt "github.com/sashabaranov/go-gpt3"
)
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)
}
}