Spaces:
Sleeping
Sleeping
File size: 25,079 Bytes
75309ed 2c0c67c 75309ed |
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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
import json
from multiprocessing import Pool, cpu_count
# import requests
# from tenacity import RetryError
import re
import logging
import chainlit as cl
from termcolor import colored
from typing import Any, Dict, Union, List
from typing import TypedDict, Annotated
from langgraph.graph.message import add_messages
from agents.base_agent import BaseAgent
from utils.read_markdown import read_markdown_file
from tools.google_serper import serper_search, serper_shopping_search
from utils.logging import log_function, setup_logging
from tools.offline_graph_rag_tool import run_rag
from prompt_engineering.guided_json_lib import (
guided_json_search_query,
guided_json_best_url_two,
guided_json_router_decision,
guided_json_parse_expert,
guided_json_search_query_two
)
setup_logging(level=logging.DEBUG)
logger = logging.getLogger(__name__)
class MessageDict(TypedDict):
role: str
content: str
class State(TypedDict):
meta_prompt: Annotated[List[dict], add_messages]
conversation_history: Annotated[List[dict], add_messages]
requirements_gathering: Annotated[List[str], add_messages]
expert_plan: str
expert_research: Annotated[List[str], add_messages]
expert_research_shopping: Annotated[List[str], add_messages]
expert_writing: str
user_input: Annotated[List[str], add_messages]
previous_search_queries: Annotated[List[dict], add_messages]
router_decision: str
chat_limit: int
chat_finished: bool
recursion_limit: int
final_answer: str
state: State = {
"meta_prompt": [],
"conversation_history": [],
"requirements_gathering": [],
"expert_plan": [],
"expert_research": [],
"expert_research_shopping": [],
"expert_writing": [],
"user_input": [],
"previous_search_queries": [],
"router_decision": None,
"chat_limit": None,
"chat_finished": False,
"recursion_limit": None,
"final_answer": None
}
def chat_counter(state: State) -> State:
chat_limit = state.get("chat_limit")
if chat_limit is None:
chat_limit = 0
chat_limit += 1
state["chat_limit"] = chat_limit
return chat_limit
def routing_function(state: State) -> str:
decision = state["router_decision"]
print(colored(f"\n\n Routing function called. Decision: {decision}\n\n", 'green'))
return decision
def set_chat_finished(state: State) -> bool:
state["chat_finished"] = True
final_response = state["meta_prompt"][-1].content
print(colored(f"\n\n DEBUG FINAL RESPONSE: {final_response}\n\n", 'green'))
# Split the response at ">> FINAL ANSWER:"
parts = final_response.split(">> FINAL ANSWER:")
if len(parts) > 1:
answer_part = parts[1].strip()
# Remove any triple quotes
final_response_formatted = answer_part.strip('"""')
# Remove leading whitespace
final_response_formatted = final_response_formatted.lstrip()
# Remove the CoR dictionary at the end
cor_pattern = r'\nCoR\s*=\s*\{[\s\S]*\}\s*$'
final_response_formatted = re.sub(cor_pattern, '', final_response_formatted)
# Remove any trailing whitespace
final_response_formatted = final_response_formatted.rstrip()
# print(colored(f"\n\n DEBUG: {final_response_formatted}\n\n", 'green'))
print(colored(f"\n\n Jarvis👩💻: {final_response_formatted}", 'cyan'))
state["final_answer"] = f'''{final_response_formatted}'''
else:
print(colored("Error: Could not find '>> FINAL ANSWER:' in the response", 'red'))
state["final_answer"] = "Error: No final answer found"
return state
class Jar3d(BaseAgent[State]):
def __init__(self, model: str = None, server: str = None, temperature: float = 0,
model_endpoint: str = None, stop: str = None):
super().__init__(model, server, temperature, model_endpoint, stop)
self.llm = self.get_llm(json_model=False)
def get_prompt(self, state: State = None) -> str:
system_prompt_md = read_markdown_file('prompt_engineering/jar3d_requirements_prompt.md')
system_prompt = f"{system_prompt_md}\n <Type2> {state.get('final_answer', '')} </Type2>"
return system_prompt
def process_response(self, response: Any, user_input: str, state: State = None) -> Dict[str, List[Dict[str, str]]]:
updates_conversation_history = {
"requirements_gathering": [
{"role": "user", "content": f"{user_input}"},
{"role": "assistant", "content": str(response)}
]
}
return updates_conversation_history
def get_conv_history(self, state: State) -> str:
conversation_history = state.get('requirements_gathering', [])
return "\n".join([f"{msg['role']}: {msg['content']}" for msg in conversation_history])
def get_user_input(self) -> str:
pass
def get_guided_json(self, state: State) -> Dict[str, Any]:
pass
def use_tool(self) -> Any:
pass
def run_chainlit(self, state: State, message: cl.Message) -> State:
user_message = message.content
# system_prompt = self.get_prompt()
user_input = f"cogor:{user_message}"
# user_input = f"{system_prompt}\n cogor {user_message}"
state = self.invoke(state=state, user_input=user_input)
response = state['requirements_gathering'][-1]["content"]
response = re.sub(r'^```python[\s\S]*?```\s*', '', response, flags=re.MULTILINE)
response = response.lstrip()
return state, response
class MetaExpert(BaseAgent[State]):
def __init__(self, model: str = None, server: str = None, temperature: float = 0,
model_endpoint: str = None, stop: str = None):
super().__init__(model, server, temperature, model_endpoint, stop)
self.llm = self.get_llm(json_model=False)
def get_prompt(self, state:None) -> str:
system_prompt = read_markdown_file('prompt_engineering/jar3d_meta_prompt.md')
return system_prompt
def process_response(self, response: Any, user_input: str, state: State = None) -> Dict[str, List[MessageDict]]:
user_input = None
updates_conversation_history = {
"meta_prompt": [
{"role": "user", "content": f"{user_input}"},
{"role": "assistant", "content": str(response)}
]
}
return updates_conversation_history
# @log_function(logger)
def get_conv_history(self, state: State) -> str:
all_expert_research = []
if state["expert_research"]:
expert_research = state["expert_research"]
all_expert_research.extend(expert_research)
else:
all_expert_research = []
expert_message_history = f"""
<expert_plan>
## Your Expert Plan:\n{state.get("expert_plan", [])}\n
</expert_plan>
<expert_writing>
## Your Expert Writing:\n{state.get("expert_writing", [])}\n
</expert_writing>
<internet_research_shopping_list>
## Your Expert Shopping List:\n{state.get("expert_research_shopping", [])}\n
</internet_research_shopping_list>
<internet_research>
## Your Expert Research:{all_expert_research}\n
</internet_research>
"""
return expert_message_history
def get_user_input(self) -> str:
user_input = input("Enter your query: ")
return user_input
def get_guided_json(self, state: State) -> Dict[str, Any]:
pass
def use_tool(self) -> Any:
pass
@log_function(logger)
def run(self, state: State) -> State:
counter = chat_counter(state) # Counts every time we invoke the Meta Agent
recursion_limit = state.get("recursion_limit")
recursions = 3*counter - 2
print(colored(f"\n\n * We have envoked the Meta-Agent {counter} times.\n * we have run {recursions} max total iterations: {recursion_limit}\n\n", "green"))
upper_limit_recursions = recursion_limit
lower_limit_recursions = recursion_limit - 2
if recursions >= lower_limit_recursions and recursions <= upper_limit_recursions:
final_answer = "**You are being explicitly told to produce your [Type 2] work now!**"
elif recursions > upper_limit_recursions:
extra_recursions = recursions - upper_limit_recursions
base_message = "**You are being explicitly told to produce your [Type 2] work now!**"
final_answer = (base_message + "\n") * (extra_recursions + 1)
else:
final_answer = None
try:
requirements = state['requirements_gathering'][-1]["content"]
except:
requirements = state['requirements_gathering'][-1].content
formatted_requirements = '\n\n'.join(re.findall(r'```python\s*([\s\S]*?)\s*```', requirements, re.MULTILINE))
print(colored(f"\n\n User Requirements: {formatted_requirements}\n\n", 'green'))
if state.get("meta_prompt"):
try:
meta_prompt = state['meta_prompt'][-1]["content"]
except:
meta_prompt = state['meta_prompt'][-1].content
# print(colored(f"\n\n DEBUG Meta-Prompt: {meta_prompt}\n\n", 'yellow'))
cor_match = '\n\n'.join(re.findall(r'```python\s*([\s\S]*?)\s*```', meta_prompt, re.MULTILINE))
# print(colored(f"\n\n DEBUG CoR Match: {cor_match}\n\n", 'yellow'))
user_input = f"<requirements>{formatted_requirements}</requirements> \n\n Here is your last CoR {cor_match} update your next CoR from here."
else:
user_input = formatted_requirements
state = self.invoke(state=state, user_input=user_input, final_answer=final_answer)
meta_prompt_cor = state['meta_prompt'][-1]["content"]
print(colored(f"\n\n Meta-Prompt Chain of Reasoning: {meta_prompt_cor}\n\n", 'green'))
return state
class NoToolExpert(BaseAgent[State]):
def __init__(self, model: str = None, server: str = None, temperature: float = 0,
model_endpoint: str = None, stop: str = None):
super().__init__(model, server, temperature, model_endpoint, stop)
self.llm = self.get_llm(json_model=False)
def get_prompt(self, state) -> str:
# print(f"\nn{state}\n")
system_prompt = state["meta_prompt"][-1].content
return system_prompt
def process_response(self, response: Any, user_input: str = None, state: State = None) -> Dict[str, Union[str, dict]]:
# meta_prompts = state.get("meta_prompt", [])
associated_meta_prompt = state["meta_prompt"][-1].content
parse_expert = self.get_llm(json_model=True)
parse_expert_prompt = """
You must parse the expert from the text. The expert will be one of the following.
1. Expert Planner
2. Expert Writer
Return your response as the following JSON
{{"expert": "Expert Planner" or "Expert Writer"}}
"""
input = [
{"role": "user", "content": associated_meta_prompt},
{"role": "assistant", "content": f"system_prompt:{parse_expert_prompt}"}
]
retries = 0
associated_expert = None
while retries < 4 and associated_expert is None:
retries += 1
if self.server == 'vllm':
guided_json = guided_json_parse_expert
parse_expert_response = parse_expert.invoke(input, guided_json)
else:
parse_expert_response = parse_expert.invoke(input)
associated_expert_json = json.loads(parse_expert_response)
associated_expert = associated_expert_json.get("expert")
# associated_expert = parse_expert_text(associated_meta_prompt)
print(colored(f"\n\n Expert: {associated_expert}\n\n", 'green'))
if associated_expert == "Expert Planner":
expert_update_key = "expert_plan"
if associated_expert == "Expert Writer":
expert_update_key = "expert_writing"
updates_conversation_history = {
"conversation_history": [
{"role": "user", "content": user_input},
{"role": "assistant", "content": f"{str(response)}"}
],
expert_update_key: {"role": "assistant", "content": f"{str(response)}"}
}
return updates_conversation_history
def get_conv_history(self, state: State) -> str:
pass
def get_user_input(self) -> str:
pass
def get_guided_json(self, state: State) -> Dict[str, Any]:
pass
def use_tool(self) -> Any:
pass
# @log_function(logger)
def run(self, state: State) -> State:
# chat_counter(state)
all_expert_research = []
meta_prompt = state["meta_prompt"][1].content
if state.get("expert_research"):
expert_research = state["expert_research"]
all_expert_research.extend(expert_research)
research_prompt = f"\n Your response must be delivered considering following research.\n ## Research\n {all_expert_research} "
user_input = f"{meta_prompt}\n{research_prompt}"
else:
user_input = meta_prompt
state = self.invoke(state=state, user_input=user_input)
return state
class ToolExpert(BaseAgent[State]):
def __init__(self, model: str = None, server: str = None, temperature: float = 0,
model_endpoint: str = None, stop: str = None, location: str = None):
super().__init__(model, server, temperature, model_endpoint, stop, location)
print(f"\n\n DEBUG LOCATION: {self.location}")
self.llm = self.get_llm(json_model=False)
def get_prompt(self, state) -> str:
system_prompt = state["meta_prompt"][-1].content
return system_prompt
def process_response(self, response: Any, user_input: str = None, state: State = None) -> Dict[str, Union[str, dict]]:
updates_conversation_history = {
"conversation_history": [
{"role": "user", "content": user_input},
{"role": "assistant", "content": f"{str(response)}"}
],
"expert_research": {"role": "assistant", "content": f"{str(response)}"}
}
return updates_conversation_history
def get_conv_history(self, state: State) -> str:
pass
def get_user_input(self) -> str:
pass
def get_guided_json(self, state: State) -> Dict[str, Any]:
pass
def use_tool(self, mode: str, engine: str, tool_input: str, query: str = None) -> Any:
if mode == "serper":
if engine == "search":
results = serper_search(tool_input, self.location)
return {"results": results, "is_shopping": False}
elif engine == "shopping":
results = serper_shopping_search(tool_input, self.location)
return {"results": results, "is_shopping": True}
elif mode == "rag":
results = run_rag(urls=tool_input, query=query)
return {"results": results, "is_shopping": False}
def generate_search_queries(self, meta_prompt: str, num_queries: int = 5) -> List[str]:
refine_query_template = """
# Objective
Your mission is to systematically address your manager's instructions by determining
the most appropriate search queries to use **AND** to determine the best engine to use for each query.
Your engine choice is either search, or shopping. You must return either the search or shopping engine for each query.
You will generate {num_queries} different search queries.
# Manager's Instructions
{manager_instructions}
# Flexible Search Algorithm for Simple and Complex Questions
1. Initial search:
- For a simple question: "[Question keywords]"
- For a complex topic: "[Main topic] overview"
2. For each subsequent search:
- Choose one of these strategies:
a. Specify:
Add a more specific term or aspect related to the topic.
b. Broaden:
Remove a specific term or add "general" or "overview" to the query.
c. Pivot:
Choose a different but related term from the topic.
d. Compare:
Add "vs" or "compared to" along with a related term.
e. Question:
Rephrase the query as a question by adding "what", "how", "why", etc.
# Response Format
**Return the following JSON:**
{{
"search_queries": [
{{"engine": "search", "query": "Query 1"}},
{{"engine": "shopping", "query": "Query 2"}},
...
{{"engine": "search", "query": "Query {num_queries}"}}
]
}}
Remember:
- Generate {num_queries} unique and diverse search queries.
- Each query should explore a different aspect or approach to the topic.
- Ensure the queries cover various aspects of the manager's instructions.
- The "engine" field should be either "search" or "shopping" for each query.
"""
refine_query = self.get_llm(json_model=True)
refine_prompt = refine_query_template.format(manager_instructions=meta_prompt, num_queries=num_queries)
input = [
{"role": "user", "content": "Generate search queries"},
{"role": "assistant", "content": f"system_prompt:{refine_prompt}"}
]
guided_json = guided_json_search_query_two
if self.server == 'vllm':
refined_queries = refine_query.invoke(input, guided_json)
else:
refined_queries = refine_query.invoke(input)
refined_queries_json = json.loads(refined_queries)
return refined_queries_json.get("search_queries", [])
def process_serper_result(self, query, serper_response ):
best_url_template = """
Given the serper results, and the search query, select the best URL
# Search Query
{search_query}
# Serper Results
{serper_results}
**Return the following JSON:**
{{"best_url": The URL of the serper results that aligns most with the search query.}}
"""
best_url = self.get_llm(json_model=True)
best_url_prompt = best_url_template.format(search_query=query["query"], serper_results=serper_response)
input = [
{"role": "user", "content": serper_response},
{"role": "assistant", "content": f"system_prompt:{best_url_prompt}"}
]
guided_json = guided_json_best_url_two
if self.server == 'vllm':
best_url = best_url.invoke(input, guided_json)
else:
best_url = best_url.invoke(input)
best_url_json = json.loads(best_url)
return {"query": query, "url": best_url_json.get("best_url")}
# return best_url_json.get("best_url")
def run(self, state: State) -> State:
meta_prompt = state["meta_prompt"][-1].content
print(colored(f"\n\n Meta-Prompt: {meta_prompt}\n\n", 'green'))
# Generate multiple search queries
search_queries = self.generate_search_queries(meta_prompt, num_queries=5)
print(colored(f"\n\n Generated Search Queries: {search_queries}\n\n", 'green'))
try:
# Use multiprocessing to call Serper tool for each query in parallel
with Pool(processes=min(cpu_count(), len(search_queries))) as pool:
serper_results = pool.starmap(
self.use_tool,
[("serper", query["engine"], query["query"], None) for query in search_queries]
)
# Collect shopping results separately
shopping_results = [result["results"] for result in serper_results if result["is_shopping"]]
if shopping_results:
state["expert_research_shopping"] = shopping_results
# Process Serper results to get best URLs
with Pool(processes=min(cpu_count(), len(serper_results))) as pool:
best_urls = pool.starmap(
self.process_serper_result,
[(query, result["results"]) for query, result in zip(search_queries, serper_results)]
# zip(search_queries, serper_results)
)
except Exception as e:
print(colored(f"Error in multithreaded processing: {str(e)}. Falling back to non-multithreaded approach.", "yellow"))
# Fallback to non-multithreaded approach
serper_results = [self.use_tool("serper", query["engine"], query["query"], None) for query in search_queries]
shopping_results = [result["results"] for result in serper_results if result["is_shopping"]]
if shopping_results:
state["expert_research_shopping"] = shopping_results
best_urls = [self.process_serper_result(query, result) for query, result in zip(search_queries, serper_results)]
# Remove duplicates from the list of URLs
unique_urls = list(dict.fromkeys(result["url"] for result in best_urls if result["url"] and result["query"]["engine"] == "search"))
# unique_urls = list(dict.fromkeys(url for url in best_urls if url))
print(colored("\n\n Sourced data from {} sources:".format(len(unique_urls)), 'green'))
for i, url in enumerate(unique_urls, 1):
print(colored(" {}. {}".format(i, url), 'green'))
print()
try:
scraper_response = self.use_tool("rag", engine=None, tool_input=unique_urls, query=meta_prompt)
except Exception as e:
scraper_response = {"results": f"Error {e}: Failed to scrape results", "is_shopping": False}
updates = self.process_response(scraper_response, user_input="Research")
for key, value in updates.items():
state = self.update_state(key, value, state)
return state
class Router(BaseAgent[State]):
def __init__(self, model: str = None, server: str = None, temperature: float = 0,
model_endpoint: str = None, stop: str = None):
super().__init__(model, server, temperature, model_endpoint, stop)
self.llm = self.get_llm(json_model=True)
def get_prompt(self, state) -> str:
system_prompt = state["meta_prompt"][-1].content
return system_prompt
def process_response(self, response: Any, user_input: str = None, state: State = None) -> Dict[str, Union[str, dict]]:
updates_conversation_history = {
"router_decision": [
{"role": "user", "content": user_input},
{"role": "assistant", "content": f"{str(response)}"}
]
}
return updates_conversation_history
def get_conv_history(self, state: State) -> str:
pass
def get_user_input(self) -> str:
pass
def get_guided_json(self, state: State) -> Dict[str, Any]:
pass
def use_tool(self, tool_input: str, mode: str) -> Any:
pass
# @log_function(logger)
def run(self, state: State) -> State:
router_template = """
Given these instructions from your manager.
# Response from Manager
{manager_response}
**Return the following JSON:**
{{""router_decision: Return the next agent to pass control to.}}
**strictly** adhere to these **guidelines** for routing.
If your maneger's response contains "Expert Internet Researcher", return "tool_expert".
If your manager's response contains "Expert Planner" or "Expert Writer", return "no_tool_expert".
If your manager's response contains '>> FINAL ANSWER:', return "end_chat".
"""
system_prompt = router_template.format(manager_response=state["meta_prompt"][-1].content)
input = [
{"role": "user", "content": ""},
{"role": "assistant", "content": f"system_prompt:{system_prompt}"}
]
router = self.get_llm(json_model=True)
if self.server == 'vllm':
guided_json = guided_json_router_decision
router_response = router.invoke(input, guided_json)
else:
router_response = router.invoke(input)
router_response = json.loads(router_response)
router_response = router_response.get("router_decision")
state = self.update_state("router_decision", router_response, state)
return state |