Dynamically inputting context based on the convers...
# support
h
Hi there I'm trying to figure out a method to dynamically change a variables value based on the conversation. Explanation: What I mean by this is say our conversation looks something a little like this: User: Hi AI: Yo User: What's your refund policy? Now during this "speech-update" where the user stopped speaking I want to input into say {{relevant_context}} a query we get based on that speech update. So for example we would query the kb for the answer and input it directly into the system prompt through the variable. I have 2 questions about this: 1st is this possible? Changing the dynamic variables mid conversation. 2nd if that is possible, when I toggle "speech-update" when say "user" and "stopped" is true the last message is the AI's which means that the transcription of what the user just finished saying has not been passed into the data being sent to my server url. Is this something we could do? I am aware the latency may be slightly affected say 50-100ms but for me this is more than worth it.
v
Hi there! 👋 I understand you're looking to dynamically update context variables based on user speech during a conversation. Let me help with what I know! I can't confirm whether the transcription is available at the exact moment you described in your second question, but I can definitely help with your first question and provide some relevant information about accessing transcriptions. ## Dynamically Changing Variables Mid-Conversation Yes, it's absolutely possible to dynamically change variable values mid-conversation in Vapi! According to the information I found, there are two main methods to do this: ### Method 1: Using the Add Message API (Recommended)
Copy code
javascript
// Send a system message with updated context
const message = {
  type: 'add-message',
  message: {
    role: 'system',
    content: `Updated context: ${JSON.stringify(relevantContext)}`,
  },
  // Set to false to silently update without triggering a response
  triggerResponseEnabled: false  
};
### Method 2: Using Variable Substitution
Copy code
javascript
const assistant = {
  model: {
    messages: [
      {
        role: 'system',
        content: 'Relevant context: {{relevant_context}}'
      }
    ]
  }
};

// Update variables
const variables = {
  relevant_context: JSON.stringify(updatedContext)
};
Both methods allow for dynamic updates during an active conversation, and changes are properly tracked without requiring full context reloads [(source)](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1343457322072145950). ## Accessing Speech Transcriptions For accessing transcriptions during a call, you can: 1. Configure
serverMessages
to include "transcript":
Copy code
json
{
  "serverMessages": [
    "transcript",
    "end-of-call-report"
  ]
}
Your server will then receive real-time transcripts via webhooks: ```json { "message": { "type": "transcript", "transcript": "User's speech text", "role": "user", "transcriptType": "final" } }
``` [(source)](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1341440757189578782) 2. Use the
ClientMessageTranscript
event and check if
transcriptType
is "final" to get the complete transcript [(source)](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1260258706667343892). There is a note that the transcription might not be immediately available when the "speech-update" is triggered [(source)](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1334902474053652500), but with your acceptance of a slight latency, this approach should work for your use case. Is there anything specific about implementing this solution that you'd like me to elaborate on? Sources: - [Discord Thread about Dynamic Variable Updates](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1343457322072145950) - [Discord Thread about Accessing Transcriptions](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1341440757189578782) - [Discord Thread about ClientMessageTranscript Event](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1260258706667343892) - [Discord Thread about Transcription Availability](https://discord.com/channels/1211482211119796234/1211483291191083018/threads/1334902474053652500)
h
the add message api isn't really a thing its only for the web sdk
@User
I'm pretty much certain I could do this with a custom LLM but I wanted to basically know if it is possible natively within vapi.
Essentially right before sending a /chat/completions into whatever provider I'm using I would update the variable and then that updated system prompt would be what is being sent to the api.
k
You can use live call control to inject the messages live during the call based on server messages which you received on your server url.
h
Is the injection of the messages not what must be said. Rather than context in the system prompt.
k
You can turn off the trigger response by setting it to false, then it will be injected into the context instead of using it to generate the next response immediately.
j
@Shubham Bajaj Is there a way to update
variableValues
mid call? Or do those need to be set at the beginning of the call?
k
You can use live call control and your promptings to update this variable mid-call. It will be mostly for your prompting work, so I'll suggest to give it a try and let me know how it goes for you.