2026-02-17 02:31:39 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""
|
2026-02-26 13:43:44 +08:00
|
|
|
LangChain + LangGraph 脚手架的基础用法示例
|
2026-02-17 02:31:39 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
|
|
|
|
|
from workflows.workflow_manager import WorkflowManager, WorkflowType
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def example_conversation():
|
2026-02-26 13:43:44 +08:00
|
|
|
"""对话工作流示例"""
|
2026-02-17 02:31:39 +08:00
|
|
|
print("=== Conversation Workflow Example ===")
|
|
|
|
|
|
2026-02-26 13:43:44 +08:00
|
|
|
# 使用 config.ini 中的默认模型
|
2026-02-17 02:31:39 +08:00
|
|
|
manager = WorkflowManager()
|
|
|
|
|
|
2026-02-26 13:43:44 +08:00
|
|
|
# 第一条消息
|
2026-02-17 02:31:39 +08:00
|
|
|
result1 = manager.execute_workflow(
|
|
|
|
|
WorkflowType.CONVERSATION,
|
2026-02-26 13:43:44 +08:00
|
|
|
"你好,你能帮我做什么?"
|
2026-02-17 02:31:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
print(f"Session ID: {result1['session_id']}")
|
|
|
|
|
print(f"Response: {result1['result']['messages'][-1].content}")
|
|
|
|
|
|
2026-02-26 13:43:44 +08:00
|
|
|
# 同一会话中的第二条消息
|
2026-02-17 02:31:39 +08:00
|
|
|
result2 = manager.execute_workflow(
|
|
|
|
|
WorkflowType.CONVERSATION,
|
2026-02-26 13:43:44 +08:00
|
|
|
"你的优势是什么?",
|
2026-02-17 02:31:39 +08:00
|
|
|
session_id=result1['session_id']
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
print(f"Second response: {result2['result']['messages'][-1].content}")
|
2026-02-26 13:43:44 +08:00
|
|
|
|
|
|
|
|
# 使用不同模型的示例
|
|
|
|
|
# print("\n--- Using a different model (gpt-3.5-turbo) ---")
|
|
|
|
|
# try:
|
|
|
|
|
# manager_alt_model = WorkflowManager(default_model_section='gpt-3.5-turbo')
|
|
|
|
|
# result_alt = manager_alt_model.execute_workflow(
|
|
|
|
|
# WorkflowType.CONVERSATION,
|
|
|
|
|
# "Hi, what model are you?"
|
|
|
|
|
# )
|
|
|
|
|
# print(f"Response from gpt-3.5-turbo: {result_alt['result']['messages'][-1].content}")
|
|
|
|
|
# except ValueError as e:
|
|
|
|
|
# print(f"Could not run alternate model example: {e}")
|
|
|
|
|
# print("Please ensure you have a [gpt-3.5-turbo] section in your config.ini")
|
|
|
|
|
#
|
|
|
|
|
# print("\n")
|
2026-02-17 02:31:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def example_tool_usage():
|
2026-02-26 13:43:44 +08:00
|
|
|
"""工具工作流示例"""
|
2026-02-17 02:31:39 +08:00
|
|
|
print("=== Tool Workflow Example ===")
|
|
|
|
|
|
|
|
|
|
manager = WorkflowManager()
|
|
|
|
|
|
2026-02-26 13:43:44 +08:00
|
|
|
# 使用计算器工具
|
2026-02-17 02:31:39 +08:00
|
|
|
result = manager.execute_workflow(
|
|
|
|
|
WorkflowType.TOOL_USING,
|
2026-02-26 13:43:44 +08:00
|
|
|
"计算 25 * 4 + 10"
|
2026-02-17 02:31:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
print(f"Session ID: {result['session_id']}")
|
|
|
|
|
|
2026-02-26 13:43:44 +08:00
|
|
|
# 提取工具消息与回复
|
2026-02-17 02:31:39 +08:00
|
|
|
for message in result['result']['messages']:
|
|
|
|
|
if hasattr(message, 'tool_calls') and message.tool_calls:
|
|
|
|
|
print(f"Tool call: {message.tool_calls}")
|
2026-02-26 13:43:44 +08:00
|
|
|
elif hasattr(message, 'content') and message.content:
|
2026-02-17 02:31:39 +08:00
|
|
|
print(f"Response: {message.content}")
|
|
|
|
|
|
|
|
|
|
print("\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def list_available_workflows():
|
2026-02-26 13:43:44 +08:00
|
|
|
"""列出可用工作流"""
|
2026-02-17 02:31:39 +08:00
|
|
|
print("=== Available Workflows ===")
|
|
|
|
|
|
|
|
|
|
manager = WorkflowManager()
|
|
|
|
|
workflows = manager.get_available_workflows()
|
|
|
|
|
|
|
|
|
|
for workflow in workflows:
|
|
|
|
|
print(f"- {workflow}")
|
|
|
|
|
|
|
|
|
|
print("\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2026-02-26 13:43:44 +08:00
|
|
|
# 检查配置是否有效
|
2026-02-17 02:31:39 +08:00
|
|
|
try:
|
|
|
|
|
from config import Config
|
|
|
|
|
Config.validate_config()
|
|
|
|
|
print("✅ Configuration is valid")
|
|
|
|
|
print("\n")
|
|
|
|
|
|
2026-02-26 13:43:44 +08:00
|
|
|
# 运行示例
|
2026-02-17 02:31:39 +08:00
|
|
|
list_available_workflows()
|
|
|
|
|
example_conversation()
|
|
|
|
|
example_tool_usage()
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"❌ Configuration error: {e}")
|
|
|
|
|
print("\nPlease make sure to:")
|
2026-02-26 13:43:44 +08:00
|
|
|
print("1. Copy 'config/config.ini.example' to 'config/config.ini'")
|
|
|
|
|
print("2. Set your API key in the 'config/config.ini' file")
|
2026-02-17 02:31:39 +08:00
|
|
|
print("3. Install dependencies: pip install -r requirements.txt")
|