This tutorial covers the implementation of the JsonOutputParser. JsonOutputParser is a tool that allows users to specify the desired JSON schema. It is designed to enable a Large Language Model (LLM) to query data and return results in JSON format that adheres to the specified schema. To ensure that the LLM processes data accurately and efficiently, generating JSON in the desired format, the model must have sufficient capacity (e.g., intelligence). For instance, the llama-70B model has a larger capacity compared to the llama-8B model, making it more suitable for handling complex data.
[Note]
JSON (JavaScript Object Notation) is a lightweight data interchange format used for storing and structuring data. It plays a crucial role in web development and is widely used for communication between servers and clients. JSON is based on text that is easy to read and simple for machines to parse and generate.
Basic Structure of JSON
JSON data consists of key-value pairs. Here, the "key" is a string, and the "value" can be various data types. JSON has two primary structures:
Object: A collection of key-value pairs enclosed in curly braces { }. Each key is associated with its value using a colon ( : ), and multiple key-value pairs are separated by commas ( , ).
Array: An ordered list of values enclosed in square brackets [ ]. Values within an array are separated by commas ( , ).
{"name":"John Doe","age":30,"is_student":false,"skills": ["Java","Python","JavaScript"],"address": {"street":"123 Main St","city":"Anytown" }}
You can alternatively set OPENAI_API_KEYin .env file and load it.
[Note] This is not necessary if your've already set OPENAI_API_KEY in previous steps.
from dotenv import load_dotenvload_dotenv(override=True)
True
Using JsonOutputParser with Pydantic
If you need to generate output in JSON format, you can easily implement it using LangChain's JsonOutputParser. There are 2 ways to generate output in JSON format:
Use Pydantic
Don't use Pydantic
Follow the steps below to implement it.
Importing Required Modules
Start by importing the necessary modules.
from langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import JsonOutputParserfrom langchain_openai import ChatOpenAIfrom pydantic import BaseModel, Field
# Create an OpenAI objectmodel =ChatOpenAI(temperature=0, model_name="gpt-4o")
Define the output data schema format.
# Use Pydantic to define the data schema for the output format.classTopic(BaseModel): description:str=Field(description="A concise description of the topic") hashtags:str=Field(description="Keywords in hashtag format (at least 2)")
Set up the parser using JsonOutputParser and inject instructions into the prompt template.
# Write your questionquestion ="Please explain the severity of global warming."# Set up the parser and inject the instructions into the prompt template.parser =JsonOutputParser(pydantic_object=Topic)print(parser.get_format_instructions())
The output should be formatted as a JSON instance that conforms to the JSON schema below.
As an example, for the schema {"properties": {"foo": {"title": "Foo", "description": "a list of strings", "type": "array", "items": {"type": "string"}}}, "required": ["foo"]}
the object {"foo": ["bar", "baz"]} is a well-formatted instance of the schema. The object {"properties": {"foo": ["bar", "baz"]}} is not well-formatted.
Here is the output schema:
```
{"properties": {"description": {"description": "A concise description of the topic", "title": "Description", "type": "string"}, "hashtags": {"description": "Keywords in hashtag format (at least 2)", "title": "Hashtags", "type": "string"}}, "required": ["description", "hashtags"]}
```
# Set up the prompt templateprompt = ChatPromptTemplate.from_messages( [ ("system", "You are a friendly AI assistant. Answer questions concisely."), ("user", "#Format: {format_instructions}\n\n#Question: {question}"), ])prompt = prompt.partial(format_instructions=parser.get_format_instructions())# Combine the prompt, model, and JsonOutputParser into a chainchain = prompt | model | parser# Run the chain with your questionanswer = chain.invoke({"question": question})
# Check the type.type(answer)
dict
# Output the answer object.answer
{'description': "Global warming is a critical environmental issue characterized by the increase in Earth's average surface temperature due to rising levels of greenhouse gases. It leads to severe weather changes, rising sea levels, and impacts on biodiversity and human life.",
'hashtags': '#GlobalWarming #ClimateChange #EnvironmentalImpact'}
Using JsonOutputParser Without Pydantic
You can generate output in JSON format without Pydantic. Follow the steps below to implement it :
# Write your questionquestion ="Please provide information about global warming. Include the explanation in description and the related keywords in `hashtags`."# Initialize JsonOutputParserparser =JsonOutputParser()# Set up the prompt templateprompt = ChatPromptTemplate.from_messages( [ ("system", "You are a friendly AI assistant. Answer questions concisely."), ("user", "#Format: {format_instructions}\n\n#Question: {question}"), ])# Inject instruction to promptprompt = prompt.partial(format_instructions=parser.get_format_instructions())# Combine the prompt, model, and JsonOutputParser into a chainchain = prompt | model | parser# Run the chain with your questionresponse = chain.invoke({"question": question})print(response)
{'description': "Global warming refers to the long-term increase in Earth's average surface temperature due to human activities, primarily the emission of greenhouse gases like carbon dioxide, methane, and nitrous oxide. These emissions result from burning fossil fuels, deforestation, and industrial processes, leading to the greenhouse effect, where heat is trapped in the atmosphere. This warming has significant impacts on weather patterns, sea levels, and ecosystems, contributing to climate change and posing risks to biodiversity and human societies.", 'hashtags': ['#GlobalWarming', '#ClimateChange', '#GreenhouseGases', '#CarbonEmissions', '#FossilFuels', '#Deforestation', '#Sustainability', '#EnvironmentalImpact']}