|
| 1 | +""" |
| 2 | +Copyright 2026 Telefónica Innovación Digital, S.L. |
| 3 | +This file is part of Toolium. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +""" |
| 17 | + |
| 18 | +import json |
| 19 | +import logging |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from toolium.utils.ai_utils.ai_agent import create_react_agent, execute_agent |
| 24 | + |
| 25 | +# Global variable to keep track of mock responses in the agent |
| 26 | +mock_response_id = 0 |
| 27 | + |
| 28 | +logger = logging.getLogger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +def tv_recommendations(user_question): # noqa: ARG001 |
| 32 | + """ |
| 33 | + Tool to help users find TV content. |
| 34 | + Asks questions to the user to understand their preferences and then recommends specific content. |
| 35 | + Takes into account previous questions to make increasingly accurate recommendations. |
| 36 | +
|
| 37 | + :param user_question: The question from the user to the tool |
| 38 | + :returns: A response from the tool based on the user's question |
| 39 | + """ |
| 40 | + mocked_responses = [ |
| 41 | + 'Hola, ¿hoy te encuentras triste o feliz?', |
| 42 | + '¿Te gustaría que busque contenidos cómicos o de acción?', |
| 43 | + 'He encontrado estas series que pueden gustarte: "The Office", "Parks and Recreation" and "Brooklyn Nine-Nine"', |
| 44 | + ] |
| 45 | + |
| 46 | + # Return the next response in the list for each call, and loop back to the start after the last one |
| 47 | + global mock_response_id |
| 48 | + response = mocked_responses[mock_response_id] |
| 49 | + mock_response_id = mock_response_id + 1 if mock_response_id < len(mocked_responses) - 1 else 0 |
| 50 | + return response |
| 51 | + |
| 52 | + |
| 53 | +TV_CONTENT_SYSTEM_MESSAGE = ( |
| 54 | + 'You are a user looking for TV content. ' |
| 55 | + 'To do this, you will be helped by an assistant who will guide you with questions. ' |
| 56 | + "Answer the assistant's questions until it recommends specific content to you. " |
| 57 | + 'CRITICAL RULE: As soon as the TV assistant responds with concrete results, ' |
| 58 | + '(I found ..., Here you have ...), stop asking questions immediately, analyze the response ' |
| 59 | + "and return an analysis about the assistant's performance, to see if it answered correctly. " |
| 60 | + 'If after 5 questions, the assistant has not given any recommendation, do not continue asking ' |
| 61 | + 'and return the analysis. ' |
| 62 | + 'Respond in JSON format: ' |
| 63 | + '{"result": RESULT, "analysis": "your analysis"} ' |
| 64 | + 'where RESULT = true if it worked well and returned relevant content, false if not.' |
| 65 | +) |
| 66 | + |
| 67 | + |
| 68 | +# @pytest.mark.skip('This test relies on mocked responses and is meant for demonstration purposes') |
| 69 | +def test_react_agent(): |
| 70 | + agent = create_react_agent(TV_CONTENT_SYSTEM_MESSAGE, tool_method=tv_recommendations, model_name='gpt-4o-mini') |
| 71 | + agent_results = execute_agent(agent) |
| 72 | + |
| 73 | + # Check if the agent's final response contains a valid JSON with the expected structure and analyze the result |
| 74 | + try: |
| 75 | + ai_agent_response = json.loads(agent_results['messages'][-1].content) |
| 76 | + except (KeyError, IndexError, json.JSONDecodeError) as e: |
| 77 | + raise AssertionError('AI Agent did not return a valid response') from e |
| 78 | + error_message = f'TV recommendations use case did not return a valid response: {ai_agent_response["analysis"]}' |
| 79 | + assert ai_agent_response['result'] is True, error_message |
0 commit comments