Rename and update docs (#120)

This commit is contained in:
sashabaranov
2023-03-04 15:18:43 +04:00
committed by GitHub
parent 202b629bc1
commit 47887bf123
11 changed files with 125 additions and 25 deletions

116
README.md
View File

@@ -1,17 +1,22 @@
# go-gpt3 # Go OpenAI
[![GoDoc](http://img.shields.io/badge/GoDoc-Reference-blue.svg)](https://godoc.org/github.com/sashabaranov/go-gpt3) [![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-gpt3)](https://goreportcard.com/report/github.com/sashabaranov/go-gpt3) [![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: Installation:
``` ```
go get github.com/sashabaranov/go-gpt3 go get github.com/sashabaranov/go-openai
``` ```
Example usage: ChatGPT example usage:
```go ```go
package main package main
@@ -22,6 +27,48 @@ import (
gogpt "github.com/sashabaranov/go-gpt3" 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() { func main() {
c := gogpt.NewClient("your token") c := gogpt.NewClient("your token")
ctx := context.Background() ctx := context.Background()
@@ -38,8 +85,10 @@ func main() {
fmt.Println(resp.Choices[0].Text) fmt.Println(resp.Choices[0].Text)
} }
``` ```
</details>
Streaming response example: <details>
<summary>GPT-3 streaming completion</summary>
```go ```go
package main package main
@@ -49,7 +98,7 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
gogpt "github.com/sashabaranov/go-gpt3" gogpt "github.com/sashabaranov/go-openai"
) )
func main() { func main() {
@@ -85,3 +134,54 @@ func main() {
} }
} }
``` ```
</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>

View File

@@ -1,7 +1,7 @@
package gogpt_test package gogpt_test
import ( import (
. "github.com/sashabaranov/go-gpt3" . "github.com/sashabaranov/go-openai"
"context" "context"
"errors" "errors"

View File

@@ -11,8 +11,8 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
. "github.com/sashabaranov/go-gpt3" . "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-gpt3/internal/test" "github.com/sashabaranov/go-openai/internal/test"
"context" "context"
"testing" "testing"

View File

@@ -1,8 +1,8 @@
package gogpt_test package gogpt_test
import ( import (
. "github.com/sashabaranov/go-gpt3" . "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-gpt3/internal/test" "github.com/sashabaranov/go-openai/internal/test"
"context" "context"
"encoding/json" "encoding/json"

View File

@@ -1,8 +1,8 @@
package gogpt_test package gogpt_test
import ( import (
. "github.com/sashabaranov/go-gpt3" . "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-gpt3/internal/test" "github.com/sashabaranov/go-openai/internal/test"
"context" "context"
"encoding/json" "encoding/json"

View File

@@ -1,7 +1,7 @@
package gogpt_test package gogpt_test
import ( import (
. "github.com/sashabaranov/go-gpt3" . "github.com/sashabaranov/go-openai"
"bytes" "bytes"
"encoding/json" "encoding/json"

View File

@@ -1,8 +1,8 @@
package gogpt_test package gogpt_test
import ( import (
. "github.com/sashabaranov/go-gpt3" . "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-gpt3/internal/test" "github.com/sashabaranov/go-openai/internal/test"
"context" "context"
"encoding/json" "encoding/json"

2
go.mod
View File

@@ -1,3 +1,3 @@
module github.com/sashabaranov/go-gpt3 module github.com/sashabaranov/go-openai
go 1.17 go 1.17

View File

@@ -1,8 +1,8 @@
package gogpt_test package gogpt_test
import ( import (
. "github.com/sashabaranov/go-gpt3" . "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-gpt3/internal/test" "github.com/sashabaranov/go-openai/internal/test"
"context" "context"
"encoding/json" "encoding/json"

View File

@@ -1,8 +1,8 @@
package gogpt_test package gogpt_test
import ( import (
. "github.com/sashabaranov/go-gpt3" . "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-gpt3/internal/test" "github.com/sashabaranov/go-openai/internal/test"
"context" "context"
"encoding/json" "encoding/json"

View File

@@ -1,8 +1,8 @@
package gogpt_test package gogpt_test
import ( import (
. "github.com/sashabaranov/go-gpt3" . "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-gpt3/internal/test" "github.com/sashabaranov/go-openai/internal/test"
"context" "context"
"errors" "errors"