Skip to content

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

feat(cmdk): gpt-3.5-turbo mvp

Greg Richardsona2ed3226fd01
diff --git a/packages/ui/src/components/Command/AiCommand.tsx b/packages/ui/src/components/Command/AiCommand.tsx
index 721fb2c5dbb..676405dac00 100644
--- a/packages/ui/src/components/Command/AiCommand.tsx
+++ b/packages/ui/src/components/Command/AiCommand.tsx
@@ -1,13 +1,16 @@
-import type { CreateCompletionResponse } from 'openai'
-import * as React from 'react'
+import type {
+ ChatCompletionResponseMessage,
+ CreateChatCompletionResponse,
+ CreateChatCompletionResponseChoicesInner,
+} from 'openai'
import { useCallback, useEffect, useReducer, useRef, useState } from 'react'
import ReactMarkdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
import { SSE } from 'sse.js'
import { Button, IconAlertCircle, IconAlertTriangle, IconLoader, IconUser, Input } from 'ui'
-import { CommandGroup, CommandItem } from './Command.utils'
import { AiIcon, AiIconChat } from './Command.icons'
+import { CommandGroup, CommandItem } from './Command.utils'
import { COMMAND_ROUTES } from './CommandMenu'
import { useCommandMenu } from './CommandMenuProvider'
@@ -20,6 +23,13 @@ const questions = [
'How do I set up authentication?',
]
+type CreateChatCompletionResponseChoicesInnerDelta = Omit<
+ CreateChatCompletionResponseChoicesInner,
+ 'message'
+> & {
+ delta: Partial<ChatCompletionResponseMessage>
+}
+
function getEdgeFunctionUrl() {
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL?.replace(/\/$/, '')
@@ -142,8 +152,14 @@ const AiCommand = () => {
setIsResponding(true)
- const completionResponse: CreateCompletionResponse = JSON.parse(e.data)
- const [{ text }] = completionResponse.choices
+ const completionResponse: CreateChatCompletionResponse = JSON.parse(e.data)
+ const [
+ {
+ delta: { content },
+ },
+ ] = completionResponse.choices as CreateChatCompletionResponseChoicesInnerDelta[]
+
+ const text = content ?? ''
setAnswer((answer) => {
const currentAnswer = answer ?? ''
@@ -194,33 +210,6 @@ const AiCommand = () => {
return (
<div onClick={(e) => e.stopPropagation()}>
<div className="relative mb-[70px] py-4 overflow-y-auto overflow-hidden max-h-[720px]">
- {/* {!isLoading && !answer && !hasClippyError && (
- <div className="">
- <div className="mt-2">
- <h2 className="text-sm text-scale-1100">Not sure where to start?</h2>
-
- <ul className="text-sm mt-4 text-scale-1100 grid md:flex gap-4 flex-wrap max-w-3xl">
- {questions.map((question) => {
- const key = question.replace(/\s+/g, '_')
- return (
- <li key={key}>
- <button
- className="hover:bg-slate-400 hover:dark:bg-slate-400 px-4 py-2 bg-slate-300 dark:bg-slate-200 rounded-lg transition-colors"
- onClick={() => {
- setQuery(question)
- handleClippyConfirm(question)
- }}
- >
- {question}
- </button>
- </li>
- )
- })}
- </ul>
- </div>
- </div>
- )} */}
-
<div className="flex flex-col gap-6">
{promptData.map((prompt, i) => {
if (!prompt.query) return <></>
diff --git a/supabase/functions/clippy-search/index.ts b/supabase/functions/clippy-search/index.ts
index 5ca3b422246..de86010809b 100644
--- a/supabase/functions/clippy-search/index.ts
+++ b/supabase/functions/clippy-search/index.ts
@@ -3,7 +3,13 @@ import 'https://deno.land/x/[email protected]/mod.ts'
import { createClient } from 'https://esm.sh/@supabase/[email protected]'
import { codeBlock, oneLine } from 'https://esm.sh/[email protected]'
import GPT3Tokenizer from 'https://esm.sh/[email protected]'
-import { Configuration, CreateCompletionRequest, OpenAIApi } from 'https://esm.sh/[email protected]'
+import {
+ ChatCompletionRequestMessage,
+ ChatCompletionRequestMessageRoleEnum,
+ Configuration,
+ CreateChatCompletionRequest,
+ OpenAIApi,
+} from 'https://esm.sh/[email protected]'
import { ApplicationError, UserError } from '../common/errors.ts'
const openAiKey = Deno.env.get('OPENAI_KEY')
@@ -46,6 +52,9 @@ serve(async (req) => {
throw new UserError('Missing query in request data')
}
+ // Intentionally log the query
+ console.log({ query })
+
const sanitizedQuery = query.trim()
const supabaseClient = createClient(supabaseUrl, supabaseServiceKey)
@@ -127,15 +136,76 @@ serve(async (req) => {
Answer as markdown (including related code snippets if available):
`
- const completionOptions: CreateCompletionRequest = {
- model: 'text-davinci-003',
- prompt,
- max_tokens: 512,
+ const messages: ChatCompletionRequestMessage[] = [
+ {
+ role: ChatCompletionRequestMessageRoleEnum.System,
+ content: codeBlock`
+ ${oneLine`
+ You are a very enthusiastic Supabase AI who loves
+ to help people! Given the following information from
+ the Supabase documentation, answer the user's question using
+ only that information, outputted in markdown format.
+ `}
+
+ ${oneLine`
+ If you are unsure
+ and the answer is not explicitly written in the documentation, say
+ "Sorry, I don't know how to help with that."
+ `}
+
+ ${oneLine`
+ Always include related code snippets if available.
+ `}
+ `,
+ },
+ {
+ role: ChatCompletionRequestMessageRoleEnum.User,
+ content: codeBlock`
+ Here is the Supabase documentation:
+ ${contextText}
+ `,
+ },
+ {
+ role: ChatCompletionRequestMessageRoleEnum.User,
+ content: codeBlock`
+ ${oneLine`
+ Answer my next question using only the above documentation.
+ You must also follow the below rules when answering:
+ `}
+ ${oneLine`
+ - Do not make up answers that are not provided in the documentation.
+ `}
+ ${oneLine`
+ - If you are unsure and the answer is not explicitly written
+ in the documentation context, say
+ "Sorry, I don't know how to help with that."
+ `}
+ ${oneLine`
+ - Prefer splitting your response into multiple paragraphs.
+ `}
+ ${oneLine`
+ - Output as markdown with code snippets if available.
+ `}
+ `,
+ },
+ {
+ role: ChatCompletionRequestMessageRoleEnum.User,
+ content: codeBlock`
+ Here is my question:
+ ${oneLine`${sanitizedQuery}`}
+ `,
+ },
+ ]
+
+ const completionOptions: CreateChatCompletionRequest = {
+ model: 'gpt-3.5-turbo',
+ messages,
+ max_tokens: 1024,
temperature: 0,
stream: true,
}
- const response = await fetch('https://api.openai.com/v1/completions', {
+ const response = await fetch('https://api.openai.com/v1/chat/completions', {
headers: {
Authorization: `Bearer ${openAiKey}`,
'Content-Type': 'application/json',
diff --git a/supabase/functions/search/index.ts b/supabase/functions/search/index.ts
index a9a09ab2866..3d5c91c2904 100644
--- a/supabase/functions/search/index.ts
+++ b/supabase/functions/search/index.ts
@@ -45,6 +45,9 @@ serve(async (req) => {
throw new UserError('Missing query in request data')
}
+ // Intentionally log the query
+ console.log({ query })
+
const sanitizedQuery = query.trim()
const supabaseClient = createClient<Database>(supabaseUrl, supabaseServiceKey)