Dond
Dond

Reputation: 11

Problem when passing input parameters to Vercel edge function

I'm struggling to pass input parameters when invoking an edge function.

I'm developing an edge function that calls OpenAI API, but the parameters needed for doing so come from the client-side. The client-side invokes the edge function when a button is pressed in the UI. Part of my client-side code:

    const requestBody = { 
        model, 
        userMessage: JSON.stringify(userMessage), 
        extSystemMessage 
    };
    
    console.log("OAIAPImanager.js - Request Body for API Call:", JSON.stringify(requestBody));

    return fetch('/api/chatCompletion', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(requestBody)
    })

...

My edge function (/api/chatCompletion.js):

export const config = {
  runtime: 'edge',
};

async function chatCompletion(model, userMessage, extSystemMessage, useIntSystemMessage = true) {
    const apiKey = process.env.OAI_API_KEY;
    const maxTokens = parseInt(process.env.OAI_MAX_TOKENS, 10) || 512;
    const temperature = parseFloat(process.env.OAI_TEMPERATURE) || 0;

    let systemMessage = ....;

    const payload = {
        model: model,
        messages: [
            { role: "system", content: systemMessage },
            { role: "user", content: userMessage }
        ],
        max_tokens: maxTokens,
        temperature: temperature
    };

    const response = await fetch("https://api.openai.com/v1/chat/completions", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${apiKey}`
        },
        body: JSON.stringify(payload)
    });

    const json = await response.json();
    console.log("Response from OpenAI API:", json);

    if (response.ok) {
        return {
            message: json.choices[0].message.content,
            totalTokens: json.usage.total_tokens
        };
    } else {
        const errorDetails = JSON.stringify(json.error, null, 2);
        throw new Error(`API call failed with status ${response.status}: ${errorDetails}`);
    }
}

export default async (req, res) => {
    const { model, userMessage, extSystemMessage, useIntSystemMessage } = req.body;
    
    try {
        const result = await chatCompletion(model, userMessage, extSystemMessage, useIntSystemMessage);
        res.status(200).json({ message: result.message, totalTokens: result.totalTokens });
    } catch (error) {
        console.error("Error occurred:", error.toString());
        res.status(500).json({ error: 'An error occurred while completing chat', details: error.toString() });
    }
};

No parameter in the list (model, userMessage, extSystemMessage, useIntSystemMessage) is obtained in the edge function. If I change it to a normal serverless function, it correctly gets the info, but I cannot use serverless functions because of its low max execution time. What is wrong here?

Upvotes: 0

Views: 83

Answers (0)

Related Questions