Update app.py
Browse files
app.py
CHANGED
@@ -1,143 +1,10 @@
|
|
1 |
-
|
2 |
-
|
3 |
import streamlit as st
|
4 |
-
import
|
5 |
-
from
|
6 |
-
from
|
7 |
-
from langchain_community.
|
8 |
-
from langchain.
|
9 |
-
from langchain.
|
10 |
-
from
|
11 |
-
|
12 |
-
from langgraph.graph import StateGraph, END
|
13 |
-
from langchain_core.messages import HumanMessage
|
14 |
-
from typing_extensions import TypedDict
|
15 |
-
from typing import Annotated, Sequence
|
16 |
-
import functools
|
17 |
-
import operator
|
18 |
-
|
19 |
-
|
20 |
-
# Initialize tools
|
21 |
-
llm = ChatOpenAI()
|
22 |
-
|
23 |
-
tavily_tool = TavilySearchResults(max_results=5)
|
24 |
-
search_google_tool = Tool(
|
25 |
-
name="GoogleSearch",
|
26 |
-
func=GoogleSearchAPIWrapper().run,
|
27 |
-
description="Search information using Google Search API."
|
28 |
-
)
|
29 |
-
|
30 |
-
duckduck_search_tool = Tool(
|
31 |
-
name="DuckDuckGoSearch",
|
32 |
-
func=DuckDuckGoSearchRun().run,
|
33 |
-
description="Search information using DuckDuckGo."
|
34 |
-
)
|
35 |
-
|
36 |
-
serper_tool = Tool(
|
37 |
-
name="GoogleSerperSearch",
|
38 |
-
func=GoogleSerperAPIWrapper(max_results=5).run,
|
39 |
-
description="Perform searches using Google Serper API."
|
40 |
-
)
|
41 |
-
|
42 |
-
tavily_tool_wrapped = Tool(
|
43 |
-
name="TavilySearch",
|
44 |
-
func=tavily_tool.run,
|
45 |
-
description="Retrieve search results from Tavily API."
|
46 |
-
)
|
47 |
-
|
48 |
-
# Define reusable function for agent creation
|
49 |
-
def create_agent(llm: ChatOpenAI, tools: list, system_prompt: str):
|
50 |
-
prompt = ChatPromptTemplate.from_messages(
|
51 |
-
[
|
52 |
-
("system", system_prompt),
|
53 |
-
MessagesPlaceholder(variable_name="messages"),
|
54 |
-
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
55 |
-
]
|
56 |
-
)
|
57 |
-
agent = create_openai_tools_agent(llm, tools, prompt)
|
58 |
-
executor = AgentExecutor(agent=agent, tools=tools)
|
59 |
-
return executor
|
60 |
-
|
61 |
-
|
62 |
-
# Define agents
|
63 |
-
def get_agents():
|
64 |
-
cto_agent = create_agent(
|
65 |
-
llm,
|
66 |
-
[duckduck_search_tool],
|
67 |
-
"You are a CTO name finder. Extract the CTO's name from the provided company data."
|
68 |
-
)
|
69 |
-
|
70 |
-
glassdoor_agent = create_agent(
|
71 |
-
llm,
|
72 |
-
[tavily_tool_wrapped, serper_tool],
|
73 |
-
"You are a Glassdoor review scraper. Retrieve reviews about the given company. "
|
74 |
-
"Consider points like Overall Rating, Compensation, Senior Management, Career Opportunities."
|
75 |
-
"Provide stars for each point."
|
76 |
-
"Always scrap the same data"
|
77 |
-
)
|
78 |
-
|
79 |
-
competitor_agent = create_agent(
|
80 |
-
llm,
|
81 |
-
[tavily_tool_wrapped, serper_tool],
|
82 |
-
"You are a competitor finder. Provide details such as a description of competitors and their primary differences."
|
83 |
-
"Output the results in a table format."
|
84 |
-
)
|
85 |
-
|
86 |
-
information_agent = create_agent(
|
87 |
-
llm,
|
88 |
-
[search_google_tool, duckduck_search_tool, serper_tool],
|
89 |
-
"You are an information collector. Retrieve details such as Website, Sector, Industry, Location, Employees, Founding Year, and LinkedIn URL."
|
90 |
-
"Linkedin URL will be always like this https://www.linkedin.com/company/company_name"
|
91 |
-
)
|
92 |
-
|
93 |
-
return cto_agent, glassdoor_agent, competitor_agent, information_agent
|
94 |
-
|
95 |
-
|
96 |
-
# Streamlit App
|
97 |
-
def main():
|
98 |
-
st.title("Company Insights API")
|
99 |
-
st.write("Enter a company name to fetch details about its CTO, competitors, Glassdoor reviews, and general information.")
|
100 |
-
|
101 |
-
# Input for company name
|
102 |
-
company_name = st.text_input("Enter company name")
|
103 |
-
run_queries = st.button("Run Queries")
|
104 |
-
|
105 |
-
if run_queries:
|
106 |
-
# Prepare agents
|
107 |
-
cto_agent, glassdoor_agent, competitor_agent, information_agent = get_agents()
|
108 |
-
|
109 |
-
# Queries
|
110 |
-
queries = {
|
111 |
-
"CTO": f"Who is the CTO of {company_name}?",
|
112 |
-
"Glassdoor Reviews": f"What are the Glassdoor reviews of {company_name}?",
|
113 |
-
"Competitors": f"What are the competitors of {company_name}?",
|
114 |
-
"Information": f"Give me all information about {company_name}.",
|
115 |
-
}
|
116 |
-
|
117 |
-
results = {}
|
118 |
-
for query_name, query in queries.items():
|
119 |
-
agent = {
|
120 |
-
"CTO": cto_agent,
|
121 |
-
"Glassdoor Reviews": glassdoor_agent,
|
122 |
-
"Competitors": competitor_agent,
|
123 |
-
"Information": information_agent,
|
124 |
-
}[query_name]
|
125 |
-
|
126 |
-
state = {
|
127 |
-
"messages": [HumanMessage(content=query)]
|
128 |
-
}
|
129 |
-
|
130 |
-
try:
|
131 |
-
response = agent.invoke(state)
|
132 |
-
results[query_name] = response.get("output", "No response")
|
133 |
-
except Exception as e:
|
134 |
-
results[query_name] = f"Error: {e}"
|
135 |
-
|
136 |
-
# Display results
|
137 |
-
for query_name, result in results.items():
|
138 |
-
st.subheader(query_name)
|
139 |
-
st.write(result)
|
140 |
-
|
141 |
-
|
142 |
-
if __name__ == "__main__":
|
143 |
-
main()
|
|
|
1 |
+
import os, dotenv
|
|
|
2 |
import streamlit as st
|
3 |
+
from langchain_groq import ChatGroq
|
4 |
+
from langchain_core.prompts import ChatPromptTemplate
|
5 |
+
from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
|
6 |
+
from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
|
7 |
+
from langchain.agents import create_react_agent
|
8 |
+
from langchain.agents import AgentExecutor
|
9 |
+
from langchain_community.callbacks.streamlit import StreamlitCallbackHandler
|
10 |
+
dotenv.load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|