In a Chatbot to chat with SQL using openai and langchain, how to integrate the chatbot to make simple conversations 
Labels: name: auto:bug, name: area: models

### System Info

python: 3.11
langchain: latest

### Who can help?

_No response_

### Information

- [ ] The official example notebooks/scripts
- [ ] My own modified scripts

### Related Components

- [ ] LLMs/Chat Models
- [ ] Embedding Models
- [ ] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [ ] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

in my chatbot which interact with sql db, if im typing hi its giving me the output as the entity of 1st row and 1st column, instead of answering with nothing or invalid question. and some time the response is generated in ascending order and when re runed the query in descending order, how to validate the answer before its given out? 

and how can i make my chatbot user friendly, like when user ask hi, it should greet back or when there is informal question it should just reply



import pandas as pd 
import sqlalchemy as sal 
import os, sys, openai
import constants 
from langchain.sql_database import SQLDatabase
from langchain.llms.openai import OpenAI
from langchain_experimental.sql import SQLDatabaseChain
from sqlalchemy import create_engine
from langchain.prompts import ChatPromptTemplate
# import ChatOpenAI
from langchain.memory import ConversationBufferMemory
# import sql_cmd 

from langchain.chat_models import ChatOpenAI 
from typing import List, Optional

from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.callbacks.manager import CallbackManagerForToolRun
from langchain.chat_models import ChatOpenAI
from langchain_experimental.plan_and_execute import (
    PlanAndExecute,
    load_agent_executor,
    load_chat_planner,
)
from langchain.sql_database import SQLDatabase
from langchain.text_splitter import TokenTextSplitter
from langchain.tools import BaseTool
from langchain.tools.sql_database.tool import QuerySQLDataBaseTool
from secret_key import openapi_key 
# from langchain import PromptTemplate
from langchain.prompts import PromptTemplate

# from langchain.prompts.PromptTemplate import PromptTemplate 

# from langchain.models import ChatGPTClient
# from langchain.utils import save_conversation


os.environ['OPENAI_API_KEY'] = openapi_key

from urllib.parse import quote_plus
server_name = constants.server_name
database_name = constants.database_name
username = constants.username 
password = constants.password
encoded_password = quote_plus(password)
connection_uri = f"mssql+pyodbc://{username}:{encoded_password}@{server_name}/{database_name}?driver=ODBC+Driver+17+for+SQL+Server"

# custom_suffix = """""
# If the SQLResult is empty, the Answer should be "No results found". DO NOT hallucinate an answer if there is no result."""  

engine = create_engine(connection_uri)

def chat(question,sql_format):
    
    model_name="gpt-3.5-turbo-16k"
    db = SQLDatabase(engine, view_support=True, include_tables=['EGV_emp_departments_ChatGPT'])
    # db2 = SQLDatabase(engine, view_support=True, include_tables=['EGV_emp_departments_ChatGPT'])
    # db = SQLDatabase(engine, view_support=True, include_tables=['EGV_emp_departments'])

    llm = ChatOpenAI(temperature=0, verbose=False, model=model_name)
    
    answer = None
    
    if sql_format==False:
        
        db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
        
        answer = db_chain.run(question)
        
    
    else:
        db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True , return_sql =True)
        
        sql_query = db_chain.run(question) 
        print("SQLQuery: "+str(sql_query))
        # result = engine.execute(sql_query)
        
        result_df = pd.read_sql(sql_query, engine)
        
        answer = result_df.to_dict()
        
        
        
    
    
    from langchain.prompts import PromptTemplate 

    PROMPT = """ 
    Given an input question, first create a syntactically correct mssql query to run,  
    then look at the results of the query and return the SQLResult as answer.  
    The question: {db_chain.run}
    """
    prompt_template = """" Use the following pieces of context to answer the question at the end. 
    If you don't know the answer, please think rationally and answer from your own knowledge base. 
    Don't consider the table which are not mentioned, if no result is matching with the keyword Please return the answer as invalid question 

    {context}

    Question: {questions}
    """
    PROMPT = PromptTemplate(
        template=prompt_template, input_variables=["context", "questions"]
    )
    
    def split_text(text, chunk_size, chunk_overlap=0):
        text_splitter = TokenTextSplitter(
            chunk_size=chunk_size, chunk_overlap=chunk_overlap
        )
        yield from text_splitter.split_text(text)


    class QuerySQLDatabaseTool2(QuerySQLDataBaseTool):
        def _run(
            self,
            query: str,
            run_manager: Optional[CallbackManagerForToolRun] = None,
        ) -> str:
            result = self.db.run_no_throw(query)

            return next(split_text(result, chunk_size=14_000))


    class SQLDatabaseToolkit2(SQLDatabaseToolkit):
        def get_tools(self) -> List[BaseTool]:
            tools = super().get_tools()
            original_query_tool_description = tools[0].description
            new_query_tool = QuerySQLDatabaseTool2(
                db=self.db, description=original_query_tool_description
            )
            tools[0] = new_query_tool

            return tools
        

    return answer
    
 
def chain1(question):
    text = chat(question,False)
    
    return text
    
    
def chain2(question):
    query = chat(question,True)
    
    return query
    
    
    it uses "\Lib\site-packages\langchain_experimental\sql\base.py"
"\Lib\site-packages\langchain_experimental\sql\vector_sql.py"
"\Lib\site-packages\langchain_experimental\sql\prompt.py"

### Expected behavior

in my chatbot which interact with sql db, if im typing hi its giving me the output as the entity of 1st row and 1st column, instead of answering with nothing or invalid question. and some time the response is generated in ascending order and when re runed the query in descending order, how to validate the answer before its given out? 

and how can i make my chatbot user friendly, like when user ask hi, it should greet back or when there is informal question it should just reply