jaimin commited on
Commit
fe03cde
·
verified ·
1 Parent(s): 48fdcac

Upload 2 files

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