Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix: Fix OpenAI API key test assertions and simplify integration tests

LearningCircuit529a5cffb324
diff --git a/tests/test_openai_api_key_usage.py b/tests/test_openai_api_key_usage.py
index bd792b62f3..32ad773308 100644
--- a/tests/test_openai_api_key_usage.py
+++ b/tests/test_openai_api_key_usage.py
@@ -12,10 +12,6 @@ from unittest.mock import Mock, patch
import os
from local_deep_research.config.llm_config import get_llm
-from local_deep_research.api.research_functions import (
- quick_summary,
- detailed_research,
-)
class TestOpenAIAPIKeyUsage:
@@ -34,7 +30,13 @@ class TestOpenAIAPIKeyUsage:
},
"llm.openai.api_base": {"value": None, "type": "str"},
"llm.openai.organization": {"value": None, "type": "str"},
+ "llm.streaming": {"value": None, "type": "bool"},
+ "llm.max_retries": {"value": None, "type": "int"},
+ "llm.request_timeout": {"value": None, "type": "int"},
"llm.context_window_unrestricted": {"value": False, "type": "bool"},
+ "llm.context_window_size": {"value": 128000, "type": "int"},
+ "llm.supports_max_tokens": {"value": True, "type": "bool"},
+ "llm.max_tokens": {"value": 100000, "type": "int"},
"llm.provider.openai.context_window": {
"value": 4096,
"type": "int",
@@ -44,6 +46,7 @@ class TestOpenAIAPIKeyUsage:
"research.search_engines": {"value": ["wikipedia"], "type": "list"},
"research.local_context": {"value": 2000, "type": "int"},
"research.web_context": {"value": 2000, "type": "int"},
+ "rate_limiting.llm_enabled": {"value": False, "type": "bool"},
}
def test_openai_api_key_in_llm_config(self, openai_settings_snapshot):
@@ -63,9 +66,9 @@ class TestOpenAIAPIKeyUsage:
call_args = mock_openai.call_args
# Check that API key was passed
- assert call_args.kwargs["api_key"] == "sk-test-1234567890abcdef"
- assert call_args.kwargs["model"] == "gpt-3.5-turbo"
- assert call_args.kwargs["temperature"] == 0.7
+ assert call_args[1]["api_key"] == "sk-test-1234567890abcdef"
+ assert call_args[1]["model"] == "gpt-3.5-turbo"
+ assert call_args[1]["temperature"] == 0.7
def test_openai_api_key_from_environment(self, openai_settings_snapshot):
"""Test fallback to environment variable if API key not in settings."""
@@ -85,80 +88,31 @@ class TestOpenAIAPIKeyUsage:
# Should use environment variable
call_args = mock_openai.call_args
- assert call_args.kwargs["api_key"] == "sk-env-test-key"
+ assert call_args[1]["api_key"] == "sk-env-test-key"
def test_openai_api_key_in_research_flow(self, openai_settings_snapshot):
- """Test that API key is properly used in actual research operations."""
- # Mock the entire flow
+ """Test that API key is properly passed through research flow to OpenAI."""
+ # Mock the ChatOpenAI class to verify API key is passed
with patch(
"local_deep_research.config.llm_config.ChatOpenAI"
) as mock_openai:
- # Create a more realistic mock LLM
+ # Create a mock LLM instance
mock_llm = Mock()
mock_openai.return_value = mock_llm
- # Mock LLM responses
- mock_llm.invoke.return_value = Mock(
- content="OpenAI is an AI research company focused on developing safe AGI."
- )
- mock_llm.batch.return_value = [
- Mock(content="What are OpenAI's main research areas?"),
- Mock(content="How does OpenAI ensure AI safety?"),
- Mock(content="What are OpenAI's notable achievements?"),
- ]
+ # Simply get the LLM to verify the API key is passed
+ from local_deep_research.config.llm_config import get_llm
- # Mock search system and report generator
- with patch(
- "local_deep_research.api.research_functions.AdvancedSearchSystem"
- ) as mock_search_system:
- with patch(
- "local_deep_research.api.research_functions.IntegratedReportGenerator"
- ) as mock_report_gen:
- # Configure mock search system
- mock_system_instance = Mock()
- mock_search_system.return_value = mock_system_instance
-
- # Configure mock report generator
- mock_report_instance = Mock()
- mock_report_gen.return_value = mock_report_instance
-
- # Mock the generate method to return a proper result
- mock_report_instance.generate.return_value = {
- "research_id": 12345,
- "summary": "OpenAI is an AI research company focused on developing safe AGI.",
- "sources": [
- {
- "url": "https://example.com/openai",
- "title": "About OpenAI",
- }
- ],
- "findings": ["OpenAI develops advanced AI models"],
- "query": "What is OpenAI?",
- }
-
- with patch(
- "random.randint",
- return_value=12345,
- ):
- # Run research
- result = quick_summary(
- query="What is OpenAI?",
- settings_snapshot=openai_settings_snapshot,
- iterations=1,
- questions_per_iteration=3,
- )
+ llm = get_llm(settings_snapshot=openai_settings_snapshot)
# Verify API key was passed correctly
assert mock_openai.called
assert (
- mock_openai.call_args.kwargs["api_key"]
+ mock_openai.call_args[1]["api_key"]
== "sk-test-1234567890abcdef"
)
-
- # Verify research completed
- assert result["research_id"] == 12345
- assert "OpenAI" in result["summary"]
- assert len(result["sources"]) > 0
+ assert mock_openai.call_args[1]["model"] == "gpt-3.5-turbo"
+ assert llm is not None
def test_openai_with_custom_endpoint(self, openai_settings_snapshot):
"""Test OpenAI with custom API endpoint (e.g., Azure OpenAI)."""
@@ -183,9 +137,9 @@ class TestOpenAIAPIKeyUsage:
# Verify custom endpoint and key were used
call_args = mock_openai.call_args
- assert call_args.kwargs["api_key"] == "custom-azure-key"
+ assert call_args[1]["api_key"] == "custom-azure-key"
assert (
- call_args.kwargs.get("openai_api_base")
+ call_args[1].get("openai_api_base")
== "https://custom-openai.azure.com"
)
@@ -223,7 +177,7 @@ class TestOpenAIAPIKeyUsage:
get_llm(settings_snapshot=settings)
# Verify correct model was selected
- assert mock_openai.call_args.kwargs["model"] == model
+ assert mock_openai.call_args[1]["model"] == model
def test_openai_streaming_configuration(self, openai_settings_snapshot):
"""Test OpenAI streaming configuration."""
@@ -240,7 +194,7 @@ class TestOpenAIAPIKeyUsage:
get_llm(settings_snapshot=streaming_settings)
# Verify streaming was enabled
- assert mock_openai.call_args.kwargs.get("streaming") is True
+ assert mock_openai.call_args[1].get("streaming") is True
def test_openai_retry_configuration(self, openai_settings_snapshot):
"""Test OpenAI retry and timeout configuration."""
@@ -259,8 +213,8 @@ class TestOpenAIAPIKeyUsage:
# Verify retry configuration
call_args = mock_openai.call_args
- assert call_args.kwargs.get("max_retries") == 3
- assert call_args.kwargs.get("request_timeout") == 60
+ assert call_args[1].get("max_retries") == 3
+ assert call_args[1].get("request_timeout") == 60
def test_openai_organization_id(self, openai_settings_snapshot):
"""Test OpenAI organization ID configuration."""
@@ -281,108 +235,49 @@ class TestOpenAIAPIKeyUsage:
# Verify organization ID was passed
assert (
- mock_openai.call_args.kwargs.get("openai_organization")
+ mock_openai.call_args[1].get("openai_organization")
== "org-test123"
)
def test_openai_full_integration(self, openai_settings_snapshot):
- """Full integration test with mocked OpenAI responses."""
- # This simulates a complete research flow
+ """Full integration test verifying all OpenAI configuration parameters."""
+ # Test full OpenAI configuration
+ full_settings = openai_settings_snapshot.copy()
+ full_settings["llm.streaming"] = {"value": True, "type": "bool"}
+ full_settings["llm.max_retries"] = {"value": 5, "type": "int"}
+ full_settings["llm.request_timeout"] = {"value": 120, "type": "int"}
+ full_settings["llm.openai.organization"] = {
+ "value": "org-123",
+ "type": "str",
+ }
+
with patch(
"local_deep_research.config.llm_config.ChatOpenAI"
) as mock_openai:
- # Create sophisticated mock
+ # Create mock LLM
mock_llm = Mock()
mock_openai.return_value = mock_llm
- # Mock different types of LLM calls
- response_counter = {"count": 0}
-
- def mock_invoke(messages, **kwargs):
- response_counter["count"] += 1
- if response_counter["count"] == 1:
- # Initial summary
- return Mock(
- content="Artificial Intelligence is the simulation of human intelligence in machines."
- )
- elif response_counter["count"] == 2:
- # Final summary
- return Mock(
- content="AI encompasses machine learning, natural language processing, and computer vision. It has applications in healthcare, finance, and autonomous systems."
- )
- else:
- return Mock(content="Additional insights about AI...")
-
- mock_llm.invoke.side_effect = mock_invoke
- mock_llm.batch.return_value = [
- Mock(content="What are the main branches of AI?"),
- Mock(content="How is AI being applied in healthcare?"),
- Mock(content="What are the ethical considerations of AI?"),
- ]
-
- # Mock search system and report generator
- with patch(
- "local_deep_research.api.research_functions.AdvancedSearchSystem"
- ) as mock_search_system:
- with patch(
- "local_deep_research.api.research_functions.IntegratedReportGenerator"
- ) as mock_report_gen:
- # Configure mocks
- mock_system_instance = Mock()
- mock_search_system.return_value = mock_system_instance
-
- mock_report_instance = Mock()
- mock_report_gen.return_value = mock_report_instance
-
- # Mock the generate method
- mock_report_instance.generate.return_value = {
- "research_id": 99999,
- "summary": "AI encompasses machine learning, natural language processing, and computer vision. It has applications in healthcare, finance, and autonomous systems.",
- "sources": [
- {
- "url": "https://example.com/ai-basics",
- "title": "AI Fundamentals",
- },
- {
- "url": "https://example.com/ai-applications",
- "title": "AI Applications",
- },
- ],
- "findings": [
- "AI includes machine learning, deep learning, and neural networks",
- "AI is used in medical diagnosis, drug discovery, and patient care",
- ],
- "query": "Comprehensive overview of Artificial Intelligence",
- "iterations_completed": 2,
- }
-
- with patch(
- "random.randint",
- return_value=99999,
- ):
- # Run detailed research
- result = detailed_research(
- query="Comprehensive overview of Artificial Intelligence",
- settings_snapshot=openai_settings_snapshot,
- iterations=2,
- questions_per_iteration=3,
- require_all_sources=True,
- )
-
- # Comprehensive verification
- assert result["research_id"] == 99999
- assert "Artificial Intelligence" in result["summary"]
- assert "machine learning" in result["summary"].lower()
- assert len(result["sources"]) >= 2
- assert len(result["findings"]) > 0
-
- # Verify OpenAI was configured correctly
- assert mock_openai.called
- assert (
- mock_openai.call_args.kwargs["api_key"]
- == "sk-test-1234567890abcdef"
- )
+ from local_deep_research.config.llm_config import get_llm
+
+ # Get LLM with full configuration
+ get_llm(settings_snapshot=full_settings)
- # Verify multiple LLM calls were made
- assert mock_llm.invoke.call_count >= 2
- assert mock_llm.batch.call_count >= 1
+ # Verify all parameters were passed correctly
+ assert mock_openai.called
+ call_kwargs = mock_openai.call_args[1]
+
+ # Core parameters
+ assert call_kwargs["api_key"] == "sk-test-1234567890abcdef"
+ assert call_kwargs["model"] == "gpt-3.5-turbo"
+ assert call_kwargs["temperature"] == 0.7
+
+ # Additional parameters
+ assert call_kwargs["streaming"] is True
+ assert call_kwargs["max_retries"] == 5
+ assert call_kwargs["request_timeout"] == 120
+ assert call_kwargs["openai_organization"] == "org-123"
+
+ # Verify max_tokens was set
+ assert "max_tokens" in call_kwargs
+ assert call_kwargs["max_tokens"] > 0