1 line
10 KiB
JSON
1 line
10 KiB
JSON
{"repo": "aliraza556/llm-openai-compatibility", "files": {"src/examples/multi_provider_comparison.py": {"14d756d2e84ddaca882eeabb2cff122c566b9392": {"t": 1785745460.9200928, "n": 4736, "h": "b3edbea89742e825", "c": "import asyncio\r\nimport os\r\nfrom typing import Dict, List, Optional\r\nimport json\r\nfrom tabulate import tabulate\r\nfrom agents import function_tool\r\nfrom llm_compatibility import run_with_multiple_providers\r\n\r\n@function_tool\r\ndef get_weather(city: str, country: Optional[str] = None) -> str:\r\n \"\"\"Get the current weather for a city.\"\"\"\r\n location = f\"{city}, {country}\" if country else city\r\n return f\"The weather in {location} is currently sunny and 75°F with 30% humidity.\"\r\n\r\n@function_tool\r\ndef search_web(query: str) -> str:\r\n \"\"\"Search the web for information (simulated).\"\"\"\r\n return f\"Here are the search results for '{query}':\\n\" + \\\r\n f\"1. Wikipedia: Information about {query}\\n\" + \\\r\n f\"2. News article: Recent developments related to {query}\\n\" + \\\r\n f\"3. Academic paper: Research on {query}\"\r\n\r\n@function_tool\r\ndef calculate(expression: str) -> str:\r\n \"\"\"Evaluate a mathematical expression.\"\"\"\r\n try:\r\n result = eval(expression)\r\n return f\"The result of {expression} is {result}\"\r\n except Exception as e:\r\n return f\"Error evaluating expression: {str(e)}\"\r\n\r\nSYSTEM_PROMPT = \"\"\"You are a helpful assistant that can answer questions using tools when needed.\r\nWhen answering questions, use the provided tools if they would help with the answer.\r\nBe concise and informative in your responses.\"\"\"\r\n\r\nEXAMPLE_QUERIES = [\r\n \"What's the weather like in Tokyo, Japan?\",\r\n \"Can you calculate 137 * 429?\",\r\n \"Search for information about quantum computing\",\r\n \"What's 15% of 230?\",\r\n \"Tell me about the weather in Paris and Rome\"\r\n]\r\n\r\nPROVIDERS = [\"openai\", \"claude\", \"gemini\", \"deepseek\"]\r\nMODEL_NAMES = {\r\n \"openai\": \"gpt-4o-2024-08-06\",\r\n \"claude\": \"claude-3-haiku-20240307\",\r\n \"gemini\": \"gemini-pro\",\r\n \"deepseek\": \"deepseek-chat\"\r\n}\r\n\r\nAPI_KEYS = {\r\n \"openai\": os.environ.get(\"OPENAI_API_KEY\", \"\"),\r\n \"claude\": os.environ.get(\"ANTHROPIC_API_KEY\", \"\"),\r\n \"gemini\": os.environ.get(\"GEMINI_API_KEY\", \"\"),\r\n \"deepseek\": os.environ.get(\"DEEPSEEK_API_KEY\", \"\")\r\n}\r\n\r\nasync def run_comparison():\r\n \"\"\"Run the comparison with multiple providers and queries.\"\"\"\r\n\r\n all_tools = [get_weather, search_web, calculate]\r\n \r\n available_providers = [p for p in PROVIDERS if API_KEYS.get(p)]\r\n \r\n if not available_providers:\r\n print(\"No API keys found. Please set at least one API key in the environment variables.\")\r\n return\r\n \r\n print(f\"Using providers: {', '.join(available_providers)}\")\r\n print(f\"Available tools: get_weather, search_web, calculate\\n\")\r\n \r\n all_results: Dict[str, Dict[str, str]] = {}\r\n \r\n for query in EXAMPLE_QUERIES:\r\n print(f\"Query: {query}\")\r\n print(\"-\" * 50)\r\n \r\n messages = [{\"role\": \"user\", \"content\": query}]\r\n \r\n results = await run_with_multiple_providers(\r\n system_prompt=SYSTEM_PROMPT,\r\n messages=messages,\r\n providers=available_providers,\r\n model_names={p: MODEL_NAMES[p] for p in available_providers},\r\n api_keys={p: API_KEYS[p] for p in available_providers},\r\n tools=all_tools,\r\n temperature=0.7,\r\n )\r\n \r\n all_results[query] = results\r\n \r\n for provider, response in results.items():\r\n print(f\"\\n--- {provider.upper()} ---\")\r\n print(response)\r\n \r\n print(\"\\n\" + \"=\" * 70 + \"\\n\")\r\n \r\n print_comparison_table(all_results)\r\n \r\n with open(\"provider_comparison_results.json\", \"w\") as f:\r\n json.dump(all_results, f, indent=2)\r\n print(\"Results saved to provider_comparison_results.json\")\r\n\r\ndef print_comparison_table(results: Dict[str, Dict[str, str]]):\r\n \"\"\"Print a comparison table of the results.\"\"\"\r\n\r\n first_query = next(iter(results.values()))\r\n headers = [\"Query\"] + [p.upper() for p in first_query.keys()]\r\n \r\n rows = []\r\n for query, provider_results in results.items():\r\n short_query = query[:30] + \"...\" if len(query) > 30 else query\r\n\r\n short_responses = [short_query]\r\n for provider, response in provider_results.items():\r\n short_response = response[:50] + \"...\" if len(response) > 50 else response\r\n short_responses.append(short_response)\r\n \r\n rows.append(short_responses)\r\n \r\n print(\"\\nComparison of Provider Responses (truncated):\")\r\n print(tabulate(rows, headers=headers, tablefmt=\"grid\"))\r\n\r\nif __name__ == \"__main__\":\r\n\r\n try:\r\n import tabulate\r\n except ImportError:\r\n print(\"This example requires the tabulate package. Install it with: pip install tabulate\")\r\n exit(1)\r\n \r\n asyncio.run(run_comparison()) "}, "8da992b567bb58d6798aac89072265fd143663af": {"t": 1785753508.3608701, "n": 4736, "h": "b3edbea89742e825", "c": "import asyncio\r\nimport os\r\nfrom typing import Dict, List, Optional\r\nimport json\r\nfrom tabulate import tabulate\r\nfrom agents import function_tool\r\nfrom llm_compatibility import run_with_multiple_providers\r\n\r\n@function_tool\r\ndef get_weather(city: str, country: Optional[str] = None) -> str:\r\n \"\"\"Get the current weather for a city.\"\"\"\r\n location = f\"{city}, {country}\" if country else city\r\n return f\"The weather in {location} is currently sunny and 75°F with 30% humidity.\"\r\n\r\n@function_tool\r\ndef search_web(query: str) -> str:\r\n \"\"\"Search the web for information (simulated).\"\"\"\r\n return f\"Here are the search results for '{query}':\\n\" + \\\r\n f\"1. Wikipedia: Information about {query}\\n\" + \\\r\n f\"2. News article: Recent developments related to {query}\\n\" + \\\r\n f\"3. Academic paper: Research on {query}\"\r\n\r\n@function_tool\r\ndef calculate(expression: str) -> str:\r\n \"\"\"Evaluate a mathematical expression.\"\"\"\r\n try:\r\n result = eval(expression)\r\n return f\"The result of {expression} is {result}\"\r\n except Exception as e:\r\n return f\"Error evaluating expression: {str(e)}\"\r\n\r\nSYSTEM_PROMPT = \"\"\"You are a helpful assistant that can answer questions using tools when needed.\r\nWhen answering questions, use the provided tools if they would help with the answer.\r\nBe concise and informative in your responses.\"\"\"\r\n\r\nEXAMPLE_QUERIES = [\r\n \"What's the weather like in Tokyo, Japan?\",\r\n \"Can you calculate 137 * 429?\",\r\n \"Search for information about quantum computing\",\r\n \"What's 15% of 230?\",\r\n \"Tell me about the weather in Paris and Rome\"\r\n]\r\n\r\nPROVIDERS = [\"openai\", \"claude\", \"gemini\", \"deepseek\"]\r\nMODEL_NAMES = {\r\n \"openai\": \"gpt-4o-2024-08-06\",\r\n \"claude\": \"claude-3-haiku-20240307\",\r\n \"gemini\": \"gemini-pro\",\r\n \"deepseek\": \"deepseek-chat\"\r\n}\r\n\r\nAPI_KEYS = {\r\n \"openai\": os.environ.get(\"OPENAI_API_KEY\", \"\"),\r\n \"claude\": os.environ.get(\"ANTHROPIC_API_KEY\", \"\"),\r\n \"gemini\": os.environ.get(\"GEMINI_API_KEY\", \"\"),\r\n \"deepseek\": os.environ.get(\"DEEPSEEK_API_KEY\", \"\")\r\n}\r\n\r\nasync def run_comparison():\r\n \"\"\"Run the comparison with multiple providers and queries.\"\"\"\r\n\r\n all_tools = [get_weather, search_web, calculate]\r\n \r\n available_providers = [p for p in PROVIDERS if API_KEYS.get(p)]\r\n \r\n if not available_providers:\r\n print(\"No API keys found. Please set at least one API key in the environment variables.\")\r\n return\r\n \r\n print(f\"Using providers: {', '.join(available_providers)}\")\r\n print(f\"Available tools: get_weather, search_web, calculate\\n\")\r\n \r\n all_results: Dict[str, Dict[str, str]] = {}\r\n \r\n for query in EXAMPLE_QUERIES:\r\n print(f\"Query: {query}\")\r\n print(\"-\" * 50)\r\n \r\n messages = [{\"role\": \"user\", \"content\": query}]\r\n \r\n results = await run_with_multiple_providers(\r\n system_prompt=SYSTEM_PROMPT,\r\n messages=messages,\r\n providers=available_providers,\r\n model_names={p: MODEL_NAMES[p] for p in available_providers},\r\n api_keys={p: API_KEYS[p] for p in available_providers},\r\n tools=all_tools,\r\n temperature=0.7,\r\n )\r\n \r\n all_results[query] = results\r\n \r\n for provider, response in results.items():\r\n print(f\"\\n--- {provider.upper()} ---\")\r\n print(response)\r\n \r\n print(\"\\n\" + \"=\" * 70 + \"\\n\")\r\n \r\n print_comparison_table(all_results)\r\n \r\n with open(\"provider_comparison_results.json\", \"w\") as f:\r\n json.dump(all_results, f, indent=2)\r\n print(\"Results saved to provider_comparison_results.json\")\r\n\r\ndef print_comparison_table(results: Dict[str, Dict[str, str]]):\r\n \"\"\"Print a comparison table of the results.\"\"\"\r\n\r\n first_query = next(iter(results.values()))\r\n headers = [\"Query\"] + [p.upper() for p in first_query.keys()]\r\n \r\n rows = []\r\n for query, provider_results in results.items():\r\n short_query = query[:30] + \"...\" if len(query) > 30 else query\r\n\r\n short_responses = [short_query]\r\n for provider, response in provider_results.items():\r\n short_response = response[:50] + \"...\" if len(response) > 50 else response\r\n short_responses.append(short_response)\r\n \r\n rows.append(short_responses)\r\n \r\n print(\"\\nComparison of Provider Responses (truncated):\")\r\n print(tabulate(rows, headers=headers, tablefmt=\"grid\"))\r\n\r\nif __name__ == \"__main__\":\r\n\r\n try:\r\n import tabulate\r\n except ImportError:\r\n print(\"This example requires the tabulate package. Install it with: pip install tabulate\")\r\n exit(1)\r\n \r\n asyncio.run(run_comparison()) "}}}} |