-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompetitive_intel_agent.py
More file actions
494 lines (451 loc) · 19 KB
/
competitive_intel_agent.py
File metadata and controls
494 lines (451 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
"""
Competitive Intelligence Agent — researches competitors and market using SerpApi + OpenAI.
Optional HubSpot integration for internal CRM context. Produces briefings with citations.
"""
import os
import json
import logging
import asyncio
from typing import Dict, List, Any, Optional
from dataclasses import dataclass
from datetime import datetime
from openai import OpenAI
import serpapi
from dotenv import load_dotenv
from urllib3.util import Retry
from hubspot import HubSpot
from hubspot.crm.companies import (
ApiException as CompaniesApiException,
Filter,
FilterGroup,
PublicObjectSearchRequest,
)
from hubspot.crm.contacts import ApiException as ContactsApiException
load_dotenv()
@dataclass
class CompetitiveIntelConfig:
"""Configuration for the competitive intelligence agent."""
openai_api_key: str
serpapi_api_key: str
hubspot_access_token: Optional[str] = None
model: str = "gpt-4o"
topn: int = 10
report_dir: Optional[str] = None
debug: bool = False
@classmethod
def from_env(cls) -> "CompetitiveIntelConfig":
required = ["OPENAI_API_KEY", "SERPAPI_API_KEY"]
missing = [v for v in required if not os.getenv(v)]
if missing:
raise ValueError(f"Missing required environment variables: {missing}")
report_dir = os.getenv("REPORT_DIR")
if report_dir and not os.path.isdir(report_dir):
os.makedirs(report_dir, exist_ok=True)
return cls(
openai_api_key=os.getenv("OPENAI_API_KEY"),
serpapi_api_key=os.getenv("SERPAPI_API_KEY"),
hubspot_access_token=os.getenv("HUBSPOT_ACCESS_TOKEN") or None,
model=os.getenv("OPENAI_MODEL", "gpt-4o"),
topn=int(os.getenv("RESULT_LIMIT", "10")),
report_dir=report_dir,
debug=os.getenv("DEBUG", "false").lower() == "true",
)
def search_web(api_key: str, query: str, topn: int = 10) -> str:
"""Search Google and return structured results."""
try:
params = {
"engine": "google",
"q": query,
"num": topn,
"api_key": api_key,
}
res = serpapi.search(params)
items = (res.get("organic_results") or [])[:topn]
data = [
{
"title": r.get("title"),
"snippet": r.get("snippet"),
"link": r.get("link"),
"source": "google",
}
for r in items
]
return json.dumps({"type": "web", "query": query, "results": data}, ensure_ascii=False)
except Exception as e:
return json.dumps({"type": "web", "query": query, "error": str(e)})
def search_news(api_key: str, query: str, date_range: str = "last_month", topn: int = 10) -> str:
"""Search Google News."""
try:
params = {
"engine": "google_news",
"q": query,
"api_key": api_key,
}
if date_range == "last_week":
params["tbs"] = "qdr:w"
elif date_range == "last_month":
params["tbs"] = "qdr:m"
res = serpapi.search(params)
items = (res.get("news_results") or [])[:topn]
data = [
{
"title": r.get("title"),
"snippet": r.get("snippet"),
"link": r.get("link"),
"date": r.get("date"),
"source": r.get("source"),
}
for r in items
]
return json.dumps({"type": "news", "query": query, "results": data}, ensure_ascii=False)
except Exception as e:
return json.dumps({"type": "news", "query": query, "error": str(e)})
def search_jobs(api_key: str, query: str, topn: int = 10) -> str:
"""Search Google Jobs (e.g. competitor hiring signals)."""
try:
params = {
"engine": "google_jobs",
"q": query,
"api_key": api_key,
}
res = serpapi.search(params)
jobs = res.get("jobs_results") or []
listings = jobs[:topn] if isinstance(jobs, list) else []
data = []
for j in listings:
if isinstance(j, dict):
data.append({
"title": j.get("title"),
"company": j.get("company_name"),
"location": j.get("location"),
"via": j.get("via"),
"description": (j.get("description") or "")[:500],
})
else:
data.append({"raw": str(j)})
return json.dumps({"type": "jobs", "query": query, "results": data}, ensure_ascii=False)
except Exception as e:
return json.dumps({"type": "jobs", "query": query, "error": str(e)})
TOOLS: List[Dict[str, Any]] = [
{
"type": "function",
"function": {
"name": "search_web",
"description": "Search Google for company info, positioning, reviews, pricing pages, and general competitive research.",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "search_news",
"description": "Search for recent news about companies: funding, product launches, exec moves, partnerships.",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"},
"date_range": {
"type": "string",
"description": "e.g. 'last_week', 'last_month'",
"enum": ["last_week", "last_month"],
},
},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "search_jobs",
"description": "Search job listings (e.g. competitor hiring, role growth, skills they invest in).",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"],
},
},
},
]
HUBSPOT_TOOLS: List[Dict[str, Any]] = [
{
"type": "function",
"function": {
"name": "hubspot_search_company_by_domain",
"description": "[HUBSPOT] Check if we have this company in HubSpot (competitor or account we track). Returns company id, name, domain.",
"parameters": {
"type": "object",
"properties": {"domain": {"type": "string", "description": "Company domain to search for"}},
"required": ["domain"],
},
},
},
{
"type": "function",
"function": {
"name": "hubspot_get_contact_by_email",
"description": "[HUBSPOT] Get contact details by email for internal context (who we know at this company).",
"parameters": {
"type": "object",
"properties": {"email": {"type": "string", "description": "Contact email address"}},
"required": ["email"],
},
},
},
{
"type": "function",
"function": {
"name": "hubspot_get_contact_activity_history",
"description": "[HUBSPOT] Get our notes, calls, and emails with this contact for internal competitive context.",
"parameters": {
"type": "object",
"properties": {"email": {"type": "string", "description": "Contact email address"}},
"required": ["email"],
},
},
},
]
class HubSpotService:
"""HubSpot CRM service for internal competitor/account context."""
def __init__(self, config: CompetitiveIntelConfig) -> None:
self.config = config
retry = Retry(
total=5,
backoff_factor=1,
status_forcelist=[429, 500, 502, 503, 504],
)
self.client = HubSpot(access_token=config.hubspot_access_token, retry=retry)
self._log = logging.getLogger("HubSpotService")
if config.debug:
self._log.setLevel(logging.DEBUG)
def search_company_by_domain(self, domain: str) -> Optional[Dict[str, Any]]:
try:
domain_filter = Filter(property_name="domain", operator="EQ", value=domain)
search_request = PublicObjectSearchRequest(
filter_groups=[FilterGroup(filters=[domain_filter])],
properties=["name", "domain", "hs_object_id"],
limit=1,
)
response = self.client.crm.companies.search_api.do_search(search_request)
if response.results:
company = response.results[0]
return {
"id": company.id,
"name": company.properties.get("name"),
"domain": company.properties.get("domain"),
}
return None
except CompaniesApiException as e:
self._log.error("Company search failed: %s", e)
return None
def get_contact_by_email(self, email: str) -> Optional[Dict[str, Any]]:
try:
contact = self.client.crm.contacts.basic_api.get_by_id(
contact_id=email,
id_property="email",
properties=["firstname", "lastname", "email", "phone", "lifecyclestage"],
)
return contact.to_dict()
except ContactsApiException as e:
if e.status == 404:
return None
self._log.error("Contact retrieval error: %s", e)
raise
def get_contact_activity_history(self, email: str) -> List[Dict[str, Any]]:
contact = self.get_contact_by_email(email)
if not contact:
return []
contact_id = contact["id"]
history: List[Dict[str, Any]] = []
activity_types = {"note": "note", "email": "email", "call": "call"}
for object_type in activity_types:
try:
assoc = self.client.crm.associations.v4.basic_api.get_page(
object_type="contact",
object_id=contact_id,
to_object_type=object_type,
)
ids = [r.to_object_id for r in assoc.results]
if not ids:
continue
props = ["hs_timestamp", "hubspot_owner_id"]
if object_type == "note":
props.append("hs_note_body")
elif object_type == "email":
props.append("hs_email_subject")
elif object_type == "call":
props.append("hs_call_title")
batch = getattr(self.client.crm.objects, f"{object_type}s").batch_api.read(
batch_read_input_simple_public_object_id={
"properties": props,
"inputs": [{"id": oid} for oid in ids],
}
)
for eng in batch.results:
history.append({
"type": object_type,
"timestamp": eng.properties.get("hs_timestamp"),
"content": self._extract_content(eng.properties, object_type),
})
except Exception as e:
self._log.warning("Failed to get %s history: %s", object_type, e)
continue
return sorted(history, key=lambda x: x.get("timestamp") or "", reverse=True)
@staticmethod
def _extract_content(properties: Dict[str, Any], object_type: str) -> str:
if object_type == "note":
return properties.get("hs_note_body") or ""
if object_type == "email":
return properties.get("hs_email_subject") or ""
if object_type == "call":
return properties.get("hs_call_title") or ""
return ""
async def call_tool(self, tool_name: str, arguments: Dict[str, Any]) -> str:
try:
if tool_name == "hubspot_search_company_by_domain":
result = self.search_company_by_domain(arguments.get("domain", ""))
return json.dumps(result or {"error": "Company not found"})
if tool_name == "hubspot_get_contact_by_email":
result = self.get_contact_by_email(arguments.get("email", ""))
return json.dumps(result or {"error": "Contact not found"})
if tool_name == "hubspot_get_contact_activity_history":
result = self.get_contact_activity_history(arguments.get("email", ""))
return json.dumps({"activities": result})
return json.dumps({"error": f"Unknown HubSpot tool: {tool_name}"})
except Exception as e:
self._log.error("HubSpot tool %s failed: %s", tool_name, e)
return json.dumps({"error": str(e)})
class CompetitiveIntelligenceAgent:
"""Agent that researches competitors and produces briefings using SerpApi + OpenAI; optional HubSpot for internal context."""
def __init__(self, config: Optional[CompetitiveIntelConfig] = None):
self.config = config or CompetitiveIntelConfig.from_env()
self.client = OpenAI(api_key=self.config.openai_api_key)
self.tools = list(TOOLS)
self.hubspot_service: Optional[HubSpotService] = None
if self.config.hubspot_access_token:
self.hubspot_service = HubSpotService(self.config)
self.tools = self.tools + list(HUBSPOT_TOOLS)
self.messages: List[Dict[str, Any]] = []
level = logging.DEBUG if self.config.debug else logging.INFO
logging.basicConfig(level=level, format="[%(levelname)s] %(message)s")
self.log = logging.getLogger("CompetitiveIntelAgent")
self._setup_system_prompt()
def _setup_system_prompt(self) -> None:
today_str = datetime.now().strftime("%Y-%m-%d")
hubspot_note = ""
if self.hubspot_service:
hubspot_note = (
" When HubSpot is available, you can look up companies by domain, contacts by email, "
"and activity history (notes, calls, emails) to combine external intel with what we already know in CRM."
)
sys_prompt = (
f"Today's date is {today_str}. "
"You are an AI competitive intelligence analyst. "
"You have access to web search, news search, and job search."
f"{hubspot_note} "
"Your job is to:\n"
"1. Research companies and competitors using search_web (positioning, pricing, reviews).\n"
"2. Find recent news with search_news (funding, launches, exec changes).\n"
"3. Use search_jobs to infer hiring focus and growth (e.g. 'Company X jobs').\n"
"4. When HubSpot tools are available, use them to add internal context (companies we track, contacts, our past interactions).\n"
"5. Synthesize a clear, actionable briefing with key findings and citations [1], [2], ...\n\n"
"Always cite sources by number. Be concise; highlight differentiators, pricing/positioning, and recent moves."
)
self.messages = [{"role": "system", "content": sys_prompt}]
async def _call_tool(self, name: str, arguments: Dict[str, Any]) -> str:
if name == "search_web":
return search_web(
self.config.serpapi_api_key,
arguments.get("query", ""),
self.config.topn,
)
if name == "search_news":
return search_news(
self.config.serpapi_api_key,
arguments.get("query", ""),
arguments.get("date_range", "last_month"),
self.config.topn,
)
if name == "search_jobs":
return search_jobs(
self.config.serpapi_api_key,
arguments.get("query", ""),
self.config.topn,
)
if name.startswith("hubspot_") and self.hubspot_service:
return await self.hubspot_service.call_tool(name, arguments)
return json.dumps({"error": f"Unknown tool: {name}"})
async def run_once(self, user_input: str) -> str:
"""Process one user message and return the final briefing."""
self.messages.append({"role": "user", "content": user_input})
while True:
response = self.client.chat.completions.create(
model=self.config.model,
messages=self.messages,
tools=self.tools,
)
msg = response.choices[0].message
tool_calls = msg.tool_calls
if not tool_calls:
answer = msg.content or ""
self.messages.append({"role": "assistant", "content": answer})
return answer
self.messages.append(msg)
for tc in tool_calls:
name = tc.function.name
args = json.loads(tc.function.arguments)
content = await self._call_tool(name, args)
self.messages.append({
"tool_call_id": tc.id,
"role": "tool",
"name": name,
"content": content,
})
def save_conversation(self, path: str) -> None:
"""Save conversation history to a JSON file."""
with open(path, "w") as f:
json.dump(self.messages, f, indent=2)
self.log.info("Conversation saved to %s", path)
async def main() -> None:
import argparse
parser = argparse.ArgumentParser(description="Competitive Intelligence Agent")
parser.add_argument("-q", "--query", help="Single query mode")
parser.add_argument("-m", "--model", default="gpt-4o", choices=["gpt-4o", "gpt-4o-mini"])
parser.add_argument("-n", "--topn", type=int, default=10, help="Max results per search")
parser.add_argument("-d", "--debug", action="store_true", help="Enable debug logging")
parser.add_argument("-o", "--outfile", help="Save conversation to JSON file")
args = parser.parse_args()
config = CompetitiveIntelConfig.from_env()
config.model = args.model
config.topn = args.topn
config.debug = args.debug
agent = CompetitiveIntelligenceAgent(config)
try:
if args.query:
result = await agent.run_once(args.query)
print(result)
else:
print("Competitive Intelligence Agent started. Type 'exit' to quit.")
while True:
try:
user_input = input("\n> ")
if user_input.strip().lower() in {"exit", "quit"}:
print("Goodbye!")
break
result = await agent.run_once(user_input)
print(result)
except (KeyboardInterrupt, EOFError):
print("\nExiting...")
break
if args.outfile:
agent.save_conversation(args.outfile)
print(f"Conversation saved to {args.outfile}")
except Exception as e:
logging.error("Application failed: %s", e)
raise
if __name__ == "__main__":
asyncio.run(main())