dspy
dspy copied to clipboard
Any use case of `dspy.ReAct` ?
i've just made a quick walkthrough with ReAct module in dspy, however, it seems like it is not well-documented, could anyone provide me an example of creating a react flow of this?
i've already search in this repo issues, what i've found is #276, where i do as follows:
import dspy
class GiveTime:
name = "GiveTime"
input_variable = "empty"
desc = "takes an empty string and returns the current local time"
def __init__(self, k=3):
pass
def __call__(self, *args, **kwargs):
return datetime.datetime.now().strftime("%H:%M:%S")
then, to utilize dspy,
mytool = GiveTime()
gen = dspy.ReAct('question -> answer', tools=[mytool])
After that, i want to make it to generate an answer, however, what i've got is something with the keyword arguments
# Call the ReAct module on a particular input
question = 'What is the current time today?'
kw = {
# where should i referenced this
}
result = gen(question=question, **kw)
print(f"Question: {question}")
print(f"Final Predicted Answer (after ReAct process): {result.answer}")
Here is my current error, i leave my keyword argument to be blank since i'm not sure where we need to fill it from
AttributeError Traceback (most recent call last)
Cell In[76], line 6
2 question = 'What is the color of the sky?' # this is a positional args
3 kw={
4
5 }
----> 6 result = gen(question=question, **kw)
8 print(f"Question: {question}")
9 print(f"Final Predicted Answer (after ReAct process): {result.answer}")
File /usr/local/lib/python3.10/dist-packages/dspy/primitives/program.py:29, in Module.__call__(self, *args, **kwargs)
28 def __call__(self, *args, **kwargs):
---> 29 return self.forward(*args, **kwargs)
File /usr/local/lib/python3.10/dist-packages/dspy/predict/react.py:78, in ReAct.forward(self, **kwargs)
74 args = {key: kwargs[key] for key in self.input_fields.keys() if key in kwargs}
76 for hop in range(self.max_iters):
77 # with dspy.settings.context(show_guidelines=(i <= 2)):
---> 78 output = self.react[hop](**args)
80 if action_val := self.act(output, hop): break
81 args.update(output)
File /usr/local/lib/python3.10/dist-packages/dspy/predict/predict.py:60, in Predict.__call__(self, **kwargs)
59 def __call__(self, **kwargs):
---> 60 return self.forward(**kwargs)
File /usr/local/lib/python3.10/dist-packages/dspy/predict/predict.py:74, in Predict.forward(self, **kwargs)
72 # If temperature is 0.0 but its n > 1, set temperature to 0.7.
73 temperature = config.get("temperature", None)
---> 74 temperature = lm.kwargs['temperature'] if temperature is None else temperature
76 num_generations = config.get("n", None)
77 if num_generations is None:
AttributeError: 'NoneType' object has no attribute 'kwargs'
Thanks for all of your help in advance.
The error is telling you that you didn't set a default LM for DSPy. After importing DSPy, do:
lm = dspy.OpenAI(model='gpt-3.5-turbo-instruct', max_tokens=300)
dspy.configure(lm=lm)
Obviously you will need an OpenAI API key. You should set it in the environment variable OPENAI_API_KEY
before running the notebook/script.
Additionally, as said in https://github.com/stanfordnlp/dspy/issues/276 , there is currently a bug in the ReAct module, and you will need to remove .passages
from the line 67
in the file dspy/predict/react.py
of your own local copy of the DSPy library.
BTW: *args
and **kwargs
are parameters that collect all the positional (* (list)) and keyword (** (dict)) arguments of a function, so you don't need to pass a **kw
to your gen
.