Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Experimenting with Email generation and summarization with Node.js and Large Language Models

November 8, 2024
Lucas Holmquist
Related topics:
Artificial intelligenceNode.js
Related products:
Red Hat build of Node.js

    Welcome back to this ongoing series of posts about using Large Language Models(LLMs) with Node.js.  In the previous post we took a look at creating and using a LLM chat bot with Node.js.  We showed it in the context of a fictitious claims management for an insurance company.  

    This next post will take a look at adding a feature that will help generate an email summarization as well as making sure the data returned from our LLM is structured as JSON.

    Summarization Prompting

    As a small refresher, the example application was a mock claims management system that an insurance company might use to service their claims.  The previous post added a chatbot where the claims adjuster could ask it questions about the information in the claims summaries.

    This post will add a new feature that will direct our LLM to write an email response that they could then send to the insured regarding their claim.  

    Instructions for running this part of the example is in its own feature branch here

    The UI has a new side tab called Email Generate and is just a TextArea to input the original customer correspondence.  

    Parasol Insurance Application Email Generator

    When that is submitted, a new prompt that we will take a look at in a second,  will send that to our LLM to produce a JSON response that we can send back to the front-end.  The front-end expects that our JSON object has both a subject and message property.  The final response on the UI could look something like this:

     

    Parasol Insurance Application Email Generator Result

     

    The prompt that we use to generate the response is fairly simple, so let’s take a look at what that:

     

    const prompt = ChatPromptTemplate.fromTemplate(`
          You are a helpful, respectful and honest assistant named Parasol Assistant.
          You work for Parasol Insurance
          Formatting Instructions: {format_instructions}
          User Input: {input}
      `);

    The prompt is pretty simple.  In this example we are using the basic fromTemplate method(Last post we used a little more advanced method).  The first two lines are also very simple, instructing the LLM that it should think of itself as someone who works for our company.

    The next line we will cover in the next section, but this where we will tell our LLM that we want a more structured response, like JSON.

    The final line is a placeholder for the input coming from the form on the UI

    Formatting Instructions

    In the chatbot example, you might remember that our response was accessed through the content parameter of an object that langchain.js creates.  While this might be ok to use, we can actually instruct the LLM to respond in a more structured way.  

    Langchain.js provides a few different output parsers for developers to use.  This is nice, because maybe in your own application the AI portion might need to pass its response off to another service, like a REST endpoint and having the LLM format the response in that services format can reduce code.

    The object format that our front-end expects is a JSON object with the two properties, subject and message.  Something like this:

    {
      subject:'',
      message: ''
    }

    For this, we use the langchain.js class StructedOutputParser.  It contains two methods for defining our object schema, and for this example we are using the one that allows our definition to be more advanced.  This is the fromZodSchema

    Just as a quick aside, Zod, while being a Superman villain, is also a popular TypeScript-first schema declaration and validation library.   

    Below is the code that is used to define our objects schema:

    // Setup the output parser using the Zod Schema
    
    const outputParser = StructuredOutputParser.fromZodSchema(
        z.object({
          subject: z.string().describe('Subject of your response, suitable to use as an email subject line.'),
          message: z.string().describe('Response text that summarizes the information they gave, and asks for any other missing information needed from Parasol.')
        })
      );

    Here we have our two properties and we describe what type they should be, in this case strings, and we also describe what they are.  For example, we define our message property saying that it should be some sort of response text that summarizes the information they gave, and asks for any other missing information needed from Parasol.  I’d like to call out the ask for any other missing information line.  In our screenshot earlier showing the result, our response was asking for a number of items, so that part worked!

    Piecing it Together

    It is now time to piece everything together.  Similar to our chatbot, we create a chain and the only difference this time is that we add another pipe method to the end that includes the output parser that was just created:

    const chain = prompt.pipe(model).pipe(outputParser);

    Finally we can invoke this chain to get the response.  In the prompt we left placeholders for both the user's input as well as the formatting instructions, which needs to be passed to the chain.  This looks something like this:

     

    return await chain.invoke({
        input: userResponse,
        format_instructions: outputParser.getFormatInstructions()
      });

     

    In this example, the invoke method is used to get the response instead of the streaming method that was used in the chatbot example in the previous post.  Once this is invoked, the LLM should provide a result similar to this console.log that I did:

    {
      subject: 'Claim Regarding Vehicle Accident - Dominic Toretto',
      message: 'Dear Mr. Toretto,\n' +
        '\n' +
        'Thank you for reaching out to Parasol Insurance regarding your recent vehicle accident. We appreciate your detailed account of the circumstances surrounding the incident.\n' +
        '\n' +
        'To expedite your claim process, please provide us with the following information:\n' +
        '\n' +
        '1. Date and time of the accident\n' +
        '2. Police report number (if any)\n' +
        '3. Contact information for witnesses (if any)\n' +
        '4. Photos or videos of the damaged vehicle\n' +
        '5. Your policy number and any other relevant policy details\n' +
        '\n' +
        'Once we have this information, we can proceed with processing your claim promptly. If you have any questions or concerns, please do not hesitate to contact us.\n' +
        '\n' +
        'Looking forward to working with you.\n' +
        '\n' +
        'Best regards,\n' +
        'Parasol Insurance Team'
    }
    

    This is then passed back to the UI and properly formatted on screen.

    Conclusion

    While this post was a bit shorter than the first post, it does show how simple returning structured output from the LLM can be.  

    The next post in the series will take a look at how to give your LLM more little more context when answering questions using the Retrieval Augmented Generation (RAG) concept.

    As always if you want to learn more about what the Red Hat Node.js team is up to check these out:

    https://developers.redhat.com/topics/nodejs

    https://developers.redhat.com/topics/nodejs/ai

    https://github.com/nodeshift/nodejs-reference-architecture

    https://developers.redhat.com/e-books/developers-guide-nodejs-reference-architecture

    Disclaimer: Please note the content in this blog post has not been thoroughly reviewed by the Red Hat Developer editorial team. Any opinions expressed in this post are the author's own and do not necessarily reflect the policies or positions of Red Hat.

    Recent Posts

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.