chatgpt

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

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

Go to latest
Published: Mar 16, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

go-chatgpt

Documentation

Index

Examples

Constants

View Source
const (
	GPT3dot5Turbo = "gpt-3.5-turbo"
)

Variables

This section is empty.

Functions

func CheckResponseError

func CheckResponseError(r *http.Response) error

CheckResponseError checks the response, and in case of error, maps it to the error structure.

Types

type APIKey

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

type AuthMethod

type AuthMethod func(c *Client)

func SetAPIKey

func SetAPIKey(key string) AuthMethod

type ChatCompletionsChoice

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

type ChatCompletionsRequest

type ChatCompletionsRequest struct {
	Model    string     `json:"model"`
	Messages []*Message `json:"messages"`
}

type ChatCompletionsResponse

type ChatCompletionsResponse struct {
	ID      string                   `json:"id"`
	Object  string                   `json:"object"`
	Created int                      `json:"created"`
	Model   string                   `json:"model"`
	Usage   *ChatCompletionsUsage    `json:"usage"`
	Choices []*ChatCompletionsChoice `json:"choices"`
}

type ChatCompletionsUsage

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

type ChatOp

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

func (*ChatOp) Completions

Example
cli, _ := chatgpt.NewClient(chatgpt.SetAPIKey(os.Getenv("OPENAI_API_KEY")))

req := &chatgpt.ChatCompletionsRequest{
	Model: chatgpt.GPT3dot5Turbo,
	Messages: []*chatgpt.Message{
		{Role: "user", Content: "Hello!"},
	},
}

res, err := cli.Chat.Completions(req)
if err != nil {
	log.Fatal(err)
}

fmt.Println(res.Choices[0].Message)
Output:

type ChatService

type ChatService interface {
	Completions(req *ChatCompletionsRequest) (*ChatCompletionsResponse, error)
}

type Client

type Client struct {
	HTTPClient *http.Client

	Chat ChatService
	// contains filtered or unexported fields
}

func NewClient

func NewClient(setAuthMethod AuthMethod, opts ...Option) (*Client, error)

func (*Client) CreateAndDo

func (c *Client) CreateAndDo(method, relPath string, data, option, resource interface{}) error

CreateAndDo performs a web request to OpenAI. The `data`, `options` and `resource` arguments are optional and only relevant in certain situations. If the data argument is non-nil, it will be used as the body of the request for POST and PUT requests. The options argument is used for specifying request options such as search parameters. The resource argument is marshalled data returned from OpenAI. If the resource contains a pointer to data, the data will be overwritten with the content of the response.

func (*Client) Delete

func (c *Client) Delete(path string) error

Delete performs a DELETE request for the given path.

func (*Client) Get

func (c *Client) Get(path string, resource interface{}, option interface{}) error

Get performs a GET request for the given path and saves the result in the given resource.

func (*Client) NewRequest

func (c *Client) NewRequest(method, path string, body, option interface{}) (*http.Request, error)

NewRequest creates an API request. After creating a request, add the authentication information according to the method specified in NewClient().

func (*Client) Patch

func (c *Client) Patch(path string, data, resource interface{}) error

Patch performs a PATCH request for the given path and saves the result in the given resource.

func (*Client) Post

func (c *Client) Post(path string, data, resource interface{}) error

Post performs a POST request for the given path and saves the result in the given resource.

func (*Client) Put

func (c *Client) Put(path string, data, resource interface{}) error

Put performs a PUT request for the given path and saves the result in the given resource.

type Message

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

type Option

type Option func(c *Client)

func WithAPIVersion

func WithAPIVersion(version string) Option

func WithBaseURL

func WithBaseURL(url *url.URL) Option

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

func WithOrganization

func WithOrganization(organization string) Option

Jump to

Keyboard shortcuts

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