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

fix(JsonSchemaValidator): fix recursive loop and general LLM (claude, mistral...) compatibility #7556

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

lambda-science
Copy link

@lambda-science lambda-science commented Apr 17, 2024

Related Issues

Proposed Changes:

  • Claude Compatibility: modified the behaviour so that (i) error template is now a single message with generated json, error and schema (ii) make it so that validated messages are always "Assistant" chatmessage (for next pipeline step) and validation_errors are always "User" chatmessage (for LLM loops)
  • Recursive Loop in type conversion: used Claude OPUS to automatically generate a fix based on the written issue.

How did you test it?

Tested on my personal use-case and it solved my issues.

Notes for the reviewer

The behaviour is modified to only include the last messages from the conversation and not the whole history of messages (less cost for long pipeline loops, not necessary to have previous messages).

For the auto-generated fix for recursive, maybe the bug comes from the fact that sometimes json.loads(value) output a string and needs to be called twice to get the actual dict/list in the string. This is weird, but I've seen it happen. I'm not sure about the fundamental difference to be honest. Maybe it doesn't work for nested json.

Checklist

…ted by ClaudeOpus). Modify the behaviour to build the error template in a single user_message instead of two separate. Modify the behaviour to only include latest message instead of full history (very costly if long looping pipeline)
@lambda-science lambda-science requested a review from a team as a code owner April 17, 2024 14:17
@lambda-science lambda-science requested review from anakin87 and removed request for a team April 17, 2024 14:17
@github-actions github-actions bot added 2.x Related to Haystack v2.0 type:documentation Improvements on the docs labels Apr 17, 2024
@anakin87 anakin87 requested a review from vblagoje April 17, 2024 14:24
@lambda-science lambda-science changed the title feat(JsonSchemaValidator): fix recursive loop and general LLM (claude, mistral...) compatibility fix(JsonSchemaValidator): fix recursive loop and general LLM (claude, mistral...) compatibility Apr 17, 2024
@vblagoje
Copy link
Member

Looks good @lambda-science , would you please add a short reno note (see https://github.com/deepset-ai/haystack/blob/main/CONTRIBUTING.md) and resolve these black issues :-)

@masci
Copy link
Member

masci commented May 15, 2024

Is this still relevant? Let's merge it or close.

@vblagoje
Copy link
Member

It's missing reno note and unit tests. It's an important addition and would love to see @lambda-science push it towards the finish line 🏁

@lambda-science
Copy link
Author

Sorry, it went out of my mind I will do it :)

@lambda-science
Copy link
Author

I know why I stopped, because I had issue setting up the env (on windows).
Now that all is set, I can see there was test failling (on top of black/reno missing) so I will work on it

@anakin87 anakin87 removed their request for review May 16, 2024 09:25
lambda-science and others added 3 commits May 16, 2024 11:54
…ted by ClaudeOpus). Modify the behaviour to build the error template in a single user_message instead of two separate. Modify the behaviour to only include latest message instead of full history (very costly if long looping pipeline)
@lambda-science lambda-science requested a review from a team as a code owner May 16, 2024 09:55
@lambda-science lambda-science requested review from dfokina and removed request for a team May 16, 2024 09:55
@CLAassistant
Copy link

CLAassistant commented May 16, 2024

CLA assistant check
All committers have signed the CLA.

@lambda-science
Copy link
Author

lambda-science commented May 16, 2024

Should be good now.
I had to change the test a bit because as I explained I suggested to only validate latest message (and include only latest message for validation) to optimize cost of long loops ! Tell me if you agree or not.
(So validation of multi-message history only return a list of 1 message)

@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 9110242140

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 28 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.01%) to 90.573%

Files with Coverage Reduction New Missed Lines %
components/validators/json_schema.py 28 0.0%
Totals Coverage Status
Change from base Build 9103206687: -0.01%
Covered Lines: 6591
Relevant Lines: 7277

💛 - Coveralls

@@ -142,18 +143,22 @@ def run(
else:
validate(instance=content, schema=validation_schema)

return {"validated": messages}
return {"validated": [last_message]}
Copy link
Member

@vblagoje vblagoje May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lambda-science looks good but isn't this changing the method contract, do you remember why this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By method contract you mean signature ?
The type is the same List[ChatMessage] but now this list is always of length 1.
The reason I did this is to not have exponentional cost by passing all message history to the LLM each time. Imagine your pipe does 20 loops, you used 20+19+18+17+16... (20x(20+1)/2) 220 messages worth of tokens.
By passing only the last error (which should be the only needed to fix the json schema) you would only use 20 messages worth of tokens

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see; but one can also argue that previous failure attempts are useful as well. We should provide some top_k last messages argument, wdyt @lambda-science ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.x Related to Haystack v2.0 topic:tests type:documentation Improvements on the docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JsonSchemaValidator: unintended primitive value conversion in _recursive_json_to_object method
5 participants