Skip to content

Commit

Permalink
csv qa code added
Browse files Browse the repository at this point in the history
  • Loading branch information
sudarshan-koirala committed Jul 31, 2023
1 parent 763dda2 commit dc848a4
Showing 1 changed file with 64 additions and 11 deletions.
75 changes: 64 additions & 11 deletions demo_app/main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,79 @@
"""Python file to serve as the frontend"""

from langchain.agents import create_pandas_dataframe_agent
from langchain.llms import OpenAI
import pandas as pd
import chainlit as cl
import io

import sys
import os
sys.path.append(os.path.abspath('.'))

from langchain import PromptTemplate, OpenAI, LLMChain
import chainlit as cl

from chainlit import user_session

user_env = user_session.get("env")

# os.environ["OPENAI_API_KEY"] = ""
# Chainlit fetches env variables from .env automatically

""" from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
OPENAI_API_KEY= os.getenv("OPENAI_API_KEY")
"""


def create_agent(data: str, llm):
"""Create a Pandas DataFrame agent."""
return create_pandas_dataframe_agent(llm, data, verbose=False)


template = """Question: {question}
@cl.on_chat_start
async def on_chat_start():

Answer: Let's think step by step."""
files = None

# Waits for user to upload csv data
while files == None:
files = await cl.AskFileMessage(
content="Please upload a csv file to begin!", accept=["text/csv"], max_size_mb= 100
).send()

# load the csv data and store in user_session
file = files[0]
csv_file = io.BytesIO(file.content)
df = pd.read_csv(csv_file, encoding="utf-8")

# creating user session to store data
cl.user_session.set('data', df)

# Send response back to user
await cl.Message(
content=f"`{file.name}` uploaded! Now you ask me anything related to your data"
).send()


@cl.on_message
async def main(message: str):

# Get data
df = cl.user_session.get('data')

@cl.langchain_factory(use_async=True)
def factory():
user_env = cl.user_session.get("env")
os.environ["OPENAI_API_KEY"] = user_env.get("OPENAI_API_KEY")
prompt = PromptTemplate(template=template, input_variables=["question"])
llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0), verbose=True)

return llm_chain
# llm
llm = OpenAI()

# Agent creation
agent = create_agent(df, llm)

# Run model
response = agent.run(message)

# Send a response back to the user
await cl.Message(
content=response,
).send()

0 comments on commit dc848a4

Please sign in to comment.