Find answers from the community

Updated 3 months ago

Hi, does anyone know how to use kg_

Hi, does anyone know how to use kg_schema_cls in SchemaLLMPathExtractor? I can't seem to find documentation about it online?
L
O
9 comments
Its fairly advanced -- I debated even exposing it lol
You need to pass in a pydantic class that represents a graph, but it has some pretty specific requirements.

Under the hood we are creating a model on the fly based on the options you pass in

If you were to do it manually, it might look like

Plain Text
class Entity(BaseModel):
  """A representation of a entity in text."""
  type: Literal["PERSON", "PLACE", "THING"]
  name: str

class Relation(BaseModel):
  """A type of relationship between two entities."""
  type: Literal["BORN_IN", "RELATED_TO"]

class Triplet(BaseModel):
  subject: Entity
  relation: Relation
  object: Entity

class KGSchema(BaseModel):
  """A graph represented by a list of triplets."""
  triplets: List[Triplet]

kg_schema_cls = KGSchema
Basically you need something like that. The field names need to be the same, but you can add custom validators, etc.
Thank you! That is very helpful! I was digging into the python code and came to something similar but wasn’t sure because of the ellipses in some of the spots.
I’m assuming we aren’t intended to make multiple Schema extractors? There is only one named validator and it throws an exception. Unless I’m doing something wrong.
If it throws an exception, usually that means the LLM is doing something very wrong (although I could have sworn that validator has a giant try/except)
Hm I’m getting pydantic.v1.errors.ConfigError: duplicate validator function (path of SchemaLLMPathExtractor)-validate.

I don’t think this is related to LLM but instead me trying to make two instances SchemaLLMPathExtractor with different schemas.

I can probably just combine the two schemas now that I know the above.
Hmm weird, pydantic.v1 isn't used anymore, but I guess you might have an older version
Oh good catch. That is odd. Everything I’m using is pydanticv2 supposedly. Hopefully this is a permissions issue. I’ll update if I find out it’s some replicable issue.
Add a reply
Sign up and join the conversation on Discord