web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Copilot Studio - Bot Extensibility
Answered

Retain conversation context when interacting with an Azure OpenAI ChatGPT model

(7) ShareShare
ReportReport
Posted on by

I previously shared how to maintain past context when interacting with an OpenAI GPT model, by setting and updating a global variable using Bot Framework Composer, here: Set and update global variables using Bot Framework Composer and how to retain conversation context when interacting with OpenAI GPT models.

 

Today I'd like to show how to do the same, but this time using Azure OpenAI ChatGPT model that just came out.

I find that model much better suited for conversation experiences and automatically formats code samples:

 

2023-03-14 15-41-50-815 (1)_ChatGPT_Bot__Microsoft_Teams_-_Charlie_Guibord.gif

 

Couple of useful resources on this topic:

Categories:
I have the same question (0)
  • Verified answer
    HenryJammes Profile Picture
    on at
    Re: Retain conversation context when interacting with an Azure OpenAI ChatGPT model

    I'm going to use the Fallback topic so that I can use Power Automate to query the Azure OpenAI ChatGPT model (note: you can also make HTTP requests directly in Bot Framework Composer) whenever PVA doesn't find a matching topic for a user utterance.

     

    It's good to understand Chat Markup Language to understand how to scope the conversation and provide instructions to ChatGPT prior to it returning an answer. That way, you can give personality traits to the bot, rules to follow when providing answers, and data or information needed for the model.

     

    Here's an example:

     

     

    <|im_start|>system
    Assistant is an intelligent chatbot designed to help users answer their tax related questions.
    <|im_end|>
    <|im_start|>user
    When do I need to file my taxes by?
    <|im_end|>
    <|im_start|>assistant
    In 2023, you will need to file your taxes by April 18th. The date falls after the usual April 15th deadline because April 15th falls on a Saturday in 2023. For more details, see https://www.irs.gov/filing/individuals/when-to-file
    <|im_end|>
    <|im_start|>user
    How can I check the status of my tax refund?
    <|im_end|>
    <|im_start|>assistant
    You can check the status of your tax refund by visiting https://www.irs.gov/refunds
    <|im_end|>

     

     

     

    For our example, I'll use a very basic system message that you can customize

     

     

    <|im_start|>system
    Assistant helps Contoso employees with their questions.
    <|im_end|>

     

     

     

    The idea for our PVA ChatGPT bot is to keep the full conversation when sending new prompts to the ChatGPT API.

     

    To manage a global variable keeping track of the conversation with Azure OpenAI ChatGPT, I'm going to use Bot Framework Composer to do 2 things:

    1. Create an InitializeFullDialog dialog, that will create and set a new variable, FullDialog, in Power Virtual Agents, that will initially contain the system message.
    2. Create a SetFullDialog dialog, that will update the FullDialog variable so that it contains the history of interactions (past questions and answers provided by Azure OpenAI ChatGPT).

     

    I start by opening the Bot Framework Composer

    If you need help setting things up, refer to this: Getting started with Bot Framework Composer - Power Virtual Agents | Microsoft Learn

    HenryJammes_0-1678805803245.png

     

     

    I then add a new dialog to my chatbot

    HenryJammes_1-1678805827663.png

     


    I call it "InitializeFullDialog"

    I create one new Output for it, that I call FullDialog, of type string:

    HenryJammes_16-1678807435124.png

     

     

    In BeginDialog, I add a Manage properties > Set a property node, in order set the FullDialog variable to my system message as well as the necessary chat markup language before I add the user utterence:

    Property: dialog.result.FullDialog
    Value: ='<|im_start|>system\nAssistant helps Contoso employees with their questions.\n<|im_end|>\n<|im_start|>user\n'

    HenryJammes_5-1678806004621.png

     

    I then add another dialog, SetFullDialog, this time with no outputs.

    In the BeginDialog, I add a Manage properties > Set a property node, in order set the Power Virtual Agents FullDialog variable as a concatenation of the previous FullDialog value as well as new values that Power Automate pass me back (I'm showing this later in this article, but they're OK to set up in advance) and some chat markup language to piece it together:

    Property: virtualagent.ConversationFullDialog
    Value: =concat(virtualagent.FullDialog,virtualagent.OriginalTriggerPhrase,'\n<|im_end|>\n<|im_start|>assistant',virtualagent.ChatGPTResponse,'\n<|im_end|>\n<|im_start|>user\n')

    HenryJammes_6-1678806103110.png

     

    Then I publish my bot in Bot Framework Composer:

    HenryJammes_17-1678807510951.png

     

     

    As I interact in this example with the Azure OpenAI ChatGPT API, I'm using the Power Virtual Agents Fallback topic. This means that whenever a user asks a question that PVA doesn't know how to answer with an existing topic (i.e. there is no match with the topics trigger phrases), it goes to the Fallback topic, and in the Fallback topic I'm calling the Azure OpenAI ChatGPT API.

    If you haven't enabled the Fallback topic in your chatbot, learn how here: Use a system fallback topic - Power Virtual Agents | Microsoft Learn

     

    In the Fallback topic, I start by removing the existing nodes as I'm changing its behavior.

    Just after the 'Trigger Phrases' node, I add a "Redirect to another topic" node, and select the InitializeFullDialog dialog.

    I click on the variable name, name it FullDialog, and change its usage to "Bot (any topic can access)" and also check "External sources can set values":

    HenryJammes_8-1678806329427.png

     

     

    Next step is to call the Azure OpenAI ChatGPT API using Power Automate.

    I add a new node, select Call an action > Create a flow.

    I assume you already have an Azure OpenAI account and an API key, but if you don't, check these:

    In your cloud flow, you'll first want to add 2 text inputsUnrecognizedTriggerPhrase and ConversationFullDialog:

    HenryJammes_9-1678806480510.png

     

     

    Then, to call the Azure OpenAI GPT API, I use the HTTP action, with this configuration

    Method: POST

    URI: you can get the URI from the ChatGPT playground in the Azure OpenAI Studio, in view code, in curl

    HenryJammes_11-1678806804562.png

     

    Headers: 

    api-key: {YourAPIKey}
    Content-Type: application/json

    Body:

    {
    "prompt": "{ExpressionBelow}",
    "temperature": 0.7,
    "top_p": 0.95,
    "frequency_penalty": 0,
    "presence_penalty": 0,
    "max_tokens": 800,
    "stop": [
    "<|im_end|>"
    ]
    }

    For the prompt, this is where I need to pass the user question.

    To keep context, the trick is to provide past questions and answers, as well as the new question.

    So, I append the FullConversation with the UnrecognizedTriggerPhrase and a bit of chat language markup to let it know I expect a response from the bot.:

    concat(
    triggerBody()['text_1'],
    triggerBody()['text'],
    '\n<|im_end|>\n<|im_start|>assistant'
    )

    HenryJammes_10-1678806645985.png

     

     

    Now I need to pass back a few things to PVA.

    • The ChatGPTResponse is the response from the ChatGPT API.
      This is the expression I'm using:
      body('HTTP:_Azure_OpenAI_ChatGPT')?['choices'][0]?['text']
    • The OriginalTriggerPhrase - that's the individual question that was raised in this flow, that I also need to append to the ConversationFullDialog variable. I use the UnrecognizedTriggerPhrase value in dynamic content.

    HenryJammes_12-1678807002343.png

     

    I give my flow a name, and  save it.

     

    Back in PVA, still in my Fallback topic, I select my flow, and can map the UnrecognizedTriggerPhrase and FullDialog to the Power Automate inputs.

    I can see that Power Automate has 2 ouputs. I set their Usage to "Bot (any topic can access)":

    HenryJammes_13-1678807213198.png

     

     

    I now add a new Show a message node, where I select the bot.ChatGPTResponse variable:

    HenryJammes_14-1678807258616.png

     

     

    The final step is to add a "Redirect to another topic" node, and select the SetFullDialog dialog, that will take care of updating the FullDialog variable with the past questions and answers.

     

    HenryJammes_15-1678807275597.png

     

     

    And that's it! 🎉

  • Verified answer
    MattJimison Profile Picture
    577 Most Valuable Professional on at
    Re: Retain conversation context when interacting with an Azure OpenAI ChatGPT model

    This is awesome, @HenryJammes !

     

    I just went through a very similar exercise, but am using Unified Canvas instead, so I thought I'd share the code view of my Fallback topic, since it's a breeze copy/pasting a topic in the Unified Canvas! 😀

     

    A few notes to get it set up:

    1. disable / remove any unnecessary existing topics to avoid triggering them if your intent is to handle all requests via ChatGPT
    2. The Power Automate Flow is the same as the example shown above with Bot Composer except I'm not passing the user's query into the flow and back out since I'm just concatenating with that value in the same topic.
    3. You'll need to get the ID of your flow in your environment and paste it into the part below where I've entered an empty Guid (all 0's) You could also manually recreate this part in the UI if desired
    4. Just open up your fallback topic, go to code view, replace the Flow Id as noted above, select all text, delete, and copy in the below

     

     

     

    kind: AdaptiveDialog
    beginDialog:
     kind: OnUnknownIntent
     id: main
     actions:
     - kind: SetVariable
     id: setVariable_Kyy1Yh
     variable: Topic.UserQuery
     value: =System.Activity.Text
    
     - kind: ConditionGroup
     id: conditionGroup_Nuj40I
     conditions:
     - id: conditionItem_iRCr9Y
     condition: =IsBlank(Global.FullConversation)
     actions:
     - kind: SetVariable
     id: 36cfgS
     variable: Global.FullConversation
     value: <|im_start|>system\nI am a virtual assistant that can answer questions\n<|im_end|>\n<|im_start|>user\n
    
     - kind: InvokeFlowAction
     id: invokeFlowAction_Ho6dKr
     input:
     binding:
     text: =Topic.UserQuery
     text_1: =Global.FullConversation
    
     output:
     binding:
     response: Topic.Response
    
     flowId: 00000000-0000-0000-0000-000000000000
    
     - kind: SendMessage
     id: sendMessage_UgHog9
     message: "{Topic.Response}"
    
     - kind: SetVariable
     id: setVariable_0LLSEn
     variable: Global.FullConversation
     value: =Concatenate(Global.FullConversation,Topic.UserQuery,"\n<|im_end|>\n<|im_start|>assistant", Topic.Response, "\n<|im_end|>\n<|im_start|>user\n")

     

     

     

  • angerfire1213 Profile Picture
    90 on at
    Re: Retain conversation context when interacting with an Azure OpenAI ChatGPT model

    How to do that for GPT-35-Turbo (preview) & GPT-4 (preview)   ?

    In GPT-35-Turbo (preview) & GPT-4 (preview) use messages not prompt

    messages=[ {"role": "system", "content": "Assistant is a large language model trained by OpenAI."}, {"role": "user", "content": "What's the difference between garbanzo beans and chickpeas?"}, ]

     

    learn more about GPT-35-Turbo (preview) & GPT-4 (preview):

    https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/chatgpt?pivots=programming-language-chat-completions#working-with-the-chatgpt-and-gpt-4-models-preview

     

  • HenryJammes Profile Picture
    on at
    Re: Retain conversation context when interacting with an Azure OpenAI ChatGPT model

    Hi @angerfire1213 the example above was created on the GPT 3.5 Turbo model with Chat Markup Language and works.

    I haven't tried the Chat Completion format yet, and GPT-4 might require that format, so the above approach would indeed need reworking to meet the new JSON format requirements, but the approach should be the same.

    I'll try to update or create a new article once I get access to GPT-4.

  • angerfire1213 Profile Picture
    90 on at
    Re: Retain conversation context when interacting with an Azure OpenAI ChatGPT model

    Thank you ! Hope you got Azure OpenAI GPT-4 soon...

  • angerfire1213 Profile Picture
    90 on at
    Re: Retain conversation context when interacting with an Azure OpenAI ChatGPT model

    @HenryJammes   Let me show you our different about our OpenAI chatGPT trubo

    Screenshot 2023-03-31 at 10.22.33 AM.png

     

     

  • angerfire1213 Profile Picture
    90 on at
    Re: Retain conversation context when interacting with an Azure OpenAI ChatGPT model

    I already use the same way to use azure openai gpt-4. thank you!

  • angerfire1213 Profile Picture
    90 on at
    Re: Retain conversation context when interacting with an Azure OpenAI ChatGPT model

    now It's worked good in our prod env.

    our PVA in teams version bot face to about 1000+ users

  • HenryJammes Profile Picture
    on at
    Re: Retain conversation context when interacting with an Azure OpenAI ChatGPT model

    FYI I've created a new version of this article using the Chat Completion API format, in both the classic and new version of Power Virtual Agents:

    Integrate a PVA chatbot with Azure OpenAI ChatGPT using the Chat Completion API format

  • minakshimathpal Profile Picture
    32 on at
    Re: Retain conversation context when interacting with an Azure OpenAI ChatGPT model

    @HenryJammes How can this be done in MS copilot studio . I want my bot to maintain the context of the conversation, however currently it is not.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Tom Macfarlan – Community Spotlight

We are honored to recognize Tom Macfarlan as our Community Spotlight for October…

Leaderboard > Copilot Studio

#1
Romain The Low-Code Bearded Bear Profile Picture

Romain The Low-Code... 500 Super User 2025 Season 2

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 478 Super User 2025 Season 2

#3
DAnny3211 Profile Picture

DAnny3211 132

Last 30 days Overall leaderboard

Featured topics