gochatgptsdk

package module
v0.0.0-...-e815c29 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 3 Imported by: 3

README

go-chatgpt-sdk

This Library Provides Unofficial Go Client SDK for OpenAI API

Install

go get -u github.com/ak9024/go-chatgpt-sdk

Model Support

  • Chat
  • Text
  • Image
  • Moderations
  • Audio

Prerequisite

Usage with model chat

to usage with model chat, please usage c.ChatCompletions

package main

import (
	"fmt"

	gochatgptsdk "github.com/ak9024/go-chatgpt-sdk"
)

func main() {
	c := gochatgptsdk.NewConfig(gochatgptsdk.Config{
		OpenAIKey: "",
	})

	resp, _ := c.ChatCompletions(gochatgptsdk.ModelChat{
		Model: "gpt-3.5-turbo",
		Messages: []gochatgptsdk.Message{
			{
				Role:    "system",
				Content: "You are a Software engineer",
			},
			{
				Role:    "user",
				Content: "Please create a simple function to using Go language",
			},
		},
	})

	fmt.Println(resp.Choices)
}

Usage with model text

To usage with model text, please usage c.Completions()

package main

import (
	"fmt"

	gochatgptsdk "github.com/ak9024/go-chatgpt-sdk"
)

func main() {
	c := gochatgptsdk.NewConfig(gochatgptsdk.Config{
		OpenAIKey: "",
	})

	resp, _ := c.Completions(gochatgptsdk.ModelText{
		Model:     "text-davinci-003",
		Prompt:    "What the weather Kota Palu for today?",
		MaxTokens: 100, // max generates of word
	})

	fmt.Println(resp.Choices)
}

Usage with model images

Create images, please use c.ImagesGenerations, but if you want generate base64 image usage c.ImageGenerationsB64JSON

package main

import (
	"fmt"

	gochatgptsdk "github.com/ak9024/go-chatgpt-sdk"
)

func main() {
	c := gochatgptsdk.NewConfig(gochatgptsdk.Config{
		OpenAIKey: "",
	})

	resp, _ := c.ImagesGenerations(gochatgptsdk.ModelImages{
		Prompt: "Sunset sketch art",
		N:      5, // generates 5 images
		Size:   gochatgptsdk.Size512, // with size 512x512
	})

	fmt.Println(resp.Data)
}

Create images variations of a given image, please use c.ImagesVariations() but if you want generate base64 image usage c.ImageVariationsB64JSON

package main

import (
	"fmt"

	gochatgptsdk "github.com/ak9024/go-chatgpt-sdk"
)

func main() {
	c := gochatgptsdk.NewConfig(gochatgptsdk.Config{
		OpenAIKey: "",
	})

	resp, _ := c.ImagesVariations(gochatgptsdk.ModelImagesVariations{
		Image: "./path/to/example-img.png", // please suitable with your path image
		N:     "2",                  // generate 2 images
		Size:  gochatgptsdk.Size256, // with size 256x256
	})

	fmt.Println(resp.Data)
}

For all of response data please read more in file struct_response.go

Documentation

Index

Constants

View Source
const ChatGPTAPIV1 = "https://api.openai.com/v1"

Variables

View Source
var (
	Size1024 = "1024x1024"
	Size512  = "512x512"
	Size256  = "256x256"

	ResponseFormatURL     = "url"
	ResponseFormatB64JSON = "b64_json"
)

Functions

func DoRequest

func DoRequest(t string) *resty.Request

DoRequest(t string) to compose HTTP Request

Types

type Chatgpt

type Chatgpt interface {
	ChatCompletions(b ModelChat) (*ModelChatResponse, *ErrorResponse)
	Completions(b ModelText) (*ModelTextResponse, *ErrorResponse)
	ImagesGenerations(b ModelImages) (*ModelImagesResponse[DataURL], *ErrorResponse)
	ImagesGenerationsB64JSON(b ModelImages) (*ModelImagesResponse[DataB64JSON], *ErrorResponse)
	ImagesVariations(b ModelImagesVariations) (*ModelImagesResponse[DataURL], *ErrorResponse)
	ImagesVariationsB64JSON(b ModelImagesVariations) (*ModelImagesResponse[DataB64JSON], *ErrorResponse)
}

func NewConfig

func NewConfig(c Config) Chatgpt

type Choice

type Choice struct {
	Index        int     `json:"index"`
	Message      Message `json:"message"`
	FinishReason string  `json:"finish_reason"`
}

type ChoiceText

type ChoiceText struct {
	Text         string      `json:"text"`
	Index        int         `json:"index"`
	Logprobs     interface{} `json:"logprobs"`
	FinishReason string      `json:"finish_reason"`
}

type Config

type Config struct {
	OpenAIKey string
}

type DataB64JSON

type DataB64JSON struct {
	B64JSON string `json:"b64_json"`
}

type DataURL

type DataURL struct {
	URL string `json:"url"`
}

type Error

type Error struct {
	Code    interface{} `json:"code"`
	Message string      `json:"message"`
	Param   interface{} `json:"param"`
	Type    string      `json:"type"`
}

type ErrorResponse

type ErrorResponse struct {
	Error Error `json:"error"`
}

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type ModelChat

type ModelChat struct {
	Model            string    `json:"model"`
	Messages         []Message `json:"messages"`
	MaxTokens        int       `json:"max_tokens,omitempty"`
	Temperature      int       `json:"temperature,omitempty"`
	TopP             int       `json:"top_p,omitempty"`
	FrequencyPenalty int       `json:"frequency_penalty,omitempty"`
	PresencePenalty  int       `json:"presence_penalty,omitempty"`
}

type ModelChatResponse

type ModelChatResponse struct {
	ID      string   `json:"id"`
	Object  string   `json:"object"`
	Created int64    `json:"created"`
	Choices []Choice `json:"choices"`
	Usage   Usage    `json:"usage"`
}

type ModelImages

type ModelImages struct {
	Prompt         string `json:"prompt"`
	N              int    `json:"n,omitempty"`               // default to 1
	Size           string `json:"size,omitempty"`            // default 1024x1024
	ResponseFormat string `json:"response_format,omitempty"` // url or b64_json
	User           string `json:"user,omitempty"`
}

type ModelImagesResponse

type ModelImagesResponse[T DataURL | DataB64JSON] struct {
	Created int64 `json:"created"`
	Data    []T   `json:"data"`
}

type ModelImagesVariations

type ModelImagesVariations struct {
	Image          string `json:"image"`                     // must be valid PNG file, less than 4MB, and square
	N              string `json:"n,omitempty"`               // default to 1
	Size           string `json:"size,omitempty"`            // default 1024x1024
	ResponseFormat string `json:"response_format,omitempty"` // url or b64_json
	User           string `json:"user,omitempty"`
}

type ModelText

type ModelText struct {
	Model            string `json:"model"`
	Prompt           string `json:"prompt"`
	MaxTokens        int    `json:"max_tokens,omitempty"`
	Temperature      int    `json:"temperature,omitempty"`
	TopP             int    `json:"top_p,omitempty"`
	FrequencyPenalty int    `json:"frequency_penalty,omitempty"`
	PresencePenalty  int    `json:"presence_penalty,omitempty"`
}

type ModelTextResponse

type ModelTextResponse struct {
	ID      string       `json:"id"`
	Object  string       `json:"object"`
	Created int64        `json:"created"`
	Model   string       `json:"model"`
	Choices []ChoiceText `json:"choices"`
	Usage   Usage        `json:"usage"`
}

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL