Spaces:
Paused
Paused
Update graph.py
Browse files
graph.py
CHANGED
|
@@ -1,331 +1,331 @@
|
|
| 1 |
-
from typing import Dict, List, TypedDict, Sequence
|
| 2 |
-
from langgraph.graph import StateGraph, END
|
| 3 |
-
from langchain.schema import StrOutputParser
|
| 4 |
-
from langchain.schema.runnable import RunnablePassthrough
|
| 5 |
-
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 6 |
-
import models
|
| 7 |
-
import prompts
|
| 8 |
-
import json
|
| 9 |
-
from operator import itemgetter
|
| 10 |
-
from langgraph.errors import GraphRecursionError
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#######################################
|
| 14 |
-
### Research Team Components ###
|
| 15 |
-
#######################################
|
| 16 |
-
class ResearchState(TypedDict):
|
| 17 |
-
workflow: List[str]
|
| 18 |
-
topic: str
|
| 19 |
-
research_data: Dict[str, str]
|
| 20 |
-
next: str
|
| 21 |
-
message_to_manager: str
|
| 22 |
-
message_from_manager: str
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
# Reserach Chains and Tools
|
| 26 |
-
#
|
| 27 |
-
qdrant_research_chain = (
|
| 28 |
-
{"context": itemgetter("topic") | models.compression_retriever, "topic": itemgetter("topic")}
|
| 29 |
-
| RunnablePassthrough.assign(context=itemgetter("context"))
|
| 30 |
-
| {"response": prompts.research_query_prompt | models.gpt4o_mini | StrOutputParser(), "context": itemgetter("context")}
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
tavily_tool = TavilySearchResults(max_results=3)
|
| 34 |
-
query_chain = ( prompts.search_query_prompt | models.gpt4o_mini | StrOutputParser() )
|
| 35 |
-
tavily_simple = ({"tav_results": tavily_tool} | prompts.tavily_prompt | models.gpt4o_mini | StrOutputParser())
|
| 36 |
-
tavily_chain = (
|
| 37 |
-
{"query": query_chain} | tavily_simple
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
research_supervisor_chain = (
|
| 41 |
-
prompts.research_supervisor_prompt | models.gpt4o | StrOutputParser()
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
-
# Reserach Node Defs
|
| 46 |
-
#
|
| 47 |
-
def query_qdrant(state: ResearchState) -> ResearchState:
|
| 48 |
-
topic = state["topic"]
|
| 49 |
-
result = qdrant_research_chain.invoke({"topic": topic})
|
| 50 |
-
print(result)
|
| 51 |
-
state["research_data"]["qdrant_results"] = result["response"]
|
| 52 |
-
state['workflow'].append("query_qdrant")
|
| 53 |
-
print(state['workflow'])
|
| 54 |
-
|
| 55 |
-
return state
|
| 56 |
-
|
| 57 |
-
def web_search(state: ResearchState) -> ResearchState:
|
| 58 |
-
topic = state["topic"]
|
| 59 |
-
qdrant_results = state["research_data"].get("qdrant_results", "No previous results available.")
|
| 60 |
-
result = tavily_chain.invoke({"topic": topic,"qdrant_results": qdrant_results })
|
| 61 |
-
print(result)
|
| 62 |
-
state["research_data"]["web_search_results"] = result
|
| 63 |
-
state['workflow'].append("web_search")
|
| 64 |
-
print(state['workflow'])
|
| 65 |
-
return state
|
| 66 |
-
|
| 67 |
-
def research_supervisor(state):
|
| 68 |
-
message_from_manager = state["message_from_manager"]
|
| 69 |
-
collected_data = state["research_data"]
|
| 70 |
-
topic = state['topic']
|
| 71 |
-
supervisor_result = research_supervisor_chain.invoke({"message_from_manager": message_from_manager, "collected_data": collected_data, "topic": topic})
|
| 72 |
-
lines = supervisor_result.split('\n')
|
| 73 |
-
print(supervisor_result)
|
| 74 |
-
for line in lines:
|
| 75 |
-
if line.startswith('Next Action: '):
|
| 76 |
-
state['next'] = line[len('Next Action: '):].strip() # Extract the next action content
|
| 77 |
-
elif line.startswith('Message to project manager: '):
|
| 78 |
-
state['message_to_manager'] = line[len('Message to project manager: '):].strip()
|
| 79 |
-
state['workflow'].append("research_supervisor")
|
| 80 |
-
print(state['workflow'])
|
| 81 |
-
return state
|
| 82 |
-
|
| 83 |
-
def research_end(state):
|
| 84 |
-
state['workflow'].append("research_end")
|
| 85 |
-
print(state['workflow'])
|
| 86 |
-
return state
|
| 87 |
-
|
| 88 |
-
#######################################
|
| 89 |
-
### Writing Team Components ###
|
| 90 |
-
#######################################
|
| 91 |
-
class WritingState(TypedDict):
|
| 92 |
-
workflow: List[str]
|
| 93 |
-
topic: str
|
| 94 |
-
research_data: Dict[str, str]
|
| 95 |
-
draft_posts: Sequence[str]
|
| 96 |
-
final_post: str
|
| 97 |
-
next: str
|
| 98 |
-
message_to_manager: str
|
| 99 |
-
message_from_manager: str
|
| 100 |
-
review_comments: str
|
| 101 |
-
style_checked: bool
|
| 102 |
-
|
| 103 |
-
#
|
| 104 |
-
# Writing Chains
|
| 105 |
-
#
|
| 106 |
-
writing_supervisor_chain = (
|
| 107 |
-
prompts.writing_supervisor_prompt | models.gpt4o | StrOutputParser()
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
post_creation_chain = (
|
| 111 |
-
prompts.post_creation_prompt | models.gpt4o_mini | StrOutputParser()
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
post_editor_chain = (
|
| 115 |
-
prompts.post_editor_prompt | models.gpt4o | StrOutputParser()
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
post_review_chain = (
|
| 119 |
-
prompts.post_review_prompt | models.gpt4o | StrOutputParser()
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
#
|
| 123 |
-
# Writing Node Defs
|
| 124 |
-
#
|
| 125 |
-
def post_creation(state):
|
| 126 |
-
topic = state['topic']
|
| 127 |
-
drafts = state['draft_posts']
|
| 128 |
-
collected_data = state["research_data"]
|
| 129 |
-
review_comments = state['review_comments']
|
| 130 |
-
results = post_creation_chain.invoke({"topic": topic, "collected_data": collected_data, "drafts": drafts, "review_comments": review_comments})
|
| 131 |
-
print(results)
|
| 132 |
-
state['draft_posts'].append(results)
|
| 133 |
-
state['workflow'].append("post_creation")
|
| 134 |
-
print(state['workflow'])
|
| 135 |
-
return state
|
| 136 |
-
|
| 137 |
-
def post_editor(state):
|
| 138 |
-
current_draft = state['draft_posts'][-1]
|
| 139 |
-
styleguide = prompts.style_guide_text
|
| 140 |
-
review_comments = state['review_comments']
|
| 141 |
-
results = post_editor_chain.invoke({"current_draft": current_draft, "styleguide": styleguide, "review_comments": review_comments})
|
| 142 |
-
print(results)
|
| 143 |
-
state['draft_posts'].append(results)
|
| 144 |
-
state['workflow'].append("post_editor")
|
| 145 |
-
print(state['workflow'])
|
| 146 |
-
return state
|
| 147 |
-
|
| 148 |
-
def post_review(state):
|
| 149 |
-
print("post_review node")
|
| 150 |
-
current_draft = state['draft_posts'][-1]
|
| 151 |
-
styleguide = prompts.style_guide_text
|
| 152 |
-
results = post_review_chain.invoke({"current_draft": current_draft, "styleguide": styleguide})
|
| 153 |
-
print(results)
|
| 154 |
-
data = json.loads(results.strip())
|
| 155 |
-
state['review_comments'] = data["Comments on current draft"]
|
| 156 |
-
if data["Draft Acceptable"] == 'Yes':
|
| 157 |
-
state['final_post'] = state['draft_posts'][-1]
|
| 158 |
-
state['workflow'].append("post_review")
|
| 159 |
-
print(state['workflow'])
|
| 160 |
-
return state
|
| 161 |
-
|
| 162 |
-
def writing_end(state):
|
| 163 |
-
print("writing_end node")
|
| 164 |
-
state['workflow'].append("writing_end")
|
| 165 |
-
print(state['workflow'])
|
| 166 |
-
return state
|
| 167 |
-
|
| 168 |
-
def writing_supervisor(state):
|
| 169 |
-
print("writing_supervisor node")
|
| 170 |
-
message_from_manager = state['message_from_manager']
|
| 171 |
-
topic = state['topic']
|
| 172 |
-
drafts = state['draft_posts']
|
| 173 |
-
final_draft = state['final_post']
|
| 174 |
-
review_comments = state['review_comments']
|
| 175 |
-
supervisor_result = writing_supervisor_chain.invoke({"review_comments": review_comments, "message_from_manager": message_from_manager, "topic": topic, "drafts": drafts, "final_draft": final_draft})
|
| 176 |
-
print(supervisor_result)
|
| 177 |
-
lines = supervisor_result.split('\n')
|
| 178 |
-
for line in lines:
|
| 179 |
-
if line.startswith('Next Action: '):
|
| 180 |
-
state['next'] = line[len('Next Action: '):].strip() # Extract the next action content
|
| 181 |
-
elif line.startswith('Message to project manager: '):
|
| 182 |
-
state['message_to_manager'] = line[len('Message to project manager: '):].strip()
|
| 183 |
-
state['workflow'].append("writing_supervisor")
|
| 184 |
-
print(state['workflow'])
|
| 185 |
-
return state
|
| 186 |
-
|
| 187 |
-
#######################################
|
| 188 |
-
### Overarching Graph Components ###
|
| 189 |
-
#######################################
|
| 190 |
-
class State(TypedDict):
|
| 191 |
-
workflow: List[str]
|
| 192 |
-
topic: str
|
| 193 |
-
research_data: Dict[str, str]
|
| 194 |
-
draft_posts: Sequence[str]
|
| 195 |
-
final_post: str
|
| 196 |
-
next: str
|
| 197 |
-
user_input: str
|
| 198 |
-
message_to_manager: str
|
| 199 |
-
message_from_manager: str
|
| 200 |
-
last_active_team :str
|
| 201 |
-
next_team: str
|
| 202 |
-
review_comments: str
|
| 203 |
-
|
| 204 |
-
#
|
| 205 |
-
# Complete Graph Chains
|
| 206 |
-
#
|
| 207 |
-
overall_supervisor_chain = (
|
| 208 |
-
prompts.overall_supervisor_prompt | models.gpt4o | StrOutputParser()
|
| 209 |
-
)
|
| 210 |
-
|
| 211 |
-
#
|
| 212 |
-
# Complete Graph Node defs
|
| 213 |
-
#
|
| 214 |
-
def overall_supervisor(state):
|
| 215 |
-
init_user_query = state["user_input"]
|
| 216 |
-
message_to_manager = state['message_to_manager']
|
| 217 |
-
last_active_team = state['last_active_team']
|
| 218 |
-
final_post = state['final_post']
|
| 219 |
-
supervisor_result = overall_supervisor_chain.invoke({"query": init_user_query, "message_to_manager": message_to_manager, "last_active_team": last_active_team, "final_post": final_post})
|
| 220 |
-
print(supervisor_result)
|
| 221 |
-
lines = supervisor_result.split('\n')
|
| 222 |
-
for line in lines:
|
| 223 |
-
if line.startswith('Next Action: '):
|
| 224 |
-
state['next_team'] = line[len('Next Action: '):].strip() # Extract the next action content
|
| 225 |
-
elif line.startswith('Extracted Topic: '):
|
| 226 |
-
state['topic'] = line[len('Extracted Topic: '):].strip() # Extract the next action content
|
| 227 |
-
elif line.startswith('Message to supervisor: '):
|
| 228 |
-
state['message_from_manager'] = line[len('Message to supervisor: '):].strip() # Extract the next action content
|
| 229 |
-
state['workflow'].append("overall_supervisor")
|
| 230 |
-
print(state['workflow'])
|
| 231 |
-
return state
|
| 232 |
-
|
| 233 |
-
#######################################
|
| 234 |
-
### Graph structures ###
|
| 235 |
-
#######################################
|
| 236 |
-
|
| 237 |
-
#
|
| 238 |
-
# Reserach Graph Nodes
|
| 239 |
-
#
|
| 240 |
-
research_graph = StateGraph(ResearchState)
|
| 241 |
-
research_graph.add_node("query_qdrant", query_qdrant)
|
| 242 |
-
research_graph.add_node("web_search", web_search)
|
| 243 |
-
research_graph.add_node("research_supervisor", research_supervisor)
|
| 244 |
-
research_graph.add_node("research_end", research_end)
|
| 245 |
-
#
|
| 246 |
-
# Reserach Graph Edges
|
| 247 |
-
#
|
| 248 |
-
research_graph.set_entry_point("research_supervisor")
|
| 249 |
-
research_graph.add_edge("query_qdrant", "research_supervisor")
|
| 250 |
-
research_graph.add_edge("web_search", "research_supervisor")
|
| 251 |
-
research_graph.add_conditional_edges(
|
| 252 |
-
"research_supervisor",
|
| 253 |
-
lambda x: x["next"],
|
| 254 |
-
{"query_qdrant": "query_qdrant", "web_search": "web_search", "FINISH": "research_end"},
|
| 255 |
-
)
|
| 256 |
-
research_graph_comp = research_graph.compile()
|
| 257 |
-
|
| 258 |
-
#
|
| 259 |
-
# Writing Graph Nodes
|
| 260 |
-
#
|
| 261 |
-
writing_graph = StateGraph(WritingState)
|
| 262 |
-
writing_graph.add_node("post_creation", post_creation)
|
| 263 |
-
writing_graph.add_node("post_editor", post_editor)
|
| 264 |
-
writing_graph.add_node("post_review", post_review)
|
| 265 |
-
writing_graph.add_node("writing_supervisor", writing_supervisor)
|
| 266 |
-
writing_graph.add_node("writing_end", writing_end)
|
| 267 |
-
#
|
| 268 |
-
# Writing Graph Edges
|
| 269 |
-
#
|
| 270 |
-
writing_graph.set_entry_point("writing_supervisor")
|
| 271 |
-
writing_graph.add_edge("post_creation", "post_editor")
|
| 272 |
-
writing_graph.add_edge("post_editor", "post_review")
|
| 273 |
-
writing_graph.add_edge("post_review", "writing_supervisor")
|
| 274 |
-
writing_graph.add_conditional_edges(
|
| 275 |
-
"writing_supervisor",
|
| 276 |
-
lambda x: x["next"],
|
| 277 |
-
{"NEW DRAFT": "post_creation",
|
| 278 |
-
"FINISH": "writing_end"},
|
| 279 |
-
)
|
| 280 |
-
writing_graph_comp = writing_graph.compile()
|
| 281 |
-
|
| 282 |
-
#
|
| 283 |
-
# Complete Graph Nodes
|
| 284 |
-
#
|
| 285 |
-
overall_graph = StateGraph(State)
|
| 286 |
-
overall_graph.add_node("overall_supervisor", overall_supervisor)
|
| 287 |
-
overall_graph.add_node("research_team_graph", research_graph_comp)
|
| 288 |
-
overall_graph.add_node("writing_team_graph", writing_graph_comp)
|
| 289 |
-
#
|
| 290 |
-
# Complete Graph Edges
|
| 291 |
-
#
|
| 292 |
-
overall_graph.set_entry_point("overall_supervisor")
|
| 293 |
-
overall_graph.add_edge("research_team_graph", "overall_supervisor")
|
| 294 |
-
overall_graph.add_edge("writing_team_graph", "overall_supervisor")
|
| 295 |
-
overall_graph.add_conditional_edges(
|
| 296 |
-
"overall_supervisor",
|
| 297 |
-
lambda x: x["next_team"],
|
| 298 |
-
{"research_team": "research_team_graph",
|
| 299 |
-
"writing_team": "writing_team_graph",
|
| 300 |
-
"FINISH": END},
|
| 301 |
-
)
|
| 302 |
-
app = overall_graph.compile()
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
#######################################
|
| 306 |
-
### Run method ###
|
| 307 |
-
#######################################
|
| 308 |
-
|
| 309 |
-
def getSocialMediaPost(userInput: str) -> str:
|
| 310 |
-
finalPost = ""
|
| 311 |
-
initial_state = State(
|
| 312 |
-
workflow = [],
|
| 313 |
-
topic= "",
|
| 314 |
-
research_data = {},
|
| 315 |
-
draft_posts = [],
|
| 316 |
-
final_post = [],
|
| 317 |
-
next = [],
|
| 318 |
-
next_team = [],
|
| 319 |
-
user_input=userInput,
|
| 320 |
-
message_to_manager="",
|
| 321 |
-
message_from_manager="",
|
| 322 |
-
last_active_team="",
|
| 323 |
-
review_comments=""
|
| 324 |
-
)
|
| 325 |
-
results = app.invoke(initial_state)
|
| 326 |
-
try:
|
| 327 |
-
results = app.invoke(initial_state, {"recursion_limit": 40})
|
| 328 |
-
except GraphRecursionError:
|
| 329 |
-
return "Recursion Error"
|
| 330 |
-
finalPost = results['final_post']
|
| 331 |
return finalPost
|
|
|
|
| 1 |
+
from typing import Dict, List, TypedDict, Sequence
|
| 2 |
+
from langgraph.graph import StateGraph, END
|
| 3 |
+
from langchain.schema import StrOutputParser
|
| 4 |
+
from langchain.schema.runnable import RunnablePassthrough
|
| 5 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 6 |
+
import models
|
| 7 |
+
import prompts
|
| 8 |
+
import json
|
| 9 |
+
from operator import itemgetter
|
| 10 |
+
from langgraph.errors import GraphRecursionError
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
#######################################
|
| 14 |
+
### Research Team Components ###
|
| 15 |
+
#######################################
|
| 16 |
+
class ResearchState(TypedDict):
|
| 17 |
+
workflow: List[str]
|
| 18 |
+
topic: str
|
| 19 |
+
research_data: Dict[str, str]
|
| 20 |
+
next: str
|
| 21 |
+
message_to_manager: str
|
| 22 |
+
message_from_manager: str
|
| 23 |
+
|
| 24 |
+
#
|
| 25 |
+
# Reserach Chains and Tools
|
| 26 |
+
#
|
| 27 |
+
qdrant_research_chain = (
|
| 28 |
+
{"context": itemgetter("topic") | models.compression_retriever, "topic": itemgetter("topic")}
|
| 29 |
+
| RunnablePassthrough.assign(context=itemgetter("context"))
|
| 30 |
+
| {"response": prompts.research_query_prompt | models.gpt4o_mini | StrOutputParser(), "context": itemgetter("context")}
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
tavily_tool = TavilySearchResults(max_results=3)
|
| 34 |
+
query_chain = ( prompts.search_query_prompt | models.gpt4o_mini | StrOutputParser() )
|
| 35 |
+
tavily_simple = ({"tav_results": tavily_tool} | prompts.tavily_prompt | models.gpt4o_mini | StrOutputParser())
|
| 36 |
+
tavily_chain = (
|
| 37 |
+
{"query": query_chain} | tavily_simple
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
research_supervisor_chain = (
|
| 41 |
+
prompts.research_supervisor_prompt | models.gpt4o | StrOutputParser()
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
#
|
| 45 |
+
# Reserach Node Defs
|
| 46 |
+
#
|
| 47 |
+
def query_qdrant(state: ResearchState) -> ResearchState:
|
| 48 |
+
topic = state["topic"]
|
| 49 |
+
result = qdrant_research_chain.invoke({"topic": topic})
|
| 50 |
+
print(result)
|
| 51 |
+
state["research_data"]["qdrant_results"] = result["response"]
|
| 52 |
+
state['workflow'].append("query_qdrant")
|
| 53 |
+
print(state['workflow'])
|
| 54 |
+
|
| 55 |
+
return state
|
| 56 |
+
|
| 57 |
+
def web_search(state: ResearchState) -> ResearchState:
|
| 58 |
+
topic = state["topic"]
|
| 59 |
+
qdrant_results = state["research_data"].get("qdrant_results", "No previous results available.")
|
| 60 |
+
result = tavily_chain.invoke({"topic": topic,"qdrant_results": qdrant_results })
|
| 61 |
+
print(result)
|
| 62 |
+
state["research_data"]["web_search_results"] = result
|
| 63 |
+
state['workflow'].append("web_search")
|
| 64 |
+
print(state['workflow'])
|
| 65 |
+
return state
|
| 66 |
+
|
| 67 |
+
def research_supervisor(state):
|
| 68 |
+
message_from_manager = state["message_from_manager"]
|
| 69 |
+
collected_data = state["research_data"]
|
| 70 |
+
topic = state['topic']
|
| 71 |
+
supervisor_result = research_supervisor_chain.invoke({"message_from_manager": message_from_manager, "collected_data": collected_data, "topic": topic})
|
| 72 |
+
lines = supervisor_result.split('\n')
|
| 73 |
+
print(supervisor_result)
|
| 74 |
+
for line in lines:
|
| 75 |
+
if line.startswith('Next Action: '):
|
| 76 |
+
state['next'] = line[len('Next Action: '):].strip() # Extract the next action content
|
| 77 |
+
elif line.startswith('Message to project manager: '):
|
| 78 |
+
state['message_to_manager'] = line[len('Message to project manager: '):].strip()
|
| 79 |
+
state['workflow'].append("research_supervisor")
|
| 80 |
+
print(state['workflow'])
|
| 81 |
+
return state
|
| 82 |
+
|
| 83 |
+
def research_end(state):
|
| 84 |
+
state['workflow'].append("research_end")
|
| 85 |
+
print(state['workflow'])
|
| 86 |
+
return state
|
| 87 |
+
|
| 88 |
+
#######################################
|
| 89 |
+
### Writing Team Components ###
|
| 90 |
+
#######################################
|
| 91 |
+
class WritingState(TypedDict):
|
| 92 |
+
workflow: List[str]
|
| 93 |
+
topic: str
|
| 94 |
+
research_data: Dict[str, str]
|
| 95 |
+
draft_posts: Sequence[str]
|
| 96 |
+
final_post: str
|
| 97 |
+
next: str
|
| 98 |
+
message_to_manager: str
|
| 99 |
+
message_from_manager: str
|
| 100 |
+
review_comments: str
|
| 101 |
+
style_checked: bool
|
| 102 |
+
|
| 103 |
+
#
|
| 104 |
+
# Writing Chains
|
| 105 |
+
#
|
| 106 |
+
writing_supervisor_chain = (
|
| 107 |
+
prompts.writing_supervisor_prompt | models.gpt4o | StrOutputParser()
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
post_creation_chain = (
|
| 111 |
+
prompts.post_creation_prompt | models.gpt4o_mini | StrOutputParser()
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
post_editor_chain = (
|
| 115 |
+
prompts.post_editor_prompt | models.gpt4o | StrOutputParser()
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
post_review_chain = (
|
| 119 |
+
prompts.post_review_prompt | models.gpt4o | StrOutputParser()
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
#
|
| 123 |
+
# Writing Node Defs
|
| 124 |
+
#
|
| 125 |
+
def post_creation(state):
|
| 126 |
+
topic = state['topic']
|
| 127 |
+
drafts = state['draft_posts']
|
| 128 |
+
collected_data = state["research_data"]
|
| 129 |
+
review_comments = state['review_comments']
|
| 130 |
+
results = post_creation_chain.invoke({"topic": topic, "collected_data": collected_data, "drafts": drafts, "review_comments": review_comments})
|
| 131 |
+
print(results)
|
| 132 |
+
state['draft_posts'].append(results)
|
| 133 |
+
state['workflow'].append("post_creation")
|
| 134 |
+
print(state['workflow'])
|
| 135 |
+
return state
|
| 136 |
+
|
| 137 |
+
def post_editor(state):
|
| 138 |
+
current_draft = state['draft_posts'][-1]
|
| 139 |
+
styleguide = prompts.style_guide_text
|
| 140 |
+
review_comments = state['review_comments']
|
| 141 |
+
results = post_editor_chain.invoke({"current_draft": current_draft, "styleguide": styleguide, "review_comments": review_comments})
|
| 142 |
+
print(results)
|
| 143 |
+
state['draft_posts'].append(results)
|
| 144 |
+
state['workflow'].append("post_editor")
|
| 145 |
+
print(state['workflow'])
|
| 146 |
+
return state
|
| 147 |
+
|
| 148 |
+
def post_review(state):
|
| 149 |
+
print("post_review node")
|
| 150 |
+
current_draft = state['draft_posts'][-1]
|
| 151 |
+
styleguide = prompts.style_guide_text
|
| 152 |
+
results = post_review_chain.invoke({"current_draft": current_draft, "styleguide": styleguide})
|
| 153 |
+
print(results)
|
| 154 |
+
data = json.loads(results.strip())
|
| 155 |
+
state['review_comments'] = data["Comments on current draft"]
|
| 156 |
+
if data["Draft Acceptable"] == 'Yes':
|
| 157 |
+
state['final_post'] = state['draft_posts'][-1]
|
| 158 |
+
state['workflow'].append("post_review")
|
| 159 |
+
print(state['workflow'])
|
| 160 |
+
return state
|
| 161 |
+
|
| 162 |
+
def writing_end(state):
|
| 163 |
+
print("writing_end node")
|
| 164 |
+
state['workflow'].append("writing_end")
|
| 165 |
+
print(state['workflow'])
|
| 166 |
+
return state
|
| 167 |
+
|
| 168 |
+
def writing_supervisor(state):
|
| 169 |
+
print("writing_supervisor node")
|
| 170 |
+
message_from_manager = state['message_from_manager']
|
| 171 |
+
topic = state['topic']
|
| 172 |
+
drafts = state['draft_posts']
|
| 173 |
+
final_draft = state['final_post']
|
| 174 |
+
review_comments = state['review_comments']
|
| 175 |
+
supervisor_result = writing_supervisor_chain.invoke({"review_comments": review_comments, "message_from_manager": message_from_manager, "topic": topic, "drafts": drafts, "final_draft": final_draft})
|
| 176 |
+
print(supervisor_result)
|
| 177 |
+
lines = supervisor_result.split('\n')
|
| 178 |
+
for line in lines:
|
| 179 |
+
if line.startswith('Next Action: '):
|
| 180 |
+
state['next'] = line[len('Next Action: '):].strip() # Extract the next action content
|
| 181 |
+
elif line.startswith('Message to project manager: '):
|
| 182 |
+
state['message_to_manager'] = line[len('Message to project manager: '):].strip()
|
| 183 |
+
state['workflow'].append("writing_supervisor")
|
| 184 |
+
print(state['workflow'])
|
| 185 |
+
return state
|
| 186 |
+
|
| 187 |
+
#######################################
|
| 188 |
+
### Overarching Graph Components ###
|
| 189 |
+
#######################################
|
| 190 |
+
class State(TypedDict):
|
| 191 |
+
workflow: List[str]
|
| 192 |
+
topic: str
|
| 193 |
+
research_data: Dict[str, str]
|
| 194 |
+
draft_posts: Sequence[str]
|
| 195 |
+
final_post: str
|
| 196 |
+
next: str
|
| 197 |
+
user_input: str
|
| 198 |
+
message_to_manager: str
|
| 199 |
+
message_from_manager: str
|
| 200 |
+
last_active_team :str
|
| 201 |
+
next_team: str
|
| 202 |
+
review_comments: str
|
| 203 |
+
|
| 204 |
+
#
|
| 205 |
+
# Complete Graph Chains
|
| 206 |
+
#
|
| 207 |
+
overall_supervisor_chain = (
|
| 208 |
+
prompts.overall_supervisor_prompt | models.gpt4o | StrOutputParser()
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
#
|
| 212 |
+
# Complete Graph Node defs
|
| 213 |
+
#
|
| 214 |
+
def overall_supervisor(state):
|
| 215 |
+
init_user_query = state["user_input"]
|
| 216 |
+
message_to_manager = state['message_to_manager']
|
| 217 |
+
last_active_team = state['last_active_team']
|
| 218 |
+
final_post = state['final_post']
|
| 219 |
+
supervisor_result = overall_supervisor_chain.invoke({"query": init_user_query, "message_to_manager": message_to_manager, "last_active_team": last_active_team, "final_post": final_post})
|
| 220 |
+
print(supervisor_result)
|
| 221 |
+
lines = supervisor_result.split('\n')
|
| 222 |
+
for line in lines:
|
| 223 |
+
if line.startswith('Next Action: '):
|
| 224 |
+
state['next_team'] = line[len('Next Action: '):].strip() # Extract the next action content
|
| 225 |
+
elif line.startswith('Extracted Topic: '):
|
| 226 |
+
state['topic'] = line[len('Extracted Topic: '):].strip() # Extract the next action content
|
| 227 |
+
elif line.startswith('Message to supervisor: '):
|
| 228 |
+
state['message_from_manager'] = line[len('Message to supervisor: '):].strip() # Extract the next action content
|
| 229 |
+
state['workflow'].append("overall_supervisor")
|
| 230 |
+
print(state['workflow'])
|
| 231 |
+
return state
|
| 232 |
+
|
| 233 |
+
#######################################
|
| 234 |
+
### Graph structures ###
|
| 235 |
+
#######################################
|
| 236 |
+
|
| 237 |
+
#
|
| 238 |
+
# Reserach Graph Nodes
|
| 239 |
+
#
|
| 240 |
+
research_graph = StateGraph(ResearchState)
|
| 241 |
+
research_graph.add_node("query_qdrant", query_qdrant)
|
| 242 |
+
research_graph.add_node("web_search", web_search)
|
| 243 |
+
research_graph.add_node("research_supervisor", research_supervisor)
|
| 244 |
+
research_graph.add_node("research_end", research_end)
|
| 245 |
+
#
|
| 246 |
+
# Reserach Graph Edges
|
| 247 |
+
#
|
| 248 |
+
research_graph.set_entry_point("research_supervisor")
|
| 249 |
+
research_graph.add_edge("query_qdrant", "research_supervisor")
|
| 250 |
+
research_graph.add_edge("web_search", "research_supervisor")
|
| 251 |
+
research_graph.add_conditional_edges(
|
| 252 |
+
"research_supervisor",
|
| 253 |
+
lambda x: x["next"],
|
| 254 |
+
{"query_qdrant": "query_qdrant", "web_search": "web_search", "FINISH": "research_end"},
|
| 255 |
+
)
|
| 256 |
+
research_graph_comp = research_graph.compile()
|
| 257 |
+
|
| 258 |
+
#
|
| 259 |
+
# Writing Graph Nodes
|
| 260 |
+
#
|
| 261 |
+
writing_graph = StateGraph(WritingState)
|
| 262 |
+
writing_graph.add_node("post_creation", post_creation)
|
| 263 |
+
writing_graph.add_node("post_editor", post_editor)
|
| 264 |
+
writing_graph.add_node("post_review", post_review)
|
| 265 |
+
writing_graph.add_node("writing_supervisor", writing_supervisor)
|
| 266 |
+
writing_graph.add_node("writing_end", writing_end)
|
| 267 |
+
#
|
| 268 |
+
# Writing Graph Edges
|
| 269 |
+
#
|
| 270 |
+
writing_graph.set_entry_point("writing_supervisor")
|
| 271 |
+
writing_graph.add_edge("post_creation", "post_editor")
|
| 272 |
+
writing_graph.add_edge("post_editor", "post_review")
|
| 273 |
+
writing_graph.add_edge("post_review", "writing_supervisor")
|
| 274 |
+
writing_graph.add_conditional_edges(
|
| 275 |
+
"writing_supervisor",
|
| 276 |
+
lambda x: x["next"],
|
| 277 |
+
{"NEW DRAFT": "post_creation",
|
| 278 |
+
"FINISH": "writing_end"},
|
| 279 |
+
)
|
| 280 |
+
writing_graph_comp = writing_graph.compile()
|
| 281 |
+
|
| 282 |
+
#
|
| 283 |
+
# Complete Graph Nodes
|
| 284 |
+
#
|
| 285 |
+
overall_graph = StateGraph(State)
|
| 286 |
+
overall_graph.add_node("overall_supervisor", overall_supervisor)
|
| 287 |
+
overall_graph.add_node("research_team_graph", research_graph_comp)
|
| 288 |
+
overall_graph.add_node("writing_team_graph", writing_graph_comp)
|
| 289 |
+
#
|
| 290 |
+
# Complete Graph Edges
|
| 291 |
+
#
|
| 292 |
+
overall_graph.set_entry_point("overall_supervisor")
|
| 293 |
+
overall_graph.add_edge("research_team_graph", "overall_supervisor")
|
| 294 |
+
overall_graph.add_edge("writing_team_graph", "overall_supervisor")
|
| 295 |
+
overall_graph.add_conditional_edges(
|
| 296 |
+
"overall_supervisor",
|
| 297 |
+
lambda x: x["next_team"],
|
| 298 |
+
{"research_team": "research_team_graph",
|
| 299 |
+
"writing_team": "writing_team_graph",
|
| 300 |
+
"FINISH": END},
|
| 301 |
+
)
|
| 302 |
+
app = overall_graph.compile()
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
#######################################
|
| 306 |
+
### Run method ###
|
| 307 |
+
#######################################
|
| 308 |
+
|
| 309 |
+
def getSocialMediaPost(userInput: str) -> str:
|
| 310 |
+
finalPost = ""
|
| 311 |
+
initial_state = State(
|
| 312 |
+
workflow = [],
|
| 313 |
+
topic= "",
|
| 314 |
+
research_data = {},
|
| 315 |
+
draft_posts = [],
|
| 316 |
+
final_post = [],
|
| 317 |
+
next = [],
|
| 318 |
+
next_team = [],
|
| 319 |
+
user_input=userInput,
|
| 320 |
+
message_to_manager="",
|
| 321 |
+
message_from_manager="",
|
| 322 |
+
last_active_team="",
|
| 323 |
+
review_comments=""
|
| 324 |
+
)
|
| 325 |
+
results = app.invoke(initial_state, {"recursion_limit": 40})
|
| 326 |
+
try:
|
| 327 |
+
results = app.invoke(initial_state, {"recursion_limit": 40})
|
| 328 |
+
except GraphRecursionError:
|
| 329 |
+
return "Recursion Error"
|
| 330 |
+
finalPost = results['final_post']
|
| 331 |
return finalPost
|