Find answers from the community

Updated 3 months ago

Pydantic

What are some strategies to limit hallucinated responses when using structured output parsers with Llama models? For example, I ask the model to identify the number of hours that employees work per week from paragraph text. My pydantic class looks like this:
Plain Text
python class WorkWeek(BaseModel):
    hours_per_week: Optional[float]
    employee_type: Optional[str]
I find I always get some answer back like hours_of_work = 40 and employee_type = 'full-time' regardless of whether the document contains relevant text or not. I could create a document that just has the text This is a test and I would still get results back that are clearly not from the document like shown above. Interestingly, when I simply ask the model for a basic freeform text response, I get an answer that makes sense when the document doesn't mention hours of work (something like Based on the context, there isn't enough information to answer your question).
L
k
5 comments
Try adding more descriptions to your pydantic class

Plain Text
from pydantic import BaseModel, Field

class WorkWeek(BaseModel):
  """A class containing information on a work week, if found."""
  hours_per_week: Optional[float] = Field(description="The number of hours required per work week. If not found, leave blank.")
  employee_type: Optional[float] = Field(description="The employee type. Leave blank if not found.")
Thanks @Logan M I tried adding the description and doc strings. It doesn't seem to have made a difference, in fact I think it might be slightly worse. I'll try some more experiments.
Lol rip. Maybe append something to your query mentioning that the info may or may not be found. The LLM needs to know its OK to leave it blank
That made a difference! I just prepended "If only found in the document" to the question and now I get "Empty Response" as the string response rather than a hallucinated response. Now, to get it to return the empty case in a json format rather than string so it doesn't cause a Pydantic validation error is the next challenge.
LLMs hallucinate a lot less when they know theres an option to "not answer"

Hopefully you can get it to create the object πŸ™
Add a reply
Sign up and join the conversation on Discord