Skip to content

Commit

Permalink
✨ Add modify and retrieve openAI assistant (langchain-ai#3387)
Browse files Browse the repository at this point in the history
  • Loading branch information
castroCrea authored Nov 28, 2023
1 parent 2c519d7 commit f6d0614
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,49 @@ console.log(assistantResponse);

Here the assistant was able to utilize the `code_interpreter` tool to calculate the answer to our question.

## Retrieves an assistant

Retrieves an assistant.

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

const assistant = new OpenAIAssistantRunnable({
assistantId,
});
const assistantResponse = await assistant.getAssistant();
```

## Modifies an assistant

Modifies an assistant.

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

const assistant = await OpenAIAssistantRunnable.createAssistant({
name: "Personal Assistant",
model: "gpt-4-1106-preview",
});
const assistantModified = await assistant.modifyAssistant({
name: "Personal Assistant 2",
});
```

## Delete an assistant

Delete an assistant.

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

const assistant = await OpenAIAssistantRunnable.createAssistant({
name: "Personal Assistant",
model: "gpt-4-1106-preview",
});
const deleteStatus = await assistant.deleteAssistant();
```

# OpenAI Files

Files are used to upload documents that can be used with features like Assistants and Fine-tuning.
Expand Down Expand Up @@ -224,7 +267,7 @@ The size of individual files can be a maximum of **512 MB**. See the Assistants
import { OpenAIFiles } from "langchain/experimental/openai_files";

const openAIFiles = new OpenAIFiles();
const file = await openAIFiles.create({
const file = await openAIFiles.createFile({
file: fs.createReadStream(path.resolve(__dirname, `./test.txt`)),
purpose: "assistants",
});
Expand Down
45 changes: 45 additions & 0 deletions langchain/src/experimental/openai_assistant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,51 @@ export class OpenAIAssistantRunnable<
return this._getResponse(run.id, run.thread_id);
}

/**
* Delete an assistant.
*
* @link {https://platform.openai.com/docs/api-reference/assistants/deleteAssistant}
* @returns {Promise<AssistantDeleted>}
*/
public async deleteAssistant() {
return await this.client.beta.assistants.del(this.assistantId);
}

/**
* Retrieves an assistant.
*
* @link {https://platform.openai.com/docs/api-reference/assistants/getAssistant}
* @returns {Promise<OpenAIClient.Beta.Assistants.Assistant>}
*/
public async getAssistant() {
return await this.client.beta.assistants.retrieve(this.assistantId);
}

/**
* Modifies an assistant.
*
* @link {https://platform.openai.com/docs/api-reference/assistants/modifyAssistant}
* @returns {Promise<OpenAIClient.Beta.Assistants.Assistant>}
*/
public async modifyAssistant<AsAgent extends boolean>({
model,
name,
instructions,
fileIds,
}: Omit<OpenAIAssistantRunnableInput<AsAgent>, "assistantId" | "tools"> & {
model?: string;
name?: string;
instructions?: string;
fileIds?: string[];
}) {
return await this.client.beta.assistants.update(this.assistantId, {
name,
instructions,
model,
file_ids: fileIds,
});
}

private async _parseStepsInput(input: RunInput): Promise<RunInput> {
const {
action: { runId, threadId },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,41 @@ test.skip("New OpenAIAssistantRunnable can be passed as an agent", async () => {
*/
});

test("OpenAIAssistantRunnable create and delete assistant", async () => {
const assistant = await OpenAIAssistantRunnable.createAssistant({
name: "Personal Assistant",
model: "gpt-4-1106-preview",
});
const deleteStatus = await assistant.deleteAssistant();
expect(deleteStatus).toEqual({
id: assistant.assistantId,
object: "assistant.deleted",
deleted: true,
});
console.log(deleteStatus);
/**
{
id: 'asst_jwkJPzFkIL2ei9Kn1SZzmR6Y',
object: 'assistant.deleted',
deleted: true
}
*/
});

test("OpenAIAssistantRunnable create and modify assistant", async () => {
const assistant = await OpenAIAssistantRunnable.createAssistant({
name: "Personal Assistant",
model: "gpt-4-1106-preview",
});
const assistantResponse = await assistant.getAssistant();
expect(assistantResponse.name).toEqual("Personal Assistant");
const assistantResponseModified = await assistant.modifyAssistant({
name: "Personal Assistant 2",
});
expect(assistantResponseModified.name).toEqual("Personal Assistant 2");
expect(assistantResponseModified.model).toEqual("gpt-4-1106-preview");
});

test("OpenAIAssistantRunnable can be passed as an agent", async () => {
const tools = [new WeatherTool(), new HumanReadableChecker()];
const agent = new OpenAIAssistantRunnable({
Expand Down

0 comments on commit f6d0614

Please sign in to comment.