diff --git a/apps/postgres-new/.env.example b/apps/postgres-new/.env.example index af216d77..425deb9d 100644 --- a/apps/postgres-new/.env.example +++ b/apps/postgres-new/.env.example @@ -4,6 +4,10 @@ NEXT_PUBLIC_IS_PREVIEW=true OPENAI_API_KEY="" +# Optional +# OPENAI_API_BASE="" +# OPENAI_MODEL="" + # Vercel KV (local Docker available) KV_REST_API_URL="http://localhost:8080" KV_REST_API_TOKEN="local_token" diff --git a/apps/postgres-new/README.md b/apps/postgres-new/README.md index 7b5cb23b..233348aa 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 diff --git a/apps/postgres-new/app/api/chat/route.ts b/apps/postgres-new/app/api/chat/route.ts index 4a1371c1..b81e4af5 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 { Ratelimit } from '@upstash/ratelimit' import { kv } from '@vercel/kv' import { ToolInvocation, convertToCoreMessages, streamText } from 'ai' @@ -27,6 +27,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 supabase = createClient() @@ -82,7 +91,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: @@ -94,7 +103,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), async onFinish({ usage }) {