Spaces:
Sleeping
Sleeping
Update agents.py
Browse files
agents.py
CHANGED
@@ -1,106 +1,109 @@
|
|
1 |
-
from
|
2 |
-
|
3 |
-
|
4 |
-
from langgraph.
|
5 |
-
from
|
6 |
-
from
|
7 |
-
import
|
8 |
-
from
|
9 |
-
from
|
10 |
-
|
11 |
-
|
12 |
-
from
|
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 |
-
|
|
|
|
|
|
|
|
1 |
+
from patch_langmem import patch_langmem_for_python310
|
2 |
+
patch_langmem_for_python310()
|
3 |
+
|
4 |
+
from langgraph.graph import StateGraph, MessagesState, START, END
|
5 |
+
from langmem.short_term import SummarizationNode
|
6 |
+
from langchain_core.messages.utils import count_tokens_approximately
|
7 |
+
from langgraph.prebuilt.chat_agent_executor import AgentState
|
8 |
+
from langgraph.checkpoint.memory import InMemorySaver
|
9 |
+
from typing import Any
|
10 |
+
import os
|
11 |
+
from langchain.chat_models import init_chat_model
|
12 |
+
from langgraph.prebuilt import create_react_agent
|
13 |
+
from langgraph_supervisor import create_supervisor
|
14 |
+
import custom_tools as custom_tools
|
15 |
+
from dotenv import load_dotenv
|
16 |
+
|
17 |
+
load_dotenv()
|
18 |
+
|
19 |
+
supervisor_prompt = os.getenv("SUPERVISOR_PROMPT")
|
20 |
+
|
21 |
+
def build_graph():
|
22 |
+
a_debug = False
|
23 |
+
model = init_chat_model("llama3-70b-8192", model_provider="groq")
|
24 |
+
summarization_node = SummarizationNode(token_counter=count_tokens_approximately,
|
25 |
+
model=model,
|
26 |
+
max_tokens=2048,
|
27 |
+
max_summary_tokens=1024,
|
28 |
+
output_messages_key="llm_input_messages",)
|
29 |
+
|
30 |
+
class State(AgentState):
|
31 |
+
context: dict[str, Any]
|
32 |
+
|
33 |
+
checkpointer = InMemorySaver()
|
34 |
+
|
35 |
+
|
36 |
+
agent0= create_react_agent(model=model,
|
37 |
+
tools= [custom_tools.stock_data_tool],
|
38 |
+
prompt=("Give the company name to the stock tool produce the stock fundamentals. Summarize all the information in one paragraph in 200 words."),
|
39 |
+
name="Stock Analyst",
|
40 |
+
pre_model_hook= summarization_node,
|
41 |
+
state_schema=State,
|
42 |
+
checkpointer=checkpointer,
|
43 |
+
debug=a_debug)
|
44 |
+
|
45 |
+
agent1= create_react_agent(model=model,
|
46 |
+
tools= [custom_tools.web_search],
|
47 |
+
prompt='''You are a geek that can find anything from the internet. Do the research and provide the summery in 250 words and in only one paragraph.''',
|
48 |
+
name="Web Surfer",
|
49 |
+
pre_model_hook= summarization_node,
|
50 |
+
state_schema=State,
|
51 |
+
checkpointer=checkpointer,
|
52 |
+
debug=a_debug)
|
53 |
+
|
54 |
+
agent2= create_react_agent(model=model,
|
55 |
+
tools= [custom_tools.reddit_search_tool],
|
56 |
+
prompt='''You are a social media analyst. your job is to search social media and find out the sentiments of public on any topic. Do the research and provide the summery in 250 words and in only one paragraph.''',
|
57 |
+
name="Social Media Analyst",
|
58 |
+
pre_model_hook= summarization_node,
|
59 |
+
state_schema=State,
|
60 |
+
checkpointer=checkpointer,
|
61 |
+
debug=a_debug)
|
62 |
+
|
63 |
+
agent3= create_react_agent(model=model,
|
64 |
+
tools= [custom_tools.tech_news_tool],
|
65 |
+
prompt='''You are a Technology news analyst. You find the latest news related to a topic and summerize it to findout the overall picture. Do the research and provide the summery in 250 words and in only one paragraph.''',
|
66 |
+
name="Tech Journalist",
|
67 |
+
pre_model_hook= summarization_node,
|
68 |
+
state_schema=State,
|
69 |
+
checkpointer=checkpointer,
|
70 |
+
debug=a_debug)
|
71 |
+
|
72 |
+
agent4= create_react_agent(model=model,
|
73 |
+
tools= [custom_tools.politics_news_tool],
|
74 |
+
prompt='''You are a Politics news analyst. You find the latest news related to a topic and summerize it to findout the overall picture. Do the research and provide the summery in 250 words and in only one paragraph.''',
|
75 |
+
name="Political Journalist",
|
76 |
+
pre_model_hook= summarization_node,
|
77 |
+
state_schema=State,
|
78 |
+
checkpointer=checkpointer,
|
79 |
+
debug=a_debug)
|
80 |
+
|
81 |
+
agent5= create_react_agent(model=model,
|
82 |
+
tools= [custom_tools.business_news_tool],
|
83 |
+
prompt='''You are a Business news analyst. You find the latest news related to a topic and summerize it to findout the overall picture. Do the research and provide the summery in 250 words and in only one paragraph.''',
|
84 |
+
name="Business Journalist",
|
85 |
+
pre_model_hook= summarization_node,
|
86 |
+
state_schema=State,
|
87 |
+
checkpointer=checkpointer,
|
88 |
+
debug=a_debug)
|
89 |
+
|
90 |
+
agent6= create_react_agent(model=model,
|
91 |
+
tools= [custom_tools.world_news_tool],
|
92 |
+
prompt='''You are a Geopolitical news analyst. You find the latest news related to a topic and summerize it to findout the overall picture. Do the research and provide the summery in 250 words and in only one paragraph.''',
|
93 |
+
name="Geopolitical Journalist",
|
94 |
+
pre_model_hook= summarization_node,
|
95 |
+
state_schema=State,
|
96 |
+
checkpointer=checkpointer,
|
97 |
+
debug=a_debug)
|
98 |
+
|
99 |
+
|
100 |
+
supervisor_prompt = '''
|
101 |
+
|
102 |
+
'''
|
103 |
+
|
104 |
+
|
105 |
+
graph = create_supervisor(agents=[agent0,agent1,agent2,agent3,agent4,agent5,agent6],
|
106 |
+
model=model,
|
107 |
+
prompt= supervisor_prompt)
|
108 |
+
return graph.compile()
|
109 |
+
|