Issue: <How to store/export the output of a chatbot to excel  ' prefix>
Labels: name: auto:question, name: area: models

### Issue you'd like to raise.

i have created a chatbot to chat with the sql database using openai and langchain, but how to store or output data into excel using langchain.

I got some idea from chatgpt which i have integrated with my code, but there is an error while importing the modules

below is my 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.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_acadamics_ChatGPT'])

    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 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("give the names of employees who have completed PG")
answer= chat("give the list of employees joined in january and february of 2023 with Employee ID, Name, Department,Date of join")
print(answer) 

conversation_data= chatgpt.chat(prompt="convert into .csv and .xlsx only if the multiple values are asked in the question, if one a single thing is asked, just give the answer in chatbot no need to save the answer")
# conversation_data = chat("convert into .csv and .xlsx only if the multiple values are asked in the question, if one a single thing is asked, just give the answer in chatbot no need to save the answer")

save_conversation(conversation_data, "chat_data.csv")

df = pd.read_csv("chat_data.csv")

path = r"C:\Users\rndbcpsoft\OneDrive\Desktop\test\chat_data.xlsx"
df.to_excel(path, index=False)

print(f"Conversation data has been saved to '{path}' in Excel format.")


### Suggestion:

_No response_