Skip to content

Commit

Permalink
Rename and update docs (sashabaranov#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
sashabaranov authored Mar 4, 2023
1 parent 202b629 commit 47887bf
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 25 deletions.
116 changes: 108 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:

<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()
Expand All @@ -38,8 +85,10 @@ func main() {
fmt.Println(resp.Choices[0].Text)
}
```
</details>

Streaming response example:
<details>
<summary>GPT-3 streaming completion</summary>

```go
package main
Expand All @@ -49,7 +98,57 @@ import (
"context"
"fmt"
"io"
gogpt "github.com/sashabaranov/go-gpt3"
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() {
Expand Down Expand Up @@ -85,3 +184,4 @@ func main() {
}
}
```
</details>
2 changes: 1 addition & 1 deletion api_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gogpt_test

import (
. "github.com/sashabaranov/go-gpt3"
. "github.com/sashabaranov/go-openai"

"context"
"errors"
Expand Down
4 changes: 2 additions & 2 deletions audio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions completion_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions edits_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion embeddings_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gogpt_test

import (
. "github.com/sashabaranov/go-gpt3"
. "github.com/sashabaranov/go-openai"

"bytes"
"encoding/json"
Expand Down
4 changes: 2 additions & 2 deletions files_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/sashabaranov/go-gpt3
module github.com/sashabaranov/go-openai

go 1.17
4 changes: 2 additions & 2 deletions image_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions moderation_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions stream_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 47887bf

Please sign in to comment.