CoPhilot
The fallback topic
The fallback topic triggers when the orchestration engine has detected nothing. Counterintuitively in this solution it is our core topic.
As we have not created any custom topic yet, all we are doing is forcing the user to keep looping over and over in the fallback topic, building the conversation recursively.
All we do is save the last user message in our Conversation table, call the assistant API, save the assistant answer and display it in the chat and it keeps going.



Calling Power Automate to create a new user or assistant message
All we are doing in this flow is saving the last user or assistant message in our Conversation and Message tables.
- WhoSystem is either user or assistant
- WhoUser is the actual name of the user or the assistant
- saidWhat is what the user or assistant said
- SystemPrompt is the log of the system prompt if it is an assistant message
- ConversationId is the internal Dataverse record Id of our current conversation


Here are the formulas I used when adding a record to the Message table:
- When is the ‘name’ of the record that we will sort the messages by
formatDateTime(convertTimeZone(utcNow(),'UTC','Eastern Standard Time'),'HH:mm tt ss')
- Said What is what the user or assistant just said
replace(triggerBody()?['text_2'],'"','')
The first important thing to notice here is how to save a look up field in Power Automate with a Dataverse action. Make sure to use the internal name of the table you are pointing to, including the ‘s’ and in parathesis the record id (here ConversationId)
Replace function is just a ‘trick’. We are in effect removing all the double quote from the message as the parseJSON action in Power Automate doesn’t understand easily double quote (there must be a way to figure it out, maybe in more pro code fashion, but have not figure it out yet).
Once we saved the message table, the rest of the flow is building the JSON message table and the user readable conversation history.


Here is the ‘replace’ PowerFx formula:
replace(replace(triggerBody()?['text_2'],'"',''),'\','')



Calling Power Automate to chat with the model

We use this flow to call whatever Gen AI problem. We use a condition to fork depending on if the model is OpenAI or not. The API call to those models is often the same, the headers can be different and not everyone understand the concept of system prompt.
Below is the “OpenAI” detail flow. For Phi 3 and Llama 3.1 use Bearer authentication and switch the ‘system’ role to a ‘user’ role. We add the system prompt and parse our JSON message table, ready to be send to the model.

Here is the ‘replace’ formula:
replace(triggerBody()?['text_1'],'"','')
Here is the Schema:
{
"type": "array",
"items": {
"type": "object",
"properties": {
"role": {
"type": "string"
},
"content": {
"type": "string"
}
},
"required": [
"role",
"content"
]
}
}