File size: 4,226 Bytes
55a034a 831f7e7 55a034a 831f7e7 55a034a 831f7e7 8725cc4 831f7e7 8725cc4 831f7e7 8725cc4 ac04e03 13a00bb 8725cc4 831f7e7 8725cc4 55a034a 8725cc4 55a034a 8725cc4 55a034a 8725cc4 55a034a 8725cc4 55a034a 8725cc4 55a034a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { Mistral } from "npm:@mistralai/mistralai";
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
};
const languagePrompts = {
en: {
systemPrompt: "You are playing a word guessing game in English. Given a descriptive sentence, your task is to guess the single word being described.",
instruction: "Based on this description, what single word is being described:"
},
fr: {
systemPrompt: "Vous jouez à un jeu de devinettes de mots en français. À partir d'une phrase descriptive, votre tâche est de deviner le mot unique décrit.",
instruction: "D'après cette description, quel mot unique est décrit :"
},
de: {
systemPrompt: "Sie spielen ein Worträtselspiel auf Deutsch. Anhand eines beschreibenden Satzes ist es Ihre Aufgabe, das beschriebene einzelne Wort zu erraten.",
instruction: "Welches einzelne Wort wird basierend auf dieser Beschreibung beschrieben:"
},
it: {
systemPrompt: "Stai giocando a un gioco di indovinelli in italiano. Data una frase descrittiva, il tuo compito è indovinare la singola parola descritta.",
instruction: "In base a questa descrizione, quale singola parola viene descritta:"
},
es: {
systemPrompt: "Estás jugando a un juego de adivinanzas de palabras en español. Dada una frase descriptiva, tu tarea es adivinar la única palabra que se describe.",
instruction: "Según esta descripción, ¿qué palabra única se está describiendo:"
}
};
serve(async (req) => {
if (req.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
try {
const { sentence, language = 'en' } = await req.json();
console.log('Trying to guess word from sentence:', sentence, 'language:', language);
const client = new Mistral({
apiKey: Deno.env.get('MISTRAL_API_KEY'),
});
const prompts = languagePrompts[language as keyof typeof languagePrompts] || languagePrompts.en;
const maxRetries = 3;
let retryCount = 0;
let lastError = null;
while (retryCount < maxRetries) {
try {
const response = await client.chat.complete({
model: "mistral-large-latest",
messages: [
{
role: "system",
content: `${prompts.systemPrompt} Respond with ONLY the word you think is being described, in uppercase letters. Do not add any explanation or punctuation.`
},
{
role: "user",
content: `${prompts.instruction} "${sentence}"`
}
],
maxTokens: 50,
temperature: 0.1
});
const guess = response.choices[0].message.content.trim().toUpperCase();
console.log('AI guess:', guess);
return new Response(
JSON.stringify({ guess }),
{ headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
);
} catch (error) {
console.error(`Attempt ${retryCount + 1} failed:`, error);
lastError = error;
if (error.message?.includes('rate limit') || error.status === 429) {
const waitTime = Math.pow(2, retryCount) * 1000;
console.log(`Rate limit hit, waiting ${waitTime}ms before retry`);
await new Promise(resolve => setTimeout(resolve, waitTime));
retryCount++;
continue;
}
throw error;
}
}
throw new Error(`Failed after ${maxRetries} attempts. Last error: ${lastError?.message}`);
} catch (error) {
console.error('Error generating guess:', error);
const errorMessage = error.message?.includes('rate limit')
? "The AI service is currently busy. Please try again in a few moments."
: "Sorry, there was an error generating the guess. Please try again.";
return new Response(
JSON.stringify({
error: errorMessage,
details: error.message
}),
{
status: error.message?.includes('rate limit') ? 429 : 500,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
}
);
}
}); |