|
| 1 | +# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== |
| 2 | +# Licensed under the Apache License, Version 2.0 (the “License”); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an “AS IS” BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== |
| 14 | +import asyncio |
| 15 | +from unittest import mock |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from oasis.inference import InferencerManager |
| 20 | +from oasis.social_platform import Channel |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.asyncio |
| 24 | +async def test_manager_run_with_mocked_response(): |
| 25 | + channel = Channel() |
| 26 | + |
| 27 | + # Setup the InferencerManager with the real channel |
| 28 | + manager = InferencerManager( |
| 29 | + channel=channel, |
| 30 | + model_type="llama-3", |
| 31 | + model_path="/path/to/model", |
| 32 | + stop_tokens=["\n"], |
| 33 | + server_url=[{ |
| 34 | + "host": "localhost", |
| 35 | + "ports": [8000] |
| 36 | + }], |
| 37 | + ) |
| 38 | + |
| 39 | + # Mocking the run method of model_backend to return a mocked response |
| 40 | + mock_response = mock.Mock() |
| 41 | + mock_response.choices = [ |
| 42 | + mock.Mock(message=mock.Mock(content="Mock Response")) |
| 43 | + ] |
| 44 | + |
| 45 | + # Mocking channel.send_to as well |
| 46 | + with mock.patch.object(manager.threads[0].model_backend, |
| 47 | + 'run', |
| 48 | + return_value=mock_response): |
| 49 | + |
| 50 | + openai_messages = [{ |
| 51 | + "role": "assistant", |
| 52 | + "content": 'mock_message', |
| 53 | + }] |
| 54 | + |
| 55 | + # Run the manager asynchronously |
| 56 | + task = asyncio.create_task(manager.run()) |
| 57 | + |
| 58 | + # Add a message to the receive_queue |
| 59 | + mes_id = await channel.write_to_receive_queue(openai_messages) |
| 60 | + mes_id, content = await channel.read_from_send_queue(mes_id) |
| 61 | + assert content == "Mock Response" |
| 62 | + |
| 63 | + await manager.stop() |
| 64 | + await task |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.asyncio |
| 68 | +async def test_multiple_threads(): |
| 69 | + # Create a Channel instance |
| 70 | + channel = Channel() |
| 71 | + |
| 72 | + # Set up multiple ports to simulate multiple threads |
| 73 | + server_url = [{ |
| 74 | + "host": "localhost", |
| 75 | + "ports": [8000, 8001, 8002] |
| 76 | + } # 3 ports |
| 77 | + ] |
| 78 | + |
| 79 | + # Initialize InferencerManager with multiple threads |
| 80 | + manager = InferencerManager( |
| 81 | + channel=channel, |
| 82 | + model_type="llama-3", |
| 83 | + model_path="/path/to/model", |
| 84 | + stop_tokens=["\n"], |
| 85 | + server_url=server_url, |
| 86 | + threads_per_port=2, # 2 threads per port |
| 87 | + ) |
| 88 | + |
| 89 | + # Mock the response for multiple threads |
| 90 | + mock_response = mock.Mock() |
| 91 | + mock_response.choices = [ |
| 92 | + mock.Mock(message=mock.Mock(content="Mock Response")) |
| 93 | + ] |
| 94 | + |
| 95 | + # Replace the model_backend.run method for all threads with the mock |
| 96 | + for thread in manager.threads: |
| 97 | + thread.model_backend.run = mock.Mock(return_value=mock_response) |
| 98 | + |
| 99 | + # Start the manager |
| 100 | + task = asyncio.create_task(manager.run()) |
| 101 | + |
| 102 | + # Send multiple messages to the queue |
| 103 | + openai_messages = [{ |
| 104 | + "role": "assistant", |
| 105 | + "content": f"mock_message_{i}" |
| 106 | + } for i in range(10)] |
| 107 | + |
| 108 | + # Write messages to the receive queue |
| 109 | + message_ids = [] |
| 110 | + for message in openai_messages: |
| 111 | + message_id = await channel.write_to_receive_queue([message]) |
| 112 | + message_ids.append(message_id) |
| 113 | + |
| 114 | + # Read results from the send queue |
| 115 | + results = [] |
| 116 | + for message_id in message_ids: |
| 117 | + _, content = await channel.read_from_send_queue(message_id) |
| 118 | + results.append(content) |
| 119 | + |
| 120 | + # Validate the results |
| 121 | + assert len(results) == 10 # Ensure all messages are processed |
| 122 | + assert all(content == "Mock Response" |
| 123 | + for content in results) # Ensure all responses are correct |
| 124 | + |
| 125 | + # Stop the manager |
| 126 | + await manager.stop() |
| 127 | + await task |
0 commit comments