suneeln-duke commited on
Commit
545c4ba
1 Parent(s): 36ea3c3

Added all agents

Browse files
agents/eda_agent.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from langchain.agents.agent_types import AgentType
2
+ import pandas as pd
3
+
4
+ import sys
5
+
6
+ sys.path.append('..')
7
+
8
+ from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
9
+ from langchain_openai import OpenAI, ChatOpenAI
10
+ from langchain.agents.agent_types import AgentType
11
+
12
+ def init_config(df):
13
+
14
+ llm = ChatOpenAI(model_name="gpt-4")
15
+
16
+ agent = create_pandas_dataframe_agent(llm=llm, df=df, verbose=True)
17
+
18
+ return agent
19
+
20
+ def answer_question(agent, question):
21
+ return agent.invoke(question)['output']
22
+
agents/master_agent.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain import hub
2
+ from langchain.agents import AgentExecutor, create_openai_tools_agent
3
+ from langchain.agents import load_tools
4
+ from langchain_openai import ChatOpenAI
5
+
6
+ def init_config():
7
+
8
+ search_tool = load_tools(['serpapi'])
9
+
10
+ tools = [search_tool[0]]
11
+
12
+ prompt = hub.pull("hwchase17/openai-tools-agent")
13
+
14
+ llm = ChatOpenAI(model="gpt-4", temperature=0)
15
+
16
+ # Construct the OpenAI Tools agent
17
+ agent = create_openai_tools_agent(llm, tools, prompt)
18
+
19
+ agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
20
+
21
+ return agent_executor
22
+
23
+ def answer_question(agent, question):
24
+
25
+ full_prompt = f"""
26
+ You are a master AI that can control other AI agents. You are specifically designed to automate plant maintenance.
27
+ You will recieve a prompt from a user, and will have to classify the prompt's purpose as one of the following categories:
28
+ [1] Plant Maintenance
29
+ [2] AI modeling & EDA
30
+ [3] Questions about the plant
31
+
32
+ Some samples of the prompts are:
33
+ [1] "Release the fertilizer", "Turn lights on", "Turn lights off", "Water the plant"
34
+ [2] "What is the accuracy of the model?", "What is the distribution of the data?", "What is the correlation between the features?", "Train a regression model", "Plot the distribution of the data"
35
+ [3] "What's the optimum moisture of the plant?", "Where does it generally grow?", "What is the plant's life cycle?", "What is the plant's scientific name?.
36
+
37
+ Return the category number and name of the prompt in a JSON format.
38
+
39
+ User: {question}
40
+ """
41
+
42
+ return agent.invoke({"input": full_prompt})['output']
agents/plant_agent.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain import hub
2
+ from langchain.agents import AgentExecutor, create_openai_tools_agent
3
+ from langchain.agents import load_tools
4
+ from langchain_openai import ChatOpenAI
5
+
6
+ def init_config():
7
+
8
+ search_tool = load_tools(['serpapi'])
9
+
10
+ tools = [search_tool[0]]
11
+
12
+ prompt = hub.pull("hwchase17/openai-tools-agent")
13
+
14
+ llm = ChatOpenAI(model="gpt-4", temperature=0)
15
+
16
+ # Construct the OpenAI Tools agent
17
+ agent = create_openai_tools_agent(llm, tools, prompt)
18
+
19
+ agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
20
+
21
+ return agent_executor
22
+
23
+ def answer_question(agent, question):
24
+
25
+ full_prompt = f"""
26
+ You are a master AI that can control other AI agents. You are specifically designed to automate plant maintenance.
27
+ You will recieve a prompt from a user regarding plant maintenance, and will have to classify the prompt's purpose as one of the following categories:
28
+ [0] Fertilizing the plant
29
+ [1] Turning plant lights on
30
+ [2] Turning plant lights off
31
+ [3] Watering the plant
32
+
33
+ Return the category number and name of the prompt in a JSON format.
34
+
35
+ User: {question}
36
+ """
37
+
38
+ return agent.invoke({"input": full_prompt})['output']
agents/rag_agent.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ from langchain.embeddings import OpenAIEmbeddings
5
+ from langchain.vectorstores.chroma import Chroma
6
+ import os
7
+ import shutil
8
+
9
+ from langchain.vectorstores.chroma import Chroma
10
+ from langchain.embeddings import OpenAIEmbeddings
11
+
12
+ from langchain.agents.agent_toolkits import create_retriever_tool
13
+ from langchain.agents.agent_toolkits import create_conversational_retrieval_agent
14
+
15
+ from langchain.agents import load_tools
16
+
17
+ from langchain_openai import ChatOpenAI
18
+
19
+
20
+ def init_config(loader):
21
+ # We use the loader created above to load the document
22
+ documents = loader.load()
23
+
24
+ # We split the document into several chunks as mentioned above
25
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=50)
26
+ texts = text_splitter.split_documents(documents)
27
+
28
+ CHROMA_PATH = "../data/plant_chroma"
29
+
30
+ if os.path.exists(CHROMA_PATH):
31
+ db = Chroma(persist_directory=CHROMA_PATH,embedding_function=OpenAIEmbeddings())
32
+
33
+ else:
34
+ db = Chroma.from_documents(
35
+ texts, OpenAIEmbeddings(), persist_directory=CHROMA_PATH
36
+ )
37
+ db.persist()
38
+ print(f"Saved {len(texts)} chunks to {CHROMA_PATH}.")
39
+
40
+ retriever = db.as_retriever()
41
+
42
+ # This is the prompt to create a RAG agent for us
43
+
44
+
45
+ retriever_name = "plant_os_pdf"
46
+ retriever_desc = """The purpose of this tool is to answer questions about the blue indigo false plant and its maintenance."""
47
+
48
+ rag_tool = create_retriever_tool(
49
+ retriever,
50
+ retriever_name,
51
+ retriever_desc
52
+ )
53
+
54
+ search_tool = load_tools(['serpapi'])
55
+ tools = [rag_tool, search_tool[0]]
56
+
57
+ llm = ChatOpenAI(model_name="gpt-4")
58
+
59
+ RAG_executor = create_conversational_retrieval_agent(llm=llm, tools=tools, verbose=True) # setting verbose=True to output the thought process of the agent
60
+
61
+ return RAG_executor
62
+
63
+ def answer_question(agent, question):
64
+ question = "what is the scientific for the plant?"
65
+
66
+ user_query = {"input": question}
67
+
68
+ result = agent(user_query)
69
+
70
+ return result['output']
main.py CHANGED
@@ -1,6 +1,35 @@
1
  from fastapi import FastAPI
2
 
 
 
 
 
 
3
  from langchain.document_loaders import DirectoryLoader
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  app = FastAPI()
6
 
 
1
  from fastapi import FastAPI
2
 
3
+ import os
4
+ import json
5
+ import sys
6
+
7
+ import pandas as pd
8
  from langchain.document_loaders import DirectoryLoader
9
+ from agents import master_agent, plant_agent, eda_agent, rag_agent
10
+
11
+ app = FastAPI()
12
+
13
+ os.environ['OPENAI_API_KEY'] = os.getenv['openapi-key']
14
+ os.environ['SERPAPI_API_KEY'] = os.getenv['serpapi-key']
15
+
16
+ master = master_agent.init_config()
17
+
18
+ print("init master agent")
19
+
20
+ plant = plant_agent.init_config()
21
+
22
+ print("init plant agent")
23
+
24
+ df = pd.read_csv('data/csv/plant_syn.csv')
25
+
26
+ eda = eda_agent.init_config(df)
27
+
28
+ print("init eda agent")
29
+
30
+ loader = DirectoryLoader("data/txt", glob="*.txt")
31
+
32
+ rag = rag_agent.init_config(loader)
33
 
34
  app = FastAPI()
35