eyurtsev / kor

LLM(😽)
https://eyurtsev.github.io/kor/
MIT License
1.6k stars 88 forks source link

NotImplementedError: parse_raw is not supported for pydantic 2 - how to do it with Pydantic 2? #228

Open ChristianWeyer opened 11 months ago

ChristianWeyer commented 11 months ago

Hi!

based on this: https://eyurtsev.github.io/kor/schema_serialization.html#deserialization

I am trying to deserialize from a JSON schema into an Object like this:

with open("schema.json", "r") as f:
    schema = Object.parse_raw(f.read())

Here, I get an error:

python3.11/site-packages/kor/nodes.py", line 280, in parse_raw
    raise NotImplementedError(
NotImplementedError: parse_raw is not supported for pydantic 2

What would be the way to do this with Pydantic 2? Thanks!

eyurtsev commented 11 months ago

I was thinking about moving the serialization logic into a standalone function and implement something that doesn't rely as much on pydantic internals (and have pydantic only do the validation), but there's no solution yet.

Let me know if you're interested in working on it or have a solution in mind already.

ChristianWeyer commented 11 months ago

For now, I could move back to Pydantic 1...

eyurtsev commented 11 months ago

@ChristianWeyer Linked PR that adds support for pydantic v2. It's a breaking change w/ respect to format, could you check whether you'll be able to make it work w/ your data?

Brandl commented 6 months ago

What is the progress on this, any updates?

I wrote a short progam, but there are neither errors nor parsed entities in the output, so I needed to do parsing myself:

schema, validator = from_pydantic(EntityExtraction)
chain = create_extraction_chain(llm, schema, encoder_or_encoder_class="json", validator=validator)
json_encoder = JSONEncoder(use_tags=True)
result = chain.invoke(text_input.text)
return json_encoder.decode(result['text']['raw'])