Skip to content

Commit 771b9d3

Browse files
authored
Refactor CLI to use official OpenAI client (#7)
* Deprecate client packages * Center project around CLI * Update CLI
1 parent f0fa00b commit 771b9d3

31 files changed

+395
-10676
lines changed

README.md

Lines changed: 32 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,18 @@
11
# 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)
22

3-
An unofficial community-maintained Go client package and CLI for [OpenAI](https://openai.com/).
3+
An unofficial community-maintained CLI application for [OpenAI](https://openai.com/).
44

55
## Installation
66

7-
To use this package in your own Go project:
8-
9-
```console
10-
$ go get github.com/picatz/openai@latest
11-
```
12-
13-
To use the `openai` CLI:
14-
157
```console
168
$ go install github.com/picatz/openai/cmd/openai@latest
179
```
1810

19-
<center>
20-
<img src="./vhs/demo.gif"></img>
21-
</center>
22-
2311
> [!IMPORTANT]
2412
> To use the CLI you must have a valid `OPENAI_API_KEY` environment variable set. You can get one [here](https://platform.openai.com/).
2513
2614
> [!TIP]
27-
> 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.
28-
29-
## Usage
30-
31-
```go
32-
import "github.com/picatz/openai"
33-
34-
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
35-
```
36-
37-
### Assistants API
38-
39-
```go
40-
assistant, _ := client.CreateAssistant(ctx, &openai.CreateAssistantRequest{
41-
Model: openai.ModelGPT4TurboPreview,
42-
Instructions: "You are a helpful assistant for all kinds of tasks. Answer as concisely as possible.",
43-
Name: "openai-cli-assistant",
44-
Description: "A helpful assistant for all kinds of tasks.",
45-
Tools: []map[string]any{
46-
{
47-
"type": "code_interpreter",
48-
},
49-
{
50-
"type": "retrieval",
51-
},
52-
// {
53-
// "type": "function",
54-
// ...
55-
// },
56-
},
57-
})
58-
59-
thread, _ := client.CreateThread(ctx, nil)
60-
61-
client.CreateMessage(ctx, &openai.CreateMessageRequest{
62-
ThreadID: thread.ID,
63-
Role: openai.ChatRoleUser,
64-
Content: input,
65-
})
66-
67-
runResp, _ := client.CreateRun(ctx, &openai.CreateRunRequest{
68-
ThreadID: thread.ID,
69-
AssistantID: assistant.ID,
70-
})
71-
72-
openai.WaitForRun(ctx, client, thread.ID, runResp.ID, 700*time.Millisecond)
73-
74-
listResp, _ := client.ListMessages(ctx, &openai.ListMessagesRequest{
75-
ThreadID: thread.ID,
76-
Limit: 1,
77-
})
78-
79-
fmt.Println(listResp.Data[0].Content[0].Text())
80-
```
81-
82-
### Chat API
83-
84-
```go
85-
var history []openai.ChatMessage{
86-
{
87-
Role: openai.ChatRoleSystem,
88-
Content: "You are a helpful assistant for this example.",
89-
},
90-
{
91-
Role: openai.ChatRoleUser,
92-
Content: "Hello!", // Get input from user.
93-
},
94-
}
95-
96-
resp, _ := client.CreateChat(ctx, &openai.CreateChatRequest{
97-
Model: openai.ModelGPT35Turbo,
98-
Messages: history,
99-
})
100-
101-
fmt.Println(resp.Choices[0].Message.Content)
102-
// Hello how may I help you today?
103-
104-
// Update history, summarize, forget, etc. Then repeat.
105-
history = appened(history, resp.Choices[0].Message)
106-
```
107-
108-
### `openai` CLI
109-
110-
Use OpenAI's chat or edit and completion features on the command-line.
111-
112-
```console
113-
$ go install github.com/picatz/openai/cmd/openai@latest
114-
```
15+
> 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.
11516
11617
#### Usage
11718

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

13940
```console
140-
$ openai assistant
41+
$ openai assistant --help
42+
Interact with the OpenAI API using the assistant API.
14143

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

146-
> Hello!
46+
Usage:
47+
openai assistant [flags]
48+
openai assistant [command]
49+
50+
Examples:
51+
$ openai assistant # create a temporary assistant and start chatting
52+
$ openai assistant chat # same as above
53+
$ openai assistant create --name "Example" --model "gpt-4-turbo-preview" --description "..." --instructions "..." --code-interpreter --retrieval
54+
$ openai assistant list
55+
$ openai assistant info <assistant-id>
56+
$ openai assistant chat <assistant-id>
57+
$ openai assistant delete <assistant-id>
58+
59+
Available Commands:
60+
chat Start an interactive assistant chat session
61+
create Create an assistant
62+
delete Delete an assistant
63+
file Manage assistant files
64+
info Get information about an assistant
65+
list List assistants
66+
update Update an assistant
14767

148-
Hello there! How may I assist you today?
68+
Flags:
69+
-h, --help help for assistant
14970

150-
...
71+
Use "openai assistant [command] --help" for more information about a command.
15172
```
15273

15374
> [!TIP]
154-
> If no subcommand (like `assistant` or `chat`) is provided, the CLI will default to `assistant` mode.
155-
156-
```console
157-
$ openai chat
158-
159-
Welcome to the OpenAI API CLI chat mode. Type 'exit' to quit.
160-
161-
> Hello!
162-
163-
Hello there! How may I assist you today?
164-
165-
...
166-
```
75+
>
76+
> If provided no arguments, the CLI will default to the `assistant` command with an ephemeral session,
77+
> meaning messages and files will be deleted after exiting the session.

chat_roles.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)