From 36bbdc776eae57c8d3a6363633c120a866bf7d02 Mon Sep 17 00:00:00 2001 From: "Luiz F. G. dos Santos" Date: Mon, 2 Sep 2024 13:21:51 -0500 Subject: [PATCH] community: fix bug to support for `file_search` tool from OpenAI (#25927) - **Description:** The function `_is_assistants_builtin_tool` didn't had support for `file_search` from OpenAI. This was creating conflict and blocking the usage of such. OpenAI Assistant changed from`retrieval` to `file_search`. The following code ``` agent = OpenAIAssistantV2Runnable.create_assistant( name="Data Analysis Assistant", instructions=prompt[0].content, tools={'type': 'file_search'}, model=self.chat_config.connection.deployment_name, client=llm, as_agent=True, tool_resources={ "file_search": { "vector_store_ids": vector_store_id } } ) ``` Was throwing the following error ``` Traceback (most recent call last): File "/Users/l.guedesdossantos/Documents/codes/shellai-nlp-backend/app/chat/chat_decorators.py", line 500, in get_response return await super().get_response(post, context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/l.guedesdossantos/Documents/codes/shellai-nlp-backend/app/chat/chat_decorators.py", line 96, in get_response response = await self.inner_chat.get_response(post, context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/l.guedesdossantos/Documents/codes/shellai-nlp-backend/app/chat/chat_decorators.py", line 96, in get_response response = await self.inner_chat.get_response(post, context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/l.guedesdossantos/Documents/codes/shellai-nlp-backend/app/chat/chat_decorators.py", line 96, in get_response response = await self.inner_chat.get_response(post, context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [Previous line repeated 4 more times] File "/Users/l.guedesdossantos/Documents/codes/shellai-nlp-backend/app/chat/azure_open_ai_chat.py", line 147, in get_response chain = chain_factory.get_chain(prompts, post.conversation.id, overrides, context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/l.guedesdossantos/Documents/codes/shellai-nlp-backend/app/llm_connections/chains.py", line 1324, in get_chain agent = OpenAIAssistantV2Runnable.create_assistant( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/l.guedesdossantos/anaconda3/envs/shell-e/lib/python3.11/site-packages/langchain_community/agents/openai_assistant/base.py", line 256, in create_assistant tools=[_get_assistants_tool(tool) for tool in tools], # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/l.guedesdossantos/anaconda3/envs/shell-e/lib/python3.11/site-packages/langchain_community/agents/openai_assistant/base.py", line 256, in tools=[_get_assistants_tool(tool) for tool in tools], # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/l.guedesdossantos/anaconda3/envs/shell-e/lib/python3.11/site-packages/langchain_community/agents/openai_assistant/base.py", line 119, in _get_assistants_tool return convert_to_openai_tool(tool) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/l.guedesdossantos/anaconda3/envs/shell-e/lib/python3.11/site-packages/langchain_core/utils/function_calling.py", line 255, in convert_to_openai_tool function = convert_to_openai_function(tool) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/l.guedesdossantos/anaconda3/envs/shell-e/lib/python3.11/site-packages/langchain_core/utils/function_calling.py", line 230, in convert_to_openai_function raise ValueError( ValueError: Unsupported function {'type': 'file_search'} Functions must be passed in as Dict, pydantic.BaseModel, or Callable. If they're a dict they must either be in OpenAI function format or valid JSON schema with top-level 'title' and 'description' keys. ``` With the proposed changes, this is fixed and the function will have support for `file_search`. This was the only place missing the support for `file_search`. Reference doc https://platform.openai.com/docs/assistants/tools/file-search - **Twitter handle:** luizf0992 --------- Co-authored-by: Harrison Chase --- .../langchain_community/agents/openai_assistant/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/community/langchain_community/agents/openai_assistant/base.py b/libs/community/langchain_community/agents/openai_assistant/base.py index 6fb86486ca7..07bd8566099 100644 --- a/libs/community/langchain_community/agents/openai_assistant/base.py +++ b/libs/community/langchain_community/agents/openai_assistant/base.py @@ -91,7 +91,7 @@ def _is_assistants_builtin_tool( A boolean response of true or false indicating if the tool corresponds to OpenAI Assistants built-in. """ - assistants_builtin_tools = ("code_interpreter", "retrieval") + assistants_builtin_tools = ("code_interpreter", "retrieval", "file_search") return ( isinstance(tool, dict) and ("type" in tool)