10000 fix: Update the function code template for OpenAI completion API to w… · CodersSampling/supabase@6610621 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6610621

Browse files
authored
fix: Update the function code template for OpenAI completion API to work with current packages (supabase#34890)
fix: update the code sample for openai-completion to work with latest packages
1 parent 3989395 commit 6610621

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

apps/studio/components/interfaces/Functions/Functions.templates.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,32 @@ app.listen(8000);`,
140140
name: 'OpenAI Text Completion',
141141
description: 'Generate text completions using OpenAI GPT-3',
142142
content: `// Setup type definitions for built-in Supabase Runtime APIs
143-
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
144-
import { Configuration, OpenAIApi } from 'npm:openai@3.3.0'
143+
import "jsr:@supabase/functions-js/edge-runtime.d.ts"
144+
import { OpenAI } from "npm:openai@4.8.0"
145145
146-
const openAi = new OpenAIApi(
147-
new Configuration({
148-
apiKey: Deno.env.get('OPENAI_API_KEY')
149-
})
150-
)
146+
const openai = new OpenAI({
147+
apiKey: Deno.env.get('OPENAI_API_KEY')
148+
})
151149
152-
Deno.serve(async (req) => {
150+
Deno.serve(async (req)=>{
153151
const { prompt } = await req.json()
154-
const completion = await openAi.createCompletion({
155-
model: 'text-davinci-003',
156-
prompt,
157-
max_tokens: 200
152+
const response = await openai.chat.completions.create({
153+
model: "gpt-3.5-turbo",
154+
messages: [
155+
{
156+
role: "user",
157+
content: prompt
158+
}
159+
]
160+
})
161+
return new Response(JSON.stringify({
162+
text: response.choices[0].message.content
163+
}), {
164+
headers: {
165+
'Content-Type': 'application/json',
166+
'Connection': 'keep-alive'
167+
}
158168
})
159-
return new Response(
160-
JSON.stringify({ text: completion.data.choices[0].text }),
161-
{ headers: { 'Content-Type': 'application/json' }}
162-
)
163169
})`,
164170
},
165171
{

0 commit comments

Comments
 (0)
0