go-chatgpt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
DengBiao 53f13dd698 add Reverse: for v0.0.2 1 year ago
..
.gitignore add Reverse: for v0.0.1 1 year ago
.golangci.yml add Reverse: for v0.0.1 1 year ago
LICENSE add Reverse: for v0.0.1 1 year ago
Makefile add Reverse: for v0.0.1 1 year ago
README.md add Reverse: for v0.0.1 1 year ago
answers.go add Reverse: for v0.0.1 1 year ago
api.go add Reverse: for v0.0.1 1 year ago
chat.go add Reverse: for v0.0.2 1 year ago
common.go add Reverse: for v0.0.1 1 year ago
completion.go add Reverse: for v0.0.1 1 year ago
config.go add Reverse: for v0.0.1 1 year ago
edits.go add Reverse: for v0.0.1 1 year ago
embeddings.go add Reverse: for v0.0.1 1 year ago
engines.go add Reverse: for v0.0.1 1 year ago
error.go add Reverse: for v0.0.1 1 year ago
files.go add Reverse: for v0.0.1 1 year ago
image.go add Reverse: for v0.0.1 1 year ago
models.go add Reverse: for v0.0.1 1 year ago
moderation.go add Reverse: for v0.0.1 1 year ago
stream.go add Reverse: for v0.0.1 1 year ago

README.md

go-gpt3

GoDoc Go Report Card

OpenAI ChatGPT and GPT-3 API client for Go

Installation:

go get github.com/sashabaranov/go-gpt3

Example usage:

package main

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

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 {
		return
	}
	fmt.Println(resp.Choices[0].Text)
}

Streaming response example:

package main

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

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)
	}
}