Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenAI Files for OpenAI assistant #3228

Merged
merged 16 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api_refs/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
"./langchain/src/util/time.ts",
"./langchain/src/experimental/autogpt/index.ts",
"./langchain/src/experimental/openai_assistant/index.ts",
"./langchain/src/experimental/openai_files/index.ts",
"./langchain/src/experimental/babyagi/index.ts",
"./langchain/src/experimental/generative_agents/index.ts",
"./langchain/src/experimental/plan_and_execute/index.ts",
Expand Down
166 changes: 166 additions & 0 deletions docs/core_docs/docs/modules/agents/agent_types/openai_file.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# OpenAI File

:::info
The [OpenAI File is used in OpenAI Assistant API](https://platform.openai.com/docs/assistants/how-it-works/creating-assistants) is still in beta.
[API reference](https://platform.openai.com/docs/api-reference/files/object)
:::

Files are used to upload documents that can be used with features like Assistants and Fine-tuning.

We've implemented the File API in LangChain with create and delete.

The `File` object represents a document that has been uploaded to OpenAI.

```
{
"id": "file-abc123",
"object": "file",
"bytes": 120000,
"created_at": 1677610602,
"filename": "salesOverview.pdf",
"purpose": "assistants",
}
```

## Create a File

Upload a file that can be used across various endpoints. The size of all the files uploaded by one organization can be up to **100 GB**.

The size of individual files can be a maximum of **512 MB**. See the Assistants Tools guide to learn more about the types of files supported. The Fine-tuning API only supports `.jsonl` files.

```typescript
import { OpenAIFiles } from "langchain/experimental/openai_files";

const openAIFiles = new OpenAIFiles();
const file = await openAIFiles.create({
file: fs.createReadStream(path.resolve(__dirname, `./test.txt`)),
purpose: "assistants",
});
/**
* Output
{
"id": "file-BK7bzQj3FfZFXr7DbL6xJwfo",
"object": "file",
"bytes": 120000,
"created_at": 1677610602,
"filename": "salesOverview.pdf",
"purpose": "assistants",
}
*/
```

## Use File in AI Assistant

```typescript
import { OpenAIAssistantRunnable } from "langchain/experimental/openai_assistant";
import { OpenAIFiles } from "langchain/experimental/openai_files";

const openAIFiles = new OpenAIFiles();
const file = await openAIFiles.createFile({
file: fs.createReadStream(path.resolve(__dirname, `./test.txt`)),
purpose: "assistants",
});

const agent = await OpenAIAssistantRunnable.createAssistant({
model: "gpt-3.5-turbo-1106",
instructions:
"You are a weather bot. Use the provided functions to answer questions.",
name: "Weather Assistant",
tools,
asAgent: true,
file_ids: [file.id],
});
```

## Delete a File

Delete a file.

```typescript
import { OpenAIFiles } from "langchain/experimental/openai_files";

const openAIFiles = new OpenAIFiles();
const result = await openAIFiles.deleteFile({ fileId: file.id });
/**
* Output:
{
"id": "file-abc123",
"object": "file",
"deleted": true
}
*/
```

## List all Files

Returns a list of files that belong to the user's organization.

`purpose`?: string
Only return files with the given purpose.

```typescript
import { OpenAIFiles } from "langchain/experimental/openai_files";

const openAIFiles = new OpenAIFiles();
const result = await openAIFiles.listFiles({ purpose: "assistants" });
/**
* Output:
{
"data": [
{
"id": "file-abc123",
"object": "file",
"bytes": 175,
"created_at": 1613677385,
"filename": "salesOverview.pdf",
"purpose": "assistants",
},
{
"id": "file-abc123",
"object": "file",
"bytes": 140,
"created_at": 1613779121,
"filename": "puppy.jsonl",
"purpose": "fine-tune",
}
],
"object": "list"
}
*/
```

## Retrieve File

Returns information about a specific file.

```typescript
import { OpenAIFiles } from "langchain/experimental/openai_files";

const openAIFiles = new OpenAIFiles();
const result = await openAIFiles.retrieveFile({ fileId: file.id });
/**
* Output:
{
"id": "file-abc123",
"object": "file",
"bytes": 120000,
"created_at": 1677610602,
"filename": "mydata.jsonl",
"purpose": "fine-tune",
}
*/
```

## Retrieve File Content

Returns the contents of the specified file.

You can't retrieve the contents of a file that was uploaded with the "purpose": "assistants" API.

```typescript
import { OpenAIFiles } from "langchain/experimental/openai_files";

const openAIFiles = new OpenAIFiles();
const result = await openAIFiles.retrieveFileContent({ fileId: file.id });
// Return the file content.
```
1 change: 1 addition & 0 deletions environment_tests/test-exports-bun/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export * from "langchain/util/math";
export * from "langchain/util/time";
export * from "langchain/experimental/autogpt";
export * from "langchain/experimental/openai_assistant";
export * from "langchain/experimental/openai_files";
export * from "langchain/experimental/babyagi";
export * from "langchain/experimental/generative_agents";
export * from "langchain/experimental/plan_and_execute";
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-cf/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export * from "langchain/util/math";
export * from "langchain/util/time";
export * from "langchain/experimental/autogpt";
export * from "langchain/experimental/openai_assistant";
export * from "langchain/experimental/openai_files";
export * from "langchain/experimental/babyagi";
export * from "langchain/experimental/generative_agents";
export * from "langchain/experimental/plan_and_execute";
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-cjs/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const util_math = require("langchain/util/math");
const util_time = require("langchain/util/time");
const experimental_autogpt = require("langchain/experimental/autogpt");
const experimental_openai_assistant = require("langchain/experimental/openai_assistant");
const experimental_openai_files = require("langchain/experimental/openai_files");
const experimental_babyagi = require("langchain/experimental/babyagi");
const experimental_generative_agents = require("langchain/experimental/generative_agents");
const experimental_plan_and_execute = require("langchain/experimental/plan_and_execute");
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-esbuild/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import * as util_math from "langchain/util/math";
import * as util_time from "langchain/util/time";
import * as experimental_autogpt from "langchain/experimental/autogpt";
import * as experimental_openai_assistant from "langchain/experimental/openai_assistant";
import * as experimental_openai_files from "langchain/experimental/openai_files";
import * as experimental_babyagi from "langchain/experimental/babyagi";
import * as experimental_generative_agents from "langchain/experimental/generative_agents";
import * as experimental_plan_and_execute from "langchain/experimental/plan_and_execute";
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-esm/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import * as util_math from "langchain/util/math";
import * as util_time from "langchain/util/time";
import * as experimental_autogpt from "langchain/experimental/autogpt";
import * as experimental_openai_assistant from "langchain/experimental/openai_assistant";
import * as experimental_openai_files from "langchain/experimental/openai_files";
import * as experimental_babyagi from "langchain/experimental/babyagi";
import * as experimental_generative_agents from "langchain/experimental/generative_agents";
import * as experimental_plan_and_execute from "langchain/experimental/plan_and_execute";
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-vercel/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export * from "langchain/util/math";
export * from "langchain/util/time";
export * from "langchain/experimental/autogpt";
export * from "langchain/experimental/openai_assistant";
export * from "langchain/experimental/openai_files";
export * from "langchain/experimental/babyagi";
export * from "langchain/experimental/generative_agents";
export * from "langchain/experimental/plan_and_execute";
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-vite/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export * from "langchain/util/math";
export * from "langchain/util/time";
export * from "langchain/experimental/autogpt";
export * from "langchain/experimental/openai_assistant";
export * from "langchain/experimental/openai_files";
export * from "langchain/experimental/babyagi";
export * from "langchain/experimental/generative_agents";
export * from "langchain/experimental/plan_and_execute";
Expand Down
3 changes: 3 additions & 0 deletions langchain/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,9 @@ experimental/autogpt.d.ts
experimental/openai_assistant.cjs
experimental/openai_assistant.js
experimental/openai_assistant.d.ts
experimental/openai_files.cjs
experimental/openai_files.js
experimental/openai_files.d.ts
experimental/babyagi.cjs
experimental/babyagi.js
experimental/babyagi.d.ts
Expand Down
8 changes: 8 additions & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,9 @@
"experimental/openai_assistant.cjs",
"experimental/openai_assistant.js",
"experimental/openai_assistant.d.ts",
"experimental/openai_files.cjs",
"experimental/openai_files.js",
"experimental/openai_files.d.ts",
"experimental/babyagi.cjs",
"experimental/babyagi.js",
"experimental/babyagi.d.ts",
Expand Down Expand Up @@ -2668,6 +2671,11 @@
"import": "./experimental/openai_assistant.js",
"require": "./experimental/openai_assistant.cjs"
},
"./experimental/openai_files": {
"types": "./experimental/openai_files.d.ts",
"import": "./experimental/openai_files.js",
"require": "./experimental/openai_files.cjs"
},
"./experimental/babyagi": {
"types": "./experimental/babyagi.d.ts",
"import": "./experimental/babyagi.js",
Expand Down
1 change: 1 addition & 0 deletions langchain/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ const entrypoints = {
// experimental
"experimental/autogpt": "experimental/autogpt/index",
"experimental/openai_assistant": "experimental/openai_assistant/index",
"experimental/openai_files": "experimental/openai_files/index",
"experimental/babyagi": "experimental/babyagi/index",
"experimental/generative_agents": "experimental/generative_agents/index",
"experimental/plan_and_execute": "experimental/plan_and_execute/index",
Expand Down
3 changes: 3 additions & 0 deletions langchain/src/experimental/openai_assistant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ export class OpenAIAssistantRunnable<
clientOptions,
asAgent,
pollIntervalMs,
fileIds,
}: Omit<OpenAIAssistantRunnableInput<AsAgent>, "assistantId"> & {
model: string;
name?: string;
instructions?: string;
tools?: OpenAIToolType | Array<StructuredTool>;
fileIds?: string[];
}) {
const formattedTools =
tools?.map((tool) => {
Expand All @@ -80,6 +82,7 @@ export class OpenAIAssistantRunnable<
instructions,
tools: formattedTools,
model,
file_ids: fileIds,
});

return new this({
Expand Down
Loading