Skip to content

picatz/openai

Repository files navigation

OpenAI Go Reference Go Report Card License: MPL 2.0

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_KEY environment variable set. You can get one here.

Tip

You can customize which model is used by setting the OPENAI_MODEL environment variable. The default is gpt-4-turbo-preview today, 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 assistant or chat) is provided, the CLI will default to assistant mode.

$ openai chat

Welcome to the OpenAI API CLI chat mode. Type 'exit' to quit.

> Hello!

Hello there! How may I assist you today?

...