-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathagent.py
More file actions
205 lines (169 loc) · 7.76 KB
/
Copy pathagent.py
File metadata and controls
205 lines (169 loc) · 7.76 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
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
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
""" Agent module - Smart Document Processor with Custom Agent"""
from typing import AsyncGenerator
from pydantic import ConfigDict
from trpc_agent_sdk.agents import BaseAgent
from trpc_agent_sdk.agents import ChainAgent
from trpc_agent_sdk.agents import LlmAgent
from trpc_agent_sdk.context import InvocationContext
from trpc_agent_sdk.events import Event
from trpc_agent_sdk.events import create_text_event
from trpc_agent_sdk.models import LLMModel
from trpc_agent_sdk.models import OpenAIModel
from .config import get_model_config
from .prompts import COMPLEX_ANALYZER_INSTRUCTION
from .prompts import COMPLEX_PROCESSOR_INSTRUCTION
from .prompts import DOCUMENT_ANALYZER_INSTRUCTION
from .prompts import QUALITY_VALIDATOR_INSTRUCTION
from .prompts import SIMPLE_PROCESSOR_INSTRUCTION
from .prompts import TECHNICAL_PROCESSOR_INSTRUCTION
class SmartDocumentProcessor(BaseAgent):
"""Smart document processing Custom Agent
Dynamically select processing strategies based on document type and complexity:
- Simple documents: Direct processing
- Complex documents: Analyze → Process → Validate
- Technical documents: Special processing process
This demonstrates the core capabilities of Custom Agents: conditional logic and dynamic agent selection
"""
model_config = ConfigDict(arbitrary_types_allowed=True)
document_analyzer: LlmAgent
simple_processor: LlmAgent
complex_processor_chain: ChainAgent
technical_processor: LlmAgent
quality_validator: LlmAgent
def __init__(self, model, **kwargs):
document_analyzer = LlmAgent(
name="document_analyzer",
model=model,
description="Analyze document type and complexity",
instruction=DOCUMENT_ANALYZER_INSTRUCTION,
output_key="doc_type",
)
simple_processor = LlmAgent(
name="simple_processor",
model=model,
description="Process simple documents efficiently",
instruction=SIMPLE_PROCESSOR_INSTRUCTION,
output_key="processed_content",
)
complex_analyzer = LlmAgent(
name="complex_analyzer",
model=model,
description="Analyze complex document structure",
instruction=COMPLEX_ANALYZER_INSTRUCTION,
output_key="complex_analysis",
)
complex_processor = LlmAgent(
name="complex_processor",
model=model,
description="Process complex documents based on analysis",
instruction=COMPLEX_PROCESSOR_INSTRUCTION,
output_key="processed_content",
)
complex_processor_chain = ChainAgent(
name="complex_processor_chain",
description="Complex document processing: analyze → process",
sub_agents=[complex_analyzer, complex_processor],
)
technical_processor = LlmAgent(
name="technical_processor",
model=model,
description="Process technical documents with specialized approach",
instruction=TECHNICAL_PROCESSOR_INSTRUCTION,
output_key="processed_content",
)
quality_validator = LlmAgent(
name="quality_validator",
model=model,
description="Validate processing quality and suggest improvements",
instruction=QUALITY_VALIDATOR_INSTRUCTION,
output_key="quality_feedback",
)
sub_agents_list = [
document_analyzer,
simple_processor,
complex_processor_chain,
technical_processor,
quality_validator,
]
super().__init__(
document_analyzer=document_analyzer,
simple_processor=simple_processor,
complex_processor_chain=complex_processor_chain,
technical_processor=technical_processor,
quality_validator=quality_validator,
sub_agents=sub_agents_list,
**kwargs,
)
async def _run_async_impl(self, ctx: InvocationContext) -> AsyncGenerator[Event, None]:
"""Customized orchestration logic for smart document processing
1. Conditional logic: Select different processing flows based on document type
2. State management: Pass analysis results between agents
3. Dynamic decision: Based on processing results, decide whether to validate
"""
print(f" 📋 [{self.name}] Start smart document processing workflow")
internal_start = create_text_event(
ctx=ctx,
text=f"[Internal log] Workflow started, session_id={ctx.session.id}",
)
yield internal_start
# First stage: Document type analysis
print(f" 🔍 [{self.name}] Stage 1: Analyze document type...")
async for event in self.document_analyzer.run_async(ctx):
yield event
doc_type = ctx.session.state.get("doc_type", "simple").lower().strip()
print(f" 📊 [{self.name}] Document type recognition: {doc_type}")
# Second stage: Select processing strategy based on document type
if doc_type == "simple":
print(f" ⚡ [{self.name}] Stage 2: Use simple processing flow...")
async for event in self.simple_processor.run_async(ctx):
yield event
elif doc_type == "complex":
print(f" 🧠 [{self.name}] Stage 2: Use complex document processing flow...")
print(f" 🔗 [{self.name}] Use ChainAgent: Analyze → Process")
async for event in self.complex_processor_chain.run_async(ctx):
yield event
elif doc_type == "technical":
print(f" 🔧 [{self.name}] Stage 2: Use technical document processing flow...")
async for event in self.technical_processor.run_async(ctx):
yield event
else:
print(f" ❓ [{self.name}] Unknown document type, use simple processing...")
async for event in self.simple_processor.run_async(ctx):
yield event
# Third stage: Quality validation decision
if doc_type in ["complex", "technical"]:
print(f" ✅ [{self.name}] Stage 3: Execute quality validation...")
async for event in self.quality_validator.run_async(ctx):
yield event
quality_feedback = ctx.session.state.get("quality_feedback", "")
if "Quality validation passed" in quality_feedback:
print(f" 🎉 [{self.name}] Quality validation passed, processing completed!")
else:
print(f" 📝 [{self.name}] Quality validation found improvement points, provided suggestions")
else:
print(f" ⏭️ [{self.name}] Simple document skipped quality validation stage")
internal_complete = create_text_event(
ctx=ctx,
text=f"[Internal log] Workflow completed, document type={doc_type}",
)
yield internal_complete
print(f" ✨ [{self.name}] Smart document processing workflow completed!")
def _create_model() -> LLMModel:
""" Create a model"""
api_key, url, model_name = get_model_config()
model = OpenAIModel(model_name=model_name, api_key=api_key, base_url=url)
return model
def create_agent() -> SmartDocumentProcessor:
""" Create the smart document processor agent"""
return SmartDocumentProcessor(
name="smart_document_processor",
description=
"A smart document processing system that dynamically selects the optimal processing strategy based on document type",
model=_create_model(),
)
root_agent = create_agent()