Find answers from the community

Updated 4 months ago

how to generate output in the json format ?

At a glance

The community member asked how to generate output in JSON format. Another community member provided a step-by-step guide on how to use the LlamaIndex library to extract data from an email message and return it in JSON format. The guide includes instructions on importing the necessary modules, creating a ChatPromptTemplate, initializing the OpenAI model and OpenAIPydanticProgram, and calling the program with the email content to print the output in JSON format. The community member also noted that the user would need to replace the placeholder values with the actual email content and schema they are using, and provided a link to the LlamaIndex documentation for more details.

@kapa.ai how to generate output in the json format ?
k
3 comments
Please stand by @praveenks30#120294 as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
To generate output in JSON format using LlamaIndex, you can use the LLM function to extract content. Here's a step-by-step guide:

  1. Import the necessary modules from the llama_index package. You'll need OpenAIPydanticProgram, ChatPromptTemplate, ChatMessage, and OpenAI.
Plain Text
from llama_index.program.openai import OpenAIPydanticProgram
from llama_index.core import ChatPromptTemplate
from llama_index.core.llms import ChatMessage
from llama_index.llms.openai import OpenAI


  1. Create a ChatPromptTemplate that defines the role and content of the system and user. The system's content should instruct the AI to extract insights from an email in JSON format. The user's content should provide the email message.
Plain Text
prompt = ChatPromptTemplate(
    message_templates=[
        ChatMessage(
            role="system",
            content=(
                "You are an expert assistant for extracting insights from email in JSON format. \n"
                "You extract data and returns it in JSON format, according to provided JSON schema, from given email message. \n"
                "REMEMBER to return extracted data only from provided email message."
            ),
        ),
        ChatMessage(
            role="user",
            content=(
                "Email Message: \n" "------\n" "{email_msg_content}\n" "------"
            ),
        ),
    ]
)


  1. Create an instance of the OpenAI model and the OpenAIPydanticProgram.
Plain Text
llm = OpenAI(model="gpt-3.5-turbo-1106")

program = OpenAIPydanticProgram.from_defaults(
    output_cls=EmailData,
    llm=llm,
    prompt=prompt,
    verbose=True,
)


  1. Call the program with the email content and print the output in JSON format.
Plain Text
output = program(email_msg_content=email_content)
print("Output JSON From .eml File: ")
print(json.dumps(output.dict(), indent=2))


Please note that you need to replace email_content with the actual content of the email you want to extract data from. Also, EmailData should be replaced with the actual schema you are using.

For more details, you can refer to the LlamaIndex documentation ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord