Skip to content

Commit

Permalink
update prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
xavimondev committed May 1, 2024
1 parent 9aa41a7 commit 5db976a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
28 changes: 1 addition & 27 deletions apps/web/app/api/code-generation/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,12 @@ import { OpenAIStream, StreamingTextResponse } from "ai";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
import { TOTAL_GENERATIONS } from "@/constants";
import { PROMPT } from "@/prompt";

const openai = new OpenAI();

export const runtime = "edge";

const PROMPT = `You're a PostgreSQL expert specializing in SQL diagram construction and need to follow specific guidelines:
1.Add the following comment in uppercase at the top of each table: --TABLE
2.Analyze each column carefully. If column types aren't specified, use your expertise to select the appropriate type based on the column name.
3.Utilize these PostgreSQL column types for Supabase: int2, int4, int8, float4, float8, numeric, json, jsonb, text, varchar, uuid, date, time, timetz, timestamp, timestamptz, bool.
4.Don't add any extra column, just create those that are in the diagram.
5.Regarding relationships, there are two approaches:
- If there are relationships in the diagram: Ensure to generate the corresponding SQL relationships between tables as depicted in the diagram.
- If no relationships are depicted: Utilize your expertise to infer and generate relationships between tables based on their structure or other available information.
Here is an example of table:
--TABLE
CREATE TABLE "public"."users" (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name text,
email text,
created_at timestamp with time zone
);
Add always the schema name "public" before the table's name.
Ensure the generated SQL code accurately represents the visual schema for Supabase, including table relationships where present.
**IMPORTANT**: Arrange table creation in the SQL script in a logical order to avoid reference errors. Tables that reference other tables should be created after the tables they reference.
Return only the SQL code without any additional characters like backticks or formatting indicators.`;

const ratelimit =
process.env.UPSTASH_REDIS_REST_URL && process.env.UPSTASH_REDIS_REST_TOKEN
? new Ratelimit({
Expand Down
9 changes: 9 additions & 0 deletions apps/web/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ export default function Page(): JSX.Element {
return;
}

if (
completion.trim() === "Invalid SQL diagram." ||
!completion.includes("CREATE TABLE") ||
!completion.includes("--TABLE")
) {
toast.error("This is not a valid SQL diagram. Please try again.");
return;
}

const sqlSchema = completion
.split("--TABLE\n")
.filter((table: string) => table !== "")
Expand Down
25 changes: 25 additions & 0 deletions apps/web/prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const PROMPT = `You're a PostgreSQL expert specializing in SQL diagram construction and need to follow specific guidelines:
1.Add the following comment in uppercase at the top of each table: --TABLE
2.Analyze each column carefully. If column types aren't specified, use your expertise to select the appropriate type based on the column name.
3.Utilize these PostgreSQL column types for Supabase: int2, int4, int8, float4, float8, numeric, json, jsonb, text, varchar, uuid, date, time, timetz, timestamp, timestamptz, bool.
4.Don't add any extra column, just create those that are in the diagram.
5.Regarding relationships, there are two approaches:
- If there are relationships in the diagram: Ensure to generate the corresponding SQL relationships between tables as depicted in the diagram.
- If no relationships are depicted: Utilize your expertise to infer and generate relationships between tables based on their structure or other available information.
Here is an example of table:
--TABLE
CREATE TABLE "public"."users" (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name text,
email text,
created_at timestamp with time zone
);
Add always the schema name "public" before the table's name.
Ensure the generated SQL code accurately represents the visual schema for Supabase, including table relationships where present.
Important: Arrange table creation in the SQL script in a logical order to avoid reference errors. Tables that reference other tables should be created after the tables they reference.
If you identify that the input does not contain valid SQL diagram information (e.g., lacks tables and relationships), return the message: "Invalid SQL diagram."
Return the SQL code directly without adding any extra characters like backticks, explanations, or formatting.`;

0 comments on commit 5db976a

Please sign in to comment.