-
Notifications
You must be signed in to change notification settings - Fork 286
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
Examples builder #93
Comments
im 99% sure is the first one |
Hi @samuelcolvin |
On a related note then I often want to use a pydantic object as input to an llm. This involves converting the pydantic object to a string representation that is easy to read for the llm. Having a llm_str method would be really handy. That way it is easy to get a pydantic object as output from an llm call using Instructor or PydanticAI and then you can pass that pydantic object to another llm function and call llm_str, for instance. Something along those lines have been implemented here: https://github.com/AnswerDotAI/toolslm Following anthropic documentation and prompt generator then it seems the llms prefer a very simple form of xml. Currently i use the following method: def llm_str(self) -> str:
root_tag = self.__class__.__name__
lines = [f"<{root_tag}>"]
for field_name, field in self.model_fields.items():
value = getattr(self, field_name)
if isinstance(value, BaseModel):
lines.append(value.to_xml())
elif isinstance(value, list):
lines.append(f"<{field_name}>")
for item in value:
if isinstance(item, BaseModel):
lines.append(item.llm_str())
else:
lines.append(f"- {item}")
lines.append(f"</{field_name}>")
else:
lines.append(f"{field_name}: {value}")
lines.append(f"</{root_tag}>")
return "\n".join(lines) But you could also have a general pydantic to xml converter. Something like this: def pydantic_obj_to_xml_repr(obj: BaseModel, tag_name: str = None) -> str:
if tag_name is None:
tag_name = obj.__class__.__name__.lower()
def to_element(o: BaseModel, name: str) -> etree._Element:
e = etree.Element(name)
for field_name in o.model_fields:
value = getattr(o, field_name)
if isinstance(value, BaseModel):
e.append(to_element(value, field_name))
elif isinstance(value, list):
sub = etree.SubElement(e, field_name)
for item in value:
if isinstance(item, BaseModel):
sub.append(to_element(item, "item"))
else:
item_el = etree.SubElement(sub, "item")
item_el.text = str(item)
elif isinstance(value, dict):
sub = etree.SubElement(e, field_name)
for k, v in value.items():
if isinstance(v, BaseModel):
sub.append(to_element(v, k))
else:
kv_el = etree.SubElement(sub, k)
kv_el.text = str(v)
else:
sub = etree.SubElement(e, field_name)
if value is not None:
sub.text = str(value)
return e
root = to_element(obj, tag_name)
return etree.tostring(root, pretty_print=True, encoding="unicode") |
We plan to add an examples builder which would take a sequence of things (e.g. pydantic models, dataclasses, dicts etc.) and serialize them.
Usage would be something like
The suggest is that LLMs find it particularly easy to read XML, so we'll offer (among other formats) XML as way to format the examples.
By default, should it use
or
?
The text was updated successfully, but these errors were encountered: