-
Notifications
You must be signed in to change notification settings - Fork 71
Add Graph and DataSetGenerator integration tests #524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| """Integration tests for DataSetGenerator with real API calls.""" | ||
|
|
||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
| from deepfabric import DataSetGenerator, Graph | ||
|
|
||
| from .conftest import requires_gemini, requires_openai | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def openai_generator(openai_config): | ||
| """Create a DataSetGenerator configured for OpenAI.""" | ||
| return DataSetGenerator( | ||
| provider=openai_config["provider"], | ||
| model_name=openai_config["model_name"], | ||
| generation_system_prompt="You are a helpful assistant that generates training data.", | ||
| temperature=openai_config["temperature"], | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def gemini_generator(gemini_config): | ||
| """Create a DataSetGenerator configured for Gemini.""" | ||
| return DataSetGenerator( | ||
| provider=gemini_config["provider"], | ||
| model_name=gemini_config["model_name"], | ||
| generation_system_prompt="You are a helpful assistant that generates training data.", | ||
| temperature=gemini_config["temperature"], | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def small_topic_graph(openai_config): | ||
| """Create a small topic graph for generator tests.""" | ||
| graph = Graph( | ||
| topic_prompt="Python Programming", | ||
| provider=openai_config["provider"], | ||
| model_name=openai_config["model_name"], | ||
| temperature=openai_config["temperature"], | ||
| degree=2, | ||
| depth=1, | ||
| ) | ||
|
|
||
| async def run_build(): | ||
| return [event async for event in graph.build_async()] | ||
|
|
||
| asyncio.run(run_build()) | ||
| return graph | ||
|
|
||
|
|
||
| class TestDataSetGeneratorOpenAI: | ||
| """Integration tests for DataSetGenerator with OpenAI provider.""" | ||
|
|
||
| @requires_openai | ||
| @pytest.mark.openai | ||
| def test_basic_generation(self, openai_generator): | ||
| """Test basic dataset generation without topic model.""" | ||
| result = openai_generator.create_data( | ||
| num_steps=1, | ||
| batch_size=2, | ||
| ) | ||
|
|
||
| # Result should be a HuggingFace Dataset | ||
| assert result is not None | ||
| assert len(result) >= 1 | ||
|
|
||
| @requires_openai | ||
| @pytest.mark.openai | ||
| def test_generation_with_topic_model(self, openai_generator, small_topic_graph): | ||
| """Test dataset generation with a topic graph.""" | ||
| result = openai_generator.create_data( | ||
| num_steps=1, | ||
| batch_size=2, | ||
| topic_model=small_topic_graph, | ||
| ) | ||
|
|
||
| assert result is not None | ||
| assert len(result) >= 1 | ||
|
|
||
| @requires_openai | ||
| @pytest.mark.openai | ||
| def test_async_generation(self, openai_generator): | ||
| """Test async dataset generation.""" | ||
|
|
||
| async def run_async(): | ||
| return await openai_generator.create_data_async( | ||
| num_steps=1, | ||
| batch_size=2, | ||
| ) | ||
|
|
||
| result = asyncio.run(run_async()) | ||
|
|
||
| assert result is not None | ||
| assert len(result) >= 1 | ||
|
|
||
| @requires_openai | ||
| @pytest.mark.openai | ||
| def test_generation_with_chain_of_thought(self, openai_config): | ||
| """Test generation with chain_of_thought conversation type.""" | ||
| generator = DataSetGenerator( | ||
| provider=openai_config["provider"], | ||
| model_name=openai_config["model_name"], | ||
| generation_system_prompt="You are a reasoning assistant.", | ||
| temperature=openai_config["temperature"], | ||
| conversation_type="chain_of_thought", | ||
| reasoning_style="freetext", | ||
| ) | ||
|
|
||
| result = generator.create_data( | ||
| num_steps=1, | ||
| batch_size=1, | ||
| ) | ||
|
|
||
| assert result is not None | ||
| assert len(result) >= 1 | ||
|
|
||
|
|
||
| class TestDataSetGeneratorGemini: | ||
| """Integration tests for DataSetGenerator with Gemini provider.""" | ||
|
|
||
| @requires_gemini | ||
| @pytest.mark.gemini | ||
| def test_basic_generation(self, gemini_generator): | ||
| """Test basic dataset generation with Gemini.""" | ||
|
|
||
| async def run_async(): | ||
| return await gemini_generator.create_data_async( | ||
| num_steps=1, | ||
| batch_size=2, | ||
| ) | ||
|
|
||
| result = asyncio.run(run_async()) | ||
|
|
||
| assert result is not None | ||
| assert len(result) >= 1 | ||
|
|
||
| @requires_gemini | ||
| @pytest.mark.gemini | ||
| def test_generation_saves_to_file(self, tmp_path, gemini_generator): | ||
| """Test that generated data can be saved.""" | ||
|
|
||
| async def run_async(): | ||
| return await gemini_generator.create_data_async( | ||
| num_steps=1, | ||
| batch_size=2, | ||
| ) | ||
|
|
||
| asyncio.run(run_async()) | ||
|
|
||
| # Save dataset | ||
| out_path = tmp_path / "dataset.jsonl" | ||
| gemini_generator.save_dataset(str(out_path)) | ||
|
|
||
| # Verify file was created | ||
| assert out_path.exists() | ||
| lines = out_path.read_text().strip().split("\n") | ||
| assert len(lines) >= 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| """Integration tests for Graph with real API calls.""" | ||
|
|
||
| import asyncio | ||
| import json | ||
|
|
||
| import pytest # pyright: ignore[reportMissingImports] | ||
|
|
||
| from deepfabric import Graph | ||
|
|
||
| from .conftest import requires_gemini, requires_openai | ||
|
|
||
|
|
||
| class TestGraphOpenAI: | ||
| """Integration tests for Graph with OpenAI provider.""" | ||
|
|
||
| @requires_openai | ||
| @pytest.mark.openai | ||
| def test_graph_builds_basic(self, openai_config): | ||
| """Test basic graph building with OpenAI.""" | ||
| degree = 2 | ||
| depth = 1 | ||
| topic = "Machine Learning" | ||
|
|
||
| graph = Graph( | ||
| topic_prompt=topic, | ||
| provider=openai_config["provider"], | ||
| model_name=openai_config["model_name"], | ||
| temperature=openai_config["temperature"], | ||
| degree=degree, | ||
| depth=depth, | ||
| ) | ||
|
|
||
| async def run_build(): | ||
| return [event async for event in graph.build_async()] | ||
|
|
||
| events = asyncio.run(run_build()) | ||
|
|
||
| # Verify build completion | ||
| completes = [e for e in events if e.get("event") == "build_complete"] | ||
| assert len(completes) == 1 | ||
|
|
||
| # Verify paths were built | ||
| paths = graph.get_all_paths() | ||
| assert len(paths) >= 1 | ||
| assert all(p[0] == topic for p in paths) | ||
|
|
||
| @requires_openai | ||
| @pytest.mark.openai | ||
| def test_graph_save_and_load_roundtrip(self, tmp_path, openai_config): | ||
| """Test saving and loading a graph with OpenAI.""" | ||
| degree = 2 | ||
| depth = 1 | ||
| topic = "Data Science" | ||
|
|
||
| graph = Graph( | ||
| topic_prompt=topic, | ||
| provider=openai_config["provider"], | ||
| model_name=openai_config["model_name"], | ||
| temperature=openai_config["temperature"], | ||
| degree=degree, | ||
| depth=depth, | ||
| ) | ||
|
|
||
| async def run_build(): | ||
| return [event async for event in graph.build_async()] | ||
|
|
||
| asyncio.run(run_build()) | ||
lukehinds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Save to file | ||
| out_path = tmp_path / "graph.json" | ||
| graph.save(str(out_path)) | ||
|
|
||
| # Verify file structure | ||
| data = json.loads(out_path.read_text()) | ||
| assert "nodes" in data | ||
| assert "root_id" in data | ||
| assert len(data["nodes"]) >= 1 | ||
|
|
||
| # Load into new graph and verify | ||
| graph_params = { | ||
| "topic_prompt": "placeholder", | ||
lukehinds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "provider": openai_config["provider"], | ||
| "model_name": openai_config["model_name"], | ||
| "degree": degree, | ||
| "depth": depth, | ||
| } | ||
| new_graph = Graph.from_json(str(out_path), graph_params) | ||
|
|
||
| assert new_graph.get_all_paths() == graph.get_all_paths() | ||
|
|
||
|
|
||
| class TestGraphGemini: | ||
| """Integration tests for Graph with Gemini provider.""" | ||
|
|
||
| @requires_gemini | ||
| @pytest.mark.gemini | ||
| def test_graph_builds_basic(self, gemini_config): | ||
| """Test basic graph building with Gemini.""" | ||
| degree = 2 | ||
| depth = 1 | ||
| topic = "Cloud Computing" | ||
|
|
||
| graph = Graph( | ||
| topic_prompt=topic, | ||
| provider=gemini_config["provider"], | ||
| model_name=gemini_config["model_name"], | ||
| temperature=gemini_config["temperature"], | ||
| degree=degree, | ||
| depth=depth, | ||
| ) | ||
|
|
||
| async def run_build(): | ||
| return [event async for event in graph.build_async()] | ||
|
|
||
| events = asyncio.run(run_build()) | ||
|
|
||
| # Verify build completion | ||
| completes = [e for e in events if e.get("event") == "build_complete"] | ||
| assert len(completes) == 1 | ||
|
|
||
| # Verify paths were built | ||
| paths = graph.get_all_paths() | ||
| assert len(paths) >= 1 | ||
| assert all(p[0] == topic for p in paths) | ||
|
|
||
| @requires_gemini | ||
| @pytest.mark.gemini | ||
| def test_graph_no_cycles(self, gemini_config): | ||
| """Test that generated graphs have no cycles.""" | ||
| graph = Graph( | ||
| topic_prompt="Software Engineering", | ||
| provider=gemini_config["provider"], | ||
| model_name=gemini_config["model_name"], | ||
| temperature=gemini_config["temperature"], | ||
| degree=2, | ||
| depth=2, | ||
| ) | ||
|
|
||
| async def run_build(): | ||
| return [event async for event in graph.build_async()] | ||
|
|
||
| asyncio.run(run_build()) | ||
lukehinds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Verify no cycles | ||
| assert not graph.has_cycle() | ||
| # Verify we got paths | ||
| paths = graph.get_all_paths() | ||
| assert len(paths) >= 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.