forked from stanfordnlp/dspy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'stanfordnlp:main' into main
- Loading branch information
Showing
48 changed files
with
6,398 additions
and
981 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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,8 @@ | ||
{ | ||
"label": "Functional", | ||
"position": 2, | ||
"link": { | ||
"type": "generated-index", | ||
"description": "This documentation provides an overview of the Typed Predictors." | ||
} | ||
} |
This file contains 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,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"]) | ||
``` |
This file contains 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,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"]) | ||
``` |
This file contains 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,30 @@ | ||
--- | ||
sidebar_position: 4 | ||
--- | ||
|
||
# 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?" | ||
|
||
@dspy.cot | ||
def generate_answer(self, context: list[str], question) -> str: | ||
"""Answer questions with short factoid answers.""" | ||
pass | ||
|
||
generate_answer(context=context, question=question) | ||
``` |
This file contains 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,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?" | ||
|
||
@dspy.predictor | ||
def generate_answer(self, context: list[str], question) -> str: | ||
"""Answer questions with short factoid answers.""" | ||
pass | ||
|
||
generate_answer(context=context, question=question) | ||
``` |
This file contains 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 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 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 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.