The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Update AI models to gpt-3.5-turbo-0125 (#21090)
Greg Richardson17a982f24db6
diff --git a/package-lock.json b/package-lock.jsonindex 1adf206c35f..7a2707a2f63 100644--- a/package-lock.json+++ b/package-lock.json@@ -28003,9 +28003,9 @@}},"node_modules/openai": {- "version": "4.20.1",- "resolved": "https://registry.npmjs.org/openai/-/openai-4.20.1.tgz",- "integrity": "sha512-Dd3q8EvINfganZFtg6V36HjrMaihqRgIcKiHua4Nq9aw/PxOP48dhbsk8x5klrxajt5Lpnc1KTOG5i1S6BKAJA==",+ "version": "4.26.1",+ "resolved": "https://registry.npmjs.org/openai/-/openai-4.26.1.tgz",+ "integrity": "sha512-DvWbjhWbappsFRatOWmu4Dp1/Q4RG9oOz6CfOSjy0/Drb8G+5iAiqWAO4PfpGIkhOOKtvvNfQri2SItl+U7LhQ==","dependencies": {"@types/node": "^18.11.18","@types/node-fetch": "^2.6.4",@@ -36964,12 +36964,13 @@"version": "0.0.0","license": "MIT","dependencies": {+ "@gregnr/libpg-query": "^13.4.0-dev.12","@serafin/schema-builder": "^0.18.5","ai": "^2.2.29","common-tags": "^1.8.2","config": "*","jsonrepair": "^3.5.0",- "openai": "^4.20.1"+ "openai": "^4.26.1"},"devDependencies": {"@babel/core": "^7.23.6",diff --git a/packages/ai-commands/package.json b/packages/ai-commands/package.jsonindex 4a5e25a1b1f..c8ceca9c6c5 100644--- a/packages/ai-commands/package.json+++ b/packages/ai-commands/package.json@@ -9,12 +9,13 @@"test": "jest"},"dependencies": {+ "@gregnr/libpg-query": "^13.4.0-dev.12","@serafin/schema-builder": "^0.18.5","ai": "^2.2.29","common-tags": "^1.8.2","config": "*","jsonrepair": "^3.5.0",- "openai": "^4.20.1"+ "openai": "^4.26.1"},"devDependencies": {"@babel/core": "^7.23.6",diff --git a/packages/ai-commands/src/__snapshots__/sql.test.ts.snap b/packages/ai-commands/src/__snapshots__/sql.test.ts.snapindex ca462cb7b87..ed775186dac 100644--- a/packages/ai-commands/src/__snapshots__/sql.test.ts.snap+++ b/packages/ai-commands/src/__snapshots__/sql.test.ts.snap@@ -39,7 +39,10 @@ exports[`generate single table with specified columns 1`] = ``;exports[`rls chat select policy using table definition 1`] = `-"create policy select_todo_policy on todos for-select- using (user_id = auth.uid ());"+{+ "checkExpression": undefined,+ "command": "select",+ "table": "todos",+ "usingExpression": "user_id = auth.uid ()",+}`;diff --git a/packages/ai-commands/src/sql.edge.ts b/packages/ai-commands/src/sql.edge.tsindex b6c04ce878f..02ac9a7f970 100644--- a/packages/ai-commands/src/sql.edge.ts+++ b/packages/ai-commands/src/sql.edge.ts@@ -41,8 +41,10 @@ export async function chatRlsPolicy(- If the user asks for something that's not related to SQL policies, explain to the userthat you can only help with policies.- The output should look like this:- "CREATE POLICY user_policy ON users FOR INSERT USING (user_name = current_user) WITH (true);"+ The output should look like this:+ \`\`\`sql+ CREATE POLICY "My descriptive policy." ON users FOR INSERT USING (user_name = current_user) WITH (true);+ \`\`\``,},]@@ -72,7 +74,7 @@ export async function chatRlsPolicy(try {const response = await openai.chat.completions.create({- model: 'gpt-3.5-turbo-1106',+ model: 'gpt-3.5-turbo-0125',messages: initMessages,max_tokens: 1024,temperature: 0,diff --git a/packages/ai-commands/src/sql.test.ts b/packages/ai-commands/src/sql.test.tsindex 7ff9513d7f4..730832d51ce 100644--- a/packages/ai-commands/src/sql.test.ts+++ b/packages/ai-commands/src/sql.test.ts@@ -1,7 +1,7 @@import { describe, expect, test } from '@jest/globals'import { codeBlock } from 'common-tags'import OpenAI from 'openai'-import { collectStream, extractMarkdownSql, formatSql } from '../test/util'+import { collectStream, extractMarkdownSql, formatSql, getPolicyInfo } from '../test/util'import { debugSql, editSql, generateSql, titleSql } from './sql'import { chatRlsPolicy } from './sql.edge'@@ -123,6 +123,11 @@ describe('rls chat', () => {const responseText = await collectStream(responseStream)const [sql] = extractMarkdownSql(responseText)- expect(formatSql(sql)).toMatchSnapshot()+ const { name, ...otherInfo } = await getPolicyInfo(sql)++ expect(otherInfo).toMatchSnapshot()+ await expect(name).toMatchCriteria(+ 'policy says that users can select their own todos (not insert, update, delete, etc)'+ )})})diff --git a/packages/ai-commands/src/sql.ts b/packages/ai-commands/src/sql.tsindex 99966ed9ddb..bd39f633c9a 100644--- a/packages/ai-commands/src/sql.ts+++ b/packages/ai-commands/src/sql.ts@@ -15,6 +15,7 @@ const generateSqlSchema = SchemaBuilder.emptySchema()- Prefer 'timestamp with time zone' over 'date'- Use vector(384) data type for any embedding/vector related query- Always use double apostrophe in SQL strings (eg. 'Night''s watch')+ - Always use semicolons`,}).addString('title', {@@ -36,6 +37,7 @@ const editSqlSchema = SchemaBuilder.emptySchema().addString('sql', {- Always use double apostrophe in SQL strings (eg. 'Night''s watch')- Use real examples when possible- Add constraints if requested+ - Always use semicolons`,})@@ -44,7 +46,8 @@ const debugSqlSchema = SchemaBuilder.emptySchema()description: 'A short suggested solution for the error (as concise as possible).',}).addString('sql', {- description: 'The SQL rewritten to apply the solution. Includes all the original logic, but modified to fix the issue.',+ description:+ 'The SQL rewritten to apply the solution. Includes all the original logic, but modified to fix the issue.',})const generateTitleSchema = SchemaBuilder.emptySchema()@@ -91,6 +94,7 @@ const completionFunctions = {- Prefer 'timestamp with time zone' over 'date'- Use vector(384) data type for any embedding/vector related query- Always use double apostrophe in SQL strings (eg. 'Night''s watch')+ - Always use semicolons`,parameters: debugSqlSchema.schema as Record<string, unknown>,},@@ -132,7 +136,7 @@ export async function generateSql(openai: OpenAI, prompt: string, entityDefinititry {const completionResponse = await openai.chat.completions.create({- model: 'gpt-3.5-turbo-1106',+ model: 'gpt-3.5-turbo-0125',messages: completionMessages,max_tokens: 1024,temperature: 0,@@ -218,7 +222,7 @@ export async function editSql(try {const completionResponse = await openai.chat.completions.create({- model: 'gpt-3.5-turbo-1106',+ model: 'gpt-3.5-turbo-0125',messages: completionMessages,max_tokens: 2048,temperature: 0,@@ -307,7 +311,7 @@ export async function debugSql(try {const completionResponse = await openai.chat.completions.create({- model: 'gpt-3.5-turbo-1106',+ model: 'gpt-3.5-turbo-0125',messages: completionMessages,max_tokens: 2048,temperature: 0,@@ -367,7 +371,7 @@ export async function titleSql(openai: OpenAI, sql: string) {try {const completionResponse = await openai.chat.completions.create({- model: 'gpt-3.5-turbo-1106',+ model: 'gpt-3.5-turbo-0125',messages: completionMessages,max_tokens: 1024,temperature: 0,diff --git a/packages/ai-commands/test/util.ts b/packages/ai-commands/test/util.tsindex b5f00b696c3..d6193bacf55 100644--- a/packages/ai-commands/test/util.ts+++ b/packages/ai-commands/test/util.ts@@ -1,3 +1,4 @@+import { parseQuery } from '@gregnr/libpg-query'import { fromMarkdown } from 'mdast-util-from-markdown'import type { Code } from 'mdast-util-from-markdown/lib'import { format } from 'sql-formatter'@@ -8,6 +9,14 @@ declare global {}}+export type PolicyInfo = {+ name: string+ table: string+ command?: string+ usingExpression?: string+ checkExpression?: string+}+/*** Formats Postgres SQL into a consistent format.*@@ -45,3 +54,52 @@ export function extractMarkdownSql(markdown: string) {.filter((node): node is Code => node.type === 'code' && node.lang === 'sql').map(({ value }) => value)}++/**+ * Parses a Postgres SQL policy.+ *+ * @returns Information about the policy, including its name, table, command, and expressions.+ */+export async function getPolicyInfo(sql: string) {+ const result = await parseQuery(sql)++ if (result.stmts.length === 0) {+ throw new Error('Expected a statement, but received none')+ }++ if (result.stmts.length > 1) {+ throw new Error('Expected a single statement, but received multiple')+ }++ const [{ stmt }] = result.stmts++ const createPolicyStatement = stmt.CreatePolicyStmt++ if (!createPolicyStatement) {+ throw new Error('Expected a create policy statement')+ }++ const formattedSql = formatSql(sql)++ const usingMatch = formattedSql.match(/using\s*\((.*)\)/is)+ let usingExpression+ if (usingMatch) {+ usingExpression = usingMatch[1]+ }++ const withCheckMatch = formattedSql.match(/with check\s*\((.*)\)/is)+ let checkExpression+ if (withCheckMatch) {+ checkExpression = withCheckMatch[1]+ }++ const policyInfo: PolicyInfo = {+ name: createPolicyStatement.policy_name,+ table: createPolicyStatement.table.relname,+ command: createPolicyStatement.cmd_name,+ usingExpression,+ checkExpression,+ }++ return policyInfo+}