-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutils.py
36 lines (28 loc) · 1.07 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# type: ignore
import os
from typing import TextIO
import openai
import pandas as pd
import streamlit as st
from langchain.agents import create_csv_agent, create_pandas_dataframe_agent
from langchain.llms import OpenAI
#openai.api_key = st.secrets["OPENAI_API_KEY"]
def get_answer_csv(file: TextIO, query: str) -> str:
"""
Returns the answer to the given query by querying a CSV file.
Args:
- file (str): the file path to the CSV file to query.
- query (str): the question to ask the agent.
Returns:
- answer (str): the answer to the query from the CSV file.
"""
# Load the CSV file as a Pandas dataframe
# df = pd.read_csv(file)
#df = pd.read_csv("titanic.csv")
# Create an agent using OpenAI and the Pandas dataframe
agent = create_csv_agent(OpenAI(temperature=0), file, verbose=False)
#agent = create_pandas_dataframe_agent(OpenAI(temperature=0), df, verbose=False)
# Run the agent on the given query and return the answer
#query = "whats the square root of the average age?"
answer = agent.run(query)
return answer