hongdachun / openai

OpenAi SDK for Go (community-maintained)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OpenAi (community-maintained) Go Reference

Package openai provides a Go SDK for the OpenAI API.this package supports several models, including GPT-4, GPT-3.5, GPT-3, DALL-E, and audio models. You can specify the desired model using the Model field in the request object.

Feature

  • ChatGPT (GPT-3, GPT-3.5, GPT-4)
  • DALL·E 2
  • Embedding
  • Audio
  • Fine-Tune
  • File
  • Moderations
  • Completion Patterns
  • Multiple API keys support

Install Go Version

$ go get -u github.com/GoFarsi/openai

Example Completion

package main

import (
	"context"
	"github.com/GoFarsi/openai"
	"github.com/GoFarsi/openai/client"
	"github.com/GoFarsi/openai/entity"
	"github.com/GoFarsi/openai/models"
	"log"
	"os"
)

func main() {
	apiKey := os.Getenv("OPENAI_API_KEY")
	cli, err := client.New([]string{apiKey})
	if err != nil {
		log.Fatalln(err)
	}

	c := openai.NewCompletion(cli)
	resp, err := c.CreateCompletion(context.Background(), entity.CompletionRequest{
		Model:  models.TEXT_DAVINCI_002,
		Prompt: "can you explain bubble sort algorithm?",
	})

	if err != nil {
		log.Fatalln(err)
	}

	log.Println(resp)
}

Example Completion Patterns

package main

import (
	"context"
	"github.com/GoFarsi/openai"
	"github.com/GoFarsi/openai/client"
	"github.com/GoFarsi/openai/patterns/completion"
	"github.com/GoFarsi/openai/types/programming"
	"log"
	"os"
)

var code string = `
func add(a, b int) int {
	return a + b
}
`

func main() {
	apiKey := os.Getenv("OPENAI_API_KEY")
	cli, err := client.New([]string{apiKey})
	if err != nil {
		log.Fatalln(err)
	}

	c := openai.NewCompletion(cli)
	resp, err := c.CreateCompletionFromPattern(context.Background(), completion.ProgrammingLanguageTranslator(
		code,
		programming.Go,
		programming.Python,
		0,
	))

	if err != nil {
		log.Fatalln(err)
	}

	log.Println(resp.Choices[0].Text)
}

See more details in documentation.

TODO

  • Stream Support
  • Moderation API
  • Example API
  • Fine-Tune API
  • File API
  • Engine API
  • Azure API Support
  • Client, API Unit test

Contributing

  1. fork project in your GitHub account.
  2. create new branch for new changes.
  3. after change code, send Pull Request.

About

OpenAi SDK for Go (community-maintained)

License:Apache License 2.0


Languages

Language:Go 100.0%