diff --git a/README.md b/README.md index 207e554..076c1a3 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,22 @@ -# go-gpt3 -[![GoDoc](http://img.shields.io/badge/GoDoc-Reference-blue.svg)](https://godoc.org/github.com/sashabaranov/go-gpt3) -[![Go Report Card](https://goreportcard.com/badge/github.com/sashabaranov/go-gpt3)](https://goreportcard.com/report/github.com/sashabaranov/go-gpt3) +# Go OpenAI +[![GoDoc](http://img.shields.io/badge/GoDoc-Reference-blue.svg)](https://godoc.org/github.com/sashabaranov/go-openai) +[![Go Report Card](https://goreportcard.com/badge/github.com/sashabaranov/go-openai)](https://goreportcard.com/report/github.com/sashabaranov/go-openai) -[OpenAI ChatGPT](https://platform.openai.com/), GPT-3, DALL·E 2, and Whisper API client for Go +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-gpt3 +go get github.com/sashabaranov/go-openai ``` -Example usage: +ChatGPT example usage: ```go package main @@ -22,6 +27,48 @@ import ( 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: + +
+GPT-3 completion + +```go +package main + +import ( + "context" + "fmt" + gogpt "github.com/sashabaranov/go-openai" +) + func main() { c := gogpt.NewClient("your token") ctx := context.Background() @@ -38,8 +85,10 @@ func main() { fmt.Println(resp.Choices[0].Text) } ``` +
-Streaming response example: +
+GPT-3 streaming completion ```go package main @@ -49,7 +98,7 @@ import ( "context" "fmt" "io" - gogpt "github.com/sashabaranov/go-gpt3" + gogpt "github.com/sashabaranov/go-openai" ) func main() { @@ -85,3 +134,54 @@ func main() { } } ``` +
+ + +
+GPT-3 streaming completion + +```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) + } +} +``` +
diff --git a/api_test.go b/api_test.go index e5c3be4..c770328 100644 --- a/api_test.go +++ b/api_test.go @@ -1,7 +1,7 @@ package gogpt_test import ( - . "github.com/sashabaranov/go-gpt3" + . "github.com/sashabaranov/go-openai" "context" "errors" diff --git a/audio_test.go b/audio_test.go index 52bf269..ab2e477 100644 --- a/audio_test.go +++ b/audio_test.go @@ -11,8 +11,8 @@ import ( "path/filepath" "strings" - . "github.com/sashabaranov/go-gpt3" - "github.com/sashabaranov/go-gpt3/internal/test" + . "github.com/sashabaranov/go-openai" + "github.com/sashabaranov/go-openai/internal/test" "context" "testing" diff --git a/completion_test.go b/completion_test.go index 594a23c..398587c 100644 --- a/completion_test.go +++ b/completion_test.go @@ -1,8 +1,8 @@ package gogpt_test import ( - . "github.com/sashabaranov/go-gpt3" - "github.com/sashabaranov/go-gpt3/internal/test" + . "github.com/sashabaranov/go-openai" + "github.com/sashabaranov/go-openai/internal/test" "context" "encoding/json" diff --git a/edits_test.go b/edits_test.go index e61c0ae..840ff72 100644 --- a/edits_test.go +++ b/edits_test.go @@ -1,8 +1,8 @@ package gogpt_test import ( - . "github.com/sashabaranov/go-gpt3" - "github.com/sashabaranov/go-gpt3/internal/test" + . "github.com/sashabaranov/go-openai" + "github.com/sashabaranov/go-openai/internal/test" "context" "encoding/json" diff --git a/embeddings_test.go b/embeddings_test.go index daa74e2..b6f3601 100644 --- a/embeddings_test.go +++ b/embeddings_test.go @@ -1,7 +1,7 @@ package gogpt_test import ( - . "github.com/sashabaranov/go-gpt3" + . "github.com/sashabaranov/go-openai" "bytes" "encoding/json" diff --git a/files_test.go b/files_test.go index 5c563f4..7f2daec 100644 --- a/files_test.go +++ b/files_test.go @@ -1,8 +1,8 @@ package gogpt_test import ( - . "github.com/sashabaranov/go-gpt3" - "github.com/sashabaranov/go-gpt3/internal/test" + . "github.com/sashabaranov/go-openai" + "github.com/sashabaranov/go-openai/internal/test" "context" "encoding/json" diff --git a/go.mod b/go.mod index 4b6bb42..aa559ab 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/sashabaranov/go-gpt3 +module github.com/sashabaranov/go-openai go 1.17 diff --git a/image_test.go b/image_test.go index c273fcf..b688fdf 100644 --- a/image_test.go +++ b/image_test.go @@ -1,8 +1,8 @@ package gogpt_test import ( - . "github.com/sashabaranov/go-gpt3" - "github.com/sashabaranov/go-gpt3/internal/test" + . "github.com/sashabaranov/go-openai" + "github.com/sashabaranov/go-openai/internal/test" "context" "encoding/json" diff --git a/moderation_test.go b/moderation_test.go index 68f0e64..ac90a9c 100644 --- a/moderation_test.go +++ b/moderation_test.go @@ -1,8 +1,8 @@ package gogpt_test import ( - . "github.com/sashabaranov/go-gpt3" - "github.com/sashabaranov/go-gpt3/internal/test" + . "github.com/sashabaranov/go-openai" + "github.com/sashabaranov/go-openai/internal/test" "context" "encoding/json" diff --git a/stream_test.go b/stream_test.go index 8d2bfa2..62f987d 100644 --- a/stream_test.go +++ b/stream_test.go @@ -1,8 +1,8 @@ package gogpt_test import ( - . "github.com/sashabaranov/go-gpt3" - "github.com/sashabaranov/go-gpt3/internal/test" + . "github.com/sashabaranov/go-openai" + "github.com/sashabaranov/go-openai/internal/test" "context" "errors"