Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Add docstring (#7766)

Chen Qian151cac22e8e0
diff --git a/docs/docs/api/primitives/Image.md b/docs/docs/api/primitives/Image.md
index 747e0ddfc..548218405 100644
--- a/docs/docs/api/primitives/Image.md
+++ b/docs/docs/api/primitives/Image.md
@@ -11,6 +11,7 @@
- model_dump
- model_dump_json
- model_post_init
+ - serialize_model
show_source: true
show_undocumented_members: true
show_root_heading: true
diff --git a/docs/docs/api/utils/StatusMessage.md b/docs/docs/api/utils/StatusMessage.md
new file mode 100644
index 000000000..5c3c108f3
--- /dev/null
+++ b/docs/docs/api/utils/StatusMessage.md
@@ -0,0 +1,14 @@
+# dspy.utils.StatusMessage
+
+::: dspy.utils.StatusMessage
+ handler: python
+ options:
+ show_source: true
+ show_undocumented_members: true
+ show_root_heading: true
+ show_inherited_members: true
+ heading_level: 2
+ docstring_style: google
+ show_root_full_path: true
+ show_object_full_path: false
+ separate_signature: false
diff --git a/docs/docs/api/utils/StatusMessageProvider.md b/docs/docs/api/utils/StatusMessageProvider.md
new file mode 100644
index 000000000..fe0d22406
--- /dev/null
+++ b/docs/docs/api/utils/StatusMessageProvider.md
@@ -0,0 +1,21 @@
+# dspy.utils.StatusMessageProvider
+
+::: dspy.utils.StatusMessageProvider
+ handler: python
+ options:
+ members:
+ - lm_end_status_message
+ - lm_start_status_message
+ - module_end_status_message
+ - module_start_status_message
+ - tool_end_status_message
+ - tool_start_status_message
+ show_source: true
+ show_undocumented_members: true
+ show_root_heading: true
+ show_inherited_members: true
+ heading_level: 2
+ docstring_style: google
+ show_root_full_path: true
+ show_object_full_path: false
+ separate_signature: false
diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml
index bdb643217..51b8a6aca 100644
--- a/docs/mkdocs.yml
+++ b/docs/mkdocs.yml
@@ -118,6 +118,8 @@ nav:
- Embeddings: api/tools/Embeddings.md
- PythonInterpreter: api/tools/PythonInterpreter.md
- Utils:
+ - StatusMessage: api/utils/StatusMessage.md
+ - StatusMessageProvider: api/utils/StatusMessageProvider.md
- asyncify: api/utils/asyncify.md
- disable_litellm_logging: api/utils/disable_litellm_logging.md
- disable_logging: api/utils/disable_logging.md
diff --git a/docs/scripts/generate_api_docs.py b/docs/scripts/generate_api_docs.py
index 39ba45cd4..f49a6e404 100644
--- a/docs/scripts/generate_api_docs.py
+++ b/docs/scripts/generate_api_docs.py
@@ -51,6 +51,8 @@ API_MAPPING = {
dspy.disable_logging,
dspy.enable_litellm_logging,
dspy.disable_litellm_logging,
+ dspy.utils.streaming.StatusMessageProvider,
+ dspy.utils.streaming.StatusMessage,
],
"evaluation": [
dspy.Evaluate,
diff --git a/dspy/utils/streaming.py b/dspy/utils/streaming.py
index d885591ce..6f3e43825 100644
--- a/dspy/utils/streaming.py
+++ b/dspy/utils/streaming.py
@@ -22,26 +22,53 @@ if TYPE_CHECKING:
@dataclass
class StatusMessage:
+ """Dataclass that wraps a status message for status streaming."""
+
message: str
class StatusMessageProvider:
+ """Provides customizable status message streaming for DSPy programs.
+
+ This class serves as a base for creating custom status message providers. Users can subclass
+ and override its methods to define specific status messages for different stages of program execution,
+ each method must return a string.
+
+ Example:
+ ```python
+ class MyStatusMessageProvider(StatusMessageProvider):
+ def lm_start_status_message(self, instance, inputs):
+ return f"Calling LM with inputs {inputs}..."
+
+ def module_end_status_message(self, outputs):
+ return f"Module finished with output: {outputs}!"
+
+ program = dspy.streamify(dspy.Predict("q->a"), status_message_provider=MyStatusMessageProvider())
+ ```
+ """
+
def tool_start_status_message(self, instance: Any, inputs: Dict[str, Any]):
+ """Status message before a `dspy.Tool` is called."""
return f"Calling tool {instance.name}..."
def tool_end_status_message(self, outputs: Any):
+ """Status message after a `dspy.Tool` is called."""
return "Tool calling finished! Querying the LLM with tool calling results..."
def module_start_status_message(self, instance: Any, inputs: Dict[str, Any]):
+ """Status message before a `dspy.Module` or `dspy.Predict` is called."""
pass
def module_end_status_message(self, outputs: Any):
+ """Status message after a `dspy.Module` or `dspy.Predict` is called."""
pass
def lm_start_status_message(self, instance: Any, inputs: Dict[str, Any]):
+ """Status message before a `dspy.LM` is called."""
pass
def lm_end_status_message(self, outputs: Any):
+ """Status message after a `dspy.LM` is called."""
pass