Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community: add truncation params when an openai assistant's run is created #28158

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions libs/community/langchain_community/agents/openai_assistant/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions libs/community/tests/unit_tests/agents/test_openai_assistant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import Any
from unittest.mock import AsyncMock, MagicMock

import pytest

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


@pytest.mark.requires("openai")
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'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'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},
}

assistant._create_run(input=input)
_, kwargs = client.beta.threads.runs.create.call_args

assert kwargs == expected_response
Loading