from langchain.output_parsers import ResponseSchema, StructuredOutputParser from langchain_core.prompts import PromptTemplate from langchain_openai import ChatOpenAI # Define the response schemas response_schemas = [ ResponseSchema(name="recipe", description="the recipe for the dish requested by the user"), ResponseSchema(name="ingredients", description="list of ingredients required for the recipe, should be a detailed list"), ] # Create the parser output_parser = StructuredOutputParser.from_response_schemas(response_schemas) # Generate format instructions format_instructions = output_parser.get_format_instructions() # Create the prompt template prompt = PromptTemplate( template="Provide the recipe for the dish requested.\n{format_instructions}\n{dish}", input_variables=["dish"], partial_variables={"format_instructions": format_instructions}, ) # Set up the LLM and chain model = ChatOpenAI(model="gpt-4-0613", temperature=0) chain = prompt | model | output_parser # Generate recipe and ingredients response = chain.invoke({"dish": "Spaghetti Bolognese"}) print(response) # Output: # { # "recipe": "To make Spaghetti Bolognese, cook minced beef with onions, garlic, tomatoes, and Italian herbs. Simmer until thickened and serve over cooked spaghetti.", # "ingredients": "Minced beef, onions, garlic, tomatoes, Italian herbs, spaghetti, olive oil, salt, pepper." # } __ __