Replies: 1 comment
-
Hey there, @owenffff! I'm here to assist you with any bugs, questions, or contributions you need help with. Let's tackle this together! To capture and output multiple records from a single document using a Pydantic model in LangChain, you can use the from typing import List, Optional
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
class Quotation(BaseModel):
date: str = Field(..., description="Date of the quotation")
quote_number: str = Field(..., description="Unique identifier for the quote")
currency: str = Field(..., description="Currency used in the quotation")
quote_name: str = Field(..., description="Name or title of the quotation")
description: str = Field(..., description="Description of the service")
quantity: int = Field(..., description="Quantity of the item")
one_time_charge: Optional[float] = Field(None, description="One-time charge of the item")
monthly_charge: Optional[float] = Field(None, description="Monthly charge of the item")
monthly_recurring_service_tax: Optional[float] = Field(None, description="Monthly recurring service tax")
total_price_otc_mrc: Optional[float] = Field(None, description="Total price (One-Time Charge + Monthly Recurring Charge)")
class Document(BaseModel):
quotations: List[Quotation] = Field(..., description="List of quotations mentioned in the document")
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are an extraction algorithm. Please extract every possible instance of quotation information."),
("human", "{input}")
]
)
structured_llm = llm.with_structured_output(Document)
result = structured_llm.invoke({"input": "Your document content here"})
print(result)
# -> Document(quotations=[Quotation(...), Quotation(...), ...]) This example demonstrates how to define a Pydantic model to capture multiple records (in this case, multiple quotations) from a single document and use LangChain's |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
Hi,
I'm currently experimenting with structured_llm and use it for information extraction from a number of Quotation Contracts.
When i was using structured_llm with pydantic model defined, i found that if i provide documents (that contains two or more records within a single document) to structured llm, it will not natively output all records for you. the default behavior of the structured_llm with a Pydantic model is to return a single instance of the model, which limits output to one record per query.
Wondering if there are any workarounds for letting the structured_llm to capture more than one record within a single document? Thanks!
System Info
langchain version = 0.2.12
Beta Was this translation helpful? Give feedback.
All reactions