Skip to content

GeniusAI-Platform/openai

Repository files navigation

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/GeniusAI-Platform/openai

Example Completion

package main

import (
	"context"
	"github.com/GeniusAI-Platform/openai"
	"github.com/GeniusAI-Platform/openai/client"
	"github.com/GeniusAI-Platform/openai/entity"
	"github.com/GeniusAI-Platform/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/GeniusAI-Platform/openai"
	"github.com/GeniusAI-Platform/openai/client"
	"github.com/GeniusAI-Platform/openai/patterns/completion"
	"github.com/GeniusAI-Platform/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)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages