Skip to content

Commit

Permalink
feat!: Migrate OpenAI Assistants API to v2 and add support for vector…
Browse files Browse the repository at this point in the history
… stores (#402)
  • Loading branch information
davidmigloz committed May 6, 2024
1 parent 47fad3f commit 45de29a
Show file tree
Hide file tree
Showing 51 changed files with 23,139 additions and 14,617 deletions.
195 changes: 151 additions & 44 deletions packages/openai_dart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Unofficial Dart client for [OpenAI](https://platform.openai.com/docs/api-referen
- Custom base URL, headers and query params support (e.g. HTTP proxies)
- Custom HTTP client support (e.g. SOCKS5 proxies or advanced use cases)
- Partial Azure OpenAI API support
- It can be used to consume OpenAI-compatible APIs like [TogetherAI](https://www.together.ai/), [Anyscale](https://www.anyscale.com/), [OpenRouter](https://openrouter.ai), [One API](https://github.com/songquanpeng/one-api), etc.
- It can be used to consume OpenAI-compatible APIs like [TogetherAI](https://www.together.ai/), [Anyscale](https://www.anyscale.com/), [OpenRouter](https://openrouter.ai), [One API](https://github.com/songquanpeng/one-api), [Groq](https://groq.com/), [Llamafile](https://llamafile.ai/), [GPT4All](https://gpt4all.io/), etc.

**Supported endpoints:**

Expand All @@ -28,10 +28,14 @@ Unofficial Dart client for [OpenAI](https://platform.openai.com/docs/api-referen
- Images
- Models
- Moderations
- Assistants (with tools and streaming support) `beta`
- Assistants v2 (with tools and streaming support) `beta`
* Threads
* Messages
* Runs
* Run Steps
* Vector Stores
* Vector Store Files
* Vector Store File Batches

## Table of contents

Expand All @@ -50,8 +54,12 @@ Unofficial Dart client for [OpenAI](https://platform.openai.com/docs/api-referen
* [Threads (beta)](#threads-beta)
* [Messages (beta)](#messages-beta)
* [Runs (beta)](#runs-beta)
* [Vector Stores (beta)](#vector-stores-beta)
* [Vector Store Files (beta)](#vector-store-files-beta)
* [Vector Store File Batches (beta)](#vector-store-file-batches-beta)
- [Advance Usage](#advance-usage)
* [Azure OpenAI Service](#azure-openai-service)
* [OpenAI-compatible APIs](#openai-compatible-apis)
* [Default HTTP client](#default-http-client)
* [Custom HTTP client](#custom-http-client)
* [Using a proxy](#using-a-proxy)
Expand Down Expand Up @@ -553,6 +561,12 @@ final res = await client.cancelBatch(
);
```

**List batches:**

```dart
final res = await client.listBatches();
```

### Images

Given a prompt and/or an input image, the model will generate a new image.
Expand Down Expand Up @@ -654,39 +668,18 @@ final res = await client.createAssistant(
);
```

**Create assistant file:**

```dart
final res = await client.createAssistantFile(
assistantId: assistantId,
request: CreateAssistantFileRequest(fileId: fileId),
);
```

**List assistants:**

```dart
final res = await client.listAssistants();
```

**List assistant files:**

```dart
final res = await client.listAssistantFiles(assistantId: assistantId);
```

**Retrieve assistant:**

```dart
final res = await client.getAssistant(assistantId: assistantId);
```

**Retrieve assistant file:**

```dart
final res = await client.getAssistantFile(assistantId: assistantId, fileId: fileId);
```

**Modify assistant:**

```dart
Expand All @@ -702,12 +695,6 @@ final res = await client.modifyAssistant(
final res = await client.deleteAssistant(assistantId: assistantId);
```

**Delete assistant file:**

```dart
final res = await client.deleteAssistantFile(assistantId: assistantId, fileId: fileId);
```

### Threads (beta)

Create threads that assistants can interact with.
Expand Down Expand Up @@ -767,36 +754,26 @@ final res = await client.createThreadMessage(
final res = await client.listThreadMessages(threadId: threadId);
```

**List message files:**

```dart
final res = await client.listThreadMessageFiles(threadId: threadId, messageId: messageId);
```

**Retrieve message:**

```dart
final res = await client.getThreadMessage(threadId: threadId, messageId: messageId);
```

**Retrieve message file:**
**Modify message:**

```dart
final res = await client.getThreadMessageFile(
final res = await client.modifyThreadMessage(
threadId: threadId,
messageId: messageId,
fileId: fileId,
request: ModifyMessageRequest(metadata: {'new': 'metadata'}),
);
```

**Modify message:**
**Delete message:**

```dart
final res = await client.modifyThreadMessage(
threadId: threadId,
messageId: messageId,
request: ModifyMessageRequest(metadata: {'new': 'metadata'}),
);
final res await deleteThreadMessage(threadId: threadId, messageId: messageId);
```

### Runs (beta)
Expand Down Expand Up @@ -925,6 +902,136 @@ final res = await client.submitThreadToolOutputsToRunStream(
final res = await client.cancelThreadRun(threadId: threadId, runId: runId);
```

### Vector Stores (beta)

Vector stores are used to store files for use by the `file_search` tool.

Related guide: [File Search](https://platform.openai.com/docs/assistants/tools/file-search)

**Create vector store:**

```dart
final res = await client.createVectorStore(
request: CreateVectorStoreRequest(
name: 'Support FAQ',
),
);
```

**List vector stores:**

```dart
final res = await client.listVectorStores();
```

**Retrieve vector store:**

```dart
final res = await client.getVectorStore(vectorStoreId: vectorStoreId);
```

**Modify vector store:**

```dart
final res = await client.modifyVectorStore(
vectorStoreId: vectorStoreId,
request: UpdateVectorStoreRequest(
name: 'New name',
),
);
```

**Delete vector store:**

```dart
final res = await client.deleteVectorStore(vectorStoreId: vectorStoreId);
```

### Vector Store Files (beta)

Vector store files represent files inside a vector store.

Related guide: [File Search](https://platform.openai.com/docs/assistants/tools/file-search)

**Create vector store file:**

```dart
final res = await client.createVectorStoreFile(
vectorStoreId: vectorStoreId,
request: CreateVectorStoreFileRequest(
fileId: 'file-abc123',
),
);
```

**List vector store files:**

```dart
final res = await client.listVectorStoreFiles(vectorStoreId: vectorStoreId);
```

**Retrieve vector store file:**

```dart
final res = await client.getVectorStoreFile(
vectorStoreId: vectorStoreId,
fileId: fileId,
);
```

**Delete vector store file:**

```dart
final res = await client.deleteVectorStoreFile(
vectorStoreId: vectorStoreId,
fileId: fileId,
);
```

### Vector Store File Batches (beta)

Vector store file batches represent operations to add multiple files to a vector store.

Related guide: [File Search](https://platform.openai.com/docs/assistants/tools/file-search)

**Create vector store file batch:**

```dart
final res = await client.createVectorStoreFileBatch(
vectorStoreId: vectorStoreId,
request: CreateVectorStoreFileBatchRequest(
fileIds: ['file-abc123', 'file-abc456'],
),
);
```

**Retrieve vector store file batch:**

```dart
final res = await client.getVectorStoreFileBatch(
vectorStoreId: vectorStoreId,
batchId: batchId,
);
```

**Cancel vector store file batch:**

```dart
final res = await client.cancelVectorStoreFileBatch(
vectorStoreId: vectorStoreId,
batchId: batchId,
);
```

**List vector store files in a batch:**

```dart
final res = await client.listFilesInVectorStoreBatch(
vectorStoreId: vectorStoreId,
batchId: batchId,
);
```

## Advance Usage

### Azure OpenAI Service
Expand Down
2 changes: 1 addition & 1 deletion packages/openai_dart/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class OpenAIClient extends g.OpenAIClient {
baseUrl: baseUrl,
headers: {
if (organization != null) 'OpenAI-Organization': organization,
'OpenAI-Beta': 'assistants=v1',
'OpenAI-Beta': 'assistants=v2',
...?headers,
},
queryParams: queryParams ?? const {},
Expand Down

0 comments on commit 45de29a

Please sign in to comment.