-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpipeline_simulation.py
More file actions
209 lines (173 loc) · 7.38 KB
/
pipeline_simulation.py
File metadata and controls
209 lines (173 loc) · 7.38 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
206
207
208
209
"""
AI Pipeline Simulation - Demonstrates a complete document processing workflow.
Simulates: Object Detection → Classification → OCR → RAG → LLM
"""
import asyncio
import os
import random
import time
import uuid
from datetime import datetime
from dotenv import load_dotenv
load_dotenv()
from value import initialize_async
from opentelemetry import trace
# Get agent secret from environment variable
AGENT_SECRET = os.getenv("VALUE_AGENT_SECRET", "your-agent-secret")
INVOICE_TYPES = ["invoice", "receipt", "bill", "purchase_order"]
VENDORS = ["Amazon", "Google", "Microsoft", "Apple", "Uber", "Lyft"]
CURRENCIES = ["USD", "EUR", "GBP"]
class PipelineSimulator:
def __init__(self, client):
self.client = client
self.tracer = client.tracer
async def run_pipeline(self):
user_id = f"user_{random.randint(1000, 9999)}"
anonymous_id = str(uuid.uuid4())
invoice_id = f"INV-{random.randint(10000, 99999)}"
print(f"\n--- Pipeline: {invoice_id} (User: {user_id}) ---")
await self._object_detection(user_id, anonymous_id, invoice_id)
await self._object_classification(user_id, anonymous_id, invoice_id)
text = await self._ocr(user_id, anonymous_id, invoice_id)
context = await self._rag(user_id, anonymous_id, invoice_id, text)
await self._llm(user_id, anonymous_id, invoice_id, context)
async def _delay(self):
delay = random.uniform(1.0, 2.0)
await asyncio.sleep(delay)
return delay
async def _object_detection(self, user_id: str, anonymous_id: str, invoice_id: str):
print("1. Object Detection...")
await self._delay()
with self.client.action_context(anonymous_id=anonymous_id, user_id=user_id) as ctx:
ctx.send(
action_name="object_detection",
**{
"value.action.description": f"Detected objects in {invoice_id}",
"invoice_id": invoice_id,
"model": "yolov8-invoice",
"confidence_score": random.uniform(0.85, 0.99),
"detected_boxes": random.randint(3, 10),
},
)
async def _object_classification(self, user_id: str, anonymous_id: str, invoice_id: str):
print("2. Classification...")
await self._delay()
doc_type = random.choice(INVOICE_TYPES)
with self.client.action_context(anonymous_id=anonymous_id, user_id=user_id) as ctx:
ctx.send(
action_name="object_classification",
**{
"value.action.description": f"Classified as {doc_type}",
"invoice_id": invoice_id,
"document_type": doc_type,
"confidence": random.uniform(0.90, 0.99),
},
)
async def _ocr(self, user_id: str, anonymous_id: str, invoice_id: str) -> str:
print("3. OCR...")
await self._delay()
vendor = random.choice(VENDORS)
total = round(random.uniform(10.0, 500.0), 2)
currency = random.choice(CURRENCIES)
text = f"Invoice from {vendor}\nTotal: {total} {currency}"
with self.client.action_context(anonymous_id=anonymous_id, user_id=user_id) as ctx:
ctx.send(
action_name="ocr_processing",
**{
"value.action.description": f"Extracted text from {invoice_id}",
"invoice_id": invoice_id,
"ocr_engine": "tesseract",
"text_length": len(text),
},
)
return text
async def _rag(self, user_id: str, anonymous_id: str, invoice_id: str, query: str) -> str:
print("4. RAG Retrieval...")
await self._delay()
chunks = random.randint(2, 5)
with self.client.action_context(anonymous_id=anonymous_id, user_id=user_id) as ctx:
ctx.send(
action_name="rag_retrieval",
**{
"value.action.description": f"Retrieved {chunks} chunks",
"invoice_id": invoice_id,
"vector_db": "chroma",
"chunks_count": chunks,
},
)
return f"Context for {query}"
async def _llm(self, user_id: str, anonymous_id: str, invoice_id: str, context: str):
print("5. LLM Call...")
delay = await self._delay()
if random.choice([True, False]):
await self._gemini_call(user_id, anonymous_id, invoice_id, delay)
else:
await self._ollama_call(user_id, anonymous_id, invoice_id, delay)
async def _gemini_call(self, user_id: str, anonymous_id: str, invoice_id: str, duration: float):
print(" -> Gemini API")
model = "gemini-2.5-flash"
start_time = time.time_ns()
end_time = start_time + int(duration * 1e9)
span = self.tracer.start_span(
name="gemini.generate_content",
kind=trace.SpanKind.CLIENT,
attributes={
"gen_ai.system": "Google",
"gen_ai.request.model": model,
"gen_ai.response.model": model,
"gen_ai.usage.prompt_tokens": str(random.randint(10, 50)),
"gen_ai.usage.completion_tokens": str(random.randint(50, 200)),
"value.action.user_id": user_id,
"value.action.anonymous_id": anonymous_id,
},
start_time=start_time,
)
span.end(end_time=end_time)
with self.client.action_context(anonymous_id=anonymous_id, user_id=user_id) as ctx:
ctx.send(
action_name="process_gemini_response",
**{
"value.action.description": f"Processed {model} response",
"model": model,
"prompt_type": "summarization",
},
)
async def _ollama_call(self, user_id: str, anonymous_id: str, invoice_id: str, duration: float):
print(" -> Ollama API")
model = "llama3"
start_time = time.time_ns()
end_time = start_time + int(duration * 1e9)
span = self.tracer.start_span(
name="ollama.generate",
kind=trace.SpanKind.CLIENT,
attributes={
"value.action.llm.model": model,
"value.action.llm.input_tokens": str(random.randint(10, 50)),
"value.action.llm.output_tokens": str(random.randint(50, 200)),
},
start_time=start_time,
)
span.end(end_time=end_time)
with self.client.action_context(anonymous_id=anonymous_id, user_id=user_id) as ctx:
ctx.send(
action_name="process_ollama_response",
**{
"value.action.description": f"Processed {model} response",
"model": model,
"prompt_type": "extraction",
},
)
async def main():
print("Initializing Value SDK...")
client = await initialize_async(agent_secret=AGENT_SECRET)
simulator = PipelineSimulator(client)
print("Starting simulation (Ctrl+C to stop)...")
try:
while True:
await simulator.run_pipeline()
print("Waiting 5 seconds...")
await asyncio.sleep(5)
except KeyboardInterrupt:
print("\nStopped.")
if __name__ == "__main__":
asyncio.run(main())