go-chatgpt
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
DengBiao 9e1b5f3645 add Reverse: for v0.0.8 pirms 1 gada
..
.github/workflows add Reverse: for v0.0.5 pirms 1 gada
internal/test add Reverse: for v0.0.4 pirms 1 gada
.gitignore add Reverse: for v0.0.6 pirms 1 gada
.golangci.yml add Reverse: for v0.0.1 pirms 1 gada
LICENSE add Reverse: for v0.0.1 pirms 1 gada
Makefile add Reverse: for v0.0.1 pirms 1 gada
README.md add Reverse: for v0.0.6 pirms 1 gada
api.go add Reverse: for v0.0.6 pirms 1 gada
audio.go add Reverse: for v0.0.6 pirms 1 gada
chat.go add Reverse: for v0.0.6 pirms 1 gada
chat_stream.go add Reverse: for v0.0.6 pirms 1 gada
common.go add Reverse: for v0.0.6 pirms 1 gada
completion.go add Reverse: for v0.0.6 pirms 1 gada
config.go add Reverse: for v0.0.8 pirms 1 gada
edits.go add Reverse: for v0.0.6 pirms 1 gada
embeddings.go add Reverse: for v0.0.6 pirms 1 gada
engines.go add Reverse: for v0.0.6 pirms 1 gada
error.go add Reverse: for v0.0.6 pirms 1 gada
files.go add Reverse: for v0.0.6 pirms 1 gada
fine_tunes.go add Reverse: for v0.0.6 pirms 1 gada
go.mod add Reverse: for v0.0.7 pirms 1 gada
image.go add Reverse: for v0.0.6 pirms 1 gada
marshaller.go add Reverse: for v0.0.6 pirms 1 gada
models.go add Reverse: for v0.0.6 pirms 1 gada
moderation.go add Reverse: for v0.0.6 pirms 1 gada
request_builder.go add Reverse: for v0.0.6 pirms 1 gada
stream.go add Reverse: for v0.0.6 pirms 1 gada

README.md

Go OpenAI

GoDoc Go Report Card codecov

Note: the repository was recently renamed from go-gpt3 to go-gogpt

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-gogpt

ChatGPT example usage:

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:

GPT-3 completion
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)
}
GPT-3 streaming completion
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)
	}
}
Audio Speech-To-Text
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)
}
DALL-E 2 image generation
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")
}

Configuring proxy
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

ChatGPT support context
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)
	}
}