gpt35

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2023 License: MIT Imports: 6 Imported by: 3

README

GoLang HTTP Client for ChatGPT (GPT-3.5-turbo)

A client for an official API of chat completions (known as ChatGPT) based on gpt-3.5-turbo model. Here's a guide by Open AI: https://platform.openai.com/docs/guides/chat.

Created & generated by Sergei Zaikin & OpenAI ChatGPT.

Install

go get github.com/AlmazDelDiablo/gpt3-5-turbo-go

Usage

package main

import (
	gpt35 "github.com/AlmazDelDiablo/gpt3-5-turbo-go"
)

func main() {
	c := gpt35.NewClient("sk-xxxxxxxxxxxxxxxxxxxx")
	req := &gpt35.Request{
		Model: gpt35.ModelGpt35Turbo,
		Messages: []*gpt35.Message{
			{
				Role:    gpt35.RoleUser,
				Content: "Hello",
			},
		},
	}

	resp, err := c.GetChat(req)
	if err != nil {
		panic(err)
	}

	println(resp.Choices[0].Message.Content)
	println(resp.Usage.PromptTokens)
	println(resp.Usage.CompletionTokens)
	println(resp.Usage.TotalTokens)
}

Documentation

Index

Constants

View Source
const MaxTokensGpt35Turbo = 4096
View Source
const ModelGpt35Turbo = "gpt-3.5-turbo"

Variables

This section is empty.

Functions

This section is empty.

Types

type Choice

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

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(apiKey string, opts ...OptionFunc) (*Client, error)

NewClient creates new OpenAI client

func (*Client) GetChat

func (c *Client) GetChat(r *Request) (*Response, error)

GetChat returns chat.

type ClientOptions

type ClientOptions struct {
	// HTTP transport
	Transport http.RoundTripper
	// Base url of OpenAI API
	URL string
}

ClientOptions contains parameters for Client initialization

type Error

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

type Message

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

type ModelType

type ModelType string

type OptionFunc

type OptionFunc func(opts *ClientOptions) error

func WithTransport

func WithTransport(transport http.RoundTripper) OptionFunc

WithTransport allows to override default client HTTP transport

func WithURL

func WithURL(baseURL string) OptionFunc

WithURL allows to override base url of OpenAPI

type Request

type Request struct {
	Model            ModelType   `json:"model"`
	Messages         []*Message  `json:"messages"`
	Temperature      float64     `json:"temperature,omitempty"`
	TopP             float64     `json:"top_p,omitempty"`
	N                int         `json:"n,omitempty"`
	Stream           bool        `json:"stream,omitempty"`
	Stop             interface{} `json:"stop,omitempty"`
	MaxTokens        int         `json:"max_tokens,omitempty"`
	PresencePenalty  float64     `json:"presence_penalty,omitempty"`
	FrequencyPenalty float64     `json:"frequency_penalty,omitempty"`
	LogitBias        interface{} `json:"logit_bias,omitempty"`
	User             string      `json:"user,omitempty"`
}

type Response

type Response struct {
	ID      string    `json:"id"`
	Object  string    `json:"object"`
	Created int64     `json:"created"`
	Choices []*Choice `json:"choices"`
	Usage   *Usage    `json:"usage"`
	Error   *Error    `json:"error,omitempty"`
}

type RoleType

type RoleType string
const (
	RoleUser      RoleType = "user"
	RoleAssistant RoleType = "assistant"
	RoleSystem    RoleType = "system"
)

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