| |
| from langchain.embeddings import HuggingFaceEmbeddings |
| |
| from langchain.schema import Document |
|
|
| |
| from langchain.llms import AzureMLOnlineEndpoint |
| from langchain.chat_models.azureml_endpoint import ContentFormatterBase |
| import json |
|
|
| from langchain.chains import create_sql_query_chain |
|
|
| import chainlit as cl |
|
|
|
|
| from typing import Dict |
|
|
|
|
| |
| |
| |
|
|
| from langchain.agents import create_sql_agent, AgentType |
| from langchain.agents.agent_toolkits import SQLDatabaseToolkit |
| from langchain.utilities import SQLDatabase |
| from langchain.chat_models import ChatOpenAI |
|
|
| import os |
| OPENAI_API_KEY = os.environ['OPENAI_API_KEY'] |
|
|
|
|
|
|
|
|
| def create_agent(): |
| |
|
|
| |
| db = SQLDatabase.from_uri('sqlite:///spm.db') |
| llm = ChatOpenAI(temperature=0.05, model="gpt-3.5-turbo-16k-0613") |
| db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True) |
| return db_chain |
|
|
| |
|
|
| custom_suffix = """ |
| Compose a query in the All_data table in the db database. |
| Here is a description of each column: |
| destn_area_name: The name of the destination area. |
| destn_district_name: The name of the destination district. |
| score: The score of the destination area. |
| avg_days_todelr: The average number of days to deliver to the destination area. |
| time_per: The time period of the data. |
| orgn_area: The code of the origin area. |
| orgn_dist: The code of the origin district. |
| orgn_area_name: The name of the origin area. |
| orgn_dist_name: The name of the origin district. |
| destn_area: The code of the destination area. |
| destn_dist: The code of the destination district. |
| destn_area_name: The name of the destination area. |
| destn_dist_name: The name of the destination district. |
| prodt: The product type. |
| rptg_start_date: The start date of the reporting period. |
| rptg_end_date: The end date of the reporting period. |
| mo: The month of the reporting period. |
| pstl_qtr: The quarter of the Postal reporting period. |
| pstl_yr: The year of the Postal reporting period. |
| score: The score of the destination area. |
| score_plus_1: The score of the destination area plus 1. |
| """ |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| from langchain.prompts import PromptTemplate |
|
|
|
|
| def build_sql_chain(llm, db): |
| |
| dialect = "Azure SQL" |
| table_info = "All_data" |
| few_shots = {"What are the top 10 performing areas?": "SELECT TOP 10 destn_area_name, AVG(score) AS AvgScore FROM All_data GROUP BY destn_area_name ORDER BY AvgScore DESC", |
| "What are the worst 10 performing areas?": "SELECT TOP 10 destn_area_name, AVG(score) AS AvgScore FROM All_data GROUP BY destn_area_name ORDER BY AvgScore ASC", |
| "What districts have the highest volume of mail?": "SELECT TOP 10 destn_district_name, COUNT(*) AS Volume FROM All_data GROUP BY destn_district_name ORDER BY Volume DESC", |
| "What districts have the lowest volume of mail?": "SELECT TOP 10 destn_district_name, COUNT(*) AS Volume FROM All_data GROUP BY destn_district_name ORDER BY Volume ASC", |
| "What are the top 10 performing districts?": "SELECT TOP 10 destn_district_name, AVG(score) AS AvgScore FROM All_data GROUP BY destn_district_name ORDER BY AvgScore DESC", |
| "What are the worst 10 performing districts?": "SELECT TOP 10 destn_district_name, AVG(score) AS AvgScore FROM All_data GROUP BY destn_district_name ORDER BY AvgScore ASC", |
| "What districts gave the fastest delivery time?": "SELECT TOP 10 destn_district_name, AVG(avg_days_todelr) AS AvgDeliveryTime FROM All_data GROUP BY destn_district_name ORDER BY AvgDeliveryTime ASC"} |
| fs = str(few_shots) |
| |
|
|
| TEMPLATE = """Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer. |
| Use the following format: |
| |
| Question: "Question here" |
| SQLQuery: "SQL Query to run" |
| SQLResult: "Result of the SQLQuery" |
| Answer: "Final answer here" |
| |
| Only use the following tables: |
| |
| {table_info}. |
| |
| Some examples of SQL queries that correspond to questions are: |
| |
| \{"What are the top 10 performing areas?": "SELECT TOP 10 destn_area_name, AVG(score) AS AvgScore FROM All_data GROUP BY destn_area_name ORDER BY AvgScore DESC", |
| "What are the worst 10 performing areas?": "SELECT TOP 10 destn_area_name, AVG(score) AS AvgScore FROM All_data GROUP BY destn_area_name ORDER BY AvgScore ASC", |
| "What districts have the highest volume of mail?": "SELECT TOP 10 destn_district_name, COUNT(*) AS Volume FROM All_data GROUP BY destn_district_name ORDER BY Volume DESC", |
| "What districts have the lowest volume of mail?": "SELECT TOP 10 destn_district_name, COUNT(*) AS Volume FROM All_data GROUP BY destn_district_name ORDER BY Volume ASC", |
| "What are the top 10 performing districts?": "SELECT TOP 10 destn_district_name, AVG(score) AS AvgScore FROM All_data GROUP BY destn_district_name ORDER BY AvgScore DESC", |
| "What are the worst 10 performing districts?": "SELECT TOP 10 destn_district_name, AVG(score) AS AvgScore FROM All_data GROUP BY destn_district_name ORDER BY AvgScore ASC", |
| "What districts gave the fastest delivery time?": "SELECT TOP 10 destn_district_name, AVG(avg_days_todelr) AS AvgDeliveryTime FROM All_data GROUP BY destn_district_name ORDER BY AvgDeliveryTime ASC"\} |
| |
| Question: {input}""" |
|
|
| CUSTOM_PROMPT = PromptTemplate( |
| input_variables=["input", "table_info", "dialect"], template=TEMPLATE |
| ) |
|
|
| |
| return create_sql_query_chain(llm=llm, db=db) |
|
|
| |
| from langchain_experimental.sql import SQLDatabaseChain |
| |
|
|
|
|
| @cl.on_chat_start |
| async def main(): |
| |
| |
| await cl.Message(content="Welcome to GeoData!").send() |
| |
| |
| |
|
|
| |
|
|
|
|
| @cl.on_message |
| async def msg(message: str): |
| |
| |
| agent = create_agent() |
| m = message.content |
| |
| res = agent.run({"query": m}) |
| |
|
|
| print(res) |
| await cl.Message(content=res).send() |
|
|