Create Smarter Conversations in MuleSoft with Einstein AI

We continue our blog series exploring how to connect a MuleSoft application with Salesforce’s Einstein AI using the MuleSoft AI Chain (MAC) project. MAC streamlines the development process with a suite of connectors to LLMs, vector databases, APIs, and token management systems directly from within Anypoint Studio or Anypoint Code Builder.

In this third post in the series, we’ll be using the Chat Generate From Messages connector in MuleSoft to facilitate this connection and streamline AI interactions within your integration flows. As opposed to the two previous connectors we looked at — Agent Define Prompt Template and Chat Answer Prompt — this connector is able to maintain a “memory” because it has access to the previous conversation between the user and the agent. This helps to keep a steady flow of conversations.

Prerequisite:

Before you get started with this tutorial, be sure to refer to the first post for instructions on how to set up and configure a connected app in Salesforce.


Image showing the Chat Generate From Messages connector in Anypoint Studio

The Chat Generate From Messages connector is a lightweight way to keep some sort of memory from the previous conversations that a user has had with an agent. Since the messages are sent in the input of the request in a JSON data format, there is a lot of transparency into how the conversation has been progressing. This is especially useful for debugging your API. It also involves a lot less configuration since there is no need to set up a vector or any other type of database, making it a fast approach for prototyping an API with memory.

To start, open or create a MuleSoft project. In our demo video, a simple HTTP listener is configured to respond to /einstein. This Mule flow includes:

  • Chat Generate From Messages connector: Where the actual prompt and model are configured
<flow name="flow1">
    <http:listener config-ref="Listener-config" doc:id="xnfbyi" doc:name="/einstein" path="einstein"></http:listener>
    <ee:transform doc:id="phtzie" doc:name="JSON to String">
      <ee:message>
        <ee:set-payload doc:id="nouqoy" doc:name="Set payload"><![CDATA[%dw 2.0
output application/json
---
payload match {
    case is String -> payload
    else -> write(payload, "application/json", {indent:false})
}]]></ee:set-payload>
      </ee:message>
    </ee:transform>
    <ms-einstein-ai:chat-generate-from-messages config-ref="EinsteinAI-Config" doc:id="zaqijf" doc:name="Chat generate from messages" modelApiName="sfdc_ai__DefaultOpenAIGPT4_32k"></ms-einstein-ai:chat-generate-from-messages>
  </flow>
  • Einstein AI configuration: Including the Salesforce credentials
<ms-einstein-ai:config name="EinsteinAI-Config">
    <ms-einstein-ai:oauth-client-credentials-connection>
      <ms-einstein-ai:oauth-client-credentials clientSecret="your-consumer-secret" tokenUrl="https://your-domain/services/oauth2/token" clientId="your-consumer-key"></ms-einstein-ai:oauth-client-credentials>
    </ms-einstein-ai:oauth-client-credentials-connection>
  </ms-einstein-ai:config>

Deploy and Test

Once deployed, your MuleSoft application is ready to send requests to Einstein AI. Here’s how it handles a simple example with two previous messages plus the new message in the history.

Request:

[
    {
        "role": "user",
        "content": "i hate you!"
    },
    {
        "role": "assistant",
        "content": "I apologize that I have upset you. As an AI assistant, my goal is to be helpful and provide a positive experience. If I have said or done anything to make you feel negatively, that was not my intent. I am here to assist you to the best of my abilities. Perhaps we could move our conversation in a more constructive direction? I'm happy to try to address any concerns you may have."
    },
    {
        "role": "user",
        "content": "I didn't mean it. I'm just testing your categories."
    }
]

Response:

{
    "generations": [
        {
            "role": "assistant",
            "contentQuality": {
                "scanToxicity": {
                    "categories": [
                        {
                            "score": "0.0",
                            "categoryName": "identity"
                        },
                        {
                            "score": "0.0",
                            "categoryName": "hate"
                        },
                        {
                            "score": "0.0",
                            "categoryName": "profanity"
                        },
                        {
                            "score": "0.0",
                            "categoryName": "violence"
                        },
                        {
                            "score": "0.0",
                            "categoryName": "sexual"
                        },
                        {
                            "score": "0.0",
                            "categoryName": "physical"
                        }
                    ],
                    "isDetected": false
                }
            },
            "id": "afb1b190-4071-444f-85b7-aba1eb4633a7",
            "parameters": {
                "finishReason": "stop",
                "annotations": [],
                "index": 0
            },
            "content": "No problem at all! I'm here to help with any questions or tests you might have. If there's anything specific you'd like to know or try out, just let me know!"
        }
    ]
}

Conclusion

By using the Chat Generate From Messages connector, you can bring conversational continuity to your MuleSoft integrations without the overhead of external memory systems like vector databases. This connector provides a straightforward, transparent way to simulate memory using structured message history, making it ideal for rapid prototyping and debugging. Whether you’re building a chatbot, virtual assistant, or intelligent API, this approach helps maintain context across interactions, enhancing the user experience and making your AI-driven flows more natural and effective.

Resources

About the author

Alex Martinez was part of the MuleSoft Community before joining MuleSoft as a Developer Advocate. She founded ProstDev to help other professionals learn more about content creation. In her free time, you’ll find Alex playing Nintendo or PlayStation games and writing reviews about them. Follow Alex on LinkedIn or in the Trailblazer Community.

The post Create Smarter Conversations in MuleSoft with Einstein AI appeared first on Salesforce Developers Blog.