diff --git a/docs/core_docs/docs/modules/agents/agent_types/openai_assistant.mdx b/docs/core_docs/docs/modules/agents/agent_types/openai_assistant.mdx index 3521f8432c1e..da43949da6a3 100644 --- a/docs/core_docs/docs/modules/agents/agent_types/openai_assistant.mdx +++ b/docs/core_docs/docs/modules/agents/agent_types/openai_assistant.mdx @@ -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. @@ -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", }); diff --git a/langchain/src/experimental/openai_assistant/index.ts b/langchain/src/experimental/openai_assistant/index.ts index 60f95757f778..2e39836f30f3 100644 --- a/langchain/src/experimental/openai_assistant/index.ts +++ b/langchain/src/experimental/openai_assistant/index.ts @@ -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} + */ + 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} + */ + 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} + */ + public async modifyAssistant({ + model, + name, + instructions, + fileIds, + }: Omit, "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 { const { action: { runId, threadId }, diff --git a/langchain/src/experimental/openai_assistant/tests/openai_assistant.int.test.ts b/langchain/src/experimental/openai_assistant/tests/openai_assistant.int.test.ts index 8f206ceba79f..d1dac7daed6a 100644 --- a/langchain/src/experimental/openai_assistant/tests/openai_assistant.int.test.ts +++ b/langchain/src/experimental/openai_assistant/tests/openai_assistant.int.test.ts @@ -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({