Skip to content

Commit 8bb929b

Browse files
feat(api): add vector stores (#776)
1 parent 6f72e7a commit 8bb929b

32 files changed

+2420
-690
lines changed

.stats.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
configured_endpoints: 55
1+
configured_endpoints: 62

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Documentation for each method, request param, and response field are available i
102102
103103
### Polling Helpers
104104

105-
When interacting with the API some actions such as starting a Run may take time to complete. The SDK includes
105+
When interacting with the API some actions such as starting a Run and adding files to vector stores are asynchronous and take time to complete. The SDK includes
106106
helper functions which will poll the status until it reaches a terminal state and then return the resulting object.
107107
If an API method results in an action which could benefit from polling there will be a corresponding version of the
108108
method ending in 'AndPoll'.
@@ -117,6 +117,20 @@ const run = await openai.beta.threads.runs.createAndPoll(thread.id, {
117117

118118
More information on the lifecycle of a Run can be found in the [Run Lifecycle Documentation](https://platform.openai.com/docs/assistants/how-it-works/run-lifecycle)
119119

120+
### Bulk Upload Helpers
121+
122+
When creating an interacting with vector stores, you can use the polling helpers to monitor the status of operations.
123+
For convenience, we also provide a bulk upload helper to allow you to simultaneously upload several files at once.
124+
125+
```ts
126+
const fileList = [
127+
createReadStream('/home/data/example.pdf'),
128+
...
129+
];
130+
131+
const batch = await openai.vectorStores.fileBatches.uploadAndPoll(vectorStore.id, fileList);
132+
```
133+
120134
### Streaming Helpers
121135

122136
The SDK also includes helpers to process streams and handle the incoming events.

api.md

+90-66
Large diffs are not rendered by default.

helpers.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Streaming Helpers
1+
# Helpers
22

33
OpenAI supports streaming responses when interacting with the [Chat](#chat-streaming) or [Assistant](#assistant-streaming-api) APIs.
44

@@ -449,3 +449,24 @@ See an example of a Next.JS integration here [`examples/stream-to-client-next.ts
449449
#### Proxy Streaming to a Browser
450450

451451
See an example of using express to stream to a browser here [`examples/stream-to-client-express.ts`](examples/stream-to-client-express.ts).
452+
453+
# Polling Helpers
454+
455+
When interacting with the API some actions such as starting a Run and adding files to vector stores are asynchronous and take time to complete.
456+
The SDK includes helper functions which will poll the status until it reaches a terminal state and then return the resulting object.
457+
If an API method results in an action which could benefit from polling there will be a corresponding version of the
458+
method ending in `_AndPoll`.
459+
460+
All methods also allow you to set the polling frequency, how often the API is checked for an update, via a function argument (`pollIntervalMs`).
461+
462+
The polling methods are:
463+
464+
```ts
465+
client.beta.threads.createAndRunPoll(...)
466+
client.beta.threads.runs.createAndPoll((...)
467+
client.beta.threads.runs.submitToolOutputsAndPoll((...)
468+
client.beta.vectorStores.files.uploadAndPoll((...)
469+
client.beta.vectorStores.files.createAndPoll((...)
470+
client.beta.vectorStores.fileBatches.createAndPoll((...)
471+
client.beta.vectorStores.fileBatches.uploadAndPoll((...)
472+
```

src/lib/AssistantStream.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ImageFile,
88
TextDelta,
99
Messages,
10-
} from 'openai/resources/beta/threads/messages/messages';
10+
} from 'openai/resources/beta/threads/messages';
1111
import * as Core from 'openai/core';
1212
import { RequestOptions } from 'openai/core';
1313
import {
@@ -30,7 +30,7 @@ import {
3030
MessageStreamEvent,
3131
RunStepStreamEvent,
3232
RunStreamEvent,
33-
} from 'openai/resources/beta/assistants/assistants';
33+
} from 'openai/resources/beta/assistants';
3434
import { RunStep, RunStepDelta, ToolCall, ToolCallDelta } from 'openai/resources/beta/threads/runs/steps';
3535
import { ThreadCreateAndRunParamsBase, Threads } from 'openai/resources/beta/threads/threads';
3636
import MessageDelta = Messages.MessageDelta;

src/lib/Util.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Like `Promise.allSettled()` but throws an error if any promises are rejected.
3+
*/
4+
export const allSettledWithThrow = async <R>(promises: Promise<R>[]): Promise<R[]> => {
5+
const results = await Promise.allSettled(promises);
6+
const rejected = results.filter((result): result is PromiseRejectedResult => result.status === 'rejected');
7+
if (rejected.length) {
8+
for (const result of rejected) {
9+
console.error(result.reason);
10+
}
11+
12+
throw new Error(`${rejected.length} promise(s) failed - see the above errors`);
13+
}
14+
15+
// Note: TS was complaining about using `.filter().map()` here for some reason
16+
const values: R[] = [];
17+
for (const result of results) {
18+
if (result.status === 'fulfilled') {
19+
values.push(result.value);
20+
}
21+
}
22+
return values;
23+
};

0 commit comments

Comments
 (0)