Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError on second iteration of loop - dspy.Predict #2006

Open
mtp-pura opened this issue Jan 2, 2025 · 4 comments
Open

ValueError on second iteration of loop - dspy.Predict #2006

mtp-pura opened this issue Jan 2, 2025 · 4 comments

Comments

@mtp-pura
Copy link

mtp-pura commented Jan 2, 2025

This code loops through the first 's', returns a correct bool, but then fails on the next iteration:

lm = dspy.LM('ollama_chat/llama3.2:1b', api_base='http://localhost:11434', cache=False) 
dspy.configure(lm=lm)

# Define DSPY Signatures
classify= dspy.Predict('sentence -> sentiment: bool')
user = "worker"

# method for extracing 'sentences' 

# Analyse each sentence with the LM 
for s in sentences:
    question = f"Is \"{s}\" a task performed by a {user}?"
    current_sentence_response = classify(sentence=question)
    print(current_sentence_response.sentiment)
value = self.parse(signature, output, _parse_values=_parse_values)
return fn(instance, *args, **kwargs)
raise ValueError(f"Expected {signature.output_fields.keys()} but got {fields.keys()}")
ValueError: Expected dict_keys(['sentiment']) but got dict_keys([])```

With error handling I was able to check the history and the response it provides is:

Response:

I can't assist with this request.
@okhat
Copy link
Collaborator

okhat commented Jan 2, 2025

Thanks for opening the issue! This seems like an LLM refusal. It doesn’t like your question.

You can wrap it in a try/except.

@mtp-pura
Copy link
Author

mtp-pura commented Jan 2, 2025

Thanks. Yes, I now have some nested try/except going to manage it through staged questioning. The text I am feeding is quite fuzzy and unusual at times (like a project document's footer with obscure IDs and labels). Seems to be working okay at the moment, but still running into classic prompt "engineering" struggles. Will keep going as this is only day 1 with DSPy.

@okhat
Copy link
Collaborator

okhat commented Jan 2, 2025

Awesome! Q: why are you concatenating a project document's footer into a string?

You can pass multiple fields into a module, by adjusting your signature.

instruction = "Is the supplied task something that this user can perform?"
module = dspy.Predict(dspy.Signature('task, user -> validity: bool', instruction))
module(task=..., user=...)

@mtp-pura
Copy link
Author

mtp-pura commented Jan 2, 2025

Thanks for replying. That stacking of modules looks interesting. I may try to incorporate that.

I'll explain my use case but disclaimer: I am not a trained coder and I am more or less 'winging' it; so I am sure there are better ways to do everything I am trying to do.

My code is set up to :
Pull in PDFs from a folder. Convert the entire PDF to text. Separate by sentences (tokenize_string). Analyse each sentence (string) using a series of Predict and ChainOfThought modules. The footer is just caught up in the process during PDF handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants