Agent speaks continuously even with stale requests...
# support
v
Context This is my simple endpoint for a custom llm:
Copy code
typescript
app.post("/chat/completions", async (req: Request, res: Response) => {
    req.on("close", () => {
        res.end();
    });
    Speak(res, uuidv4(), "Hello, how are you doing today? I'm doing fantastic - it's such a beautiful day outside");
    res.end();
});
Speak()
is straightforward:
Copy code
typescript
function Speak(res: Response, id: string, content: string, end = false) {
    const resp: OpenAI.ChatCompletionChunk = {
        id,
        object: "chat.completion.chunk",
        created: moment().unix(),
        model: "gpt-4o",
        system_fingerprint: undefined,
        choices: [
            { index: 0, delta: { role: "assistant", content }, logprobs: null, finish_reason: end ? "stop" : null },
        ],
    };
    res.write(`data: ${JSON.stringify(resp)}\n\n`);
    res.write("data: [DONE]\n\n");
}
Problem Vapi can ping multiple times for a user response. ie
Yeah, let's do it
will ping once for
Yeah
and once for
Yeah, let's do it
. This hits my endpoint two times which leads to the agent speaking both responses one after another. Expected Behavior The agent should cut off the stale request speech and only speak from the latest request. Data Call ID: b79b1b8c-cb49-4241-92bd-f22822b77a6d
v
message has been deleted
@VR To address the issue where Vapi pings multiple times for a user response, causing the agent to speak responses for each ping, you can implement a mechanism to track the latest request and ignore the stale ones. Here's a strategy you could use: 1. Maintain a variable that stores the ID of the latest request. 2. When a new request comes in, update this variable with the new request ID. 3. Before processing a request to speak, check if the request ID matches the latest request ID stored. 4. If it does not match, it means the request is stale, and you should not proceed with the
Speak()
function. 5. If it matches, proceed with the
Speak()
function. Here's how you could modify your endpoint to include this logic:
Copy code
typescript
let latestRequestId: string = "";

app.post("/chat/completions", async (req: Request, res: Response) => {
    const requestId = uuidv4();
    latestRequestId = requestId; // Update the latest request ID

    req.on("close", () => {
        res.end();
    });

    // Check if the current request ID is the latest before speaking
    if (requestId === latestRequestId) {
        Speak(res, requestId, "Hello, how are you doing today? I'm doing fantastic - it's such a beautiful day outside");
    }

    res.end();
});
This should ensure that only the response to the latest request is spoken by the agent. If you encounter any further issues or need more assistance, please let me know!
You can continue this conversation by mentioning me in the message.
Got it. Tagging @User for additional help.
s
call_id pls
v
@Sahil Call ID: b79b1b8c-cb49-4241-92bd-f22822b77a6d 🙏
s
This is a bug from our side, and what you can do is to act as a stateless machine and just respond back.
v
Oh, what do you mean by acting as a stateless machine and responding back?
s
stateless means not having a memory, which means your task is simply to respond to all requests.
2 Views