jaimin commited on
Commit
e56f7a3
1 Parent(s): dc9d438

Update app.py

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