Update marketing_graph.py
Browse files- marketing_graph.py +34 -6
marketing_graph.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
-
# marketing_graph.py
|
| 2 |
import os
|
| 3 |
from langgraph.graph import StateGraph, START, END
|
| 4 |
from langchain_core.tracers.context import tracing_v2_enabled
|
|
|
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
# LangSmith / LangGraph setup
|
|
@@ -10,16 +10,33 @@ os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
|
| 10 |
os.environ["LANGCHAIN_PROJECT"] = "web_Scrapping_agent21"
|
| 11 |
os.environ["LANGCHAIN_API_KEY"] = "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"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# ----------------------------
|
| 14 |
# Node functions
|
| 15 |
# ----------------------------
|
| 16 |
def market_research(state):
|
| 17 |
product_info = state.get("input_data", "")
|
|
|
|
|
|
|
|
|
|
| 18 |
return {
|
| 19 |
"MarketResearch": (
|
| 20 |
-
f"Research for: {product_info}\n"
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
)
|
| 24 |
}
|
| 25 |
|
|
@@ -29,11 +46,21 @@ def content_planner(state):
|
|
| 29 |
|
| 30 |
def post_generator(state):
|
| 31 |
plan_output = state.get("ContentPlanner", "")
|
| 32 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def analytics_agent(state):
|
| 35 |
post_output = state.get("PostGenerator", "")
|
| 36 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# ----------------------------
|
| 39 |
# Safe executor wrapper
|
|
@@ -85,4 +112,5 @@ if __name__ == "__main__":
|
|
| 85 |
result = compiled_graph.invoke(state)
|
| 86 |
|
| 87 |
# Print final result (errors included)
|
|
|
|
| 88 |
print(result)
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from langgraph.graph import StateGraph, START, END
|
| 3 |
from langchain_core.tracers.context import tracing_v2_enabled
|
| 4 |
+
from duckduckgo_search import DDGS # π¦ for web search
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
# LangSmith / LangGraph setup
|
|
|
|
| 10 |
os.environ["LANGCHAIN_PROJECT"] = "web_Scrapping_agent21"
|
| 11 |
os.environ["LANGCHAIN_API_KEY"] = "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"
|
| 12 |
|
| 13 |
+
# ----------------------------
|
| 14 |
+
# Helper: DuckDuckGo Search
|
| 15 |
+
# ----------------------------
|
| 16 |
+
def duckduckgo_search(query, max_results=3):
|
| 17 |
+
"""Fetch top web snippets using DuckDuckGo."""
|
| 18 |
+
try:
|
| 19 |
+
with DDGS() as ddgs:
|
| 20 |
+
results = list(ddgs.text(query, max_results=max_results))
|
| 21 |
+
if not results:
|
| 22 |
+
return "No results found."
|
| 23 |
+
return "\n\n".join([f"β’ {r['title']}\n{r['body']}\nURL: {r['href']}" for r in results])
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"[DuckDuckGo Error] {e}"
|
| 26 |
+
|
| 27 |
# ----------------------------
|
| 28 |
# Node functions
|
| 29 |
# ----------------------------
|
| 30 |
def market_research(state):
|
| 31 |
product_info = state.get("input_data", "")
|
| 32 |
+
search_query = f"market trends, competitors, and insights for {product_info}"
|
| 33 |
+
web_data = duckduckgo_search(search_query, max_results=5)
|
| 34 |
+
|
| 35 |
return {
|
| 36 |
"MarketResearch": (
|
| 37 |
+
f"### Research for: {product_info}\n"
|
| 38 |
+
f"π Web Insights:\n{web_data}\n\n"
|
| 39 |
+
"π Summary: The wearable tech market is rapidly expanding with focus on AI-driven health analytics."
|
| 40 |
)
|
| 41 |
}
|
| 42 |
|
|
|
|
| 46 |
|
| 47 |
def post_generator(state):
|
| 48 |
plan_output = state.get("ContentPlanner", "")
|
| 49 |
+
return {
|
| 50 |
+
"PostGenerator": (
|
| 51 |
+
f"Generated posts for campaign: 'Get Fit with SmartPulse!'\n"
|
| 52 |
+
f"Based on: {plan_output[:100]}..."
|
| 53 |
+
)
|
| 54 |
+
}
|
| 55 |
|
| 56 |
def analytics_agent(state):
|
| 57 |
post_output = state.get("PostGenerator", "")
|
| 58 |
+
return {
|
| 59 |
+
"AnalyticsAgent": (
|
| 60 |
+
f"Predicted engagement: Instagram > LinkedIn > Twitter.\n"
|
| 61 |
+
f"Suggestion: add CTA and emojis in posts.\n\nBased on content:\n{post_output[:120]}..."
|
| 62 |
+
)
|
| 63 |
+
}
|
| 64 |
|
| 65 |
# ----------------------------
|
| 66 |
# Safe executor wrapper
|
|
|
|
| 112 |
result = compiled_graph.invoke(state)
|
| 113 |
|
| 114 |
# Print final result (errors included)
|
| 115 |
+
print("\n\n=== FINAL RESULT ===")
|
| 116 |
print(result)
|