From d9f64e52e49e2b30d20adcce87c0deb1f4e2a7af Mon Sep 17 00:00:00 2001 From: Akileo Date: Sun, 25 May 2025 15:16:30 +0900 Subject: [PATCH] Refactor LangChain AI Handler for Async and Performance - Add async LLM object creation and invocation - Branch parameter handling by LLM type - Enhance error logging and handler consistency - Add performance and concurrency test code --- tests/unittest/test_langchain.py | 89 ++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/unittest/test_langchain.py diff --git a/tests/unittest/test_langchain.py b/tests/unittest/test_langchain.py new file mode 100644 index 00000000..7e9f33d7 --- /dev/null +++ b/tests/unittest/test_langchain.py @@ -0,0 +1,89 @@ +import asyncio +import os +import time +from pr_agent.algo.ai_handlers.langchain_ai_handler import LangChainOpenAIHandler +from pr_agent.config_loader import get_settings + +def check_settings(): + print('Checking settings...') + settings = get_settings() + + # Check OpenAI settings + if not hasattr(settings, 'openai'): + print('OpenAI settings not found') + return False + + if not hasattr(settings.openai, 'key'): + print('OpenAI API key not found') + return False + + print('OpenAI API key found') + return True + +async def measure_performance(handler, num_requests=3): + print(f'\nRunning performance test with {num_requests} requests...') + start_time = time.time() + + # Create multiple requests + tasks = [ + handler.chat_completion( + model='gpt-3.5-turbo', + system='You are a helpful assistant', + user=f'Test message {i}', + temperature=0.2 + ) for i in range(num_requests) + ] + + # Execute requests concurrently + responses = await asyncio.gather(*tasks) + + end_time = time.time() + total_time = end_time - start_time + avg_time = total_time / num_requests + + print(f'Performance results:') + print(f'Total time: {total_time:.2f} seconds') + print(f'Average time per request: {avg_time:.2f} seconds') + print(f'Requests per second: {num_requests/total_time:.2f}') + + return responses + +async def test(): + print('Starting test...') + + # Check settings first + if not check_settings(): + print('Please set up your environment variables or configuration file') + print('Required: OPENAI_API_KEY') + return + + try: + handler = LangChainOpenAIHandler() + print('Handler created') + + # Basic functionality test + response = await handler.chat_completion( + model='gpt-3.5-turbo', + system='You are a helpful assistant', + user='Hello', + temperature=0.2, + img_path='test.jpg' + ) + print('Response:', response) + + # Performance test + await measure_performance(handler) + + except Exception as e: + print('Error:', str(e)) + print('Error type:', type(e)) + print('Error details:', e.__dict__ if hasattr(e, '__dict__') else 'No additional details') + +if __name__ == '__main__': + # Print environment variables (without showing the actual key) + print('Environment variables:') + print('OPENAI_API_KEY:', 'Set' if os.getenv('OPENAI_API_KEY') else 'Not set') + print('OPENAI_API_TYPE:', os.getenv('OPENAI_API_TYPE', 'Not set')) + print('OPENAI_API_BASE:', os.getenv('OPENAI_API_BASE', 'Not set')) + + asyncio.run(test()) \ No newline at end of file