Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: Update the function code template for OpenAI completion API to work with current packages (#34890)

Tyler6610621f67b0
diff --git a/apps/studio/components/interfaces/Functions/Functions.templates.ts b/apps/studio/components/interfaces/Functions/Functions.templates.ts
index f4511c658bc..4a984851471 100644
--- a/apps/studio/components/interfaces/Functions/Functions.templates.ts
+++ b/apps/studio/components/interfaces/Functions/Functions.templates.ts
@@ -140,26 +140,32 @@ app.listen(8000);`,
name: 'OpenAI Text Completion',
description: 'Generate text completions using OpenAI GPT-3',
content: `// Setup type definitions for built-in Supabase Runtime APIs
-import "jsr:@supabase/functions-js/edge-runtime.d.ts";
-import { Configuration, OpenAIApi } from 'npm:[email protected]'
+import "jsr:@supabase/functions-js/edge-runtime.d.ts"
+import { OpenAI } from "npm:[email protected]"
-const openAi = new OpenAIApi(
- new Configuration({
- apiKey: Deno.env.get('OPENAI_API_KEY')
- })
-)
+const openai = new OpenAI({
+ apiKey: Deno.env.get('OPENAI_API_KEY')
+})
-Deno.serve(async (req) => {
+Deno.serve(async (req)=>{
const { prompt } = await req.json()
- const completion = await openAi.createCompletion({
- model: 'text-davinci-003',
- prompt,
- max_tokens: 200
+ const response = await openai.chat.completions.create({
+ model: "gpt-3.5-turbo",
+ messages: [
+ {
+ role: "user",
+ content: prompt
+ }
+ ]
+ })
+ return new Response(JSON.stringify({
+ text: response.choices[0].message.content
+ }), {
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Connection': 'keep-alive'
+ }
})
- return new Response(
- JSON.stringify({ text: completion.data.choices[0].text }),
- { headers: { 'Content-Type': 'application/json' }}
- )
})`,
},
{