DSPy: The framework for programming—not prompting—language models
Python
finalized avatar model
Herumb Shandilyaa832c1e7b76e
diff --git a/dspy/predict/__init__.py b/dspy/predict/__init__.pyindex b176d2a0e..cf6315f52 100644--- a/dspy/predict/__init__.py+++ b/dspy/predict/__init__.py@@ -6,4 +6,4 @@ from .multi_chain_comparison import MultiChainComparisonfrom .predict import Predictfrom .program_of_thought import ProgramOfThoughtfrom .react import ReAct-from .retry import Retry+from .retry import Retry\ No newline at end of filediff --git a/dspy/predict/avatar/avatar.py b/dspy/predict/avatar/avatar.pyindex 580fc66f3..4028681dd 100644--- a/dspy/predict/avatar/avatar.py+++ b/dspy/predict/avatar/avatar.py@@ -1,7 +1,6 @@import dspy+from copy import deepcopy-from typing import Mapping-from pydantic import create_modelfrom pydantic.fields import FieldInfofrom dspy.predict.avatar.signatures import Actor@@ -13,9 +12,9 @@ class Avatar(dspy.Module):def __init__(self,signature,- knowledge_base,tools,max_iters=3,+ verbose=False,):self.signature = ensure_signature(signature)self.input_fields = self.signature.input_fields@@ -24,24 +23,25 @@ class Avatar(dspy.Module):self.finish_tool = Tool(tool=None,name="Finish",- input_variable=[],desc="returns the final output and finishes the task",)self.tools = tools + [self.finish_tool]self.actor_signature = Actor- self.knowledge_base = knowledge_basefor field in list(self.input_fields.keys())[::-1]:- self.actor_signature = self.actor_signature.insert(- -2,+ self.actor_signature = self.actor_signature.append(field,self._get_field(self.input_fields[field]),+ type_=self.input_fields[field].annotation,)+ self.verbose = verboseself.max_iters = max_itersself.actor = dspy.TypedPredictor(self.actor_signature)+ self.actor_clone = deepcopy(self.actor)+def _get_field(self, field_info: FieldInfo):match field_info.json_schema_extra['__dspy_field_type']:@@ -62,46 +62,41 @@ class Avatar(dspy.Module):def _update_signature(self, idx: int, omit_action: bool = False):- self.actor_signature = self.actor_signature.with_updated_fields(+ self.actor.signature = self.actor.signature.with_updated_fields(f"action_{idx}",Action,__dspy_field_type="input")- self.actor_signature = self.actor_signature.append(+ self.actor.signature = self.actor.signature.append(f"result_{idx}",dspy.InputField(prefix=f"Result {idx}:",desc=f"{idx}th result",+ type_=str,))match omit_action:case True:for field in list(self.output_fields.keys()):- self.actor_signature = self.actor_signature.append(+ self.actor.signature = self.actor.signature.append(field,self._get_field(self.output_fields[field]),+ type_=self.output_fields[field].annotation,)case False:- self.actor_signature = self.actor_signature.append(+ self.actor.signature = self.actor.signature.append(f"action_{idx+1}",dspy.OutputField(- prefix=f"Action {idx}:",- desc=f"{idx}th action to taken",+ prefix=f"Action {idx+1}:",+ desc=f"{idx+1}th action to taken",))-- self.actor = self._get_actor(self.actor_signature.fields)--- def _get_actor(self, fields: Mapping[str, FieldInfo]) -> dspy.Signature:- return create_model(- "Actor",- __base__=dspy.Signature,- __doc__=Actor.__doc__,- **fields,- )+ self.actor.signature = self.actor.signature.with_updated_fields(+ f"action_{idx+1}",+ Action,+ )def _call_tool(self, tool_name: str, tool_input_query: str) -> str:@@ -111,16 +106,31 @@ class Avatar(dspy.Module):def forward(self, **kwargs):- max_iters = None if "max_iters" not in kwargs else kwargs["max_iters"]- args = {key: kwargs[key] for key in self.input_fields.keys() if key in kwargs}+ print("Starting the task...")++ args = {+ "goal" : self.signature.__doc__,+ "tools" : [tool.name for tool in self.tools],+ }++ for key in self.input_fields.keys():+ if key in kwargs:+ args[key] = kwargs[key]+idx = 1+ tool_name = None+ max_iters = None if "max_iters" not in kwargs else kwargs["max_iters"]while tool_name != "Finish" and (max_iters > 0 if max_iters else True):- action = self.actor(tools=self.tools, **args)+ actor_output = self.actor(**args)+ action = getattr(actor_output, f"action_{idx}")tool_name = action.tool_nametool_input_query = action.tool_input_query+ if self.verbose:+ print(f"Action {idx}: {tool_name} ({tool_input_query})")+if tool_name != "Finish":tool_output = self._call_tool(tool_name, tool_input_query)self._update_signature(idx)@@ -131,6 +141,7 @@ class Avatar(dspy.Module):self._update_signature(idx, omit_action=True)args[f"action_{idx}"] = action+ args[f"result_{idx}"] = "Gathered all information needed to finish the task."breakidx += 1@@ -138,7 +149,9 @@ class Avatar(dspy.Module):if max_iters:max_iters -= 1- final_answer = self.actor(tools=self.tools, **args)+ final_answer = self.actor(**args)+ self.actor = deepcopy(self.actor_clone)+return dspy.Prediction(**{key: getattr(final_answer, key) for key in self.output_fields.keys()}- )\ No newline at end of file+ )diff --git a/dspy/predict/avatar/models.py b/dspy/predict/avatar/models.pyindex 00ed47dcd..0d15ef39f 100644--- a/dspy/predict/avatar/models.py+++ b/dspy/predict/avatar/models.py@@ -1,21 +1,19 @@-from pydantic import BaseModelfrom typing import Any, Optional+from pydantic import BaseModel, Fieldclass Tool(BaseModel):tool: Anyname: str- tool_input_query: strdesc: Optional[str]def __str__(self) -> str:- return f"{self.name}({self.input_variable}): {self.desc}"+ return f"{self.name}: {self.desc}"def __repr__(self) -> str:return self.__str__()class Action(BaseModel):- rationale: str- tool_name: str- tool_input_query: str\ No newline at end of file+ tool_name: Any = Field(..., description="Name of the tool to use.")+ tool_input_query: Any = Field(..., description="Query to pass as input to the tool.")\ No newline at end of filediff --git a/dspy/predict/avatar/signatures.py b/dspy/predict/avatar/signatures.pyindex df3663a63..c2d9f97ba 100644--- a/dspy/predict/avatar/signatures.py+++ b/dspy/predict/avatar/signatures.py@@ -1,23 +1,22 @@import dspy-from dspy.predict.avatar.models import Action+from dspy.predict.avatar.models import Action, Toolclass Actor(dspy.Signature):- """You will be given `Tools` which will have tool name, input values, and description. Given the user query, your task is to decide which tool to use and what input values to provide.+ """You will be given `Tools` which will be a list of tools to use to accomplish the `Goal`. Given the user query, your task is to decide which tool to use and what input values to provide.-You will be given `Actions` which will have tool name and input values. You will respond with the output of the tool."""+You will output action needed to accomplish the `Goal`. `Action` should have a tool to use and the input query to pass to the tool."""goal: str = dspy.InputField(prefix="Goal:",desc="Task to be accomplished.",)- tools: str = dspy.InputField(+ tools: list[str] = dspy.InputField(prefix="Tools:",desc="list of tools to use",- format=lambda x: str([str(tool) for tool in x]),)action_1: Action = dspy.OutputField(prefix="Action 1:",- desc="1st action to taken",+ desc="1st action to take.",)\ No newline at end of file