Giter VIP home page Giter VIP logo

gpt-go's Introduction

GPT-Go: OpenAI ChatGPT/GPT-4/GPT-3 SDK Go Client to Interact with the GPT-4/GPT-3 APIs.


English | 中文

GitHub license Go PkgGoDev Go Report Card codecov

OpenAI Docs API Reference: https://platform.openai.com/docs/api-reference/introduction

Note: Already support GPT-4 API, please use Chat Completions API.

Quick Start

# clone the project
git clone https://github.com/hanyuancheung/gpt-go.git

# go to the project directory
cd gpt-go

# set API_KEY as environment variable
export API_KEY={YOUR_API_KEY} chatgpt

# go build example binary
make chatgpt-example

# run example
./chatgpt

Snapshot

How To Get API_KEY

https://platform.openai.com/account/api-keys

Documentation

Check out the go docs for more detailed documentation on the types and methods provided: https://pkg.go.dev/github.com/hanyuancheung/gpt-go

Support

  • List Engines API
  • Get Engine API
  • Completion API (this is the main gpt-3 API)
  • Streaming support for the Completion API
  • Document Search API
  • Image generation API
  • Overriding default url, user-agent, timeout, and other options

Usage Examples

ChatGPT streaming completion
func main() {
	client := gpt.NewClient("API_KEY")
	err := client.ChatCompletionStream(context.Background(), &gpt.ChatCompletionRequest{
		Model: gpt.GPT3Dot5Turbo,
		Messages: []gpt.ChatCompletionRequestMessage{
			{
				Role:    "user",
				Content: "Hello!",
			},
		},
	}, func(response *gpt.ChatCompletionStreamResponse) {
		fmt.Print(response.Choices[0].Delta.Content)
	})
	if err != nil {
		fmt.Printf("ChatCompletionStream error: %v\n", err)
		return
	}
}
GPT-3 completion
func main() {
	client := gpt.NewClient("API_KEY")
	rsp, err := client.CompletionWithEngine(context.Background(), &gpt.CompletionRequest{
		Model:  gpt.TextDavinci003Engine,
		Prompt: []string{"Hello!"},
	})
	if err != nil {
		fmt.Printf("ChatCompletionStream error: %v\n", err)
		return
	}
	fmt.Print(rsp.Choices[0].Text)
}
DALL-E 2 image generation
func main() {
	client := gpt.NewClient("API_KEY")
	rsp, err := client.Image(context.Background(), &gpt.ImageRequest{
		Prompt: "Chicken",
	})
	if err != nil {
		fmt.Printf("ChatCompletionStream error: %v\n", err)
		return
	}
	fmt.Print(rsp.Data[0].URL)
}

Contributor

Contribute

Please open up an issue on GitHub before you put a lot of efforts on pull request. The code submitting to PR must be filtered with gofmt.

Star History

Star History Chart

License

This package is licensed under MIT license. See LICENSE for details.

Show your support

Give a ⭐️ if this project helped you!

gpt-go's People

Contributors

hanyuancheung avatar deancn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.