How to force structured output using load_summarize_chain #22131
Replies: 1 comment 10 replies
-
Hello @hyoinandout! I'm here to help you with any questions or issues you have. I'm a bot designed to provide assistance while you wait for a human maintainer. Feel free to ask me anything! To force structured output using from typing import Optional
from langchain.chains import create_structured_output_runnable
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
# Define a structured schema using Pydantic
class DocumentSummary(BaseModel):
title: str = Field(..., description="The title of the document")
author: str = Field(..., description="The author of the document")
summary: str = Field(..., description="The summary of the document")
# Initialize your language model
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
# Create a structured output runnable
structured_llm = create_structured_output_runnable(DocumentSummary, llm, mode="openai-functions")
# Define the prompt
system = '''You are a world-class assistant for summarizing documents in a structured format.'''
prompt = ChatPromptTemplate.from_messages(
[("system", system), ("human", "{input}")]
)
# Create the chain
chain = prompt | structured_llm
# Use the chain to summarize a document
document = {
"input": "This is the content of the document that needs to be summarized."
}
summary = chain.invoke(document)
print(summary) In this example, the
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
None
Description
How can I force structured output using load_summarize_chain?
I would like to have a structured summarization from a long document.
Such like
output = {"output_text":{"title":"Alpha","Author":"Beta","Summarization":"Gamma foo bar ..."}}
System Info
langchain > 1
Beta Was this translation helpful? Give feedback.
All reactions