-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_agent.py
More file actions
80 lines (57 loc) · 2.27 KB
/
Copy pathparallel_agent.py
File metadata and controls
80 lines (57 loc) · 2.27 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
from langgraph.graph import StateGraph, START, END
from typing_extensions import TypedDict
from langchain_ollama import ChatOllama
from IPython.display import Image, display
# Graph state
class State(TypedDict):
topic: str
tagline: str
usecases: str
marketingidea: str
combined_output: str
#Initializing LLM
llm = ChatOllama( # Changed from OllamaLLM to ChatOllama
model="qwen2.5:latest",
temperature=0.7
)
# Nodes
def call_llm_1(state: State):
"""First LLM call to generate initial tag_line"""
msg = llm.invoke(f"Write a tag_line about {state['topic']}")
return {"tagline": msg.content}
def call_llm_2(state: State):
"""Second LLM call to generate use case"""
msg = llm.invoke(f"Write a use cases about {state['topic']}")
return {"usecases": msg.content}
def call_llm_3(state: State):
"""Third LLM call to generate marketing idea"""
msg = llm.invoke(f"Write a marketing idea about {state['topic']}")
return {"marketingidea": msg.content}
def aggregator(state: State):
"""Combine the tagline,use cases and marketing idea into a single output"""
combined = f"Here's a tagline , use cases, and marketing about {state['topic']}!\n\n"
combined += f"tagline:\n{state['tagline']}\n\n"
combined += f"usecases:\n{state['usecases']}\n\n"
combined += f"marketingidea:\n{state['marketingidea']}"
return {"combined_output": combined}
# Build workflow
parallel_builder = StateGraph(State)
# Add nodes
parallel_builder.add_node("call_llm_1", call_llm_1)
parallel_builder.add_node("call_llm_2", call_llm_2)
parallel_builder.add_node("call_llm_3", call_llm_3)
parallel_builder.add_node("aggregator", aggregator)
# Add edges to connect nodes
parallel_builder.add_edge(START, "call_llm_1")
parallel_builder.add_edge(START, "call_llm_2")
parallel_builder.add_edge(START, "call_llm_3")
parallel_builder.add_edge("call_llm_1", "aggregator")
parallel_builder.add_edge("call_llm_2", "aggregator")
parallel_builder.add_edge("call_llm_3", "aggregator")
parallel_builder.add_edge("aggregator", END)
parallel_workflow = parallel_builder.compile()
# Show workflow
# display(Image(parallel_workflow.get_graph().draw_mermaid_png()))
# Invoke
state = parallel_workflow.invoke({"topic": "samsung AI smart phone"})
print(state["combined_output"])