How to get the complete output as answer
Labels: name: auto:question, name: area: models

### System Info

python: 3.11
langchain:latest

### Who can help?

in an chatbot after running a query it will return the SQLResult, but while giving an output answer complete result is not displayed

code:
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
# import ChatOpenAI

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.PromptTemplate import PromptTemplate 

# from langchain.models import ChatGPTClient
# from langchain.utils import save_conversation


os.environ['OPENAI_API_KEY'] = openapi_key

def chat(question):
    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)
    model_name="gpt-3.5-turbo-16k"
    db = 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)
    db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
    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 db_chain.run(question) 

answer= chat("Aadhaar number of B#########")
print(answer) 

result:
> Entering new SQLDatabaseChain chain...
Aadhaar number of Bhuvaneshwari
SQLQuery:SELECT [Aadhaar Number] 
FROM [EGV_emp_departments_ChatGPT] 
WHERE [EmployeeName] = 'B##########'
SQLResult: [('91#######',), ('71#########',)]
Answer:91######
> Finished chain.
9########

### 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 an chatbot after running a query it will return the SQLResult, but while giving an output answer complete result is not displayed

code:
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
# import ChatOpenAI

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.PromptTemplate import PromptTemplate 

# from langchain.models import ChatGPTClient
# from langchain.utils import save_conversation


os.environ['OPENAI_API_KEY'] = openapi_key

def chat(question):
    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)
    model_name="gpt-3.5-turbo-16k"
    db = 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)
    db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
    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 db_chain.run(question) 

answer= chat("Aadhaar number of B#########")
print(answer) 

result:
> Entering new SQLDatabaseChain chain...
Aadhaar number of Bhuvaneshwari
SQLQuery:SELECT [Aadhaar Number] 
FROM [EGV_emp_departments_ChatGPT] 
WHERE [EmployeeName] = 'B##########'
SQLResult: [('91#######',), ('71#########',)]
Answer:91######
> Finished chain.
9########

### Expected behavior

in an chatbot after running a query it will return the SQLResult, but while giving an output answer complete result is not displayed

code:
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
# import ChatOpenAI

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.PromptTemplate import PromptTemplate 

# from langchain.models import ChatGPTClient
# from langchain.utils import save_conversation


os.environ['OPENAI_API_KEY'] = openapi_key

def chat(question):
    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)
    model_name="gpt-3.5-turbo-16k"
    db = 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)
    db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
    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 db_chain.run(question) 

answer= chat("Aadhaar number of B#########")
print(answer) 

result:
> Entering new SQLDatabaseChain chain...
Aadhaar number of Bhuvaneshwari
SQLQuery:SELECT [Aadhaar Number] 
FROM [EGV_emp_departments_ChatGPT] 
WHERE [EmployeeName] = 'B##########'
SQLResult: [('91#######',), ('71#########',)]
Answer:91######
> Finished chain.
9########


instead of a single output or top5 output, need the complete result from SQLResults as answe.