-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathllmtemplate.py
76 lines (59 loc) · 2.5 KB
/
llmtemplate.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.chat_models import ChatOllama
from database import getDatabaseSchema
llm = ChatOllama(model="llama3.1")
def getQueryFromLLM(question):
template = """below is the schema of MYSQL database, read the schema carefully about the table and column names. Also take care of table or column name case sensitivity.
Finally answer user's question in the form of SQL query.
{schema}
please only provide the SQL query and nothing else
for example:
question: how many albums we have in database
SQL query: SELECT COUNT(*) FROM album
question: how many customers are from Brazil in the database ?
SQL query: SELECT COUNT(*) FROM customer WHERE country=Brazil
your turn :
question: {question}
SQL query :
please only provide the SQL query and nothing else
"""
prompt = ChatPromptTemplate.from_template(template)
chain = prompt | llm
response = chain.invoke({
"question": question,
"schema": getDatabaseSchema()
})
return response.content
def getResponseForQueryResult(question, query, result):
template2 = """below is the schema of MYSQL database, read the schema carefully about the table and column names of each table.
Also look into the conversation if available
Finally write a response in natural language by looking into the conversation and result.
{schema}
Here are some example for you:
question: how many albums we have in database
SQL query: SELECT COUNT(*) FROM album;
Result : [(34,)]
Response: There are 34 albums in the database.
question: how many users we have in database
SQL query: SELECT COUNT(*) FROM customer;
Result : [(59,)]
Response: There are 59 amazing users in the database.
question: how many users above are from india we have in database
SQL query: SELECT COUNT(*) FROM customer WHERE country=india;
Result : [(4,)]
Response: There are 4 amazing users in the database.
your turn to write response in natural language from the given result :
question: {question}
SQL query : {query}
Result : {result}
Response:
"""
prompt2 = ChatPromptTemplate.from_template(template2)
chain2 = prompt2 | llm
response = chain2.invoke({
"question": question,
"schema": getDatabaseSchema(),
"query": query,
"result": result
})
return response.content