Find answers from the community

Updated 3 months ago

Classify Structured Llm Without Setup

At a glance

The community member is trying to get a structured output from a language model (LLM) using Pydantic. They have defined a classify Pydantic model and are using the llm.as_structured_llm method to set up a structured LLM. They are then using the chat method to get a response and print the values of the check_docs, web_search, and mentioned_company fields.

In the comments, the community member mentions that they are not using an agent, but are just trying to get a structured output from an LLM using Pydantic. They have found the structured_predict method and want to use the default LLM to return multiple different Pydantic objects in different places, such as an order Pydantic object in one place and an item object in another.

Another community member suggests using the structured_predict method, which the original community member confirms is the most optimal way to achieve their goal.

Plain Text
class classify(BaseModel):
    check_docs: int
    web_search: int
    mentioned_company: str


classify_llm = llm.as_structured_llm(output_cls=classify)
fprompt = CLASSIFICATION.format(
    user_input="How can I contact your firm?")
response = classify_llm.chat([ChatMessage(
    role="user", content=fprompt)])

print(response.raw.check_docs)
print(response.raw.web_search)
print(response.raw.mentioned_company)

Is there a way to do this without setting up a structured llm every time for different objects.
s
L
5 comments
I am actually not using an agent I was just trying to get a structured output from an llm using pydantic.
I found about the structured_predict, so basically I want to use the default llm to return multiple different pydantic objects in different places, for example I want to use the same llm to return an order pydantic object in one place and item object in another
Plain Text
ptemplate = PromptTemplate(CLASSIFICATION)
response2 = llm.structured_predict(
    classify,
    ptemplate,
    user_input="How can I contact your firm?"
)
this is the most optimal way right? @Logan M
Add a reply
Sign up and join the conversation on Discord