llama_index
package. You'll need OpenAIPydanticProgram
, ChatPromptTemplate
, ChatMessage
, and OpenAI
.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
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.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" "------" ), ), ] )
OpenAI
model and the OpenAIPydanticProgram
.llm = OpenAI(model="gpt-3.5-turbo-1106") program = OpenAIPydanticProgram.from_defaults( output_cls=EmailData, llm=llm, prompt=prompt, verbose=True, )
output = program(email_msg_content=email_content) print("Output JSON From .eml File: ") print(json.dumps(output.dict(), indent=2))
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.