File size: 4,064 Bytes
f2cee7b 40eb878 f2cee7b a937f8b f2cee7b e930894 f2cee7b 9a3d917 f2cee7b a937f8b | 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | import os
import openai
from langchain.agents import create_csv_agent
from langchain.llms import OpenAI
from langchain.document_loaders.csv_loader import CSVLoader
import gradio as gr
openai.api_key = os.environ["OPENAI_API_KEY"]
#Dataset - Black Friday Sales
import pandas as pd
df = pd.read_csv('./content/train.csv')
df.head()
#CSV Agent
#note: this agent calls the Pandas DataFrame agent under the hood, which in turn calls the Python agent, which executes LLM generated
# Python code - this can be bad if the LLM generated Python code is harmful. Use cautiously.
agent = create_csv_agent(OpenAI(temperature=0),
'./content/train.csv',
verbose=True)
agent
agent.agent.llm_chain.prompt.template
#You are working with a pandas dataframe in Python. The name of the dataframe is df. You should use the tools below to answer the question posed of you:
#python_repl_ast: A Python shell. Use this to execute python commands. Input should be a valid python command. When using this tool, sometimes output is abbreviated - make sure it does not look abbreviated before using it in your answer.
#Use the following format:
#Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [python_repl_ast] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question
#This is the result of print(df.head()): {df}
#Begin! Question: {input} {agent_scratchpad}
print ('Response: ' + agent.run("how many rows are there?"))
#agent.run("how many people are female?")
#agent.run("how many people have stayed more than 3 years in the city?")
#agent.run("how many people have stayed more than 3 years in the city and are female?")
#agent.run("Are there more males or females?")
#LangChain CSV Loader
loader = CSVLoader(file_path='./content/train.csv')
data = loader.load()
#Convert 1 XLS file to csv
xls_file = r'./content/excel_file_example.xlsx'
output_csv = r'./content/excel_file_example.csv'
# Read the XLS file using pandas and openpyxl as the engine
data = pd.read_excel(xls_file, engine='openpyxl')
# Save the data as a CSV file
data.to_csv(output_csv, index=False)
#Convert 1
#Convert 2 XLS file to csv
xls_file2 = r'./content/marea_negra_2022.xlsx'
output_csv2 = r'./content/marea_negra_2022.csv'
# Read the XLS file using pandas and openpyxl as the engine
data = pd.read_excel(xls_file2, engine='openpyxl')
# Save the data as a CSV file
data.to_csv(output_csv2, index=False)
#Convert 2
#Calling agent convert 1
agent = create_csv_agent(OpenAI(temperature=0),
'./content/excel_file_example.csv',
verbose=True)
#Calling agent convert 2
agent2 = create_csv_agent(OpenAI(temperature=0),
'./content/marea_negra_2022.csv',
verbose=True)
#Calling agent convert 3 Tabs From Original File
agent3 = create_csv_agent(OpenAI(temperature=0),
'./content/marea_negra_2022_clientes.csv',
verbose=True)
#Calling agent convert 4 Tabs From Original File
agent4 = create_csv_agent(OpenAI(temperature=0),
'./content/Empresas Base de prueba.csv',
verbose=True)
#agent.run("What are the column names?")
#agent3.run("Dame la lista de clientes ")
#agent3.run("Dame la lista de clientes ya contactados ")
#agent3.run("Dame la lista de clientes en prospeccion")
#agent4.run("Dame las ventas totales de agosto 2022")
#agent.run("Which country appears the most and how many times does it appear?")
#agent.run("What is the ratio of males to females?")
#First Interface
def random_response(message, history):
query = message
agent4.run(query)
return agent4.run(query)
gr.ChatInterface(random_response).launch(share=False)
#First Interface
|