|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- # Go OpenAI
- [![GoDoc](http://img.shields.io/badge/GoDoc-Reference-blue.svg)](https://godoc.org/github.com/sashabaranov/go-gogpt)
- [![Go Report Card](https://goreportcard.com/badge/github.com/sashabaranov/go-gogpt)](https://goreportcard.com/report/github.com/sashabaranov/go-gogpt)
- [![codecov](https://codecov.io/gh/sashabaranov/go-gogpt/branch/master/graph/badge.svg?token=bCbIfHLIsW)](https://codecov.io/gh/sashabaranov/go-gogpt)
-
- > **Note**: the repository was recently renamed from `go-gpt3` to `go-gogpt`
-
- This library provides Go clients for [OpenAI API](https://platform.gogpt.com/). We support:
-
- * ChatGPT
- * GPT-3, GPT-4
- * DALL·E 2
- * Whisper
-
- Installation:
- ```
- go get github.com/sashabaranov/go-gogpt
- ```
-
-
- ChatGPT example usage:
-
- ```go
- package main
-
- import (
- "context"
- "fmt"
- gogpt "github.com/sashabaranov/go-gogpt"
- )
-
- func main() {
- client := gogpt.NewClient("your token")
- resp, err := client.CreateChatCompletion(
- context.Background(),
- gogpt.ChatCompletionRequest{
- Model: gogpt.GPT3Dot5Turbo,
- Messages: []gogpt.ChatCompletionMessage{
- {
- Role: gogpt.ChatMessageRoleUser,
- Content: "Hello!",
- },
- },
- },
- )
-
- if err != nil {
- fmt.Printf("ChatCompletion error: %v\n", err)
- 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-gogpt"
- )
-
- 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 {
- fmt.Printf("Completion error: %v\n", err)
- return
- }
- fmt.Println(resp.Choices[0].Text)
- }
- ```
- </details>
-
- <details>
- <summary>GPT-3 streaming completion</summary>
-
- ```go
- package main
-
- import (
- "errors"
- "context"
- "fmt"
- "io"
- gogpt "github.com/sashabaranov/go-gogpt"
- )
-
- 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 {
- fmt.Printf("CompletionStream error: %v\n", err)
- 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>Audio Speech-To-Text</summary>
-
- ```go
- package main
-
- import (
- "context"
- "fmt"
-
- gogpt "github.com/sashabaranov/go-gogpt"
- )
-
- func main() {
- c := gogpt.NewClient("your token")
- ctx := context.Background()
-
- req := gogpt.AudioRequest{
- Model: gogpt.Whisper1,
- FilePath: "recording.mp3",
- }
- resp, err := c.CreateTranscription(ctx, req)
- if err != nil {
- fmt.Printf("Transcription error: %v\n", err)
- return
- }
- fmt.Println(resp.Text)
- }
- ```
- </details>
-
- <details>
- <summary>DALL-E 2 image generation</summary>
-
- ```go
- package main
-
- import (
- "bytes"
- "context"
- "encoding/base64"
- "fmt"
- gogpt "github.com/sashabaranov/go-gogpt"
- "image/png"
- "os"
- )
-
- func main() {
- c := gogpt.NewClient("your token")
- ctx := context.Background()
-
- // Sample image by link
- reqUrl := gogpt.ImageRequest{
- Prompt: "Parrot on a skateboard performs a trick, cartoon style, natural light, high detail",
- Size: gogpt.CreateImageSize256x256,
- ResponseFormat: gogpt.CreateImageResponseFormatURL,
- N: 1,
- }
-
- respUrl, err := c.CreateImage(ctx, reqUrl)
- if err != nil {
- fmt.Printf("Image creation error: %v\n", err)
- return
- }
- fmt.Println(respUrl.Data[0].URL)
-
- // Example image as base64
- reqBase64 := gogpt.ImageRequest{
- Prompt: "Portrait of a humanoid parrot in a classic costume, high detail, realistic light, unreal engine",
- Size: gogpt.CreateImageSize256x256,
- ResponseFormat: gogpt.CreateImageResponseFormatB64JSON,
- N: 1,
- }
-
- respBase64, err := c.CreateImage(ctx, reqBase64)
- if err != nil {
- fmt.Printf("Image creation error: %v\n", err)
- return
- }
-
- imgBytes, err := base64.StdEncoding.DecodeString(respBase64.Data[0].B64JSON)
- if err != nil {
- fmt.Printf("Base64 decode error: %v\n", err)
- return
- }
-
- r := bytes.NewReader(imgBytes)
- imgData, err := png.Decode(r)
- if err != nil {
- fmt.Printf("PNG decode error: %v\n", err)
- return
- }
-
- file, err := os.Create("image.png")
- if err != nil {
- fmt.Printf("File creation error: %v\n", err)
- return
- }
- defer file.Close()
-
- if err := png.Encode(file, imgData); err != nil {
- fmt.Printf("PNG encode error: %v\n", err)
- return
- }
-
- fmt.Println("The image was saved as example.png")
- }
-
- ```
- </details>
-
- <details>
- <summary>Configuring proxy</summary>
-
- ```go
- config := gogpt.DefaultConfig("token")
- proxyUrl, err := url.Parse("http://localhost:{port}")
- if err != nil {
- panic(err)
- }
- transport := &http.Transport{
- Proxy: http.ProxyURL(proxyUrl),
- }
- config.HTTPClient = &http.Client{
- Transport: transport,
- }
-
- c := gogpt.NewClientWithConfig(config)
- ```
-
- See also: https://pkg.go.dev/github.com/sashabaranov/go-gogpt#ClientConfig
- </details>
-
- <details>
- <summary>ChatGPT support context</summary>
-
- ```go
- package main
-
- import (
- "bufio"
- "context"
- "fmt"
- "os"
- "strings"
-
- "github.com/sashabaranov/go-gogpt"
- )
-
- func main() {
- client := gogpt.NewClient("your token")
- messages := make([]gogpt.ChatCompletionMessage, 0)
- reader := bufio.NewReader(os.Stdin)
- fmt.Println("Conversation")
- fmt.Println("---------------------")
-
- for {
- fmt.Print("-> ")
- text, _ := reader.ReadString('\n')
- // convert CRLF to LF
- text = strings.Replace(text, "\n", "", -1)
- messages = append(messages, gogpt.ChatCompletionMessage{
- Role: gogpt.ChatMessageRoleUser,
- Content: text,
- })
-
- resp, err := client.CreateChatCompletion(
- context.Background(),
- gogpt.ChatCompletionRequest{
- Model: gogpt.GPT3Dot5Turbo,
- Messages: messages,
- },
- )
-
- if err != nil {
- fmt.Printf("ChatCompletion error: %v\n", err)
- continue
- }
-
- content := resp.Choices[0].Message.Content
- messages = append(messages, gogpt.ChatCompletionMessage{
- Role: gogpt.ChatMessageRoleAssistant,
- Content: content,
- })
- fmt.Println(content)
- }
- }
- ```
- </details>
|