# README
openai-go
OpenAI API wrapper library for Go.
How to use
Generate API key from here,
and get organization id from here.
const apiKey = "ab-cdefHIJKLMNOPQRSTUVWXYZ0123456789"
const orgID = "org-0123456789ABCDefghijklmnopQRSTUVWxyz"
func main() {
client := NewClient(apiKey, orgID)
if models, err := client.ListModels(); err != nil {
log.Printf("available models = %+v", models.Data)
}
}
How to test
Export following environment variables:
$ export OPENAI_API_KEY=ab-cdefHIJKLMNOPQRSTUVWXYZ0123456789
$ export OPENAI_ORGANIZATION=org-0123456789ABCDefghijklmnopQRSTUVWxyz
# for verbose messages and http request dumps,
$ export VERBOSE=true
and then
$ go test
CAUTION: It is advised to set usage limits before running tests; running all tests at once costs about ~$0.2.
Todos/WIP
Implemented
All API functions so far (2023.11.07.) are implemented, but not all of them were tested on a paid account.
- Audio
- Chat
- Completions
- Embeddings
- Fine-tuning
- Files
- Images
- Models: works on a non-paid account
- Moderations: works on a non-paid account
Beta
Note
Beta API functions require beta header like this:
client.SetBetaHeader(`assistants=v1`)
Help Wanted
-
Stream(server-sent events) options are not implemented yet.thanks to @tectiv3 :-) - Add some sample applications.
# Functions
NewChatAssistantMessage returns a new ChatMessage with assistant role.
NewChatCompletionTool returns a ChatCompletionTool.
NewChatMessageContentWithBytes returns a ChatMessageContent struct with given `bytes`.
NewChatMessageContentWithFileParam returns a ChatMessageContent struct with given `file`.
NewChatMessageContentWithImageURL returns a ChatMessageContent struct with given `url`.
NewChatMessageContentWithText returns a ChatMessageContent struct with given `text`.
NewChatSystemMessage returns a new ChatMessage with system role.
NewChatToolMessage returns a new ChatMesssage with tool role.
NewChatUserMessage returns a new ChatMessage with user role.
NewClient returns a new API client.
NewCodeInterpreterTool returns a tool with type: 'code_interpreter'.
NewFileParamFromBytes returns a new FileParam with given bytes.
NewFileParamFromFilepath returns a new FileParam with bytes read from given filepath.
NewFunctionTool returns a tool with type: 'function'.
NewRetrievalTool returns a tool with type: 'retrieval'.
NewThreadMessage returns a new ThreadMessage with given `content`.
NewToolFunctionParameters returns an empty ToolFunctionParameters.
# Constants
ChatCompletionResponseFormatType constants.
ChatCompletionResponseFormatType constants.
ChatCompletionToolChoiceMode constants.
ChatCompletionToolChoiceMode 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
timeout seconds.
No description provided by the author
No description provided by the author
No description provided by the author
FineTuningJobStatus constants.
FineTuningJobStatus constants.
FineTuningJobStatus constants.
FineTuningJobStatus constants.
FineTuningJobStatus constants.
FineTuningJobStatus 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
for Dall-E-3.
No description provided by the author
No description provided by the author
for Dall-E-2.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
MessageContentTextAnnotationType constants.
MessageContentTextAnnotationType constants.
MessageContentType constants.
MessageContentType constants.
No description provided by the author
RunErrorCode constants.
RunErrorCode constants.
RunStatus constants.
RunStatus constants.
RunStatus constants.
RunStatus constants.
RunStatus constants.
RunStatus constants.
RunStatus constants.
RunStatus constants.
RunStepStatus constants.
RunStepStatus constants.
RunStepStatus constants.
RunStepStatus constants.
RunStepStatus constants.
RunStepType constants.
RunStepType 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
ToolCallCodeInterpreterOutputType constants.
ToolCallCodeInterpreterOutputType constants.
ToolType constants.
ToolType constants.
ToolType 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
# Structs
Assistant struct for assistant object
https://platform.openai.com/docs/api-reference/assistants/object.
AssistantDeletionStatus struct for API response.
AssistantFile struct for attached files of assistants
https://platform.openai.com/docs/api-reference/assistants/file-object.
AssistantFileDeletionStatus struct for API response.
AssistantFiless struct for API response.
Assistants struct for API response.
ChatCompletion struct for chat completion response.
ChatCompletionChoice struct for chat completion response.
ChatCompletionResponseFormat struct for chat completion request.
ChatCompletionTool struct for chat completion function
https://platform.openai.com/docs/api-reference/chat/create#chat-create-tools.
ChatCompletionToolFunction struct.
ChatMessage struct for chat completion
https://platform.openai.com/docs/guides/chat/introduction.
ChatMessageContent struct.
Classification struct for Moderation strcut.
Client struct which holds its API key, Organization ID, and HTTP client.
CommonResponse struct for responses with common properties.
Completion struct for response.
CompletionChoice struct for completion response.
DeletedFile struct for response.
Embedding struct for Embeddings struct.
Embeddings struct for response.
Error struct for response error property.
File struct for file items.
FileParam struct for multipart requests.
Files struct for response.
FineTuningHyperparameters struct.
FineTuningJob struct.
FineTuningJobEvent struct.
FineTuningJobEvents struct.
FineTuningJobs struct.
GeneratedImages struct for image creation responses.
https://platform.openai.com/docs/api-reference/messages/object.
MessageContent struct for Message.
MessageContentImageFile struct for MessageContent.
MessageContentText struct for MessageContent.
MessageContentTextAnntation struct for MessageContentText.
MessageContentTextAnnotationFileCitation struct.
MessageContentTextAnnotationFilePath struct.
https://platform.openai.com/docs/api-reference/messages/file-object.
MessageFiles struct for API response.
Messages struct for API response.
Model struct.
ModelDeletionStatus struct for API response.
ModelList struct for API response.
Moderation struct for response.
Permission struct.
https://platform.openai.com/docs/api-reference/runs/object.
RunAction struct for Run struct.
RunError struct for Run struct.
RunnableThread struct for CreateThreadAndRunOptions.
RunnableThreadMessage struct for RunnableThread struct.
Runs struct for API response.
https://platform.openai.com/docs/api-reference/runs/step-object.
RunStepDetails struct for RunStepObject struct.
RunStepDetailsMessageCreation struct for RunStepDetails struct.
RunStepDetailsToolCall struct for RunStepDetails struct.
RunStepDetailsToolCallCodeInterpreter struct for RunStepDetailsToolCall struct.
RunStepDetailsToolCallsFunction struct for RunStepDetailsToolCall struct.
RunStepDetailsToolCallRetrieval struct for RunStepDetailsToolCall struct (empty object for now).
RunSteps struct for API response.
Thread struct
https://platform.openai.com/docs/api-reference/threads/object.
ThreadDeletionStatus for API response.
ThreadMessage struct for Thread.
Tool struct for assistant object
https://platform.openai.com/docs/api-reference/assistants/object#assistants/object-tools.
ToolCall struct.
ToolCallCodeInterpreterOutput struct for RunStepDetailsToolCallCodeInterpreter struct.
ToolCallCodeInterpreterOutputImage struct for ToolCallCodeInterpreterOutput struct.
ToolCallFunction struct for ToolCall.
ToolFunction struct for Tool struct.
ToolOutput struct for API request.
Transcription struct for response.
Usage struct for reponses.
# Interfaces
ChatUserMessageContentTypes interface for type constraints in `NewChatUserMessage`.
# Type aliases
ChatCompletionOptions for creating chat completions.
ChatCompletionResponseFormatType type for constants.
ChatCompletionToolChoiceMode type
https://platform.openai.com/docs/api-reference/chat/create#chat-create-tool_choice.
ChatMessageRole type for constants.
CompletionOptions for creating completions.
CreateAssistantOptions for creating assistant.
CreateMessageOptions for creating message.
CreateRunOptions for creating run.
CreateThreadAndRunOptions for creating thread and running it.
CreateThreadOptions for creating thread.
EmbeddingEncodingFormat type for constants.
EmbeddingOptions for creating embedding.
FineTuningJobEventsOptions for listing fine-tuning job events.
FineTuningJobOptions for retrieving fine-tuning jobs.
FineTuningJobsOptions for listing fine-tuning jobs.
FineTuningJobStatus type.
ImageEditOptions for creating image edits.
ImageOptions for creating images.
ImageResponseFormat type for constants.
ImageSize type for constants.
ImageStyle type for constants.
ImageVariationOptions for creating image variations.
ListAssistantFilesOptions for listing assistant files.
ListAssistantsOptions for listing assistants.
ListMessageFilesOptions for listing message files.
ListMessagesOptions for listing messages.
ListRunsOptions for listing runs.
ListRunStepsOptions type for listing run steps.
MessageContentTextAnnotationType type for constants.
MessageContentType type for constants.
ModerationOptions for creating moderation.
ModifyAssistantOptions for modifying assistant.
ModifyMessageOptions for modifying message.
ModifyRunOptions for modifying run.
ModifyThreadOptions for modifying thread.
RetrievedFile struct for response.
RunErrorCode type for constants.
RunStatus type for constants.
RunStepStatus type for constants.
RunStepType type for constants.
SpeechOptions for creating speech.
SpeechResponseFormat type for constants.
SpeechVoice type for constants.
ToolCallCodeInterpreterOutputType type for constants.
ToolFunctionParameters type.
ToolType type for constants.
TranscriptionOptions for creating transcription.
TranscriptionResponseFormat type for constants.
Transcription struct for response.
TranslationOptions for creating transcription.
TransclationResponseFormat type for constants.
UploadedFile struct for response.