From b689c2bc9ca585a7f44638cbd1d882e913000fc2 Mon Sep 17 00:00:00 2001 From: lucho_code Date: Fri, 15 Nov 2024 17:35:18 -0500 Subject: [PATCH 1/5] pass additional params in run's input --- .../agents/openai_assistant/base.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/libs/community/langchain_community/agents/openai_assistant/base.py b/libs/community/langchain_community/agents/openai_assistant/base.py index 971e8ba8381ed..f9006f66ca5ab 100644 --- a/libs/community/langchain_community/agents/openai_assistant/base.py +++ b/libs/community/langchain_community/agents/openai_assistant/base.py @@ -543,11 +543,16 @@ def _create_run(self, input: dict) -> Any: Returns: Any: The created run object. """ - params = { - k: v - for k, v in input.items() - if k in ("instructions", "model", "tools", "tool_resources", "run_metadata") - } + allowed_assistant_params = ( + "instructions", + "model", + "tools", + "tool_resources", + "run_metadata", + "truncation_strategy", + "max_prompt_tokens", + ) + params = {k: v for k, v in input.items() if k in allowed_assistant_params} return self.client.beta.threads.runs.create( input["thread_id"], assistant_id=self.assistant_id, From 8efb4e5239a69246cf3847737c5b4708d28aa368 Mon Sep 17 00:00:00 2001 From: lucho_code Date: Sat, 16 Nov 2024 16:46:37 -0500 Subject: [PATCH 2/5] feat: create unit tests --- .../agents/test_openai_assistant.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 libs/community/tests/unit_tests/agents/test_openai_assistant.py diff --git a/libs/community/tests/unit_tests/agents/test_openai_assistant.py b/libs/community/tests/unit_tests/agents/test_openai_assistant.py new file mode 100644 index 0000000000000..55f00bfde7a36 --- /dev/null +++ b/libs/community/tests/unit_tests/agents/test_openai_assistant.py @@ -0,0 +1,36 @@ +from typing import Any +from unittest.mock import AsyncMock, MagicMock + +from langchain_community.agents.openai_assistant import OpenAIAssistantV2Runnable + + +def _create_mock_client(*args: Any, use_async: bool = False, **kwargs: Any) -> Any: + client = AsyncMock() if use_async else MagicMock() + client.beta.threads.runs.create = MagicMock(return_value=None) # type: ignore + return client + + +def test_set_run_truncation_params() -> None: + client = _create_mock_client() + + assistant = OpenAIAssistantV2Runnable(assistant_id="assistant_xyz", client=client) + input = { + "content": "AI question", + "thread_id": "thread_xyz", + "instructions": "You are a helpful assistant. Answer questions as better as you can", + "model": "gpt-4o", + "max_prompt_tokens": 2000, + "truncation_strategy": {"type": "last_messages", "last_messages": 10}, + } + expected_response = { + "assistant_id": "assistant_xyz", + "instructions": "You are a helpful assistant. Answer questions as better as you can", + "model": "gpt-4o", + "max_prompt_tokens": 2000, + "truncation_strategy": {"type": "last_messages", "last_messages": 10}, + } + + assistant._create_run(input=input) + _, kwargs = client.beta.threads.runs.create.call_args + + assert kwargs == expected_response From 777b96d148bde001726f5a92a6705826bc0d172b Mon Sep 17 00:00:00 2001 From: lucho_code Date: Sat, 16 Nov 2024 17:33:59 -0500 Subject: [PATCH 3/5] refactor: improve assistant instructions --- .../tests/unit_tests/agents/test_openai_assistant.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/community/tests/unit_tests/agents/test_openai_assistant.py b/libs/community/tests/unit_tests/agents/test_openai_assistant.py index 55f00bfde7a36..c49977658b79e 100644 --- a/libs/community/tests/unit_tests/agents/test_openai_assistant.py +++ b/libs/community/tests/unit_tests/agents/test_openai_assistant.py @@ -17,14 +17,14 @@ def test_set_run_truncation_params() -> None: input = { "content": "AI question", "thread_id": "thread_xyz", - "instructions": "You are a helpful assistant. Answer questions as better as you can", + "instructions": "You're a helpful assistant; answer questions as best you can.", "model": "gpt-4o", "max_prompt_tokens": 2000, "truncation_strategy": {"type": "last_messages", "last_messages": 10}, } expected_response = { "assistant_id": "assistant_xyz", - "instructions": "You are a helpful assistant. Answer questions as better as you can", + "instructions": "You're a helpful assistant; answer questions as best you can.", "model": "gpt-4o", "max_prompt_tokens": 2000, "truncation_strategy": {"type": "last_messages", "last_messages": 10}, From 315857cba42d25684ef5f66917916ea8e46013ea Mon Sep 17 00:00:00 2001 From: lucho_code Date: Sat, 16 Nov 2024 17:53:49 -0500 Subject: [PATCH 4/5] tests: set openai module as required --- .../tests/unit_tests/agents/test_openai_assistant.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libs/community/tests/unit_tests/agents/test_openai_assistant.py b/libs/community/tests/unit_tests/agents/test_openai_assistant.py index c49977658b79e..929595ef305b3 100644 --- a/libs/community/tests/unit_tests/agents/test_openai_assistant.py +++ b/libs/community/tests/unit_tests/agents/test_openai_assistant.py @@ -1,6 +1,8 @@ from typing import Any from unittest.mock import AsyncMock, MagicMock +import pytest + from langchain_community.agents.openai_assistant import OpenAIAssistantV2Runnable @@ -9,7 +11,7 @@ def _create_mock_client(*args: Any, use_async: bool = False, **kwargs: Any) -> A client.beta.threads.runs.create = MagicMock(return_value=None) # type: ignore return client - +@pytest.mark.requires("openai") def test_set_run_truncation_params() -> None: client = _create_mock_client() From 4a6d242c1b006abbce8fb10738c18476a41c0be6 Mon Sep 17 00:00:00 2001 From: lucho_code Date: Sat, 16 Nov 2024 18:01:36 -0500 Subject: [PATCH 5/5] fix make lint --- libs/community/tests/unit_tests/agents/test_openai_assistant.py | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/community/tests/unit_tests/agents/test_openai_assistant.py b/libs/community/tests/unit_tests/agents/test_openai_assistant.py index 929595ef305b3..ea99ab5ccd753 100644 --- a/libs/community/tests/unit_tests/agents/test_openai_assistant.py +++ b/libs/community/tests/unit_tests/agents/test_openai_assistant.py @@ -11,6 +11,7 @@ def _create_mock_client(*args: Any, use_async: bool = False, **kwargs: Any) -> A client.beta.threads.runs.create = MagicMock(return_value=None) # type: ignore return client + @pytest.mark.requires("openai") def test_set_run_truncation_params() -> None: client = _create_mock_client()