Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/picatz/openai
π€ Go package and CLI for OpenAI
https://github.com/picatz/openai
go gpt-3 openapi
Last synced: 5 days ago
JSON representation
π€ Go package and CLI for OpenAI
- Host: GitHub
- URL: https://github.com/picatz/openai
- Owner: picatz
- License: mpl-2.0
- Created: 2023-01-11T03:33:09.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-19T12:49:26.000Z (7 months ago)
- Last Synced: 2024-06-20T12:31:45.528Z (5 months ago)
- Topics: go, gpt-3, openapi
- Language: Go
- Homepage: https://pkg.go.dev/github.com/picatz/openai
- Size: 1.4 MB
- Stars: 33
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# OpenAI [![Go Reference](https://pkg.go.dev/badge/github.com/picatz/openai.svg)](https://pkg.go.dev/github.com/picatz/openai) [![Go Report Card](https://goreportcard.com/badge/github.com/picatz/openai)](https://goreportcard.com/report/github.com/picatz/openai) [![License: MPL 2.0](https://img.shields.io/badge/License-MPL_2.0-brightgreen.svg)](https://opensource.org/licenses/MPL-2.0)
An unofficial community-maintained Go client package and CLI for [OpenAI](https://openai.com/).## Installation
To use this package in your own Go project:
```console
$ go get github.com/picatz/openai@latest
```To use the `openai` CLI:
```console
$ 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](https://platform.openai.com/).> [!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
```go
import "github.com/picatz/openai"client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
```### Assistants API
```go
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
```go
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.
```console
$ go install github.com/picatz/openai/cmd/openai@latest
```#### Usage
```console
$ openai --help
OpenAI CLIUsage:
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Β·EFlags:
-h, --help help for openaiUse "openai [command] --help" for more information about a command.
``````console
$ openai assistantWelcome 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.```console
$ openai chatWelcome to the OpenAI API CLI chat mode. Type 'exit' to quit.
> Hello!
Hello there! How may I assist you today?
...
```