|
import re |
|
def generate_sql(client,query,tables): |
|
|
|
out = "## Database tables\n" |
|
for table in tables: |
|
out += table.get('content') |
|
|
|
prompt = f"Generate an sql query to answer this question {query} \n Based on this database information \n {out} /no_think" |
|
print(prompt) |
|
response = client.chat.completions.create( |
|
model="Qwen/Qwen3-8B", |
|
messages=[ |
|
{"role": "system", "content": "You are an expert in generating sql query based on a given schema. You will output the generated query in <sql> </sql> tags"}, |
|
{"role": "user", f"content": prompt} |
|
], |
|
temperature=0.7 |
|
) |
|
|
|
|
|
txt = response.choices[0].message.content |
|
match = re.search(r"<sql>(.*?)</sql>", txt, re.DOTALL | re.IGNORECASE) |
|
if match: |
|
return match.group(1).strip() |
|
return None |