botrun-flow-lang 5.12.263__py3-none-any.whl → 6.2.21__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- botrun_flow_lang/api/auth_api.py +39 -39
- botrun_flow_lang/api/auth_utils.py +183 -183
- botrun_flow_lang/api/botrun_back_api.py +65 -65
- botrun_flow_lang/api/flow_api.py +3 -3
- botrun_flow_lang/api/hatch_api.py +508 -508
- botrun_flow_lang/api/langgraph_api.py +816 -811
- botrun_flow_lang/api/langgraph_constants.py +11 -0
- botrun_flow_lang/api/line_bot_api.py +1484 -1484
- botrun_flow_lang/api/model_api.py +300 -300
- botrun_flow_lang/api/rate_limit_api.py +32 -32
- botrun_flow_lang/api/routes.py +79 -79
- botrun_flow_lang/api/search_api.py +53 -53
- botrun_flow_lang/api/storage_api.py +395 -395
- botrun_flow_lang/api/subsidy_api.py +290 -290
- botrun_flow_lang/api/subsidy_api_system_prompt.txt +109 -109
- botrun_flow_lang/api/user_setting_api.py +70 -70
- botrun_flow_lang/api/version_api.py +31 -31
- botrun_flow_lang/api/youtube_api.py +26 -26
- botrun_flow_lang/constants.py +13 -13
- botrun_flow_lang/langgraph_agents/agents/agent_runner.py +178 -178
- botrun_flow_lang/langgraph_agents/agents/agent_tools/step_planner.py +77 -77
- botrun_flow_lang/langgraph_agents/agents/checkpointer/firestore_checkpointer.py +666 -666
- botrun_flow_lang/langgraph_agents/agents/gov_researcher/GOV_RESEARCHER_PRD.md +192 -192
- botrun_flow_lang/langgraph_agents/agents/gov_researcher/gemini_subsidy_graph.py +460 -460
- botrun_flow_lang/langgraph_agents/agents/gov_researcher/gov_researcher_2_graph.py +1002 -1002
- botrun_flow_lang/langgraph_agents/agents/gov_researcher/gov_researcher_graph.py +822 -822
- botrun_flow_lang/langgraph_agents/agents/langgraph_react_agent.py +730 -723
- botrun_flow_lang/langgraph_agents/agents/search_agent_graph.py +864 -864
- botrun_flow_lang/langgraph_agents/agents/tools/__init__.py +4 -4
- botrun_flow_lang/langgraph_agents/agents/tools/gemini_code_execution.py +376 -376
- botrun_flow_lang/langgraph_agents/agents/util/gemini_grounding.py +66 -66
- botrun_flow_lang/langgraph_agents/agents/util/html_util.py +316 -316
- botrun_flow_lang/langgraph_agents/agents/util/img_util.py +336 -294
- botrun_flow_lang/langgraph_agents/agents/util/local_files.py +419 -419
- botrun_flow_lang/langgraph_agents/agents/util/mermaid_util.py +86 -86
- botrun_flow_lang/langgraph_agents/agents/util/model_utils.py +143 -143
- botrun_flow_lang/langgraph_agents/agents/util/pdf_analyzer.py +562 -486
- botrun_flow_lang/langgraph_agents/agents/util/pdf_cache.py +250 -250
- botrun_flow_lang/langgraph_agents/agents/util/pdf_processor.py +204 -204
- botrun_flow_lang/langgraph_agents/agents/util/perplexity_search.py +464 -464
- botrun_flow_lang/langgraph_agents/agents/util/plotly_util.py +59 -59
- botrun_flow_lang/langgraph_agents/agents/util/tavily_search.py +199 -199
- botrun_flow_lang/langgraph_agents/agents/util/usage_metadata.py +34 -0
- botrun_flow_lang/langgraph_agents/agents/util/youtube_util.py +90 -90
- botrun_flow_lang/langgraph_agents/cache/langgraph_botrun_cache.py +197 -197
- botrun_flow_lang/llm_agent/llm_agent.py +19 -19
- botrun_flow_lang/llm_agent/llm_agent_util.py +83 -83
- botrun_flow_lang/log/.gitignore +2 -2
- botrun_flow_lang/main.py +61 -61
- botrun_flow_lang/main_fast.py +51 -51
- botrun_flow_lang/mcp_server/__init__.py +10 -10
- botrun_flow_lang/mcp_server/default_mcp.py +854 -744
- botrun_flow_lang/models/nodes/utils.py +205 -205
- botrun_flow_lang/models/token_usage.py +34 -34
- botrun_flow_lang/requirements.txt +21 -21
- botrun_flow_lang/services/base/firestore_base.py +30 -30
- botrun_flow_lang/services/hatch/hatch_factory.py +11 -11
- botrun_flow_lang/services/hatch/hatch_fs_store.py +419 -419
- botrun_flow_lang/services/storage/storage_cs_store.py +206 -206
- botrun_flow_lang/services/storage/storage_factory.py +12 -12
- botrun_flow_lang/services/storage/storage_store.py +65 -65
- botrun_flow_lang/services/user_setting/user_setting_factory.py +9 -9
- botrun_flow_lang/services/user_setting/user_setting_fs_store.py +66 -66
- botrun_flow_lang/static/docs/tools/index.html +926 -926
- botrun_flow_lang/tests/api_functional_tests.py +1525 -1525
- botrun_flow_lang/tests/api_stress_test.py +357 -357
- botrun_flow_lang/tests/shared_hatch_tests.py +333 -333
- botrun_flow_lang/tests/test_botrun_app.py +46 -46
- botrun_flow_lang/tests/test_html_util.py +31 -31
- botrun_flow_lang/tests/test_img_analyzer.py +190 -190
- botrun_flow_lang/tests/test_img_util.py +39 -39
- botrun_flow_lang/tests/test_local_files.py +114 -114
- botrun_flow_lang/tests/test_mermaid_util.py +103 -103
- botrun_flow_lang/tests/test_pdf_analyzer.py +104 -104
- botrun_flow_lang/tests/test_plotly_util.py +151 -151
- botrun_flow_lang/tests/test_run_workflow_engine.py +65 -65
- botrun_flow_lang/tools/generate_docs.py +133 -133
- botrun_flow_lang/tools/templates/tools.html +153 -153
- botrun_flow_lang/utils/__init__.py +7 -7
- botrun_flow_lang/utils/botrun_logger.py +344 -344
- botrun_flow_lang/utils/clients/rate_limit_client.py +209 -209
- botrun_flow_lang/utils/clients/token_verify_client.py +153 -153
- botrun_flow_lang/utils/google_drive_utils.py +654 -654
- botrun_flow_lang/utils/langchain_utils.py +324 -324
- botrun_flow_lang/utils/yaml_utils.py +9 -9
- {botrun_flow_lang-5.12.263.dist-info → botrun_flow_lang-6.2.21.dist-info}/METADATA +6 -6
- botrun_flow_lang-6.2.21.dist-info/RECORD +104 -0
- botrun_flow_lang-5.12.263.dist-info/RECORD +0 -102
- {botrun_flow_lang-5.12.263.dist-info → botrun_flow_lang-6.2.21.dist-info}/WHEEL +0 -0
|
@@ -1,486 +1,562 @@
|
|
|
1
|
-
"""
|
|
2
|
-
PDF 分析模組
|
|
3
|
-
|
|
4
|
-
提供 PDF 檔案分析功能,支援:
|
|
5
|
-
- 小檔 (< 5MB):直接多模態問答
|
|
6
|
-
- 大檔 (>= 5MB):壓縮 → 切割 → 平行多模態問答 → LLM 統整結果
|
|
7
|
-
"""
|
|
8
|
-
|
|
9
|
-
import anthropic
|
|
10
|
-
import asyncio
|
|
11
|
-
import base64
|
|
12
|
-
import httpx
|
|
13
|
-
import os
|
|
14
|
-
from typing import List, Dict, Any
|
|
15
|
-
|
|
16
|
-
from dotenv import load_dotenv
|
|
17
|
-
from google.oauth2 import service_account
|
|
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
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
)
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
print(
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
print(f"[analyze_pdf_async]
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
#
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
# 3.
|
|
443
|
-
print("[analyze_pdf_async]
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
import
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
1
|
+
"""
|
|
2
|
+
PDF 分析模組
|
|
3
|
+
|
|
4
|
+
提供 PDF 檔案分析功能,支援:
|
|
5
|
+
- 小檔 (< 5MB):直接多模態問答
|
|
6
|
+
- 大檔 (>= 5MB):壓縮 → 切割 → 平行多模態問答 → LLM 統整結果
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import anthropic
|
|
10
|
+
import asyncio
|
|
11
|
+
import base64
|
|
12
|
+
import httpx
|
|
13
|
+
import os
|
|
14
|
+
from typing import List, Dict, Any, Tuple
|
|
15
|
+
|
|
16
|
+
from dotenv import load_dotenv
|
|
17
|
+
from google.oauth2 import service_account
|
|
18
|
+
|
|
19
|
+
from botrun_flow_lang.langgraph_agents.agents.util.usage_metadata import UsageMetadata
|
|
20
|
+
|
|
21
|
+
load_dotenv()
|
|
22
|
+
|
|
23
|
+
# 檔案大小閾值(MB)
|
|
24
|
+
PDF_SIZE_THRESHOLD_MB = 30.0
|
|
25
|
+
|
|
26
|
+
# 切片目標大小(MB)
|
|
27
|
+
PDF_CHUNK_TARGET_SIZE_MB = 30.0
|
|
28
|
+
|
|
29
|
+
# 最大平行問答數量
|
|
30
|
+
MAX_CONCURRENT_CHUNKS = 5
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def analyze_pdf_with_claude(
|
|
34
|
+
pdf_data: str, user_input: str, model_name: str = "claude-sonnet-4-5-20250929"
|
|
35
|
+
) -> Tuple[str, UsageMetadata]:
|
|
36
|
+
"""
|
|
37
|
+
Analyze a PDF file using Claude API
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
pdf_data: Base64-encoded PDF data
|
|
41
|
+
user_input: User's query about the PDF content
|
|
42
|
+
model_name: Claude model name to use
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
Tuple[str, UsageMetadata]: Claude's analysis and usage metadata
|
|
46
|
+
"""
|
|
47
|
+
# Initialize Anthropic client
|
|
48
|
+
client = anthropic.Anthropic()
|
|
49
|
+
|
|
50
|
+
# Send to Claude
|
|
51
|
+
message = client.messages.create(
|
|
52
|
+
model=model_name,
|
|
53
|
+
max_tokens=4096, # Increased token limit for detailed analysis
|
|
54
|
+
messages=[
|
|
55
|
+
{
|
|
56
|
+
"role": "user",
|
|
57
|
+
"content": [
|
|
58
|
+
{
|
|
59
|
+
"type": "document",
|
|
60
|
+
"source": {
|
|
61
|
+
"type": "base64",
|
|
62
|
+
"media_type": "application/pdf",
|
|
63
|
+
"data": pdf_data,
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
{"type": "text", "text": user_input},
|
|
67
|
+
],
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
# Extract usage metadata
|
|
73
|
+
usage = UsageMetadata(
|
|
74
|
+
prompt_tokens=message.usage.input_tokens,
|
|
75
|
+
completion_tokens=message.usage.output_tokens,
|
|
76
|
+
total_tokens=message.usage.input_tokens + message.usage.output_tokens,
|
|
77
|
+
cache_creation_input_tokens=getattr(message.usage, 'cache_creation_input_tokens', 0) or 0,
|
|
78
|
+
cache_read_input_tokens=getattr(message.usage, 'cache_read_input_tokens', 0) or 0,
|
|
79
|
+
model=model_name,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
print(
|
|
83
|
+
f"analyze_pdf_with_claude============> input_token: {message.usage.input_tokens} output_token: {message.usage.output_tokens}",
|
|
84
|
+
)
|
|
85
|
+
return message.content[0].text, usage
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def analyze_pdf_with_gemini(
|
|
89
|
+
pdf_data: str, user_input: str, model_name: str = "gemini-2.5-flash", pdf_url: str = ""
|
|
90
|
+
) -> Tuple[str, UsageMetadata]:
|
|
91
|
+
"""
|
|
92
|
+
Analyze a PDF file using Gemini API
|
|
93
|
+
|
|
94
|
+
Args:
|
|
95
|
+
pdf_data: Base64-encoded PDF data
|
|
96
|
+
user_input: User's query about the PDF content
|
|
97
|
+
model_name: Gemini model name to use
|
|
98
|
+
pdf_url: Original PDF URL for logging
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
Tuple[str, UsageMetadata]: Gemini's analysis and usage metadata
|
|
102
|
+
"""
|
|
103
|
+
# 放到要用的時候才 import,不然loading 會花時間
|
|
104
|
+
from google import genai
|
|
105
|
+
from google.genai import types
|
|
106
|
+
|
|
107
|
+
credentials = service_account.Credentials.from_service_account_file(
|
|
108
|
+
os.getenv("GOOGLE_APPLICATION_CREDENTIALS_FOR_FASTAPI"),
|
|
109
|
+
scopes=["https://www.googleapis.com/auth/cloud-platform"],
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
client = genai.Client(
|
|
113
|
+
credentials=credentials,
|
|
114
|
+
project="scoop-386004",
|
|
115
|
+
location="us-central1",
|
|
116
|
+
)
|
|
117
|
+
response = client.models.generate_content(
|
|
118
|
+
model=model_name,
|
|
119
|
+
contents=[
|
|
120
|
+
user_input,
|
|
121
|
+
types.Part(
|
|
122
|
+
inline_data={
|
|
123
|
+
"mime_type": "application/pdf",
|
|
124
|
+
"data": pdf_data,
|
|
125
|
+
}
|
|
126
|
+
),
|
|
127
|
+
],
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
# Extract usage metadata
|
|
131
|
+
usage = UsageMetadata(model=model_name)
|
|
132
|
+
if hasattr(response, "usage_metadata"):
|
|
133
|
+
usage_meta = response.usage_metadata
|
|
134
|
+
usage = UsageMetadata(
|
|
135
|
+
prompt_tokens=getattr(usage_meta, 'prompt_token_count', 0) or 0,
|
|
136
|
+
completion_tokens=getattr(usage_meta, 'candidates_token_count', 0) or 0,
|
|
137
|
+
total_tokens=getattr(usage_meta, 'total_token_count', 0) or 0,
|
|
138
|
+
cache_creation_input_tokens=0,
|
|
139
|
+
cache_read_input_tokens=getattr(usage_meta, 'cached_content_token_count', 0) or 0,
|
|
140
|
+
model=model_name,
|
|
141
|
+
)
|
|
142
|
+
print(
|
|
143
|
+
f"analyze_pdf_with_gemini============> input_token: {usage_meta.prompt_token_count} output_token: {usage_meta.candidates_token_count}",
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
print(f"{pdf_url} success")
|
|
147
|
+
return response.text, usage
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _analyze_single_chunk(
|
|
151
|
+
chunk_data: str, page_range: str, user_input: str, model_name: str
|
|
152
|
+
) -> Dict[str, Any]:
|
|
153
|
+
"""
|
|
154
|
+
分析單一 PDF 切片
|
|
155
|
+
|
|
156
|
+
Args:
|
|
157
|
+
chunk_data: Base64-encoded PDF chunk data
|
|
158
|
+
page_range: 頁碼範圍字串 (e.g., "page-001-015")
|
|
159
|
+
user_input: 使用者問題
|
|
160
|
+
model_name: 使用的模型名稱
|
|
161
|
+
|
|
162
|
+
Returns:
|
|
163
|
+
Dict: {"page_range": str, "answer": str, "relevant": bool, "error": str|None, "usage": UsageMetadata}
|
|
164
|
+
"""
|
|
165
|
+
# 構建切片專用的 prompt
|
|
166
|
+
chunk_prompt = f"""你正在閱讀一份大型 PDF 文件的其中一部分({page_range})。
|
|
167
|
+
|
|
168
|
+
使用者問題:{user_input}
|
|
169
|
+
|
|
170
|
+
請根據這個部分的內容回答問題:
|
|
171
|
+
- 如果這個部分包含與問題相關的資訊,請詳細回答
|
|
172
|
+
- 如果這個部分與問題完全無關,請只回答「NOT_RELEVANT」(不要回答其他內容)
|
|
173
|
+
- 回答時請標註資訊來源的頁碼"""
|
|
174
|
+
|
|
175
|
+
try:
|
|
176
|
+
if model_name.startswith("gemini-"):
|
|
177
|
+
answer, usage = analyze_pdf_with_gemini(chunk_data, chunk_prompt, model_name)
|
|
178
|
+
elif model_name.startswith("claude-"):
|
|
179
|
+
answer, usage = analyze_pdf_with_claude(chunk_data, chunk_prompt, model_name)
|
|
180
|
+
else:
|
|
181
|
+
return {
|
|
182
|
+
"page_range": page_range,
|
|
183
|
+
"answer": "",
|
|
184
|
+
"relevant": False,
|
|
185
|
+
"error": f"Unknown model type: {model_name}",
|
|
186
|
+
"usage": UsageMetadata(),
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
# 判斷是否相關
|
|
190
|
+
is_relevant = "NOT_RELEVANT" not in answer.upper()
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
"page_range": page_range,
|
|
194
|
+
"answer": answer if is_relevant else "",
|
|
195
|
+
"relevant": is_relevant,
|
|
196
|
+
"error": None,
|
|
197
|
+
"usage": usage,
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
except Exception as e:
|
|
201
|
+
import traceback
|
|
202
|
+
|
|
203
|
+
traceback.print_exc()
|
|
204
|
+
return {
|
|
205
|
+
"page_range": page_range,
|
|
206
|
+
"answer": "",
|
|
207
|
+
"relevant": False,
|
|
208
|
+
"error": str(e),
|
|
209
|
+
"usage": UsageMetadata(model=model_name),
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
async def analyze_pdf_chunks_parallel(
|
|
214
|
+
chunks: List[tuple], user_input: str, model_name: str, max_concurrent: int = 5
|
|
215
|
+
) -> Tuple[List[Dict[str, Any]], List[UsageMetadata]]:
|
|
216
|
+
"""
|
|
217
|
+
平行問答多個 PDF 切片
|
|
218
|
+
|
|
219
|
+
Args:
|
|
220
|
+
chunks: 切片清單 [(chunk_bytes, page_range), ...]
|
|
221
|
+
user_input: 使用者問題
|
|
222
|
+
model_name: 使用的模型名稱
|
|
223
|
+
max_concurrent: 最大平行數量
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
Tuple[List[Dict], List[UsageMetadata]]: 每個切片的回答結果和每次呼叫的 usage list
|
|
227
|
+
"""
|
|
228
|
+
semaphore = asyncio.Semaphore(max_concurrent)
|
|
229
|
+
|
|
230
|
+
async def analyze_with_semaphore(chunk_bytes: bytes, page_range: str):
|
|
231
|
+
async with semaphore:
|
|
232
|
+
# 將 bytes 轉為 base64
|
|
233
|
+
chunk_data = base64.standard_b64encode(chunk_bytes).decode("utf-8")
|
|
234
|
+
|
|
235
|
+
# 使用 run_in_executor 執行同步函數
|
|
236
|
+
loop = asyncio.get_event_loop()
|
|
237
|
+
return await loop.run_in_executor(
|
|
238
|
+
None,
|
|
239
|
+
_analyze_single_chunk,
|
|
240
|
+
chunk_data,
|
|
241
|
+
page_range,
|
|
242
|
+
user_input,
|
|
243
|
+
model_name,
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
# 建立所有任務
|
|
247
|
+
tasks = [
|
|
248
|
+
analyze_with_semaphore(chunk_bytes, page_range)
|
|
249
|
+
for chunk_bytes, page_range in chunks
|
|
250
|
+
]
|
|
251
|
+
|
|
252
|
+
# 平行執行
|
|
253
|
+
results = await asyncio.gather(*tasks, return_exceptions=True)
|
|
254
|
+
|
|
255
|
+
# 處理例外並收集 usage list
|
|
256
|
+
processed_results = []
|
|
257
|
+
usage_list = []
|
|
258
|
+
for i, result in enumerate(results):
|
|
259
|
+
if isinstance(result, Exception):
|
|
260
|
+
processed_results.append(
|
|
261
|
+
{
|
|
262
|
+
"page_range": chunks[i][1],
|
|
263
|
+
"answer": "",
|
|
264
|
+
"relevant": False,
|
|
265
|
+
"error": str(result),
|
|
266
|
+
"usage": UsageMetadata(model=model_name),
|
|
267
|
+
}
|
|
268
|
+
)
|
|
269
|
+
usage_list.append(UsageMetadata(model=model_name))
|
|
270
|
+
else:
|
|
271
|
+
processed_results.append(result)
|
|
272
|
+
# 收集 usage
|
|
273
|
+
if "usage" in result and isinstance(result["usage"], UsageMetadata):
|
|
274
|
+
usage_list.append(result["usage"])
|
|
275
|
+
|
|
276
|
+
return processed_results, usage_list
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def merge_chunk_results(
|
|
280
|
+
chunk_results: List[Dict[str, Any]],
|
|
281
|
+
user_input: str,
|
|
282
|
+
model_name: str = "gemini-2.5-flash",
|
|
283
|
+
) -> Tuple[str, UsageMetadata]:
|
|
284
|
+
"""
|
|
285
|
+
使用 LLM 統整多個切片的回答
|
|
286
|
+
|
|
287
|
+
Args:
|
|
288
|
+
chunk_results: 切片回答結果清單
|
|
289
|
+
user_input: 原始使用者問題
|
|
290
|
+
model_name: 統整使用的模型名稱
|
|
291
|
+
|
|
292
|
+
Returns:
|
|
293
|
+
Tuple[str, UsageMetadata]: 統整後的回答和 usage metadata
|
|
294
|
+
"""
|
|
295
|
+
# 過濾出相關的回答
|
|
296
|
+
relevant_results = [r for r in chunk_results if r.get("relevant", False)]
|
|
297
|
+
|
|
298
|
+
if not relevant_results:
|
|
299
|
+
# 沒有找到相關內容
|
|
300
|
+
error_results = [r for r in chunk_results if r.get("error")]
|
|
301
|
+
if error_results:
|
|
302
|
+
error_msgs = [f"{r['page_range']}: {r['error']}" for r in error_results]
|
|
303
|
+
return f"分析 PDF 時發生錯誤:\n" + "\n".join(error_msgs), UsageMetadata(model=model_name)
|
|
304
|
+
return "在 PDF 文件中未找到與您問題相關的內容。", UsageMetadata(model=model_name)
|
|
305
|
+
|
|
306
|
+
# 只有一個相關結果,直接回傳(不需要額外的 LLM 呼叫)
|
|
307
|
+
if len(relevant_results) == 1:
|
|
308
|
+
return relevant_results[0]["answer"], UsageMetadata(model=model_name)
|
|
309
|
+
|
|
310
|
+
# 多個相關結果,需要統整
|
|
311
|
+
combined_content = "\n\n".join(
|
|
312
|
+
[
|
|
313
|
+
f"【{r['page_range']}】\n{r['answer']}"
|
|
314
|
+
for r in relevant_results
|
|
315
|
+
]
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
merge_prompt = f"""以下是從一份大型 PDF 文件的不同部分擷取的回答,請統整這些資訊來回答使用者的問題。
|
|
319
|
+
|
|
320
|
+
使用者問題:{user_input}
|
|
321
|
+
|
|
322
|
+
各部分的回答:
|
|
323
|
+
{combined_content}
|
|
324
|
+
|
|
325
|
+
請統整以上資訊,提供一個完整、連貫的回答。如果不同部分有互補的資訊,請整合在一起。請保留頁碼引用。"""
|
|
326
|
+
|
|
327
|
+
try:
|
|
328
|
+
# 使用 LLM 統整(這裡不需要傳 PDF,只是純文字統整)
|
|
329
|
+
from google import genai
|
|
330
|
+
|
|
331
|
+
credentials = service_account.Credentials.from_service_account_file(
|
|
332
|
+
os.getenv("GOOGLE_APPLICATION_CREDENTIALS_FOR_FASTAPI"),
|
|
333
|
+
scopes=["https://www.googleapis.com/auth/cloud-platform"],
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
client = genai.Client(
|
|
337
|
+
credentials=credentials,
|
|
338
|
+
project="scoop-386004",
|
|
339
|
+
location="us-central1",
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
response = client.models.generate_content(
|
|
343
|
+
model=model_name,
|
|
344
|
+
contents=[merge_prompt],
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
# Extract usage metadata
|
|
348
|
+
usage = UsageMetadata(model=model_name)
|
|
349
|
+
if hasattr(response, "usage_metadata"):
|
|
350
|
+
usage_meta = response.usage_metadata
|
|
351
|
+
usage = UsageMetadata(
|
|
352
|
+
prompt_tokens=getattr(usage_meta, 'prompt_token_count', 0) or 0,
|
|
353
|
+
completion_tokens=getattr(usage_meta, 'candidates_token_count', 0) or 0,
|
|
354
|
+
total_tokens=getattr(usage_meta, 'total_token_count', 0) or 0,
|
|
355
|
+
cache_creation_input_tokens=0,
|
|
356
|
+
cache_read_input_tokens=getattr(usage_meta, 'cached_content_token_count', 0) or 0,
|
|
357
|
+
model=model_name,
|
|
358
|
+
)
|
|
359
|
+
print(
|
|
360
|
+
f"merge_chunk_results============> input_token: {usage_meta.prompt_token_count} output_token: {usage_meta.candidates_token_count}",
|
|
361
|
+
)
|
|
362
|
+
|
|
363
|
+
return response.text, usage
|
|
364
|
+
|
|
365
|
+
except Exception as e:
|
|
366
|
+
import traceback
|
|
367
|
+
|
|
368
|
+
traceback.print_exc()
|
|
369
|
+
# 統整失敗,直接回傳合併的內容
|
|
370
|
+
return f"統整時發生錯誤,以下是各部分的回答:\n\n{combined_content}", UsageMetadata(model=model_name)
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
async def analyze_pdf_async(pdf_url: str, user_input: str) -> Dict[str, Any]:
|
|
374
|
+
"""
|
|
375
|
+
非同步分析 PDF 檔案(智慧處理策略)
|
|
376
|
+
|
|
377
|
+
根據檔案大小自動選擇處理策略:
|
|
378
|
+
- < 5MB: 直接多模態問答
|
|
379
|
+
- >= 5MB: 壓縮 → 切割 → 平行多模態問答 → LLM 統整結果
|
|
380
|
+
|
|
381
|
+
Args:
|
|
382
|
+
pdf_url: PDF 檔案的 URL
|
|
383
|
+
user_input: 使用者問題
|
|
384
|
+
|
|
385
|
+
Returns:
|
|
386
|
+
Dict[str, Any]: {
|
|
387
|
+
"result": str, # 分析結果
|
|
388
|
+
"usage_metadata": List[Dict] # 每次 LLM 呼叫的 usage 資訊
|
|
389
|
+
}
|
|
390
|
+
"""
|
|
391
|
+
usage_list: List[UsageMetadata] = []
|
|
392
|
+
|
|
393
|
+
try:
|
|
394
|
+
# 1. 下載 PDF
|
|
395
|
+
print(f"[analyze_pdf_async] 下載 PDF: {pdf_url}")
|
|
396
|
+
pdf_content = httpx.get(pdf_url, timeout=60.0).content
|
|
397
|
+
pdf_size_mb = len(pdf_content) / (1024 * 1024)
|
|
398
|
+
print(f"[analyze_pdf_async] PDF 大小: {pdf_size_mb:.2f} MB")
|
|
399
|
+
|
|
400
|
+
# 取得模型設定
|
|
401
|
+
models_str = os.getenv("PDF_ANALYZER_MODEL", "gemini-2.5-flash")
|
|
402
|
+
print(f"[analyze_pdf_async] 使用模型: {models_str}")
|
|
403
|
+
models = [model.strip() for model in models_str.split(",")]
|
|
404
|
+
primary_model = models[0]
|
|
405
|
+
|
|
406
|
+
# 2. 判斷處理策略
|
|
407
|
+
if pdf_size_mb < PDF_SIZE_THRESHOLD_MB:
|
|
408
|
+
# 小檔:直接多模態問答
|
|
409
|
+
print(f"[analyze_pdf_async] 小檔模式 (< {PDF_SIZE_THRESHOLD_MB}MB)")
|
|
410
|
+
pdf_data = base64.standard_b64encode(pdf_content).decode("utf-8")
|
|
411
|
+
|
|
412
|
+
# 嘗試所有模型
|
|
413
|
+
last_error = None
|
|
414
|
+
for model in models:
|
|
415
|
+
try:
|
|
416
|
+
if model.startswith("gemini-"):
|
|
417
|
+
result, usage = analyze_pdf_with_gemini(pdf_data, user_input, model, pdf_url)
|
|
418
|
+
usage_list.append(usage)
|
|
419
|
+
return {
|
|
420
|
+
"result": result,
|
|
421
|
+
"usage_metadata": [u.to_dict() for u in usage_list],
|
|
422
|
+
}
|
|
423
|
+
elif model.startswith("claude-"):
|
|
424
|
+
result, usage = analyze_pdf_with_claude(pdf_data, user_input, model)
|
|
425
|
+
usage_list.append(usage)
|
|
426
|
+
return {
|
|
427
|
+
"result": result,
|
|
428
|
+
"usage_metadata": [u.to_dict() for u in usage_list],
|
|
429
|
+
}
|
|
430
|
+
except Exception as e:
|
|
431
|
+
import traceback
|
|
432
|
+
|
|
433
|
+
traceback.print_exc()
|
|
434
|
+
last_error = str(e)
|
|
435
|
+
continue
|
|
436
|
+
|
|
437
|
+
return {
|
|
438
|
+
"result": f"分析 PDF 時所有模型都失敗。最後錯誤: {last_error}",
|
|
439
|
+
"usage_metadata": [u.to_dict() for u in usage_list],
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
# 3. 大檔:壓縮 → 切割 → 平行問答 → 統整
|
|
443
|
+
print(f"[analyze_pdf_async] 大檔模式 (>= {PDF_SIZE_THRESHOLD_MB}MB)")
|
|
444
|
+
|
|
445
|
+
# 延遲 import 以加快載入
|
|
446
|
+
from botrun_flow_lang.langgraph_agents.agents.util.pdf_processor import (
|
|
447
|
+
split_pdf_smart,
|
|
448
|
+
get_pdf_page_count,
|
|
449
|
+
)
|
|
450
|
+
from botrun_flow_lang.langgraph_agents.agents.util.pdf_cache import (
|
|
451
|
+
get_cache_key,
|
|
452
|
+
check_cache,
|
|
453
|
+
save_to_cache,
|
|
454
|
+
)
|
|
455
|
+
|
|
456
|
+
# 3.1 檢查快取
|
|
457
|
+
cache_key = get_cache_key(pdf_url)
|
|
458
|
+
print(f"[analyze_pdf_async] 檢查快取: {cache_key}")
|
|
459
|
+
cached_chunks = await check_cache(cache_key)
|
|
460
|
+
|
|
461
|
+
if cached_chunks:
|
|
462
|
+
# 有快取,直接使用
|
|
463
|
+
print(f"[analyze_pdf_async] 使用快取: {len(cached_chunks)} 個切片")
|
|
464
|
+
chunks = cached_chunks
|
|
465
|
+
total_pages = sum(
|
|
466
|
+
int(pr.split("-")[-1]) - int(pr.split("-")[-2]) + 1
|
|
467
|
+
for _, pr in chunks
|
|
468
|
+
if pr.startswith("page-")
|
|
469
|
+
) if chunks else 0
|
|
470
|
+
else:
|
|
471
|
+
# 無快取,切割後存入快取
|
|
472
|
+
|
|
473
|
+
# 3.2 切割
|
|
474
|
+
print("[analyze_pdf_async] 切割 PDF...")
|
|
475
|
+
chunks = split_pdf_smart(pdf_content, target_size_mb=PDF_CHUNK_TARGET_SIZE_MB)
|
|
476
|
+
total_pages = get_pdf_page_count(pdf_content)
|
|
477
|
+
print(
|
|
478
|
+
f"[analyze_pdf_async] 切割完成: {len(chunks)} 個切片, 共 {total_pages} 頁"
|
|
479
|
+
)
|
|
480
|
+
|
|
481
|
+
# 3.3 存入快取
|
|
482
|
+
print("[analyze_pdf_async] 存入快取...")
|
|
483
|
+
await save_to_cache(
|
|
484
|
+
cache_key=cache_key,
|
|
485
|
+
chunks=chunks,
|
|
486
|
+
original_url=pdf_url,
|
|
487
|
+
original_size_mb=pdf_size_mb,
|
|
488
|
+
total_pages=total_pages,
|
|
489
|
+
)
|
|
490
|
+
|
|
491
|
+
# 3.3 平行問答
|
|
492
|
+
print(f"[analyze_pdf_async] 開始平行問答 (最大並行: {MAX_CONCURRENT_CHUNKS})...")
|
|
493
|
+
chunk_results, chunk_usage_list = await analyze_pdf_chunks_parallel(
|
|
494
|
+
chunks, user_input, primary_model, max_concurrent=MAX_CONCURRENT_CHUNKS
|
|
495
|
+
)
|
|
496
|
+
usage_list.extend(chunk_usage_list)
|
|
497
|
+
|
|
498
|
+
# 統計結果
|
|
499
|
+
relevant_count = sum(1 for r in chunk_results if r.get("relevant", False))
|
|
500
|
+
error_count = sum(1 for r in chunk_results if r.get("error"))
|
|
501
|
+
print(
|
|
502
|
+
f"[analyze_pdf_async] 問答完成: {relevant_count}/{len(chunks)} 個切片有相關內容, "
|
|
503
|
+
f"{error_count} 個錯誤"
|
|
504
|
+
)
|
|
505
|
+
|
|
506
|
+
# 3.4 統整結果
|
|
507
|
+
print("[analyze_pdf_async] 統整結果...")
|
|
508
|
+
result, merge_usage = merge_chunk_results(chunk_results, user_input, primary_model)
|
|
509
|
+
# 只有當 merge_usage 有實際 token 使用時才加入(避免加入空的 usage)
|
|
510
|
+
if merge_usage.prompt_tokens > 0 or merge_usage.completion_tokens > 0:
|
|
511
|
+
usage_list.append(merge_usage)
|
|
512
|
+
print("[analyze_pdf_async] 完成")
|
|
513
|
+
|
|
514
|
+
return {
|
|
515
|
+
"result": result,
|
|
516
|
+
"usage_metadata": [u.to_dict() for u in usage_list],
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
except Exception as e:
|
|
520
|
+
import traceback
|
|
521
|
+
|
|
522
|
+
traceback.print_exc()
|
|
523
|
+
return {
|
|
524
|
+
"result": f"分析 PDF {pdf_url} 時發生錯誤: {str(e)}",
|
|
525
|
+
"usage_metadata": [u.to_dict() for u in usage_list],
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
def analyze_pdf(pdf_url: str, user_input: str) -> Dict[str, Any]:
|
|
530
|
+
"""
|
|
531
|
+
分析 PDF 檔案(同步包裝函數)
|
|
532
|
+
|
|
533
|
+
這是一個同步函數,內部會建立事件迴圈來執行非同步的 analyze_pdf_async。
|
|
534
|
+
為了向後相容,保留這個同步介面。
|
|
535
|
+
|
|
536
|
+
Args:
|
|
537
|
+
pdf_url: PDF 檔案的 URL
|
|
538
|
+
user_input: 使用者問題
|
|
539
|
+
|
|
540
|
+
Returns:
|
|
541
|
+
Dict[str, Any]: {
|
|
542
|
+
"result": str, # 分析結果
|
|
543
|
+
"usage_metadata": List[Dict] # 每次 LLM 呼叫的 usage 資訊
|
|
544
|
+
}
|
|
545
|
+
"""
|
|
546
|
+
try:
|
|
547
|
+
# 嘗試取得現有的事件迴圈
|
|
548
|
+
loop = asyncio.get_event_loop()
|
|
549
|
+
if loop.is_running():
|
|
550
|
+
# 如果已經在事件迴圈中,建立新的任務
|
|
551
|
+
import concurrent.futures
|
|
552
|
+
|
|
553
|
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
554
|
+
future = executor.submit(
|
|
555
|
+
asyncio.run, analyze_pdf_async(pdf_url, user_input)
|
|
556
|
+
)
|
|
557
|
+
return future.result()
|
|
558
|
+
else:
|
|
559
|
+
return loop.run_until_complete(analyze_pdf_async(pdf_url, user_input))
|
|
560
|
+
except RuntimeError:
|
|
561
|
+
# 沒有事件迴圈,建立新的
|
|
562
|
+
return asyncio.run(analyze_pdf_async(pdf_url, user_input))
|