• Stars
    star
    243
  • Rank 166,460 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

OpenAI GPT3/3.5 and GPT4 ChatGPT API Client Library for Go, simple, less dependencies, and well-tested

openaigo

Go CodeQL App Test over API License FOSSA Status
Maintainability Go Report Card codecov
Reference GoDoc

Yet another API client for api.openai.com.

This library is community-maintained, NOT officially supported by OpenAI.

Usage Example

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/otiai10/openaigo"
)

func main() {
	client := openaigo.NewClient(os.Getenv("OPENAI_API_KEY"))
	request := openaigo.ChatRequest{
		Model: "gpt-3.5-turbo",
		Messages: []openaigo.Message{
			{Role: "user", Content: "Hello!"},
		},
	}
	ctx := context.Background()
	response, err := client.Chat(ctx, request)
	fmt.Println(response, err)
}

if you just want to try, hit commands below.

git clone https://github.com/otiai10/openaigo.git
cd openaigo
OPENAI_API_KEY=YourAPIKey go run ./testapp/main.go

See test app as a working example.

API Keys?

Visit https://beta.openai.com/account/api-keys and you can create your own API key to get started for free.

Endpoint Support

Need function_call?

request := openaigo.ChatRequest{
  Messages: []openaigo.Message{
    {Role: "user", Content: "How's the weather today in Tokyo?"},
  },
  Functions: []openaigo.Function{
    {
      Name: "get_weather",
      Parameters: openaigo.Parameters{
        Type:       "object",
        Properties: map[string]map[string]any{
          "location": {"type": "string"},
          "date":     {"type": "string", "description": "ISO 8601 date string"},
        },
        Required: []string{"location"},
      },
    }
  },
}

if you want shorthand, use functioncall.

import fc "github.com/otiai10/openaigo/functioncall"

request.Functions = fc.Funcs{
  "get_weather": {GetWeather, "Get weather of the location", fc.Params{
    {"location", "string", "location of the weather", true},
    {"date", "string", "ISO 8601 date string", true},
  }},
}

See test app as a working example.

Need stream?

client := openaigo.NewClient(OPENAI_API_KEY)
request := openaigo.ChatRequest{
  Stream: true,
  StreamCallback: func(res ChatCompletionResponse, done bool, err error) {
    // Do what you want!
    // You might need chan handling here.
    // See test app how you can do it.
    // https://github.com/otiai10/openaigo/search?q=chat_completion_stream
  },
}

Need Proxy?

client := openaigo.NewClient(OPENAI_API_KEY)
// You can set whatever you want
transport := &http.Transport{ Proxy: http.ProxyFromEnvironment }
client.HTTPClient = &http.Client{ Transport: transport }
// Done!

Issues

Report any issue here or any feedback is welcomed.

More Repositories

1

gosseract

Go package for OCR (Optical Character Recognition), by using Tesseract C++ library
Go
2,221
star
2

ocrserver

A simple OCR API server, seriously easy to be deployed by Docker, on Heroku as well
Go
613
star
3

copy

Go copy directory recursively
Go
559
star
4

amesh

みんなだいすき東京アメッシュ!
Go
367
star
5

chrome-extension-es6-import

Example for Chrome . Extension ES6 Import
JavaScript
97
star
6

opengraph

Open Graph Parser for Go
Go
59
star
7

lookpath

The minimum and most straightforward way to check if command exists and where the executable is, without spawning child_process.
TypeScript
56
star
8

chomex

Chrome Extension Messaging Routing Kit / Promisify Chrome Messaging / LocalStorage Object Mapper
TypeScript
44
star
9

mint

The very minimum assertion for Golang testing framework.
Go
30
star
10

hotsub

Command line tool to run batch jobs concurrently with ETL framework on AWS or other cloud computing resources
Go
30
star
11

chant

Chat Application written in Go, using Firebase, working on GAE/Go, easy to deploy
Go
25
star
12

web-push-notification-sample

Web Push Notification Sample for both Server and Client
JavaScript
25
star
13

gat

Graphical cat. Render image on your terminal.
Go
23
star
14

cwl.go

CWL (and input file) parser for Golang, used by github.com/otiai10/yacle
Go
21
star
15

twistream

Simple Twitter Streaming API for Golang. #golang
Go
19
star
16

yacle

Yet Another CWL Engine by Golang
Go
18
star
17

gh-dependents

Just a web crawler to collect dependents of a GitHub repository.
Go
17
star
18

dataviper

Data Profiling and Basic Quality Assessment tool, for the very beginning phase of your project.
Python
17
star
19

ternary

Golang ternary expression ⛳
Go
14
star
20

demado

JavaScript
14
star
21

rodeo

Simple Redis Client for Golang
Go
13
star
22

giffy

Create Animated GIF from PNGs, JPEGs and, of course, GIFs
Go
13
star
23

amesh-bot

Slackでameshって言うとアメッシュの画像出すbot。 https://github.com/otiai10/amesh をライブラリとして利用したアプリケーション実装のひとつの例。
Go
12
star
24

marmoset

The very minimum web toolkit, less than framework
Go
11
star
25

too.js

Combine multiple commands' stream, keep all foreground and kill all in one Ctrl+C
TypeScript
11
star
26

curr

__FILE__, __DIR__, __FUNCTION__, __LINE__ for Golang
Go
11
star
27

yt2mp3

Download YouTube and convert to mp3
Go
11
star
28

fresh-youtube

View component and controller of YouTube Player API, for fresh framework.
TypeScript
10
star
29

colorchat

The very simple example of chat application written in Go, just by using standard package to handle WebSocket.
Go
9
star
30

ingoose

Simple IdnexedDB driver for browser application, with Mongoose like API.
TypeScript
8
star
31

botw

Create a twitter bot easily
Go
8
star
32

escalgo

[WIP] Elasticsearch Query DSL for Golang
Go
7
star
33

chromestorm

ORM for Chrome Extension with `chrome.storage` API
TypeScript
5
star
34

electron-playground

JavaScript
5
star
35

daap

Use Docker as a process, with hijacking Stdout/Stderr
Go
5
star
36

primes

Find primes, factorize numbers and reduce fractions by Golang.
Go
5
star
37

cf-chat-bridge

Bridge communication between LINE and Slack, by using Google Cloud Functions
TypeScript
5
star
38

TubeVj

にゃーん
TypeScript
4
star
39

firebase-messaging-sample

Firebase Cloud Messaging Sample
JavaScript
4
star
40

jstorm

JavaScript Storage ORM (Object-Relational Mapper) for LocalStorage and Chrome Storage API
TypeScript
4
star
41

mockrize

Create RESTful Mock API server, based on repository folder structures.
TypeScript
3
star
42

good

Early Return for Elixir
Elixir
3
star
43

otiai10.github.io

github.io
HTML
3
star
44

DesignPattern.java

恥ずかしいことも質問していい勉強会
Java
3
star
45

homebrew-tap

My published homebrew packages
Ruby
3
star
46

log-kcwidget

ocrのlog
Go
3
star
47

webm2mp4

Convert WebM to MP4
JavaScript
3
star
48

gcp-sdk-go-sandbox

Go
3
star
49

localstorm

ORM (Object-Relation Mapper) for LocalStorage of your browsers.
TypeScript
3
star
50

prisc

プリスク!は超簡単にウェブページのスクショが撮れるChrome拡張です
TypeScript
3
star
51

mammut

Yet Another Mastodon API Client
TypeScript
3
star
52

docker-tesseract

Docker image with compiling Tesseract-OCR for specific version on demand
Shell
3
star
53

hisyotan

ぼくだけの秘書たん
Ruby
2
star
54

youtube-dl-docker

https://cloud.docker.com/repository/docker/otiai10/youtube-dl
Dockerfile
2
star
55

urisql

Go
2
star
56

goapis

Yet another Web API Client library for Go
Go
2
star
57

cigger

Command line client to Trigger CI services
Go
2
star
58

delayload.js

delayload
2
star
59

spell

Command line string parser for Go.
Go
2
star
60

totsuzen

> 突然の死 < やるやつ。もう何番煎じやねん、っていうね。
Go
2
star
61

crescentjs

JavaScript images Binarization/Comparsion 🌘
JavaScript
2
star
62

ojisan.py

アサカツおじさん
JavaScript
2
star
63

gdcdl-workflow

Dockerfile
2
star
64

react-examples

http://otiai10.github.io/react-examples
JavaScript
2
star
65

gohome

働いたら負け
Go
2
star
66

syobocal

いくつになってもしょぼかるにはお世話になっております
Go
2
star
67

ios-travis-ci-example

iOS CI Example with TravisCI
Swift
1
star
68

backlog

Backlog
1
star
69

twit.tap

とりあえずつくってみた。99%がクライアントで出来てます
JavaScript
1
star
70

fizzbuzz

なくていい
Go
1
star
71

cachely

Golang HTTP response cache, with the same interface of http package
Go
1
star
72

react-text-anchorize

Replace `foo` to `<a>foo</a>` of inner text with RegExp
JavaScript
1
star
73

gcpx

Go
1
star
74

hellohttp

Rust
1
star
75

goavcodec

Go
1
star
76

docker-nfs

NFS client / server images for Docker
1
star
77

jma

Yet another weather forecast API client in Japan
Go
1
star
78

minimum.revel

revelのminimumサーバ
Go
1
star
79

ripple-css-animation

Ripple CSS Animation
CSS
1
star
80

dotfiles

dotfiles
Vim Script
1
star
81

aws-sdk-go-sandbox

Go
1
star
82

speedweary

speedweary
Objective-C
1
star
83

teitokuClock

提督の懐中時計
TypeScript
1
star
84

iPushKanColleWidget

iPushKanColleWidget 通知支援艦隊
Objective-C
1
star
85

conway-app-go

Conway's Game of Life in Go
Go
1
star
86

gdc-client

1
star
87

gae-go-sandbox

Go
1
star
88

MiniBuddy

Swift
1
star
89

yaml2json

Go
1
star
90

soundhook

soundhook 開発バージョン => http://dev.soundhook.net
CSS
1
star
91

hello-go-wasm

Go
1
star
92

twick

https://chrome.google.com/webstore/detail/twick/fmdoehgidpmnkhakgkljpnlaccliemdf
HTML
1
star
93

jsonindent

Golang JSON Indent Encoder with io.Writer
Go
1
star
94

tplize

Embed template to your CLI binary as Go map variable, supporting `go:generate`.
Go
1
star
95

IncrementalSearch

JavaScript Incremental Search small kit.
TypeScript
1
star