-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx-dev): moved openai calls to edge function
- Loading branch information
Showing
3 changed files
with
105 additions
and
48 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
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { NextRequest } from 'next/server'; | ||
import { Configuration, OpenAIApi } from 'openai'; | ||
|
||
const openAiKey = process.env['NX_OPENAI_KEY']; | ||
const config = new Configuration({ | ||
apiKey: openAiKey, | ||
}); | ||
const openai = new OpenAIApi(config); | ||
|
||
export default async function handler(request: NextRequest) { | ||
const { action, input } = await request.json(); | ||
|
||
if (action === 'embedding') { | ||
try { | ||
const embeddingResponse = await openai.createEmbedding(input); | ||
return new Response(JSON.stringify(embeddingResponse.data), { | ||
status: embeddingResponse.status, | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
}); | ||
} catch (e) { | ||
console.error('Error processing the request:', e.message); | ||
return new Response(e.message, { status: 500 }); | ||
} | ||
} else if (action === 'chatCompletion') { | ||
try { | ||
const chatCompletionResponse = await openai.createChatCompletion(input); | ||
return new Response(JSON.stringify(chatCompletionResponse.data), { | ||
status: chatCompletionResponse.status, | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
}); | ||
} catch (e) { | ||
console.error('Error processing the request:', e.message); | ||
return new Response(e.message, { status: 500 }); | ||
} | ||
} else if (action === 'moderation') { | ||
try { | ||
const moderationResponse = await openai.createModeration(input); | ||
return new Response(JSON.stringify(moderationResponse.data), { | ||
status: moderationResponse.status, | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
}); | ||
} catch (e) { | ||
console.error('Error processing the request:', e.message); | ||
return new Response(e.message, { status: 500 }); | ||
} | ||
} else { | ||
return new Response('Invalid action', { status: 400 }); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.