Reputation: 85
Is there a way to use OpenAI Structured Outputs with the latest langchain4j version?
https://openai.com/index/introducing-structured-outputs-in-the-api/
I know it is very recent API change, but maybe there is a way to add custom parameters to OpenAiChatModel that I can't find. Specifically, I want to use the response_format parameter, so that the response is in trusted JSON.
Upvotes: 1
Views: 98
Reputation: 4253
I used xml create my structured output giving the agent a list of tasks I wanted it to perform. The structured output is described in the output.
from langchain_openai import ChatOpenAI as LangChainChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.agents import tool, AgentType, Tool,initialize_agent,load_tools
from langchain.prompts import PromptTemplate,ChatPromptTemplate
from langchain.chains import LLMChain
llm=LangChainChatOpenAI(model="gpt-4o-mini",temperature=0, openai_api_key=key)
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
input ="""
The eVTOL (electric Vertical Take-Off and Landing) Aircraft Market is revolutionizing urban transport, offering a sustainable and efficient alternative to traditional vehicles and aircraft. Driven by advancements in electric propulsion, the market is segmented by Product Type into Fully Electric and Hybrid models. Fully electric eVTOLs are gaining traction due to zero-emission capabilities, while hybrids offer extended range, making both solutions appealing based on specific urban or regional needs.
When it comes to Lift Technology, the market is further segmented into Vectored Thrust, Multirotor & Rotorcraft, and Lift Plus Cruise. Vectored Thrust, often favored for urban air taxis, combines versatility with speed, while Multirotor designs provide stability for shorter trips. Lift Plus Cruise models are gaining attention for their balance between range and lift efficiency, ideal for extended intercity travel.
Regionally, North America and Europe are at the forefront of eVTOL adoption, driven by robust R&D and favorable regulatory frameworks. Meanwhile, the Asia-Pacific region shows significant growth potential, backed by increasing urbanization and investments in air mobility infrastructure.
"""
messages = [
("system", """
<context>
<achievements>You are Louise ai agent</achievements
<task>Your task is to extract a json structure from input_data</task>
<task>Your task is to explain in detail the product features</task>
<task>Your task is to provide company information, such as location and website</task>
<task>Estimate the market sentiment for each feature</task>
<thinking></thinking>
<output>
product_name, features, company_information
</output>
</context>
"""),
("human", input)
]
template="input_data: {input}"
prompt = PromptTemplate(template=template, input_variables=["input"])
llm_chain=LLMChain(prompt=prompt,llm=llm)
output = llm.invoke(messages)
print(output)
print(llm_chain.run(messages))
output
{
"product_name": "eVTOL Aircraft",
"features": {
"Electric Propulsion": {
"description": "Utilizes electric power for propulsion, leading to zero emissions and reduced noise pollution.",
"market_sentiment": "Positive"
},
"Product Type Segmentation": {
"description": "Includes Fully Electric and Hybrid models, catering to different urban and regional needs.",
"market_sentiment": "Positive"
},
"Lift Technology": {
"description": "Segmented into Vectored Thrust, Multirotor & Rotorcraft, and Lift Plus Cruise, each offering unique advantages for urban air mobility.",
"market_sentiment": "Positive"
},
"Regional Adoption": {
"description": "North America and Europe lead in eVTOL adoption, with significant growth potential in the Asia-Pacific region.",
"market_sentiment": "Positive"
}
},
"company_information": {
"location": "Global (with significant activity in North America, Europe, and Asia-Pacific)",
"website": "N/A (specific companies not mentioned)"
}
}
Upvotes: 0
Reputation: 46
there's no way to enable it just yet, but we are working on adding this feature right now. You can subscribe to updates here: https://github.com/langchain4j/langchain4j/issues/1581
Upvotes: 1