From 5f0102242aa232deee4b06284b68668c62ad361c Mon Sep 17 00:00:00 2001 From: Mathias Marciano <102000515+mathias-marciano@users.noreply.github.com> Date: Tue, 4 Mar 2025 18:34:11 +0100 Subject: [PATCH] Fixed an issue with the OpenAI Assistant's 'retrieval' tool and adding support for the 'attachments' parameter (#30006) PR Title: langchain: add attachments support in OpenAIAssistantRunnable PR Description: This PR fixes an issue with the "retrieval" tool (internally named "file_search") in the OpenAI Assistant by adding support for the "attachments" parameter in the invoke method. This change allows files to be linked to messages when they are inserted into threads, which is essential for utilizing OpenAI's Retrieval Augmented Generation (RAG) feature. Issue: N/A Dependencies: None Twitter handle: N/A --------- Co-authored-by: Chester Curme --- libs/langchain/langchain/agents/openai_assistant/base.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libs/langchain/langchain/agents/openai_assistant/base.py b/libs/langchain/langchain/agents/openai_assistant/base.py index 2e2a2668f87..1a11052dd95 100644 --- a/libs/langchain/langchain/agents/openai_assistant/base.py +++ b/libs/langchain/langchain/agents/openai_assistant/base.py @@ -114,7 +114,7 @@ def _is_assistants_builtin_tool( tool: Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool], ) -> bool: """Determine if tool corresponds to OpenAI Assistants built-in.""" - assistants_builtin_tools = ("code_interpreter", "retrieval") + assistants_builtin_tools = ("code_interpreter", "file_search") return ( isinstance(tool, dict) and ("type" in tool) @@ -128,7 +128,7 @@ def _get_assistants_tool( """Convert a raw function/class to an OpenAI tool. Note that OpenAI assistants supports several built-in tools, - such as "code_interpreter" and "retrieval." + such as "code_interpreter" and "file_search". """ if _is_assistants_builtin_tool(tool): return tool # type: ignore @@ -300,6 +300,8 @@ class OpenAIAssistantRunnable(RunnableSerializable[Dict, OutputType]): max_completion_tokens: Allow setting max_completion_tokens for this run. max_prompt_tokens: Allow setting max_prompt_tokens for this run. run_metadata: Metadata to associate with new run. + attachments: A list of files attached to the message, and the + tools they should be added to. config: Runnable config. Defaults to None. Return: @@ -333,6 +335,7 @@ class OpenAIAssistantRunnable(RunnableSerializable[Dict, OutputType]): "role": "user", "content": input["content"], "metadata": input.get("message_metadata"), + "attachments": input.get("attachments"), } ], "metadata": input.get("thread_metadata"),