OpenAI ChatGPT and GPT-3 API client for Go
Installation:
go get github.com/zhangyiming748/go-gpt3
Example usage:
package main
import (
"context"
"fmt"
gogpt "github.com/zhangyiming748/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/zhangyiming748/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)
}
}