|
| 1 | +from typing import Any |
| 2 | +from unittest.mock import AsyncMock, MagicMock |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from langchain_community.agents.openai_assistant import OpenAIAssistantV2Runnable |
| 7 | + |
| 8 | + |
| 9 | +def _create_mock_client(*args: Any, use_async: bool = False, **kwargs: Any) -> Any: |
| 10 | + client = AsyncMock() if use_async else MagicMock() |
| 11 | + client.beta.threads.runs.create = MagicMock(return_value=None) # type: ignore |
| 12 | + return client |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.requires("openai") |
| 16 | +def test_set_run_truncation_params() -> None: |
| 17 | + client = _create_mock_client() |
| 18 | + |
| 19 | + assistant = OpenAIAssistantV2Runnable(assistant_id="assistant_xyz", client=client) |
| 20 | + input = { |
| 21 | + "content": "AI question", |
| 22 | + "thread_id": "thread_xyz", |
| 23 | + "instructions": "You're a helpful assistant; answer questions as best you can.", |
| 24 | + "model": "gpt-4o", |
| 25 | + "max_prompt_tokens": 2000, |
| 26 | + "truncation_strategy": {"type": "last_messages", "last_messages": 10}, |
| 27 | + } |
| 28 | + expected_response = { |
| 29 | + "assistant_id": "assistant_xyz", |
| 30 | + "instructions": "You're a helpful assistant; answer questions as best you can.", |
| 31 | + "model": "gpt-4o", |
| 32 | + "max_prompt_tokens": 2000, |
| 33 | + "truncation_strategy": {"type": "last_messages", "last_messages": 10}, |
| 34 | + } |
| 35 | + |
| 36 | + assistant._create_run(input=input) |
| 37 | + _, kwargs = client.beta.threads.runs.create.call_args |
| 38 | + |
| 39 | + assert kwargs == expected_response |
0 commit comments