openai
openai copied to clipboard
π€ Go package and CLI for OpenAI
OpenAI

An unofficial community-maintained Go client package and CLI for OpenAI.
Installation
To use this package in your own Go project:
$ go get github.com/picatz/openai@latest
To use the openai CLI:
$ go install github.com/picatz/openai/cmd/openai@latest
[!IMPORTANT] To use the CLI you must have a valid
OPENAI_API_KEYenvironment variable set. You can get one here.
[!TIP] You can customize which model is used by setting the
OPENAI_MODELenvironment variable. The default isgpt-4-turbo-previewtoday, but it may change in the future.
Usage
import "github.com/picatz/openai"
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
Assistants API
assistant, _ := client.CreateAssistant(ctx, &openai.CreateAssistantRequest{
Model: openai.ModelGPT4TurboPreview,
Instructions: "You are a helpful assistant for all kinds of tasks. Answer as concisely as possible.",
Name: "openai-cli-assistant",
Description: "A helpful assistant for all kinds of tasks.",
Tools: []map[string]any{
{
"type": "code_interpreter",
},
{
"type": "retrieval",
},
// {
// "type": "function",
// ...
// },
},
})
thread, _ := client.CreateThread(ctx, nil)
client.CreateMessage(ctx, &openai.CreateMessageRequest{
ThreadID: thread.ID,
Role: openai.ChatRoleUser,
Content: input,
})
runResp, _ := client.CreateRun(ctx, &openai.CreateRunRequest{
ThreadID: thread.ID,
AssistantID: assistant.ID,
})
openai.WaitForRun(ctx, client, thread.ID, runResp.ID, 700*time.Millisecond)
listResp, _ := client.ListMessages(ctx, &openai.ListMessagesRequest{
ThreadID: thread.ID,
Limit: 1,
})
fmt.Println(listResp.Data[0].Content[0].Text())
Chat API
var history []openai.ChatMessage{
{
Role: openai.ChatRoleSystem,
Content: "You are a helpful assistant for this example.",
},
{
Role: openai.ChatRoleUser,
Content: "Hello!", // Get input from user.
},
}
resp, _ := client.CreateChat(ctx, &openai.CreateChatRequest{
Model: openai.ModelGPT35Turbo,
Messages: history,
})
fmt.Println(resp.Choices[0].Message.Content)
// Hello how may I help you today?
// Update history, summarize, forget, etc. Then repeat.
history = appened(history, resp.Choices[0].Message)
openai CLI
Use OpenAI's chat or edit and completion features on the command-line.
$ go install github.com/picatz/openai/cmd/openai@latest
Usage
$ openai --help
OpenAI CLI
Usage:
openai [flags]
openai [command]
Available Commands:
assistant Start an interactive assistant chat session
chat Chat with the OpenAI API
completion Generate the autocompletion script for the specified shell
help Help about any command
image Generate an image with DALLΒ·E
Flags:
-h, --help help for openai
Use "openai [command] --help" for more information about a command.
$ openai assistant
Welcome to the OpenAI API CLI assistant mode!
WARNING: Messages and files disappear after exiting.
> Hello!
Hello there! How may I assist you today?
...
[!TIP] If no subcommand (like
assistantorchat) is provided, the CLI will default toassistantmode.
$ openai chat
Welcome to the OpenAI API CLI chat mode. Type 'exit' to quit.
> Hello!
Hello there! How may I assist you today?
...