Skip to content

Commit

Permalink
Refactor CLI to use official OpenAI client (#7)
Browse files Browse the repository at this point in the history
* Deprecate client packages
* Center project around CLI
* Update CLI
  • Loading branch information
picatz authored Jan 22, 2025
1 parent f0fa00b commit 771b9d3
Show file tree
Hide file tree
Showing 31 changed files with 395 additions and 10,676 deletions.
153 changes: 32 additions & 121 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,117 +1,18 @@
# 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/).
An unofficial community-maintained CLI application 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
```

<center>
<img src="./vhs/demo.gif"></img>
</center>

> [!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
```
> You can customize which model is used by setting the `OPENAI_MODEL` environment variable. The default is `gpt-4o` today, but it may change in the future.
#### Usage

Expand All @@ -137,30 +38,40 @@ Use "openai [command] --help" for more information about a command.
```

```console
$ openai assistant
$ openai assistant --help
Interact with the OpenAI API using the assistant API.

Welcome to the OpenAI API CLI assistant mode!
WARNING: Messages and files disappear after exiting.
This can be used to create a temporary assistant, or interact with an existing assistant.

> Hello!
Usage:
openai assistant [flags]
openai assistant [command]

Examples:
$ openai assistant # create a temporary assistant and start chatting
$ openai assistant chat # same as above
$ openai assistant create --name "Example" --model "gpt-4-turbo-preview" --description "..." --instructions "..." --code-interpreter --retrieval
$ openai assistant list
$ openai assistant info <assistant-id>
$ openai assistant chat <assistant-id>
$ openai assistant delete <assistant-id>

Available Commands:
chat Start an interactive assistant chat session
create Create an assistant
delete Delete an assistant
file Manage assistant files
info Get information about an assistant
list List assistants
update Update an assistant

Hello there! How may I assist you today?
Flags:
-h, --help help for assistant

...
Use "openai assistant [command] --help" for more information about a command.
```

> [!TIP]
> If no subcommand (like `assistant` or `chat`) is provided, the CLI will default to `assistant` mode.
```console
$ openai chat

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

> Hello!

Hello there! How may I assist you today?

...
```
>
> If provided no arguments, the CLI will default to the `assistant` command with an ephemeral session,
> meaning messages and files will be deleted after exiting the session.
17 changes: 0 additions & 17 deletions chat_roles.go

This file was deleted.

Loading

0 comments on commit 771b9d3

Please sign in to comment.