sashabaranov c34bc77f1a Add testable request builder (#162)
* Add testable request builder

* improve tests
2023-03-15 13:16:33 +04:00
2023-01-28 22:32:23 +04:00
2023-03-15 13:16:33 +04:00
2023-03-04 15:29:42 +04:00
2023-03-04 15:29:42 +04:00
2023-03-08 19:50:41 +04:00
2023-03-15 13:16:33 +04:00
2023-03-04 15:29:42 +04:00
2023-03-04 15:29:42 +04:00
2023-03-04 15:29:42 +04:00
2023-03-04 15:29:42 +04:00
2023-03-15 13:16:33 +04:00
2023-03-04 15:29:42 +04:00
2023-03-15 13:16:33 +04:00
2023-03-04 15:29:42 +04:00
2023-03-04 15:29:42 +04:00
2023-03-15 13:16:33 +04:00
2023-03-09 23:56:23 +04:00
2023-03-04 15:29:59 +04:00
2023-03-15 13:16:33 +04:00
2020-08-18 17:42:55 +03:00
2022-08-11 15:29:23 +06:00
2023-03-09 23:56:23 +04:00
2023-03-04 15:29:42 +04:00
2023-03-04 15:29:42 +04:00
2023-03-15 11:47:36 +04:00
2023-03-04 15:29:42 +04:00
2023-03-04 15:29:42 +04:00

Go OpenAI

GoDoc Go Report Card codecov

Note

: the repository was recently renamed from go-gpt3 to go-openai

This library provides Go clients for OpenAI API. We support:

  • ChatGPT
  • GPT-3, GPT-4
  • DALL·E 2
  • Whisper

Installation:

go get github.com/sashabaranov/go-openai

ChatGPT example usage:

package main

import (
	"context"
	"fmt"
	openai "github.com/sashabaranov/go-openai"
)

func main() {
	client := openai.NewClient("your token")
	resp, err := client.CreateChatCompletion(
		context.Background(),
		openai.ChatCompletionRequest{
			Model: openai.GPT3Dot5Turbo,
			Messages: []openai.ChatCompletionMessage{
				{
					Role:    openai.ChatMessageRoleUser,
					Content: "Hello!",
				},
			},
		},
	)

	if err != nil {
		fmt.Printf("ChatCompletion error: %v\n", err)
		return
	}

	fmt.Println(resp.Choices[0].Message.Content)
}

Other examples:

GPT-3 completion
package main

import (
	"context"
	"fmt"
	openai "github.com/sashabaranov/go-openai"
)

func main() {
	c := openai.NewClient("your token")
	ctx := context.Background()

	req := openai.CompletionRequest{
		Model:     openai.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)
}
GPT-3 streaming completion
package main

import (
	"errors"
	"context"
	"fmt"
	"io"
	openai "github.com/sashabaranov/go-openai"
)

func main() {
	c := openai.NewClient("your token")
	ctx := context.Background()

	req := openai.CompletionRequest{
		Model:     openai.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)
	}
}
Audio Speech-To-Text
package main

import (
	"context"
	"fmt"

	openai "github.com/sashabaranov/go-openai"
)

func main() {
	c := openai.NewClient("your token")
	ctx := context.Background()

	req := openai.AudioRequest{
		Model:    openai.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)
}
Configuring proxy
config := openai.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 := openai.NewClientWithConfig(config)

See also: https://pkg.go.dev/github.com/sashabaranov/go-openai#ClientConfig

Description
No description provided
Readme Apache-2.0 952 KiB
Languages
Go 100%