From 25b5872be58b2e16dbe917ca74852278acf82ddb Mon Sep 17 00:00:00 2001 From: Eric Wang Date: Thu, 15 Aug 2024 00:26:40 -0700 Subject: [PATCH 1/7] chat: add configure openai api base and model --- apps/postgres-new/app/api/chat/route.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/postgres-new/app/api/chat/route.ts b/apps/postgres-new/app/api/chat/route.ts index 89faf26..1b4f996 100644 --- a/apps/postgres-new/app/api/chat/route.ts +++ b/apps/postgres-new/app/api/chat/route.ts @@ -1,4 +1,4 @@ -import { openai } from '@ai-sdk/openai' +import { createOpenAI } from '@ai-sdk/openai' import { ToolInvocation, convertToCoreMessages, streamText } from 'ai' import { codeBlock } from 'common-tags' import { convertToCoreTools, maxMessageContext, maxRowLimit, tools } from '~/lib/tools' @@ -12,6 +12,15 @@ type Message = { toolInvocations?: (ToolInvocation & { result: any })[] } +const chatModel = process.env.OPENAI_MODEL || 'gpt-4o-2024-08-06' + +// Configure OpenAI client with custom base URL +const openai = createOpenAI({ + apiKey: process.env.OPENAI_API_KEY, + baseURL: process.env.OPENAI_API_BASE || 'https://api.openai.com/v1', + compatibility: 'strict', +}) + export async function POST(req: Request) { const { messages }: { messages: Message[] } = await req.json() @@ -49,7 +58,7 @@ export async function POST(req: Request) { When importing CSVs try to solve the problem yourself (eg. use a generic text column, then refine) vs. asking the user to change the CSV. No need to select rows after importing. - + You also know math. All math equations and expressions must be written in KaTex and must be wrapped in double dollar \`$$\`: - Inline: $$\\sqrt{26}$$ - Multiline: @@ -61,7 +70,7 @@ export async function POST(req: Request) { Feel free to suggest corrections for suspected typos. `, - model: openai('gpt-4o-2024-08-06'), + model: openai(chatModel), messages: convertToCoreMessages(trimmedMessageContext), tools: convertToCoreTools(tools), }) From 2d3da56913c575653c174767e85a20f1489a31a1 Mon Sep 17 00:00:00 2001 From: Eric Wang Date: Thu, 15 Aug 2024 00:28:44 -0700 Subject: [PATCH 2/7] update .env.example --- apps/postgres-new/.env.example | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/postgres-new/.env.example b/apps/postgres-new/.env.example index f94678c..65e205f 100644 --- a/apps/postgres-new/.env.example +++ b/apps/postgres-new/.env.example @@ -3,3 +3,5 @@ NEXT_PUBLIC_SUPABASE_URL="" NEXT_PUBLIC_IS_PREVIEW=true OPENAI_API_KEY="" +# OPENAI_API_BASE="https://api.openai.com/v1" +# OPENAI_MODEL="gpt-4o-2024-08-06" \ No newline at end of file From 0b891960de9c1b6a0b111fc593438df03d7c352f Mon Sep 17 00:00:00 2001 From: Eric Wang Date: Thu, 15 Aug 2024 18:35:32 -0700 Subject: [PATCH 3/7] add optional configuration Co-authored-by: Greg Richardson --- apps/postgres-new/.env.example | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/postgres-new/.env.example b/apps/postgres-new/.env.example index 65e205f..6143bd3 100644 --- a/apps/postgres-new/.env.example +++ b/apps/postgres-new/.env.example @@ -3,5 +3,7 @@ NEXT_PUBLIC_SUPABASE_URL="" NEXT_PUBLIC_IS_PREVIEW=true OPENAI_API_KEY="" -# OPENAI_API_BASE="https://api.openai.com/v1" -# OPENAI_MODEL="gpt-4o-2024-08-06" \ No newline at end of file + +# Optional +# OPENAI_API_BASE="" +# OPENAI_MODEL="" \ No newline at end of file From f44de24063ca25536ce237fc621d3ce39ca8b94a Mon Sep 17 00:00:00 2001 From: Eric Wang Date: Thu, 15 Aug 2024 18:35:54 -0700 Subject: [PATCH 4/7] Update apps/postgres-new/app/api/chat/route.ts Co-authored-by: Greg Richardson --- apps/postgres-new/app/api/chat/route.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/postgres-new/app/api/chat/route.ts b/apps/postgres-new/app/api/chat/route.ts index 1b4f996..ebe750d 100644 --- a/apps/postgres-new/app/api/chat/route.ts +++ b/apps/postgres-new/app/api/chat/route.ts @@ -17,7 +17,7 @@ const chatModel = process.env.OPENAI_MODEL || 'gpt-4o-2024-08-06' // Configure OpenAI client with custom base URL const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY, - baseURL: process.env.OPENAI_API_BASE || 'https://api.openai.com/v1', + baseURL: process.env.OPENAI_API_BASE ?? 'https://api.openai.com/v1', compatibility: 'strict', }) From 6bb3b3ac9e4ea52756046769d6e078474ecaa7fd Mon Sep 17 00:00:00 2001 From: Eric Wang Date: Thu, 15 Aug 2024 18:36:00 -0700 Subject: [PATCH 5/7] Update apps/postgres-new/app/api/chat/route.ts Co-authored-by: Greg Richardson --- apps/postgres-new/app/api/chat/route.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/postgres-new/app/api/chat/route.ts b/apps/postgres-new/app/api/chat/route.ts index ebe750d..16183b7 100644 --- a/apps/postgres-new/app/api/chat/route.ts +++ b/apps/postgres-new/app/api/chat/route.ts @@ -12,7 +12,7 @@ type Message = { toolInvocations?: (ToolInvocation & { result: any })[] } -const chatModel = process.env.OPENAI_MODEL || 'gpt-4o-2024-08-06' +const chatModel = process.env.OPENAI_MODEL ?? 'gpt-4o-2024-08-06' // Configure OpenAI client with custom base URL const openai = createOpenAI({ From e6237acfe395836a940518a7a44320e418225731 Mon Sep 17 00:00:00 2001 From: Eric Wang Date: Thu, 15 Aug 2024 00:28:44 -0700 Subject: [PATCH 6/7] update .env.example --- apps/postgres-new/.env.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/postgres-new/.env.example b/apps/postgres-new/.env.example index 6143bd3..0edad06 100644 --- a/apps/postgres-new/.env.example +++ b/apps/postgres-new/.env.example @@ -6,4 +6,4 @@ OPENAI_API_KEY="" # Optional # OPENAI_API_BASE="" -# OPENAI_MODEL="" \ No newline at end of file +# OPENAI_MODEL="" From f0f603babb7cbcc01ee599d12715b033f7166234 Mon Sep 17 00:00:00 2001 From: Eric Wang Date: Thu, 15 Aug 2024 18:59:12 -0700 Subject: [PATCH 7/7] update README --- apps/postgres-new/README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/apps/postgres-new/README.md b/apps/postgres-new/README.md index 414e074..e621707 100644 --- a/apps/postgres-new/README.md +++ b/apps/postgres-new/README.md @@ -13,9 +13,20 @@ Both databases are stored locally in the browser via IndexedDB. This means that Every PGlite instance runs in a Web Worker so that the main thread is not blocked. + ## AI -The AI component is powered by OpenAI's GPT-4o model. The project uses [Vercel's AI SDK ](https://sdk.vercel.ai/docs/introduction) to simplify message streams and tool calls. +The AI component is powered by OpenAI's GPT-4o model by default. The project uses [Vercel's AI SDK](https://sdk.vercel.ai/docs/introduction) to simplify message streams and tool calls. + +### Environment Variables + +In addition to the required `OPENAI_API_KEY`, the following environment variables can be configured: + +- `OPENAI_API_BASE`: (Optional) The base URL for the OpenAI API. Defaults to `https://api.openai.com/v1`. +- `OPENAI_MODEL`: (Optional) The model used by the AI component. Defaults to `gpt-4o-2024-08-06`. + +**NOTE**: The current prompts and tools are designed around the GPT-4o model. If you choose to use a different model, expect different behavior and results. Additionally, ensure that the model you select supports tool (function) call capabilities. + ## Authentication