2023-10-27 02:44:30 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-10-26 01:47:42 +00:00
|
|
|
from langchain.chat_models import ChatOllama
|
2023-10-27 02:44:30 +00:00
|
|
|
from langchain.memory import ConversationBufferMemory
|
|
|
|
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
2023-10-27 23:34:37 +00:00
|
|
|
from langchain.pydantic_v1 import BaseModel
|
2023-10-26 01:47:42 +00:00
|
|
|
from langchain.schema.output_parser import StrOutputParser
|
2023-10-27 02:44:30 +00:00
|
|
|
from langchain.schema.runnable import RunnableLambda, RunnablePassthrough
|
|
|
|
from langchain.utilities import SQLDatabase
|
2023-10-26 01:47:42 +00:00
|
|
|
|
|
|
|
# Add the LLM downloaded from Ollama
|
|
|
|
ollama_llm = "llama2:13b-chat"
|
|
|
|
llm = ChatOllama(model=ollama_llm)
|
|
|
|
|
2023-10-27 02:44:30 +00:00
|
|
|
|
2023-10-26 01:47:42 +00:00
|
|
|
db_path = Path(__file__).parent / "nba_roster.db"
|
|
|
|
rel = db_path.relative_to(Path.cwd())
|
|
|
|
db_string = f"sqlite:///{rel}"
|
|
|
|
db = SQLDatabase.from_uri(db_string, sample_rows_in_table_info=0)
|
|
|
|
|
2023-10-27 02:44:30 +00:00
|
|
|
|
2023-10-26 01:47:42 +00:00
|
|
|
def get_schema(_):
|
|
|
|
return db.get_table_info()
|
|
|
|
|
2023-10-27 02:44:30 +00:00
|
|
|
|
2023-10-26 01:47:42 +00:00
|
|
|
def run_query(query):
|
|
|
|
return db.run(query)
|
|
|
|
|
2023-10-27 02:44:30 +00:00
|
|
|
|
2023-10-26 01:47:42 +00:00
|
|
|
# Prompt
|
2023-10-27 02:44:30 +00:00
|
|
|
|
2023-10-26 01:47:42 +00:00
|
|
|
template = """Based on the table schema below, write a SQL query that would answer the user's question:
|
|
|
|
{schema}
|
|
|
|
|
|
|
|
Question: {question}
|
2023-10-27 02:44:30 +00:00
|
|
|
SQL Query:""" # noqa: E501
|
|
|
|
prompt = ChatPromptTemplate.from_messages(
|
|
|
|
[
|
|
|
|
("system", "Given an input question, convert it to a SQL query. No pre-amble."),
|
|
|
|
MessagesPlaceholder(variable_name="history"),
|
|
|
|
("human", template),
|
|
|
|
]
|
|
|
|
)
|
2023-10-26 01:47:42 +00:00
|
|
|
|
|
|
|
memory = ConversationBufferMemory(return_messages=True)
|
|
|
|
|
2023-10-27 02:44:30 +00:00
|
|
|
# Chain to query with memory
|
2023-10-26 01:47:42 +00:00
|
|
|
|
|
|
|
sql_chain = (
|
|
|
|
RunnablePassthrough.assign(
|
2023-10-27 02:44:30 +00:00
|
|
|
schema=get_schema,
|
|
|
|
history=RunnableLambda(lambda x: memory.load_memory_variables(x)["history"]),
|
|
|
|
)
|
|
|
|
| prompt
|
2023-10-26 01:47:42 +00:00
|
|
|
| llm.bind(stop=["\nSQLResult:"])
|
|
|
|
| StrOutputParser()
|
|
|
|
)
|
|
|
|
|
2023-10-27 02:44:30 +00:00
|
|
|
|
2023-10-26 01:47:42 +00:00
|
|
|
def save(input_output):
|
|
|
|
output = {"output": input_output.pop("output")}
|
|
|
|
memory.save_context(input_output, output)
|
2023-10-27 02:44:30 +00:00
|
|
|
return output["output"]
|
|
|
|
|
|
|
|
|
2023-10-26 01:47:42 +00:00
|
|
|
sql_response_memory = RunnablePassthrough.assign(output=sql_chain) | save
|
|
|
|
|
|
|
|
# Chain to answer
|
|
|
|
template = """Based on the table schema below, question, sql query, and sql response, write a natural language response:
|
|
|
|
{schema}
|
|
|
|
|
|
|
|
Question: {question}
|
|
|
|
SQL Query: {query}
|
2023-10-27 02:44:30 +00:00
|
|
|
SQL Response: {response}""" # noqa: E501
|
|
|
|
prompt_response = ChatPromptTemplate.from_messages(
|
|
|
|
[
|
|
|
|
(
|
|
|
|
"system",
|
|
|
|
"Given an input question and SQL response, convert it to a natural "
|
|
|
|
"language answer. No pre-amble.",
|
|
|
|
),
|
|
|
|
("human", template),
|
|
|
|
]
|
|
|
|
)
|
2023-10-26 01:47:42 +00:00
|
|
|
|
2023-10-27 23:34:37 +00:00
|
|
|
# Supply the input types to the prompt
|
|
|
|
class InputType(BaseModel):
|
|
|
|
question: str
|
|
|
|
|
2023-10-26 01:47:42 +00:00
|
|
|
chain = (
|
2023-10-27 23:34:37 +00:00
|
|
|
RunnablePassthrough.assign(query=sql_response_memory).with_types(input_type=InputType)
|
2023-10-26 01:47:42 +00:00
|
|
|
| RunnablePassthrough.assign(
|
|
|
|
schema=get_schema,
|
|
|
|
response=lambda x: db.run(x["query"]),
|
|
|
|
)
|
2023-10-27 02:44:30 +00:00
|
|
|
| prompt_response
|
2023-10-26 01:47:42 +00:00
|
|
|
| llm
|
|
|
|
)
|