Skip to content

Commit

Permalink
Add RunnableSequence for graph node execution
Browse files Browse the repository at this point in the history
  • Loading branch information
coolbeevip committed Jul 11, 2024
1 parent 588339e commit d38bcef
Show file tree
Hide file tree
Showing 8 changed files with 419 additions and 22 deletions.
37 changes: 36 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ beautifulsoup4 = "4.12.3"
SQLAlchemy = "2.0.22"
wikipedia = "1.4.0"
tabulate = "0.9.0"
openpyxl = "3.1.5"

[tool.pytest.ini_options]
pythonpath = ["src"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pandas as pd
from langchain_core.messages import BaseMessage, FunctionMessage, HumanMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables import RunnableSequence
from langchain_core.tools import tool
from langchain_core.utils.function_calling import convert_to_openai_function
from langchain_experimental.utilities import PythonREPL
Expand All @@ -26,7 +27,8 @@ class MarketingAnalysisAssistant:
def __init__(self, openai_api_base: str, openai_api_key: str, model_name: str, recursion_limit: int = 20):
self.model_name = model_name
self.recursion_limit = recursion_limit
self.llm = ChatOpenAI(model_name=model_name, openai_api_base=openai_api_base, openai_api_key=openai_api_key, temperature=0.7, request_timeout=600, streaming=True)
self.llm = ChatOpenAI(model_name=model_name, openai_api_base=openai_api_base, openai_api_key=openai_api_key,
temperature=0.7, request_timeout=600, streaming=True)
# 定义智能体
sales_staff = self.create_agent(
self.llm,
Expand All @@ -40,24 +42,23 @@ def __init__(self, openai_api_base: str, openai_api_key: str, model_name: str, r
system_message="负责团队管理和指导。设定销售目标,制定销售策略,监控绩效,并向团队成员提供反馈。",
)

sales_staff_node = functools.partial(self.agent_node, agent=sales_staff, name="sales_staff")
sales_manager_node = functools.partial(self.agent_node, agent=sales_manager, name="sales_manager")

# 定义图
workflow = StateGraph(AgentState)

workflow.add_node("sales_staff", sales_staff_node)
workflow.add_node("sales_manager", sales_manager_node)
workflow.add_node("sales_tool", self.sales_tool)
workflow.add_node("sales_staff",
functools.partial(self.graph_node_agent, agent=sales_staff, name="sales_staff"))
workflow.add_node("sales_manager",
functools.partial(self.graph_node_agent, agent=sales_manager, name="sales_manager"))
workflow.add_node("sales_tool", self.graph_node_sales_tool)

workflow.add_conditional_edges(
"sales_staff",
self.router,
self.graph_node_router,
{"continue": "sales_manager", "sales_tool": "sales_tool", "end": END},
)
workflow.add_conditional_edges(
"sales_manager",
self.router,
self.graph_node_router,
{"continue": "sales_staff", "sales_tool": "sales_tool", "end": END},
)

Expand Down Expand Up @@ -119,7 +120,7 @@ def data_analysis_tool():
return response

@staticmethod
def agent_node(state, agent, name):
def graph_node_agent(state: AgentState, agent: RunnableSequence, name: str):
print(f"Executing {name} node!")
result = agent.invoke(state)
# 将代理的输出转换为适合添加到全局状态的格式。
Expand All @@ -135,7 +136,7 @@ def agent_node(state, agent, name):
}

@staticmethod
def router(state):
def graph_node_router(state: AgentState):
messages = state["messages"]
last_message = messages[-1]

Expand All @@ -147,7 +148,7 @@ def router(state):

return "continue"

def sales_tool(self, state):
def graph_node_sales_tool(self, state: AgentState):
"""This runs tools in the graph
It takes in an agent action and calls that tool and returns the result."""
Expand Down Expand Up @@ -210,15 +211,15 @@ def run(self):
"messages": [
HumanMessage(
content="利用事先准备好的agent和tool进行会话。"
"会话的主题是'调查我们公司商品A、B、C过去5年的数据,并制定本期的销售战略。"
"会话由sales_staff开始。"
"数据分析工具必须使用'./sales_data.csv'文件,并已表格形式输出数据。"
"数据分析工具将从'./sales_data.csv'文件中读取数据,进行基本统计和相关关系分析。"
"数据分析工具将输出文本形式的分析结果,并提供基于分析结果的见解。"
"接下来,将数据分析工具给出的分析结果和见解传达给sales_staff。"
"然后,sales_staff和sales_manager根据数据分析工具提供的分析结果和见解进行交流,并共同制定本期的销售策略。"
"sales_staff和sales_manager的会话总次数最多为20次。"
"最后,sales_manager在总结所有对话后,列出重要的要点并结束。"
"会话的主题是'调查我们公司商品A、B、C过去5年的数据,并制定本期的销售战略。"
"会话由sales_staff开始。"
"数据分析工具必须使用'./sales_data.csv'文件,并已表格形式输出数据。"
"数据分析工具将从'./sales_data.csv'文件中读取数据,进行基本统计和相关关系分析。"
"数据分析工具将输出文本形式的分析结果,并提供基于分析结果的见解。"
"接下来,将数据分析工具给出的分析结果和见解传达给sales_staff。"
"然后,sales_staff和sales_manager根据数据分析工具提供的分析结果和见解进行交流,并共同制定本期的销售策略。"
"sales_staff和sales_manager的会话总次数最多为20次。"
"最后,sales_manager在总结所有对话后,列出重要的要点并结束。"
)
],
},
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@startuml
start

:网络运营经理 (NetworkOpsManager);
:接收统计报表并初步审核;
:召集分析会议;

:无线网络工程师 (WirelessNetworkEngineer);
:分析无线网络性能和资源数据;


:数据分析师 (DataAnalyst);
:深入挖掘数据,提供建议;
:准备数据可视化报告;

:网络维护人员 (NetworkMaintenanceTech);
:确认并解决技术问题;


:IT运营经理 (ITOpsManager);
:协调IT资源,支持分析和解决问题;
:优化网络操作流程;

:客户服务经理 (CustomerServiceManager);
:整理客户投诉和反馈;


:高层管理人员 (ExecutiveManagement);
:审核整体报告和建议;
:批准必要的优化项目;

:质量保证(QA)团队 (QATeam);
:评估最终网络服务质量;
:确保符合公司标准和法规;

stop
@enduml
Loading

0 comments on commit d38bcef

Please sign in to comment.