logo
P
Prompt Master

Prompt 大师

掌握和 AI 对话的艺术

MySQL query

generate a SQL query from schema

#TL;DR(中文)

  • 这是一个
    code
    SQL
    生成测试:给定 schema 描述,让模型输出可执行的
    code
    MySQL
    query。
  • 常见失败:join 条件写错、列名写错、遗漏过滤条件、输出不是 MySQL 兼容语法。
  • 落地建议:把 schema 写得更结构化(PK/FK/index),并要求输出同时给解释与可验证的假设(例如 join key)。

#Background

This prompt tests an LLM's code generation capabilities by prompting it to generate a valid MySQL query given a schema description.

#How to Apply(中文)

迁移到真实业务时,建议把输入拆成两部分:

  • code
    Schema
    :表、列、PK/FK、字段类型(如果重要)
  • code
    Requirement
    :要查什么、过滤条件、排序、是否需要聚合

并明确输出格式,例如:

  • code
    SQL
    (只输出 SQL)
  • code
    Assumptions
    (如果 schema 不够完整,列出假设)

#How to Iterate(中文)

  1. 加入字段类型与约束(nullable/unique),减少歧义
  2. 要求输出包含 join rationale(为什么用哪个 key join)
  3. code
    evaluation
    :用一个小的 mock dataset 或 SQL validator 校验语法与结果
  4. 增加复杂度:group by / window function / subquery,观察鲁棒性

#Self-check rubric(中文)

  • SQL 是否 MySQL 兼容?是否能直接执行?
  • 表名/列名是否都来自 schema?是否有 hallucinated column?
  • 过滤条件是否满足需求?是否遗漏 Computer Science Department 的约束?
  • join 是否正确、是否会产生重复行?

#Practice(中文)

练习:把这个例子扩展成 3 个版本:

  • 只查学生姓名
  • 查学生姓名 + department name
  • 加一个排序(按 StudentName)与分页(limit/offset)

#Prompt

markdown
""" Table departments, columns = [DepartmentId, DepartmentName] Table students, columns = [DepartmentId, StudentId, StudentName] Create a MySQL query for all students in the Computer Science Department """

#Code / API

#OpenAI (Python)

python
from openai import OpenAI client = OpenAI() response = client.chat.completions.create( model="gpt-4", messages=[ { "role": "user", "content": "\"\"\"\nTable departments, columns = [DepartmentId, DepartmentName]\nTable students, columns = [DepartmentId, StudentId, StudentName]\nCreate a MySQL query for all students in the Computer Science Department\n\"\"\"", } ], temperature=1, max_tokens=1000, top_p=1, frequency_penalty=0, presence_penalty=0, )

#Fireworks (Python)

python
import fireworks.client fireworks.client.api_key = "<FIREWORKS_API_KEY>" completion = fireworks.client.ChatCompletion.create( model="accounts/fireworks/models/mixtral-8x7b-instruct", messages=[ { "role": "user", "content": "\"\"\"\nTable departments, columns = [DepartmentId, DepartmentName]\nTable students, columns = [DepartmentId, StudentId, StudentName]\nCreate a MySQL query for all students in the Computer Science Department\n\"\"\"", } ], stop=["<|im_start|>", "<|im_end|>", "<|endoftext|>"], stream=True, n=1, top_p=1, top_k=40, presence_penalty=0, frequency_penalty=0, prompt_truncate_len=1024, context_length_exceeded_behavior="truncate", temperature=0.9, max_tokens=4000, )
1v1免费职业咨询