-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm.py
More file actions
30 lines (24 loc) · 1012 Bytes
/
Copy pathllm.py
File metadata and controls
30 lines (24 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import httpx
import re
import os
def ask_llm(question, table_name, schema):
api_key = os.getenv('GROQ_API_KEY')
if not api_key:
return "SELECT * FROM " + table_name + " LIMIT 5"
schema_str = ', '.join([f"{col['name']} ({col['type']})" for col in schema])
prompt = f"Table: {table_name} with columns: {schema_str}\nQuestion: {question}\nReturn ONLY the SQLite SELECT query, no markdown, no explanation."
response = httpx.post(
'https://api.groq.com/openai/v1/chat/completions',
headers={'Authorization': f'Bearer {api_key}'},
json={
'model': 'llama-3.3-70b-versatile',
'messages': [{'role': 'user', 'content': prompt}],
'temperature': 0
},
timeout=30
)
if response.status_code != 200:
return "SELECT * FROM " + table_name + " LIMIT 5"
sql = response.json()['choices'][0]['message']['content']
sql = re.sub(r'```sql\n?|```\n?', '', sql)
return sql.strip()