Categorygithub.com/gopenai/openai-client
modulepackage
0.1.2
Repository: https://github.com/gopenai/openai-client.git
Documentation: pkg.go.dev

# README

OpenAI Golang SDK Go Reference

This is an unofficial Golang SDK for the OpenAI API. It provides a simple and easy-to-use way to interact with the OpenAI API using Golang.

Installation

To install the OpenAI Golang SDK, simply run:

go get github.com/gopenai/openai-client

Usage

The OpenAI Golang SDK can be easily generated using a _oas/schema.yaml file that defines specification for the OpenAI API. To generate the SDK, project uses the ogen tool, which is a code generator for OpenAPI specifications. This allows you to easily update the SDK as the OpenAI API changes. If you change schema and want to generate sdk you can run the following command in makefile:

make generate

To use the OpenAI Golang SDK, you will need to have an API key for the OpenAI API. You can obtain an API key by creating an account on the OpenAI website.

Once you have your API key, you can create a new OpenAI client and start making requests to the API. Here's an example:

package main

import (
	"context"
	"fmt"

	openai "github.com/gopenai/openai-client"
)

func main() {
	// Replace SERVER_URL with your actual SERVER_URL key
	client, err := openai.NewClient("SERVER_URL")
	if err != nil {
		panic(err)
	}

	// Generate a text completion
	prompt := "Once upon a time,"
	req := &openai.CreateChatCompletionRequest{
		Model: "gpt-3.5-turbo",
		Messages: []openai.ChatCompletionRequestMessage{
			{
				Role:    openai.ChatCompletionRequestMessageRoleUser,
				Content: prompt,
			},
		},
	}
	response, err := client.CreateChatCompletion(context.Background(), req)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	// Print the completed text
	fmt.Println(response.Choices[0].Message.Value.Content)
}


This example generates a text completion using the OpenAI API, based on a prompt of "Once upon a time,". The resulting completed text is printed to the console.

You can also use bearer token auth. Here's an example:

package main

import (
	"context"
	"fmt"
	"os"

	openai "github.com/gopenai/openai-client"
)

func main() {
	// Replace SERVER_URL and API_TOKEN with your actual SERVER_URL and API_TOKEN values
	client, err := openai.NewBearerAuthClient(os.Getenv("SERVER_URL"), os.Getenv("API_TOKEN"))
	if err != nil {
		panic(err)
	}

	// Generate a text completion
	prompt := "Introduce himself please"
	req := &openai.CreateChatCompletionRequest{
		Model: "gpt-3.5-turbo",
		Messages: []openai.ChatCompletionRequestMessage{
			{
				Role:    openai.ChatCompletionRequestMessageRoleUser,
				Content: prompt,
			},
		},
	}
	response, err := client.CreateChatCompletion(context.Background(), req)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	// Print the completed text
	fmt.Println(response.Choices[0].Message.Value.Content)
}

Contributing

This SDK is open source, and contributions are welcome! If you have any bug reports, feature requests, or patches, please submit them through the GitHub issue tracker and pull request system.

Licence

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

# Packages

No description provided by the author

# Functions

NewBearerAuthClient creates new client ready for doing requests with bearer token.
NewClient initializes new Client defined by OAS.
NewNullCreateChatCompletionRequestStop returns new CreateChatCompletionRequestStop from struct{}.
NewOptBool returns new OptBool with value set to v.
NewOptChatCompletionResponseMessage returns new OptChatCompletionResponseMessage with value set to v.
NewOptCreateChatCompletionRequestLogitBias returns new OptCreateChatCompletionRequestLogitBias with value set to v.
NewOptCreateChatCompletionRequestStop returns new OptCreateChatCompletionRequestStop with value set to v.
NewOptCreateChatCompletionResponseUsage returns new OptCreateChatCompletionResponseUsage with value set to v.
NewOptInt returns new OptInt with value set to v.
NewOptMultipartFile returns new OptMultipartFile with value set to v.
NewOptNilBool returns new OptNilBool with value set to v.
NewOptNilCreateEditResponseChoicesItemLogprobs returns new OptNilCreateEditResponseChoicesItemLogprobs with value set to v.
NewOptNilCreateImageEditRequestResponseFormat returns new OptNilCreateImageEditRequestResponseFormat with value set to v.
NewOptNilCreateImageEditRequestSize returns new OptNilCreateImageEditRequestSize with value set to v.
NewOptNilCreateImageRequestResponseFormat returns new OptNilCreateImageRequestResponseFormat with value set to v.
NewOptNilCreateImageRequestSize returns new OptNilCreateImageRequestSize with value set to v.
NewOptNilCreateImageVariationRequestResponseFormat returns new OptNilCreateImageVariationRequestResponseFormat with value set to v.
NewOptNilCreateImageVariationRequestSize returns new OptNilCreateImageVariationRequestSize with value set to v.
NewOptNilFloat64 returns new OptNilFloat64 with value set to v.
NewOptNilFloat64Array returns new OptNilFloat64Array with value set to v.
NewOptNilInt returns new OptNilInt with value set to v.
NewOptNilString returns new OptNilString with value set to v.
NewOptString returns new OptString with value set to v.
NewServer creates new Server.
NewStringArrayCreateChatCompletionRequestStop returns new CreateChatCompletionRequestStop from []string.
NewStringArrayCreateModerationRequestInput returns new CreateModerationRequestInput from []string.
NewStringCreateChatCompletionRequestStop returns new CreateChatCompletionRequestStop from string.
NewStringCreateModerationRequestInput returns new CreateModerationRequestInput from string.
WithClient specifies http client to use.
WithErrorHandler specifies error handler to use.
WithMaxMultipartMemory specifies limit of memory for storing file parts.
WithMeterProvider specifies a meter provider to use for creating a meter.
WithMethodNotAllowed specifies Method Not Allowed handler to use.
WithMiddleware specifies middlewares to use.
WithNotFound specifies Not Found handler to use.
WithPathPrefix specifies server path prefix.
WithServerURL sets context key to override server URL.
WithTracerProvider specifies a tracer provider to use for creating a tracer.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Possible values for CreateChatCompletionRequestStopType.
Possible values for CreateChatCompletionRequestStopType.
Possible values for CreateModerationRequestInputType.
Possible values for CreateChatCompletionRequestStopType.
Possible values for CreateModerationRequestInputType.

# Structs

CancelFineTuneParams is parameters of cancelFineTune operation.
Ref: #/components/schemas/ChatCompletionRequestMessage.
Ref: #/components/schemas/ChatCompletionResponseMessage.
Client implements OAS client.
Ref: #/components/schemas/CreateChatCompletionRequest.
Modify the likelihood of specified tokens appearing in the completion.
Up to 4 sequences where the API will stop generating further tokens.
Ref: #/components/schemas/CreateChatCompletionResponse.
No description provided by the author
No description provided by the author
Ref: #/components/schemas/CreateEditRequest.
Ref: #/components/schemas/CreateEditResponse.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Ref: #/components/schemas/CreateFineTuneRequest.
Ref: #/components/schemas/CreateImageEditRequest.
Ref: #/components/schemas/CreateImageEditRequest.
Ref: #/components/schemas/CreateImageRequest.
Ref: #/components/schemas/CreateImageVariationRequest.
Ref: #/components/schemas/CreateImageVariationRequest.
Ref: #/components/schemas/CreateModerationRequest.
The input text to classify.
Ref: #/components/schemas/CreateModerationResponse.
No description provided by the author
No description provided by the author
No description provided by the author
DeleteFileParams is parameters of deleteFile operation.
Ref: #/components/schemas/DeleteFileResponse.
DeleteModelParams is parameters of deleteModel operation.
Ref: #/components/schemas/DeleteModelResponse.
DownloadFileParams is parameters of downloadFile operation.
Ref: #/components/schemas/ListFilesResponse.
ListFineTuneEventsParams is parameters of listFineTuneEvents operation.
Ref: #/components/schemas/ListFineTuneEventsResponse.
Ref: #/components/schemas/ListFineTunesResponse.
Ref: #/components/schemas/ListModelsResponse.
OptBool is optional bool.
OptChatCompletionResponseMessage is optional ChatCompletionResponseMessage.
OptCreateChatCompletionRequestLogitBias is optional *CreateChatCompletionRequestLogitBias.
OptCreateChatCompletionRequestStop is optional CreateChatCompletionRequestStop.
OptCreateChatCompletionResponseUsage is optional CreateChatCompletionResponseUsage.
OptInt is optional int.
OptMultipartFile is optional ht.MultipartFile.
OptNilBool is optional nullable bool.
OptNilCreateEditResponseChoicesItemLogprobs is optional nullable CreateEditResponseChoicesItemLogprobs.
OptNilCreateImageEditRequestResponseFormat is optional nullable CreateImageEditRequestResponseFormat.
OptNilCreateImageEditRequestSize is optional nullable CreateImageEditRequestSize.
OptNilCreateImageRequestResponseFormat is optional nullable CreateImageRequestResponseFormat.
OptNilCreateImageRequestSize is optional nullable CreateImageRequestSize.
OptNilCreateImageVariationRequestResponseFormat is optional nullable CreateImageVariationRequestResponseFormat.
OptNilCreateImageVariationRequestSize is optional nullable CreateImageVariationRequestSize.
OptNilFloat64 is optional nullable float64.
OptNilFloat64Array is optional nullable []float64.
OptNilInt is optional nullable int.
OptNilString is optional nullable string.
OptString is optional string.
RetrieveFileParams is parameters of retrieveFile operation.
RetrieveFineTuneParams is parameters of retrieveFineTune operation.
RetrieveModelParams is parameters of retrieveModel operation.
Route is route object.
Server implements http server based on OpenAPI v3 specification and calls Handler to handle requests.
UnimplementedHandler is no-op Handler which returns http.ErrNotImplemented.

# Interfaces

ClientOption is client config option.
Handler handles operations described by OpenAPI v3 specification.
Option is config option.
ServerOption is server config option.

# Type aliases

The role of the author of this message.
The role of the author of this message.
CreateChatCompletionRequestStopType is oneOf type of CreateChatCompletionRequestStop.
The format in which the generated images are returned.
The size of the generated images.
The format in which the generated images are returned.
The size of the generated images.
The format in which the generated images are returned.
The size of the generated images.
CreateModerationRequestInputType is oneOf type of CreateModerationRequestInput.
ErrorHandler is error handler.
No description provided by the author
No description provided by the author
No description provided by the author
Middleware is middleware type.
No description provided by the author
No description provided by the author