Skip to content

[Discussion] Visualization for the dspy codebase #8350

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .codeboarding/Data Management.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
```mermaid
graph LR
Dataset["Dataset"]
DataLoader["DataLoader"]
MATH["MATH"]
GSM8K["GSM8K"]
Colors["Colors"]
HotPotQA["HotPotQA"]
AlfWorld["AlfWorld"]
MATH -- "inherits from" --> Dataset
GSM8K -- "inherits from" --> Dataset
Colors -- "inherits from" --> Dataset
HotPotQA -- "inherits from" --> Dataset
AlfWorld -- "inherits from" --> Dataset
DataLoader -- "loads data into" --> Dataset
```

## Component Details

The Data Management component in DSPy provides a set of tools for loading, processing, and managing datasets used for training and evaluating DSPy programs. It offers a unified interface for accessing data from various sources, including local files and Hugging Face datasets. The core functionality revolves around the `Dataset` class, which serves as the base class for all datasets, and the `DataLoader` class, which handles the loading and conversion of data into `Dataset` objects. Specific dataset classes like `MATH`, `GSM8K`, `Colors`, `HotPotQA`, and `AlfWorld` provide standardized access to commonly used datasets, each tailored to its specific data format and evaluation metrics. This component streamlines the data preparation process, making it easier for users to work with different datasets in a consistent manner.

### Dataset
The `Dataset` class serves as the foundation for all datasets within DSPy. It provides core functionalities for data manipulation, including splitting the dataset into training, validation, and testing subsets, shuffling the data to ensure randomness, and preparing the data for consumption by DSPy modules. All specific dataset implementations inherit from this base class, ensuring a consistent interface for data access and manipulation.
- **Related Classes/Methods**: `dspy.datasets.dataset.Dataset`

### DataLoader
The `DataLoader` class is responsible for loading datasets from various sources, such as CSV files, JSON files, Pandas DataFrames, Hugging Face datasets, Parquet files, and RM datasets. It transforms the loaded data into a `Dataset` object, making it compatible with DSPy's data processing pipeline. The `DataLoader` abstracts away the complexities of data loading and formatting, providing a simple and unified interface for accessing data from different sources.
- **Related Classes/Methods**: `dspy.datasets.dataloader.DataLoader`

### MATH
The `MATH` dataset class provides a standardized way to access and utilize the MATH dataset within DSPy. It inherits from the base `Dataset` class and offers specific functionalities or configurations tailored to the MATH dataset's structure and requirements. This ensures that the MATH dataset can be seamlessly integrated into DSPy programs.
- **Related Classes/Methods**: `dspy.datasets.math.MATH`

### GSM8K
The `GSM8K` dataset class provides a standardized way to access and utilize the GSM8K dataset within DSPy. It inherits from the base `Dataset` class and includes a metric function (`gsm8k_metric`) for evaluating performance on this dataset. This ensures that the GSM8K dataset can be seamlessly integrated into DSPy programs and evaluated effectively.
- **Related Classes/Methods**: `dspy.datasets.gsm8k.GSM8K`, `dspy.datasets.gsm8k.gsm8k_metric`

### Colors
The `Colors` dataset class provides a standardized way to access and utilize the Colors dataset within DSPy. It inherits from the base `Dataset` class and offers specific functionalities or configurations tailored to the Colors dataset's structure and requirements. This ensures that the Colors dataset can be seamlessly integrated into DSPy programs.
- **Related Classes/Methods**: `dspy.datasets.colors.Colors`

### HotPotQA
The `HotPotQA` dataset class provides a standardized way to access and utilize the HotPotQA dataset within DSPy. It inherits from the base `Dataset` class and offers specific functionalities or configurations tailored to the HotPotQA dataset's structure and requirements. This ensures that the HotPotQA dataset can be seamlessly integrated into DSPy programs.
- **Related Classes/Methods**: `dspy.datasets.hotpotqa.HotPotQA`

### AlfWorld
The `AlfWorld` dataset class provides a standardized way to access and utilize the AlfWorld dataset within DSPy. It inherits from the base `Dataset` class and includes environment interaction functionalities, allowing DSPy programs to interact with the AlfWorld environment. This enables the development of DSPy programs that can reason and act within the AlfWorld environment.
- **Related Classes/Methods**: `dspy.datasets.alfworld.alfworld.AlfWorld`, `dspy.datasets.alfworld.alfworld.EnvPool`
36 changes: 36 additions & 0 deletions .codeboarding/Evaluation and Performance Monitoring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
```mermaid
graph LR
Evaluate["Evaluate"]
Metric_Functions_answer_exact_match_answer_passage_match_["Metric Functions (answer_exact_match, answer_passage_match)"]
SemanticF1["SemanticF1"]
CompleteAndGrounded["CompleteAndGrounded"]
display_dataframe["display_dataframe"]
Evaluate -- "orchestrates evaluation using" --> Metric_Functions_answer_exact_match_answer_passage_match_
Evaluate -- "orchestrates evaluation using" --> SemanticF1
Evaluate -- "orchestrates evaluation using" --> CompleteAndGrounded
Evaluate -- "presents results using" --> display_dataframe
```

## Component Details

The Evaluation and Performance Monitoring component in DSPy provides a comprehensive suite of tools for assessing the quality and effectiveness of DSPy programs. It encompasses functionalities for defining custom metrics, executing evaluations on datasets, and leveraging automated LM-based evaluation methods. The primary goal is to ensure program reliability and offer actionable insights for continuous improvement and optimization.

### Evaluate
The Evaluate class serves as the central orchestrator for the evaluation process. It takes a DSPy predictor (module), a dataset, and a set of evaluation metrics as input. It then iterates through the dataset, applies the predictor to each data point, calculates the specified metrics by comparing the predictor's output to the ground truth, and presents the results in a structured table format. It relies on various metric components and the display_dataframe function to present the results.
- **Related Classes/Methods**: `dspy.evaluate.evaluate.Evaluate:__call__`, `dspy.evaluate.evaluate.Evaluate:_construct_result_table`, `dspy.evaluate.evaluate.Evaluate:_display_result_table`

### Metric Functions (answer_exact_match, answer_passage_match)
These functions define fundamental metrics for evaluating answer quality. `answer_exact_match` checks if the predicted answer exactly matches the ground truth, while `answer_passage_match` determines if the predicted answer is present within a retrieved context passage. These metrics provide a basic level of assessment for answer correctness and relevance and are used by the Evaluate class.
- **Related Classes/Methods**: `dspy.evaluate.metrics:answer_exact_match`, `dspy.evaluate.metrics:answer_passage_match`, `dspy.evaluate.metrics:_answer_match`, `dspy.evaluate.metrics:_passage_match`

### SemanticF1
The SemanticF1 class implements an automated evaluation metric that leverages semantic similarity to assess answer quality. It compares the predicted answer with the ground truth using a semantic similarity model, calculating an F1 score to quantify the degree of semantic overlap. This metric offers a more nuanced evaluation compared to exact match, capturing semantic equivalence and is used by the Evaluate class.
- **Related Classes/Methods**: `dspy.evaluate.auto_evaluation.SemanticF1:__init__`, `dspy.evaluate.auto_evaluation.SemanticF1:forward`

### CompleteAndGrounded
The CompleteAndGrounded class implements an automated evaluation metric that assesses the completeness and groundedness of generated answers. It checks whether the answer covers all relevant aspects of the question and whether it is adequately supported by the retrieved context. This metric is particularly useful for evaluating the quality of answers generated by retrieval-augmented language models and is used by the Evaluate class.
- **Related Classes/Methods**: `dspy.evaluate.auto_evaluation.CompleteAndGrounded:__init__`, `dspy.evaluate.auto_evaluation.CompleteAndGrounded:forward`

### display_dataframe
The `display_dataframe` function is responsible for presenting the evaluation results in a user-friendly, tabular format using a Pandas DataFrame. It takes the calculated metrics and corresponding data points as input and generates a visually appealing table that facilitates easy interpretation and analysis of the evaluation results. It is used by the Evaluate class to present the results.
- **Related Classes/Methods**: `dspy.evaluate.evaluate:display_dataframe`
54 changes: 54 additions & 0 deletions .codeboarding/Knowledge Integration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
```mermaid
graph LR
Retrieve["Retrieve"]
AzureAISearchRM["AzureAISearchRM"]
MarqoRM["MarqoRM"]
MilvusRM["MilvusRM"]
MongoDBAtlasRM["MongoDBAtlasRM"]
Neo4jRM["Neo4jRM"]
ClarifaiRM["ClarifaiRM"]
PgVectorRM["PgVectorRM"]
Retrieve -- "uses" --> AzureAISearchRM
Retrieve -- "uses" --> MarqoRM
Retrieve -- "uses" --> MilvusRM
Retrieve -- "uses" --> MongoDBAtlasRM
Retrieve -- "uses" --> Neo4jRM
Retrieve -- "uses" --> ClarifaiRM
Retrieve -- "uses" --> PgVectorRM
```

## Component Details

The Knowledge Integration component in DSPy focuses on retrieving relevant information from external sources to enhance program capabilities. The core of this component is the `Retrieve` class, which acts as an orchestrator, taking a query and using various Retriever Modules (RMs) to fetch relevant passages. Each RM is responsible for interacting with a specific external knowledge source, such as vector databases or search engines. The retrieved passages are then returned to the program for further processing, enabling the integration of external knowledge into the program's reasoning and decision-making processes.

### Retrieve
The `Retrieve` class is an abstract component responsible for managing the retrieval process. It takes a query as input and uses one or more Retriever modules to fetch relevant passages. It orchestrates the retrieval process and returns the retrieved passages.
- **Related Classes/Methods**: `dspy.retrieve.retrieve.Retrieve`, `dspy.retrieve.retrieve.Retrieve:__call__`, `dspy.retrieve.retrieve.Retrieve:forward`

### AzureAISearchRM
The `AzureAISearchRM` class is a Retriever module that interacts with Azure AI Search to retrieve relevant passages. It takes a query as input, constructs a search request, and returns the retrieved passages from Azure AI Search.
- **Related Classes/Methods**: `dspy.retrieve.azureaisearch_rm.AzureAISearchRM`, `dspy.retrieve.azureaisearch_rm.AzureAISearchRM:__init__`, `dspy.retrieve.azureaisearch_rm.AzureAISearchRM:azure_search_request`, `dspy.retrieve.azureaisearch_rm.AzureAISearchRM:forward`

### MarqoRM
The `MarqoRM` class is a Retriever module that interacts with Marqo to retrieve relevant passages. It takes a query as input and returns the retrieved passages from Marqo.
- **Related Classes/Methods**: `dspy.retrieve.marqo_rm.MarqoRM`, `dspy.retrieve.marqo_rm.MarqoRM:__init__`, `dspy.retrieve.marqo_rm.MarqoRM:forward`

### MilvusRM
The `MilvusRM` class is a Retriever module that interacts with Milvus to retrieve relevant passages. It takes a query as input and returns the retrieved passages from Milvus.
- **Related Classes/Methods**: `dspy.retrieve.milvus_rm.MilvusRM`, `dspy.retrieve.milvus_rm.MilvusRM:forward`

### MongoDBAtlasRM
The `MongoDBAtlasRM` class is a Retriever module that interacts with MongoDB Atlas Vector Search to retrieve relevant passages. It takes a query as input and returns the retrieved passages from MongoDB Atlas.
- **Related Classes/Methods**: `dspy.retrieve.mongodb_atlas_rm.MongoDBAtlasRM`, `dspy.retrieve.mongodb_atlas_rm.MongoDBAtlasRM:__init__`, `dspy.retrieve.mongodb_atlas_rm.MongoDBAtlasRM:forward`

### Neo4jRM
The `Neo4jRM` class is a Retriever module that interacts with Neo4j to retrieve relevant passages. It takes a query as input and returns the retrieved passages from Neo4j.
- **Related Classes/Methods**: `dspy.retrieve.neo4j_rm.Neo4jRM`, `dspy.retrieve.neo4j_rm.Neo4jRM:__init__`, `dspy.retrieve.neo4j_rm.Neo4jRM:forward`

### ClarifaiRM
The `ClarifaiRM` class is a Retriever module that interacts with Clarifai to retrieve relevant passages. It takes a query as input and returns the retrieved passages from Clarifai.
- **Related Classes/Methods**: `dspy.retrieve.clarifai_rm.ClarifaiRM`, `dspy.retrieve.clarifai_rm.ClarifaiRM:__init__`, `dspy.retrieve.clarifai_rm.ClarifaiRM:forward`

### PgVectorRM
The `PgVectorRM` class is a Retriever module that interacts with PgVector to retrieve relevant passages. It takes a query as input and returns the retrieved passages from PgVector.
- **Related Classes/Methods**: `dspy.retrieve.pgvector_rm.PgVectorRM`, `dspy.retrieve.pgvector_rm.PgVectorRM:__init__`, `dspy.retrieve.pgvector_rm.PgVectorRM:forward`
57 changes: 57 additions & 0 deletions .codeboarding/LM Interaction Management.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
```mermaid
graph LR
Signature["Signature"]
Adapter["Adapter"]
BaseLM["BaseLM"]
LM["LM"]
Cache["Cache"]
OpenAIProvider["OpenAIProvider"]
ChatAdapter["ChatAdapter"]
JSONAdapter["JSONAdapter"]
Signature -- "defines structure for" --> LM_calls
Adapter -- "uses" --> Signature
LM -- "inherits from" --> BaseLM
LM -- "uses" --> Cache
LM -- "uses" --> OpenAIProvider
ChatAdapter -- "inherits from" --> Adapter
JSONAdapter -- "inherits from" --> Adapter
ChatAdapter -- "formats prompts and parses responses using" --> Signature
JSONAdapter -- "formats prompts and parses responses using" --> Signature
Adapter -- "calls" --> LM
```

## Component Details

The LM Interaction Management component in DSPy orchestrates communication with various Language Models (LMs). It defines signatures for LM calls using `Signature` and its related classes (`InputField`, `OutputField`), handles API requests through `BaseLM` and its subclasses (`LM`, `OpenAIProvider`, `ArborProvider`, `LocalProvider`), caches responses using `Cache`, and adapts input/output formats using `Adapter` and its subclasses (`ChatAdapter`, `JSONAdapter`, `TwoStepAdapter`). This system abstracts away the complexities of different LM providers, ensuring consistent and reliable LM behavior across diverse platforms.

### Signature
Defines the structure of LM calls, specifying input and output fields. It acts as a contract between DSPy modules and LMs, ensuring that data is formatted correctly for both input and output.
- **Related Classes/Methods**: `dspy.signatures.signature`, `dspy.signatures.field`

### Adapter
Abstract base class responsible for adapting between DSPy Signatures and LM APIs. It handles formatting prompts, parsing responses, and managing conversation history, bridging the gap between DSPy's structured data and the LM's API.
- **Related Classes/Methods**: `dspy.adapters.base`

### BaseLM
Abstract base class for language model clients. It provides a unified interface for calling different LMs, abstracting away the specific API details of each LM provider.
- **Related Classes/Methods**: `dspy.clients.base_lm`

### LM
Base class for language model clients, inheriting from BaseLM. It handles calling LMs, caching responses, and fine-tuning, providing a concrete implementation for interacting with LMs.
- **Related Classes/Methods**: `dspy.clients.lm`

### Cache
Caches LM responses to improve efficiency and reduce API costs. It stores and retrieves LM responses based on the input prompt, avoiding redundant API calls.
- **Related Classes/Methods**: `dspy.clients.cache`

### OpenAIProvider
Client for interacting with the OpenAI API. It implements the BaseLM interface and provides specific methods for calling OpenAI's LMs.
- **Related Classes/Methods**: `dspy.clients.openai`

### ChatAdapter
Adapter specialized for chat-based LM APIs. It formats prompts as chat messages and parses responses accordingly, tailoring the interaction for chat-style LMs.
- **Related Classes/Methods**: `dspy.adapters.chat_adapter`

### JSONAdapter
Adapter that formats prompts and parses responses as JSON objects. It's useful for LMs that work well with structured JSON data.
- **Related Classes/Methods**: `dspy.adapters.json_adapter`
37 changes: 37 additions & 0 deletions .codeboarding/Module Composition and Execution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
```mermaid
graph LR
BaseModule["BaseModule"]
Module["Module"]
ModuleGraph["ModuleGraph"]
Parameter["Parameter"]
Predictor["Predictor"]
BaseModule -- "is base class of" --> Module
ModuleGraph -- "manages" --> Module
BaseModule -- "is base class of" --> ModuleGraph
BaseModule -- "manages" --> Parameter
Predictor -- "is a type of" --> Module
```

## Component Details

This graph represents the module composition and execution functionality in DSPy. It allows users to define reusable components (Modules) that encapsulate LM calls and other operations. These modules can be composed into complex data processing pipelines, managed by the ModuleGraph. The BaseModule provides the foundation for all modules, handling parameters and state. The flow involves defining Modules, composing them within a ModuleGraph, and then executing the graph to perform the desired data processing tasks.

### BaseModule
The BaseModule class serves as the foundation for all modules within DSPy. It offers core functionalities for managing parameters, submodules, and saving/loading module state. It provides a consistent interface for all module types, enabling modular and reusable component design.
- **Related Classes/Methods**: `dspy.primitives.module.BaseModule`

### Module
The Module class, a subclass of BaseModule, represents a program or a collection of predictors. It provides methods for managing predictors, setting/getting the language model, and defining the forward pass. It is the primary way to define a reusable program in DSPy, encapsulating the logic and parameters for a specific task.
- **Related Classes/Methods**: `dspy.primitives.module.Module`

### ModuleGraph
The ModuleGraph class is designed for managing and organizing modules in a graph structure. It allows adding modules, processing submodules, and defining the connections between them. This enables the creation of complex, modular programs by connecting individual modules and managing the data flow between them.
- **Related Classes/Methods**: `dspy.experimental.module_graph.ModuleGraph`

### Parameter
The Parameter class represents a configurable parameter within a module. It allows users to define and manage the parameters that control the behavior of a module. Parameters can be set, retrieved, and updated, enabling dynamic configuration of modules.
- **Related Classes/Methods**: `dspy.primitives.module.Parameter`

### Predictor
The Predictor class is a specialized type of Module that is designed to make predictions. It typically encapsulates a language model call and provides methods for formatting the input and output. Predictors are used as building blocks for more complex programs.
- **Related Classes/Methods**: `dspy.primitives.module.Predictor`
Loading