-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clarified that the application will define a limited monthly token usage for all users. [skip gpt_engineer]
- Loading branch information
1 parent
af48674
commit 848918f
Showing
1 changed file
with
19 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { serve } from "https://deno.land/[email protected]/http/server.ts" | |
import "https://deno.land/x/[email protected]/mod.ts" | ||
|
||
const openAIApiKey = Deno.env.get('OPENAI_API_KEY') | ||
const MONTHLY_TOKEN_LIMIT = 100000 // Limite de 100k tokens par mois | ||
|
||
const corsHeaders = { | ||
'Access-Control-Allow-Origin': '*', | ||
|
@@ -21,13 +22,29 @@ serve(async (req) => { | |
throw new Error('OpenAI API key not configured') | ||
} | ||
|
||
// If it's a title generation request, use a specific system prompt | ||
// Si c'est une génération de titre, on utilise un prompt système spécifique | ||
const systemPrompt = type === 'title-generation' | ||
? "Tu es un assistant qui génère des titres courts et concis (maximum 5 mots) pour des conversations. Réponds uniquement avec le titre, sans ponctuation ni guillemets." | ||
: "Tu es un assistant pédagogique français qui aide les utilisateurs à apprendre et à comprendre des concepts. Tu es amical et encourageant." | ||
|
||
console.log('Calling OpenAI API with message:', message) | ||
|
||
// Calculer une estimation approximative des tokens pour ce message | ||
const estimatedTokens = Math.ceil((message.length + systemPrompt.length) / 4) | ||
|
||
// Vérifier si le message dépasserait la limite mensuelle | ||
if (estimatedTokens > MONTHLY_TOKEN_LIMIT) { | ||
return new Response( | ||
JSON.stringify({ | ||
error: "Limite mensuelle de tokens dépassée", | ||
details: "Vous avez atteint votre limite mensuelle de tokens. Réessayez le mois prochain ou passez à un forfait supérieur." | ||
}), { | ||
status: 429, | ||
headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | ||
} | ||
) | ||
} | ||
|
||
const response = await fetch('https://api.openai.com/v1/chat/completions', { | ||
method: 'POST', | ||
headers: { | ||
|
@@ -41,7 +58,7 @@ serve(async (req) => { | |
{ role: 'user', content: message } | ||
], | ||
temperature: 0.7, | ||
max_tokens: 1000, | ||
max_tokens: Math.min(1000, MONTHLY_TOKEN_LIMIT - estimatedTokens), // S'assurer de ne pas dépasser la limite | ||
}), | ||
}) | ||
|
||
|