Giter Club home page Giter Club logo

go-gpt3's Introduction

go-gpt3

An OpenAI GPT-3 API client enabling Go/Golang programs to interact with the gpt3 APIs.

Supports using the completion APIs with or without streaming.

PkgGoDev

Usage

Simple usage to call the main gpt-3 API, completion:

client := gpt3.NewClient(apiKey)
resp, err := client.Completion(ctx, gpt3.CompletionRequest{
    Prompt: []string{"2, 3, 5, 7, 11,"},
})

fmt.Print(resp.Choices[0].Text)
// prints " 13, 17, 19, 23, 29, 31", etc

Documentation

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

Full Examples

Try out any of these examples with putting the contents in a main.go and running go run main.go. I would recommend using go modules in which case you will also need to run go mod init within your test repo. Alternatively you can clone this repo and run the test script with go run cmd/test/main.go.

You will also need to have a .env file that looks like this to use these examples:

API_KEY=<openAI API Key>
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/PullRequestInc/go-gpt3"
	"github.com/joho/godotenv"
)

func main() {
	godotenv.Load()

	apiKey := os.Getenv("API_KEY")
	if apiKey == "" {
		log.Fatalln("Missing API KEY")
	}

	ctx := context.Background()
	client := gpt3.NewClient(apiKey)

	resp, err := client.Completion(ctx, gpt3.CompletionRequest{
		Prompt:    []string{"The first thing you should know about javascript is"},
		MaxTokens: gpt3.IntPtr(30),
		Stop:      []string{"."},
		Echo:      true,
	})
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(resp.Choices[0].Text)
}

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
  • Overriding default url, user-agent, timeout, and other options

Powered by

go-gpt3's People

Contributors

3l0w avatar abatilo avatar bakks avatar dabdine avatar hagemt avatar jaderdias avatar letschers avatar mrene avatar snehalchennuru avatar thehappydinoa avatar tylermann avatar wbrown avatar yorinasub17 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-gpt3's Issues

The Functions feature is causing the API to return [400:invalid_request_error]

#36

I am using the ChatCompletionRequest endpoint, upgraded from v1.1.15 to v1.1.16 and I am now getting an error response from the API - I haven't done any other changes on my end.

[400:invalid_request_error] None is not of type 'array' - 'functions'

I did not add any Functions to my request, and it doesn't seem that Functions is a required parameter, but somehow the API is now replying as if it was included in the request with the None value by default, and complaining that value is not of type array.

Adding an empty Functions parameter to the request and the API responds "correctly" with [400:invalid_request_error] '' does not match '^[a-zA-Z0-9_-]{1,64}$' - 'functions.0.name'

I noticed that Functions support was recently added in the latest pull request but have not been able to figure out what is going wrong. Perhaps the API expects an empty array instead of the None value it is receiving or interpreting?

Rolling back to v1.1.15 fixes the issue.

Unrecognized request argument supplied: engine_id

Hello, thanks for making this!

When making a completion request, this error is returned:

[400:invalid_request_error] Unrecognized request argument supplied: engine_id

I think the code should ignore CompletionRequest.EngineID since the engine ID would be controlled by the engine argument when present.

EDIT: Since this would change behavior, there would need to be a new version.

Temperature 0 not really working out

Hello there. I don't know if this is a bug specific to this library or openai api itself, but I only encounter this problem with go-gpt3.

The following code will output different generations for 0 temperature.

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/PullRequestInc/go-gpt3"
)

func onErrPanic(err *error) {
	if *err != nil {
		panic((*err).Error())
	}
}

func generate() (err error) {
	defer onErrPanic(&err)

	ctx := context.Background()
	client := gpt3.NewClient("your own api key")

	var completion string

	content := fmt.Sprintf(`
-- This is the postgresql schema

%v


-- here are the queries. They have a name, annotated by ":name". They
-- also have another tag that represents how many rows should be returned.
-- :one means that only one row is expected to be returned
-- :many means that more than one row will be returned

%v


-- Write go functions that consume the previous queries. You won't write queries using a specific driver.
-- Rather, the queries will be provided an interface that abstracts the database.
-- That interface won't have a constructor. You won't write a main function. You won't write comments. The
-- package will be main.
`, exampleSchema, exampleQueries)

	err = client.ChatCompletionStream(ctx, gpt3.ChatCompletionRequest{
		Model: gpt3.GPT3Dot5Turbo,
		Messages: []gpt3.ChatCompletionRequestMessage{
			{
				Role:    "system",
				Content: `You are a command line tool that generates golang code contacting the database. You only output go code, nothing else`,
			},
			{
				Role: "user",
				Content: content,
			},
		},
		Temperature:      0, // note this, temperature is set to 0
		Stream:           true,
		MaxTokens:        1000,
	}, func(res *gpt3.ChatCompletionStreamResponse) {
		fmt.Print(res.Choices[0].Delta.Content)
		completion += res.Choices[0].Delta.Content
	})
	if err != nil {
		return err
	}

	fmt.Println()

	return nil
}

var exampleSchema = `
CREATE TABLE authors (
    id   BIGSERIAL PRIMARY KEY,
    name text      NOT NULL,
    bio  text
);
`

var exampleQueries = `
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;
`

func main() {
	generate()
}

Output:

image

Here's the equivalent in the python openai sdk, with temperature equivalent to 0 I get consistent results:

import openai

openai.api_key = "your api key"

def gen_prompt(schema: str, queries: str):
    return f"""
-- This is the postgresql schema

{schema}


-- here are the queries. They have a name, annotated by ":name". They
-- also have another tag that represents how many rows should be returned.
-- :one means that only one row is expected to be returned
-- :many means that more than one row will be returned

{queries}


-- Write go functions that consume the previous queries. You won't write queries using a specific driver.
-- Rather, the queries will be provided an interface that abstracts the database.
-- That interface won't have a constructor. You won't write a main function. You won't write comments. The
-- package will be main.
"""

exampleSchema = """
CREATE TABLE authors (
    id   BIGSERIAL PRIMARY KEY,
    name text      NOT NULL,
    bio  text
);
"""

exampleQueries = """
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;
"""


def main():
    prompt = gen_prompt(exampleSchema, exampleQueries)

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages = [
			{
				"role":    "system",
				"content": "You are a command line tool that generates golang code contacting the database. You only output go code, nothing else",
			},
			{
				"role": "user",
				"content": prompt,
			},
        ],
        max_tokens = 1000,
        temperature = 0,
        stream = True
    )

    for completion in response:
        try:
            content = completion["choices"][0]["delta"]["content"]
            # print content unbuffered without printing newlines
            print(content, end="", flush=True)
        except:
            pass

main()

Migrate to new endpoints

OpenAI's /engine/{engine_id} endpoints have been deprecated. In fact, the concept of "engines" has been deprecated (https://help.openai.com/en/articles/6283125-what-happened-to-engines). Instead, OpenAI is using "model" now to reference what was previously an engine. As such, all newer API endpoints now no longer accept the engine name in the URI path and instead favor the model in the JSON payload.

Additionally, several API endpoints now accept a "user" parameter that can be used to store a unique identifier to trace requests when dealing with abuse complaints. Support for this parameter would be nice to add when migrating to the new endpoints.

I see a few options:

  1. Version this library (keep 1.x.x for the deprecated APIs, add a 2.x.x for the newer API endpoints). However, I'm not sure how long OpenAI will support them. They state "We will maintain backward compatibility for requests using ‘engine’ as a parameter, but recommend updating your implementation as soon as you can to prevent future confusion." on their blog post).
  2. Do nothing Their blog post doesn't set an end date to the deprecation warning. However, since documentation will reflect the latest version of the endpoints, it's likely that over time this library will use a mix of both the old deprecated APIs and new APIs, causing confusion (NOTE this is already happening with embeddings and edits).
  3. Migrate everything over to the new version, ditch the deprecated APIs fully - probably the least favorable option for folks using it in production.

noob question

I'm trying to add a key to code and run it

I create and save this file
go run main.go

but it just says

main.go:8:2: found packages gpt3 (client_options.go) and main (main.go) in /Users/johndpope/Documents/mlWorkspace/go-gpt3

I try importing
go get -u github.com/PullRequestInc/go-gpt3

it fails
can't load package: package github.com/PullRequestInc/go-gpt3: found packages gpt3 (client_options.go) and main (main.go) in /Users/johndpope/Documents/mlWorkspace/go-gpt3

package main

import (
	"context"
	"log"
	"os"

	"github.com/PullRequestInc/go-gpt3"
	"github.com/joho/godotenv"
)

func main() {
	godotenv.Load()

	apiKey := os.Getenv("key")
	if apiKey == "" {
		log.Fatalln("Missing API KEY")
	}

	ctx := context.Background()
	client := gpt3.NewClient(apiKey)

	resp, err := client.Completion(ctx, gpt3.CompletionRequest{
		Prompt: []string{
			"1\n2\n3\n4",
		},
		MaxTokens: gpt3.IntPtr(10),
	})
	if err != nil {
		log.Fatalln(err)
	}
	log.Printf("hellooo%+v\n", resp)

	resp, err = client.Completion(ctx, gpt3.CompletionRequest{
		Prompt: []string{
			"go:golang\npy:python\njs:",
		},
		Stop: []string{"\n"},
	})
	if err != nil {
		log.Fatalln(err)
	}
	log.Printf("%+v\n", resp)
}

Setting the Temperature in the CompletionRequest

Hi there,

So I am trying to add the Temperature in the CompletionRequest as shown below :
Temperature: float32(0.45),

However, when I try to run the file, the error I receive is the following :
cannot use float32(0.45) (type float32) as type *float32 in field value

Can someone help me the value that is expected in the Temperature field ? Thanks.

Vision (GPT-V) support

The new model gpt-4-1106-vision-preview can take an image as input and work on the image.
Is this something easy/possible to do with this library?

context deadline exceeded (Client.Timeout or context cancellation while reading body)

when I ask a complex question , It will random interrupt.

请输入你的问题(quit 离开): 帮我写个关于 c++ 基础知识的博客

C++是一种面向对象的编程语言,它是一种多范式的编程语言,可以用于开发桌面应用程序、移动应用程序、服务器应用程序、游戏开发等。C++是一种非常强大的编程语言,它拥有非常强大的功能,可以帮助开发者实现复杂的功能。

C++的基础知识包括:

数据类型:C++支持多种数据类型,包括基本数据类型(int、float、char等)、枚举类型、结构体、类等。

变量:变量是程序中用于存储数据的容器,它们可以是基本数据类型,也可以是复合数据类型,如结构体和类。

运算符:C++支持多种运算符,包括算术运算符、关系运算符、逻辑运算符、位运算符等。

控制语句:C++支持多种控制语句,包括if-else、switch-case、while、do-while、for等。

函数:函数是程序中的一种重要组成部分,它可以实现特定功能,并且可以被多次调用。

模板:模板是C++中的一种重要概念,它可以帮助开发者实现代码的复用,提高程序的可维护性。context deadline exceeded (Client.Timeout or context cancellation while reading body)

No Token Usage Information when Streaming

Hello, when I use the CompletionStreamWithEngine function I notice that all token values are marked as 0. Is it safe to assume that every message that is streamed is a single token?

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.