跳到主要内容

使用 LLM 生成 MySQL 查询

背景

这个提示通过提供关于数据库模式的信息来测试LLM的代码生成能力,提示其生成有效的MySQL查询。

提示

"""
表 departments,列 = [DepartmentId, DepartmentName]
表 students,列 = [DepartmentId, StudentId, StudentName]
为计算机科学系的所有学生创建一个MySQL查询
"""

代码 / API

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
)

参考