Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

feat(docker image): add support for custom inference servers (#543)

Calum Siemer1878083056ea
diff --git a/Dockerfile b/Dockerfile
index 9672d1fc..67ef56e3 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,6 +1,8 @@
FROM node:22-alpine AS builder
ARG VITE_OPENAI_API_KEY
+ARG VITE_OPENAI_API_ENDPOINT
+ARG VITE_LLM_MODEL_NAME
WORKDIR /usr/src/app
@@ -10,9 +12,12 @@ RUN npm ci
COPY . .
+RUN echo "VITE_OPENAI_API_KEY=${VITE_OPENAI_API_KEY}" > .env && \
+ echo "VITE_OPENAI_API_ENDPOINT=${VITE_OPENAI_API_ENDPOINT}" >> .env && \
+ echo "VITE_LLM_MODEL_NAME=${VITE_LLM_MODEL_NAME}" >> .env
+
RUN npm run build
-# Use a lightweight web server to serve the production build
FROM nginx:stable-alpine AS production
COPY --from=builder /usr/src/app/dist /usr/share/nginx/html
@@ -20,7 +25,6 @@ COPY ./default.conf.template /etc/nginx/conf.d/default.conf.template
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
-# Expose the default port for the Nginx web server
EXPOSE 80
-ENTRYPOINT ["/entrypoint.sh"]
+ENTRYPOINT ["/entrypoint.sh"]
\ No newline at end of file
diff --git a/README.md b/README.md
index 0cbf99b8..36a4eddd 100644
--- a/README.md
+++ b/README.md
@@ -107,8 +107,33 @@ docker build -t chartdb .
docker run -e OPENAI_API_KEY=<YOUR_OPEN_AI_KEY> -p 8080:80 chartdb
```
+#### Using Custom Inference Server
+
+```bash
+# Build
+docker build \
+ --build-arg VITE_OPENAI_API_ENDPOINT=<YOUR_ENDPOINT> \
+ --build-arg VITE_LLM_MODEL_NAME=<YOUR_MODEL_NAME> \
+ -t chartdb .
+
+# Run
+docker run \
+ -e OPENAI_API_ENDPOINT=<YOUR_ENDPOINT> \
+ -e LLM_MODEL_NAME=<YOUR_MODEL_NAME> \
+ -p 8080:80 chartdb
+```
+
+> **Note:** You must configure either Option 1 (OpenAI API key) OR Option 2 (Custom endpoint and model name) for AI capabilities to work. Do not mix the two options.
+
Open your browser and navigate to `http://localhost:8080`.
+Example configuration for a local vLLM server:
+
+```bash
+VITE_OPENAI_API_ENDPOINT=http://localhost:8000/v1
+VITE_LLM_MODEL_NAME=Qwen/Qwen2.5-32B-Instruct-AWQ
+```
+
## Try it on our website
1. Go to [ChartDB.io](https://chartdb.io?ref=github_readme_2)
diff --git a/default.conf.template b/default.conf.template
index 82325096..bc403365 100644
--- a/default.conf.template
+++ b/default.conf.template
@@ -10,7 +10,11 @@ server {
location /config.js {
default_type application/javascript;
- return 200 "window.env = { OPENAI_API_KEY: \"$OPENAI_API_KEY\" };";
+ return 200 "window.env = {
+ OPENAI_API_KEY: \"$OPENAI_API_KEY\",
+ OPENAI_API_ENDPOINT: \"$OPENAI_API_ENDPOINT\",
+ LLM_MODEL_NAME: \"$LLM_MODEL_NAME\"
+ };";
}
error_page 500 502 503 504 /50x.html;
diff --git a/entrypoint.sh b/entrypoint.sh
index 7a883abd..05d41b8f 100644
--- a/entrypoint.sh
+++ b/entrypoint.sh
@@ -1,7 +1,7 @@
#!/bin/sh
# Replace placeholders in nginx.conf
-envsubst '${OPENAI_API_KEY}' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf
+envsubst '${OPENAI_API_KEY} ${OPENAI_API_ENDPOINT} ${LLM_MODEL_NAME}' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf
# Start Nginx
nginx -g "daemon off;"
diff --git a/src/lib/data/export-metadata/export-sql-script.ts b/src/lib/data/export-metadata/export-sql-script.ts
index a0dadd08..a90a9dc8 100644
--- a/src/lib/data/export-metadata/export-sql-script.ts
+++ b/src/lib/data/export-metadata/export-sql-script.ts
@@ -1,5 +1,5 @@
import type { Diagram } from '../../domain/diagram';
-import { OPENAI_API_KEY } from '@/lib/env';
+import { OPENAI_API_KEY, OPENAI_API_ENDPOINT, LLM_MODEL_NAME } from '@/lib/env';
import type { DatabaseType } from '@/lib/domain/database-type';
import type { DBTable } from '@/lib/domain/db-table';
import type { DataType } from '../data-types/data-types';
@@ -196,6 +196,26 @@ export const exportBaseSQL = (diagram: Diagram): string => {
return sqlScript;
};
+const validateConfiguration = () => {
+ const apiKey = window?.env?.OPENAI_API_KEY ?? OPENAI_API_KEY;
+ const baseUrl = window?.env?.OPENAI_API_ENDPOINT ?? OPENAI_API_ENDPOINT;
+ const modelName = window?.env?.LLM_MODEL_NAME ?? LLM_MODEL_NAME;
+
+ // If using custom endpoint and model, don't require OpenAI API key
+ if (baseUrl && modelName) {
+ return { useCustomEndpoint: true };
+ }
+
+ // If using OpenAI's service, require API key
+ if (apiKey) {
+ return { useCustomEndpoint: false };
+ }
+
+ throw new Error(
+ 'Configuration Error: Either provide an OpenAI API key or both a custom endpoint and model name'
+ );
+};
+
export const exportSQL = async (
diagram: Diagram,
databaseType: DatabaseType,
@@ -213,43 +233,73 @@ export const exportSQL = async (
return cachedResult;
}
+ // Validate configuration before proceeding
+ const { useCustomEndpoint } = validateConfiguration();
+
const [{ streamText, generateText }, { createOpenAI }] = await Promise.all([
import('ai'),
import('@ai-sdk/openai'),
]);
- const openai = createOpenAI({
- apiKey: window?.env?.OPENAI_API_KEY ?? OPENAI_API_KEY,
- });
+ const apiKey = window?.env?.OPENAI_API_KEY ?? OPENAI_API_KEY;
+ const baseUrl = window?.env?.OPENAI_API_ENDPOINT ?? OPENAI_API_ENDPOINT;
+ const modelName = window?.env?.LLM_MODEL_NAME || 'gpt-4o-mini-2024-07-18';
+
+ let config: { apiKey: string; baseUrl?: string };
+
+ if (useCustomEndpoint) {
+ config = {
+ apiKey: 'sk-xxx', // minimal valid API key format
+ baseUrl: baseUrl,
+ };
+ } else {
+ config = {
+ apiKey: apiKey,
+ };
+ }
+
+ const openai = createOpenAI(config);
const prompt = generateSQLPrompt(databaseType, sqlScript);
- if (options?.stream) {
- const { textStream, text: textPromise } = await streamText({
- model: openai('gpt-4o-mini-2024-07-18'),
- prompt: prompt,
- });
+ try {
+ if (options?.stream) {
+ const { textStream, text: textPromise } = await streamText({
+ model: openai(modelName),
+ prompt: prompt,
+ });
- for await (const textPart of textStream) {
- if (options.signal?.aborted) {
- return '';
+ for await (const textPart of textStream) {
+ if (options.signal?.aborted) {
+ return '';
+ }
+ options.onResultStream(textPart);
}
- options.onResultStream(textPart);
+
+ const text = await textPromise;
+
+ setInCache(cacheKey, text);
+ return text;
}
- const text = await textPromise;
+ const { text } = await generateText({
+ model: openai(modelName),
+ prompt: prompt,
+ });
setInCache(cacheKey, text);
return text;
+ } catch (error: unknown) {
+ console.error('Error generating SQL:', error);
+ if (error instanceof Error && error.message.includes('API key')) {
+ throw new Error(
+ 'Error: Please check your API configuration. If using a custom endpoint, make sure the endpoint URL is correct.'
+ );
+ }
+ throw new Error(
+ 'Error generating SQL script. Please check your configuration and try again.'
+ );
}
-
- const { text } = await generateText({
- model: openai('gpt-4o-mini-2024-07-18'),
- prompt: prompt,
- });
-
- setInCache(cacheKey, text);
- return text;
};
function getMySQLDataTypeSize(type: DataType) {
diff --git a/src/lib/env.ts b/src/lib/env.ts
index 8731314c..1b7a23bc 100644
--- a/src/lib/env.ts
+++ b/src/lib/env.ts
@@ -1,4 +1,7 @@
export const OPENAI_API_KEY: string = import.meta.env.VITE_OPENAI_API_KEY;
+export const OPENAI_API_ENDPOINT: string = import.meta.env
+ .VITE_OPENAI_API_ENDPOINT;
+export const LLM_MODEL_NAME: string = import.meta.env.VITE_LLM_MODEL_NAME;
export const IS_CHARTDB_IO: boolean =
import.meta.env.VITE_IS_CHARTDB_IO === 'true';
export const APP_URL: string = import.meta.env.VITE_APP_URL;