import os from typing import Optional, Tuple import gradio as gr from langchain.agents import create_pandas_dataframe_agent from langchain.llms import Anthropic import pandas as pd def load_data(): """Load the data you want to use for the agent.""" return pd.read_parquet("data/part.17.parquet") def load_agent(trans: pd.DataFrame) -> create_pandas_dataframe_agent: """Logic for loading the agent we want to use.""" llm = Anthropic(temperature=0) trans = load_data() return create_pandas_dataframe_agent(llm, trans, verbose=True) def set_anthropic_api_key(api_key: str): """Set the api key and return agent. If no api_key, then None is returned. """ if api_key: os.environ["ANTHROPIC_API_KEY"] = api_key agent = load_agent() os.environ["ANTHROPIC_API_KEY"] = "" return agent def chat( inp: str, history: Optional[Tuple[str, str]], agent: Optional[create_pandas_dataframe_agent] ): """Execute the chat functionality.""" history = history or [] # If agent is None, that is because no API key was provided. if agent is None: history.append((inp, "Please paste your Anthropic key to use")) return history, history # Run agent and append input. output = agent.run(input=inp) history.append((inp, output)) return history, history block = gr.Blocks(css=".gradio-container {background-color: lightgray}") with block: with gr.Row(): gr.Markdown("