DSPy: The framework for programming—not prompting—language models
Python
adding api references and docs files for functional
Herumb Shandilya39f5ba3729ef
diff --git a/docs/api/functional/_category_.json b/docs/api/functional/_category_.jsonnew file mode 100644index 000000000..70bd70104--- /dev/null+++ b/docs/api/functional/_category_.json@@ -0,0 +1,8 @@+{+ "label": "Functional",+ "position": 2,+ "link": {+ "type": "generated-index",+ "description": "This documentation provides an overview of the Typed Predictors."+ }+}\ No newline at end of filediff --git a/docs/api/functional/dspy_TypedCoT.md b/docs/api/functional/dspy_TypedCoT.mdnew file mode 100644index 000000000..e728bdad0--- /dev/null+++ b/docs/api/functional/dspy_TypedCoT.md@@ -0,0 +1,41 @@+---+sidebar_position: 2+---++# dspy.TypedChainOfThought++### Overview++#### `def TypedChainOfThought(signature, max_retries=3) -> dspy.Module`++Adds a Chain of Thoughts `dspy.OutputField` to the `dspy.TypedPredictor` module by prepending it to the Signature. Similar to `dspy.TypedPredictor` but automatically adds a "reasoning" output field.++* **Inputs**:+ * `signature`: The `dspy.Signature` specifying the input/output fields+ * `max_retries`: Maximum number of retries if outputs fail validation+* **Output**: A dspy.Module instance capable of making predictions.++### Example++```python+from dspy import InputField, OutputField, Signature+from dspy.functional import TypedChainOfThought+from pydantic import BaseModel++# We define a pydantic type that automatically checks if it's argument is valid python code.+class CodeOutput(BaseModel):+ code: str+ api_reference: str++class CodeSignature(Signature):+ function_description: str = InputField()+ solution: CodeOutput = OutputField()++cot_predictor = TypedChainOfThought(CodeSignature)+prediction = cot_predictor(+ function_description="Write a function that adds two numbers."+)++print(prediction["code"])+print(prediction["api_reference"])+```\ No newline at end of filediff --git a/docs/api/functional/dspy_TypedPredictor.md b/docs/api/functional/dspy_TypedPredictor.mdnew file mode 100644index 000000000..8e5fd5857--- /dev/null+++ b/docs/api/functional/dspy_TypedPredictor.md@@ -0,0 +1,78 @@+---+sidebar_position: 1+---++# dspy.TypedPredictor++The `TypedPredictor` class is a sophisticated module designed for making predictions with strict type validations. It leverages a signature to enforce type constraints on inputs and outputs, ensuring that the data follows to the expected schema.++### Constructor++```python+TypedPredictor(+ CodeSignature+ max_retries=3+)+```++Parameters:+* `signature` (dspy.Signature): The signature that defines the input and output fields along with their types.+* `max_retries` (int, optional): The maximum number of retries for generating a valid prediction output. Defaults to 3.++### Methods++#### `copy() -> "TypedPredictor"`++Creates and returns a deep copy of the current TypedPredictor instance.++**Returns:** A new instance of TypedPredictor that is a deep copy of the original instance.++#### `_make_example(type_: Type) -> str`++A static method that generates a JSON object example based pn the schema of the specified Pydantic model type. This JSON object serves as an example for the expected input or output format.++**Parameters:**+* `type_`: A Pydantic model class for which an example JSON object is to be generated.++**Returns:** A string that represents a JSON object example, which validates against the provided Pydantic model's JSON schema. If the method is unable to generate a valid example, it returns an empty string.++#### `_prepare_signature() -> dspy.Signature`++Prepares and returns a modified version of the signature associated with the TypedPredictor instance. This method iterates over the signature's fields to add format and parser functions based on their type annotations.++**Returns:** A dspy.Signature object that has been enhanced with formatting and parsing specifications for its fields.++#### `forward(**kwargs) -> dspy.Prediction`++Executes the prediction logic, making use of the `dspy.Predict` component to generate predictions based on the input arguments. This method handles type validation, parsing of output data, and implements retry logic in case the output does not initially follow to the specified output schema.++**Parameters:**++* `**kwargs`: Keyword arguments corresponding to the input fields defined in the signature.++**Returns:** A dspy.Prediction object containing the prediction results. Each key in this object corresponds to an output field defined in the signature, and its value is the parsed result of the prediction.++### Example++```python+from dspy import InputField, OutputField, Signature+from dspy.functional import TypedPredictor+from pydantic import BaseModel++# We define a pydantic type that automatically checks if it's argument is valid python code.+class CodeOutput(BaseModel):+ code: str+ api_reference: str++class CodeSignature(Signature):+ function_description: str = InputField()+ solution: CodeOutput = OutputField()++cot_predictor = TypedPredictor(CodeSignature)+prediction = cot_predictor(+ function_description="Write a function that adds two numbers."+)++print(prediction["code"])+print(prediction["api_reference"])+```\ No newline at end of filediff --git a/docs/api/functional/dspy_cot.md b/docs/api/functional/dspy_cot.mdnew file mode 100644index 000000000..9b9e3bbea--- /dev/null+++ b/docs/api/functional/dspy_cot.md@@ -0,0 +1,34 @@+---+sidebar_position: 4+---++---+sidebar_position: 3+---++# dspy.cot++### Overview++#### `def cot(func) -> dspy.Module`++The `@cot` decorator is used to create a Chain of Thoughts module based on the provided function. It automatically generates a `dspy.TypedPredictor` and from the function's type annotations and docstring. Similar to predictor, but adds a "Reasoning" output field to capture the model's step-by-step thinking.++* **Input**: Function with input parameters and return type annotation.+* **Output**: A dspy.Module instance capable of making predictions.++### Example++```python+import dspy++context = ["Roses are red.", "Violets are blue"]+question = "What color are roses?"++def generate_answer(self, context: list[str], question) -> str:+ """Answer questions with short factoid answers."""+ pass++generate_answer(context=context, question=question)+```\ No newline at end of filediff --git a/docs/api/functional/dspy_predictor.md b/docs/api/functional/dspy_predictor.mdnew file mode 100644index 000000000..9d4814c0f--- /dev/null+++ b/docs/api/functional/dspy_predictor.md@@ -0,0 +1,30 @@+---+sidebar_position: 3+---++# dspy.predictor++### Overview++#### `def predictor(func) -> dspy.Module`++The `@predictor` decorator is used to create a predictor module based on the provided function. It automatically generates a `dspy.TypedPredictor` and from the function's type annotations and docstring.++* **Input**: Function with input parameters and return type annotation.+* **Output**: A dspy.Module instance capable of making predictions.++### Example++```python+import dspy++context = ["Roses are red.", "Violets are blue"]+question = "What color are roses?"++def generate_answer(self, context: list[str], question) -> str:+ """Answer questions with short factoid answers."""+ pass++generate_answer(context=context, question=question)+```\ No newline at end of filediff --git a/docs/api/language_model_clients/_category_.json b/docs/api/language_model_clients/_category_.jsonindex 3f6129bae..1527ce187 100644--- a/docs/api/language_model_clients/_category_.json+++ b/docs/api/language_model_clients/_category_.json@@ -1,6 +1,6 @@{"label": "Language Model API Clients",- "position": 4,+ "position": 5,"link": {"type": "generated-index","description": "This documentation provides an overview of the DSPy Language Model Clients."diff --git a/docs/api/optimizers/_category_.json b/docs/api/optimizers/_category_.jsonindex 5d7edc9df..8f98c5b3b 100644--- a/docs/api/optimizers/_category_.json+++ b/docs/api/optimizers/_category_.json@@ -1,6 +1,6 @@{"label": "Optimizers",- "position": 2,+ "position": 3,"link": {"type": "generated-index","description": "Teleprompters are powerful optimizers (included in DSPy) that can learn to bootstrap and select effective prompts for the modules of any program. (The \"tele-\" in the name means \"at a distance\", i.e., automatic prompting at a distance.)\n\nThis documentation provides an overview of the DSPy Teleprompters."diff --git a/docs/api/retrieval_model_clients/_category_.json b/docs/api/retrieval_model_clients/_category_.jsonindex 0c3ec89a3..964dab01b 100644--- a/docs/api/retrieval_model_clients/_category_.json+++ b/docs/api/retrieval_model_clients/_category_.json@@ -1,6 +1,6 @@{"label": "Retrieval Model Clients",- "position": 3,+ "position": 4,"link": {"type": "generated-index","description": "This documentation provides an overview of the DSPy Retrieval Model Clients."diff --git a/docs/docs/building-blocks/8-typed_predictors.md b/docs/docs/building-blocks/8-typed_predictors.mdnew file mode 100644index 000000000..65032474b--- /dev/null+++ b/docs/docs/building-blocks/8-typed_predictors.md@@ -0,0 +1,13 @@+# Typed Predictors++In DSPy, alongside Signatures a++## Executing Typed Predictors++## Chain of Thoughts with Typed Predictors++## Optimizing Typed Predictors++## Typed Predictors via Decorators++## Composing Functional Typed Predictors in `dspy.Module`\ No newline at end of filediff --git a/docs/docs/deep-dive/typed_predictors/_category_.json b/docs/docs/deep-dive/typed_predictors/_category_.jsonnew file mode 100644index 000000000..e69de29bbdiff --git a/docs/docs/deep-dive/typed_predictors/functional_typed_predictors.md b/docs/docs/deep-dive/typed_predictors/functional_typed_predictors.mdnew file mode 100644index 000000000..9a8f79ae9--- /dev/null+++ b/docs/docs/deep-dive/typed_predictors/functional_typed_predictors.md@@ -0,0 +1,7 @@+# Functional Typed Predictors++## Typed Predictors as Decorators++## Functional Typed Predictors in `dspy.Module`++## How Functional Typed Predictors work?\ No newline at end of filediff --git a/docs/docs/deep-dive/typed_predictors/understanding_predictors.md b/docs/docs/deep-dive/typed_predictors/understanding_predictors.mdnew file mode 100644index 000000000..f0ad17354--- /dev/null+++ b/docs/docs/deep-dive/typed_predictors/understanding_predictors.md@@ -0,0 +1,9 @@+# Understanding Typed Predictors++## Why use a Typed Predictor?++## How to use a Typed Predictor?++## Prompt of Typed Predictors++## How Typed Predictors work?\ No newline at end of file