11const axios = require ( "axios" ) ;
22
33module . exports = async function ( context , req ) {
4- // ✅ Log incoming request method
4+
55 context . log ( "🔹 Received request:" , req . method ) ;
66
7- // ✅ Handle CORS Preflight (OPTIONS Request)
7+
88 if ( req . method === "OPTIONS" ) {
99 context . log ( "🔹 Handling OPTIONS preflight request." ) ;
1010 context . res = {
11- status : 204 , // No content response
11+ status : 204 ,
1212 headers : {
1313 "Access-Control-Allow-Origin" : "*" ,
1414 "Access-Control-Allow-Methods" : "GET, POST, OPTIONS" ,
@@ -18,15 +18,15 @@ module.exports = async function (context, req) {
1818 return ;
1919 }
2020
21- // ✅ Default CORS Headers for every response
21+
2222 const corsHeaders = {
2323 "Content-Type" : "application/json" ,
2424 "Access-Control-Allow-Origin" : "*" ,
2525 "Access-Control-Allow-Methods" : "GET, POST, OPTIONS" ,
2626 "Access-Control-Allow-Headers" : "Content-Type, Authorization"
2727 } ;
2828
29- // ✅ Check if the request has a body
29+
3030 if ( ! req . body ) {
3131 context . log ( "❌ Request body is missing!" ) ;
3232 context . res = { status : 400 , body : { error : "Request body is missing!" } , headers : corsHeaders } ;
@@ -44,7 +44,7 @@ module.exports = async function (context, req) {
4444 }
4545
4646 try {
47- // Step 1: Detect Language Using OpenAI
47+
4848 const languageResponse = await axios . post (
4949 `${ process . env . OPENAI_ENDPOINT } /openai/deployments/gpt-4o/chat/completions?api-version=2024-02-15-preview` ,
5050 {
@@ -61,7 +61,7 @@ module.exports = async function (context, req) {
6161 const detectedLanguage = languageResponse . data . choices [ 0 ] . message . content . trim ( ) ;
6262 let translatedPrompt = prompt ;
6363
64- // Step 2: Translate Non-English Text to English
64+
6565 if ( detectedLanguage !== "English" ) {
6666 const translationResponse = await axios . post (
6767 `${ process . env . OPENAI_ENDPOINT } /openai/deployments/gpt-4o/chat/completions?api-version=2024-02-15-preview` ,
@@ -79,7 +79,7 @@ module.exports = async function (context, req) {
7979 translatedPrompt = translationResponse . data . choices [ 0 ] . message . content . trim ( ) ;
8080 }
8181
82- // Step 3: Clarify Ambiguous Prompt
82+
8383 const contextAnalysisResponse = await axios . post (
8484 `${ process . env . OPENAI_ENDPOINT } /openai/deployments/gpt-4o/chat/completions?api-version=2024-02-15-preview` ,
8585 {
@@ -94,7 +94,7 @@ module.exports = async function (context, req) {
9494 ) ;
9595 const clarifiedPrompt = contextAnalysisResponse . data . choices [ 0 ] . message . content . trim ( ) ;
9696
97- // Step 4: Run Content Safety Check
97+
9898 const moderationResponse = await axios . post (
9999 `${ process . env . CONTENT_SAFETY_ENDPOINT } /contentsafety/text:analyze?api-version=2023-10-01` ,
100100 { text : clarifiedPrompt , categories : [ "Hate" , "Sexual" , "Violence" , "SelfHarm" ] } ,
@@ -147,7 +147,7 @@ module.exports = async function (context, req) {
147147 } ;
148148 }
149149
150- // Step 6: Return Final Processed Prompt
150+
151151 context . res = {
152152 status : 200 ,
153153 body : jsonResponse ,
0 commit comments