hamtaa-texttools 1.1.1__py3-none-any.whl → 1.2.0__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.
Files changed (47) hide show
  1. hamtaa_texttools-1.2.0.dist-info/METADATA +212 -0
  2. hamtaa_texttools-1.2.0.dist-info/RECORD +34 -0
  3. texttools/__init__.py +6 -8
  4. texttools/batch/__init__.py +0 -4
  5. texttools/batch/config.py +40 -0
  6. texttools/batch/{batch_manager.py → manager.py} +41 -42
  7. texttools/batch/runner.py +228 -0
  8. texttools/core/__init__.py +0 -0
  9. texttools/core/engine.py +254 -0
  10. texttools/core/exceptions.py +22 -0
  11. texttools/core/internal_models.py +58 -0
  12. texttools/core/operators/async_operator.py +194 -0
  13. texttools/core/operators/sync_operator.py +192 -0
  14. texttools/models.py +88 -0
  15. texttools/prompts/categorize.yaml +36 -0
  16. texttools/prompts/check_fact.yaml +24 -0
  17. texttools/prompts/extract_entities.yaml +7 -3
  18. texttools/prompts/extract_keywords.yaml +80 -18
  19. texttools/prompts/is_question.yaml +6 -2
  20. texttools/prompts/merge_questions.yaml +12 -5
  21. texttools/prompts/propositionize.yaml +24 -0
  22. texttools/prompts/rewrite.yaml +9 -10
  23. texttools/prompts/run_custom.yaml +2 -2
  24. texttools/prompts/subject_to_question.yaml +7 -3
  25. texttools/prompts/summarize.yaml +6 -2
  26. texttools/prompts/text_to_question.yaml +12 -6
  27. texttools/prompts/translate.yaml +7 -2
  28. texttools/py.typed +0 -0
  29. texttools/tools/__init__.py +0 -4
  30. texttools/tools/async_tools.py +1093 -0
  31. texttools/tools/sync_tools.py +1092 -0
  32. hamtaa_texttools-1.1.1.dist-info/METADATA +0 -183
  33. hamtaa_texttools-1.1.1.dist-info/RECORD +0 -30
  34. texttools/batch/batch_runner.py +0 -263
  35. texttools/prompts/README.md +0 -35
  36. texttools/prompts/categorizer.yaml +0 -28
  37. texttools/tools/async_the_tool.py +0 -414
  38. texttools/tools/internals/async_operator.py +0 -179
  39. texttools/tools/internals/base_operator.py +0 -91
  40. texttools/tools/internals/formatters.py +0 -24
  41. texttools/tools/internals/operator.py +0 -179
  42. texttools/tools/internals/output_models.py +0 -59
  43. texttools/tools/internals/prompt_loader.py +0 -57
  44. texttools/tools/the_tool.py +0 -412
  45. {hamtaa_texttools-1.1.1.dist-info → hamtaa_texttools-1.2.0.dist-info}/WHEEL +0 -0
  46. {hamtaa_texttools-1.1.1.dist-info → hamtaa_texttools-1.2.0.dist-info}/licenses/LICENSE +0 -0
  47. {hamtaa_texttools-1.1.1.dist-info → hamtaa_texttools-1.2.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,1092 @@
1
+ import sys
2
+ from collections.abc import Callable
3
+ from time import perf_counter
4
+ from typing import Any, Literal
5
+
6
+ from openai import OpenAI
7
+
8
+ from ..core.engine import text_to_chunks
9
+ from ..core.exceptions import LLMError, PromptError, TextToolsError, ValidationError
10
+ from ..core.internal_models import (
11
+ Bool,
12
+ ListDictStrStr,
13
+ ListStr,
14
+ ReasonListStr,
15
+ Str,
16
+ create_dynamic_model,
17
+ )
18
+ from ..core.operators.sync_operator import Operator
19
+ from ..models import CategoryTree, ToolOutput, ToolOutputMetadata
20
+
21
+
22
+ class TheTool:
23
+ """
24
+ Each method configures the operator with a specific YAML prompt,
25
+ output schema, and flags, then delegates execution to `operator.run()`.
26
+ """
27
+
28
+ def __init__(
29
+ self,
30
+ client: OpenAI,
31
+ model: str,
32
+ ):
33
+ self._operator = Operator(client=client, model=model)
34
+
35
+ def categorize(
36
+ self,
37
+ text: str,
38
+ categories: list[str] | CategoryTree,
39
+ with_analysis: bool = False,
40
+ user_prompt: str | None = None,
41
+ temperature: float | None = 0.0,
42
+ logprobs: bool = False,
43
+ top_logprobs: int = 3,
44
+ validator: Callable[[Any], bool] | None = None,
45
+ max_validation_retries: int | None = None,
46
+ priority: int | None = None,
47
+ ) -> ToolOutput:
48
+ """
49
+ Categorize a text into a category / category tree.
50
+
51
+ Important Note: category_tree mode is EXPERIMENTAL, you can use it but it isn't reliable.
52
+
53
+ Arguments:
54
+ text: The input text
55
+ categories: The category list / category tree
56
+ with_analysis: Whether to include detailed reasoning analysis
57
+ user_prompt: Additional instructions
58
+ temperature: Controls randomness (0.0 - 2.0)
59
+ logprobs: Whether to return token probability information
60
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
61
+ validator: Custom validation function to validate the output
62
+ max_validation_retries: Maximum number of retry attempts if validation fails
63
+ priority: Task execution priority (if enabled by vLLM and the model)
64
+
65
+ Returns:
66
+ ToolOutput
67
+
68
+ """
69
+ tool_name = sys._getframe().f_code.co_name
70
+ start = perf_counter()
71
+
72
+ try:
73
+ if isinstance(categories, list):
74
+ operator_output = self._operator.run(
75
+ # User parameters
76
+ text=text,
77
+ category_list=categories,
78
+ with_analysis=with_analysis,
79
+ user_prompt=user_prompt,
80
+ temperature=temperature,
81
+ logprobs=logprobs,
82
+ top_logprobs=top_logprobs,
83
+ validator=validator,
84
+ max_validation_retries=max_validation_retries,
85
+ priority=priority,
86
+ # Internal parameters
87
+ tool_name=tool_name,
88
+ output_model=create_dynamic_model(categories),
89
+ mode=None,
90
+ output_lang=None,
91
+ )
92
+
93
+ metadata = ToolOutputMetadata(
94
+ tool_name=tool_name, execution_time=perf_counter() - start
95
+ )
96
+ tool_output = ToolOutput(
97
+ result=operator_output.result,
98
+ analysis=operator_output.analysis,
99
+ logprobs=operator_output.logprobs,
100
+ metadata=metadata,
101
+ )
102
+
103
+ else:
104
+ levels = categories.get_level_count()
105
+ parent_node = categories.get_node("root")
106
+ final_categories = []
107
+ analysis = ""
108
+ logprobs_list = []
109
+
110
+ for _ in range(levels):
111
+ if not parent_node.children:
112
+ break
113
+
114
+ category_list = [
115
+ f"Category Name: {name}, Description: {node.description}"
116
+ for name, node in parent_node.children.items()
117
+ ]
118
+ category_names = list(parent_node.children.keys())
119
+
120
+ level_operator_output = self._operator.run(
121
+ # User parameters
122
+ text=text,
123
+ category_list=category_list,
124
+ with_analysis=with_analysis,
125
+ user_prompt=user_prompt,
126
+ temperature=temperature,
127
+ logprobs=logprobs,
128
+ top_logprobs=top_logprobs,
129
+ validator=validator,
130
+ max_validation_retries=max_validation_retries,
131
+ priority=priority,
132
+ # Internal parameters
133
+ tool_name=tool_name,
134
+ output_model=create_dynamic_model(category_names),
135
+ mode=None,
136
+ output_lang=None,
137
+ )
138
+
139
+ chosen_category = level_operator_output.result
140
+ parent_node = categories.get_node(chosen_category)
141
+ if not parent_node:
142
+ break
143
+ final_categories.append(chosen_category)
144
+
145
+ if with_analysis:
146
+ analysis += level_operator_output.analysis
147
+ if logprobs:
148
+ logprobs_list.extend(level_operator_output.logprobs)
149
+
150
+ metadata = ToolOutputMetadata(
151
+ tool_name=tool_name, execution_time=(perf_counter() - start)
152
+ )
153
+ tool_output = ToolOutput(
154
+ result=final_categories,
155
+ analysis=analysis,
156
+ logprobs=logprobs_list,
157
+ metadata=metadata,
158
+ )
159
+
160
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
161
+ metadata = ToolOutputMetadata(tool_name=tool_name)
162
+ tool_output = ToolOutput(
163
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
164
+ )
165
+
166
+ return tool_output
167
+
168
+ def extract_keywords(
169
+ self,
170
+ text: str,
171
+ with_analysis: bool = False,
172
+ output_lang: str | None = None,
173
+ user_prompt: str | None = None,
174
+ temperature: float | None = 0.0,
175
+ logprobs: bool = False,
176
+ top_logprobs: int = 3,
177
+ mode: Literal["auto", "threshold", "count"] = "auto",
178
+ number_of_keywords: int | None = None,
179
+ validator: Callable[[Any], bool] | None = None,
180
+ max_validation_retries: int | None = None,
181
+ priority: int | None = None,
182
+ ) -> ToolOutput:
183
+ """
184
+ Extract salient keywords from text.
185
+
186
+ Arguments:
187
+ text: The input text
188
+ with_analysis: Whether to include detailed reasoning analysis
189
+ output_lang: Language for the output
190
+ user_prompt: Additional instructions
191
+ temperature: Controls randomness (0.0 - 2.0)
192
+ logprobs: Whether to return token probability information
193
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
194
+ validator: Custom validation function to validate the output
195
+ max_validation_retries: Maximum number of retry attempts if validation fails
196
+ priority: Task execution priority (if enabled by vLLM and the model)
197
+
198
+ Returns:
199
+ ToolOutput
200
+ """
201
+ tool_name = sys._getframe().f_code.co_name
202
+ start = perf_counter()
203
+
204
+ try:
205
+ operator_output = self._operator.run(
206
+ # User parameters
207
+ text=text,
208
+ with_analysis=with_analysis,
209
+ output_lang=output_lang,
210
+ user_prompt=user_prompt,
211
+ temperature=temperature,
212
+ logprobs=logprobs,
213
+ top_logprobs=top_logprobs,
214
+ mode=mode,
215
+ number_of_keywords=number_of_keywords,
216
+ validator=validator,
217
+ max_validation_retries=max_validation_retries,
218
+ priority=priority,
219
+ # Internal parameters
220
+ tool_name=tool_name,
221
+ output_model=ListStr,
222
+ )
223
+
224
+ metadata = ToolOutputMetadata(
225
+ tool_name=tool_name, execution_time=perf_counter() - start
226
+ )
227
+ tool_output = ToolOutput(
228
+ result=operator_output.result,
229
+ logprobs=operator_output.logprobs,
230
+ analysis=operator_output.analysis,
231
+ metadata=metadata,
232
+ )
233
+
234
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
235
+ metadata = ToolOutputMetadata(tool_name=tool_name)
236
+ tool_output = ToolOutput(
237
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
238
+ )
239
+
240
+ return tool_output
241
+
242
+ def extract_entities(
243
+ self,
244
+ text: str,
245
+ entities: list[str] | None = None,
246
+ with_analysis: bool = False,
247
+ output_lang: str | None = None,
248
+ user_prompt: str | None = None,
249
+ temperature: float | None = 0.0,
250
+ logprobs: bool = False,
251
+ top_logprobs: int = 3,
252
+ validator: Callable[[Any], bool] | None = None,
253
+ max_validation_retries: int | None = None,
254
+ priority: int | None = None,
255
+ ) -> ToolOutput:
256
+ """
257
+ Perform Named Entity Recognition (NER) over the input text.
258
+
259
+ Arguments:
260
+ text: The input text
261
+ entities: List of entities provided by user
262
+ with_analysis: Whether to include detailed reasoning analysis
263
+ output_lang: Language for the output
264
+ user_prompt: Additional instructions
265
+ temperature: Controls randomness (0.0 - 2.0)
266
+ logprobs: Whether to return token probability information
267
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
268
+ validator: Custom validation function to validate the output
269
+ max_validation_retries: Maximum number of retry attempts if validation fails
270
+ priority: Task execution priority (if enabled by vLLM and the model)
271
+
272
+ Returns:
273
+ ToolOutput
274
+ """
275
+ tool_name = sys._getframe().f_code.co_name
276
+ start = perf_counter()
277
+
278
+ try:
279
+ operator_output = self._operator.run(
280
+ # User parameters
281
+ text=text,
282
+ entities=entities
283
+ or "all named entities (e.g., PER, ORG, LOC, DAT, etc.)",
284
+ with_analysis=with_analysis,
285
+ output_lang=output_lang,
286
+ user_prompt=user_prompt,
287
+ temperature=temperature,
288
+ logprobs=logprobs,
289
+ top_logprobs=top_logprobs,
290
+ validator=validator,
291
+ max_validation_retries=max_validation_retries,
292
+ priority=priority,
293
+ # Internal parameters
294
+ tool_name=tool_name,
295
+ output_model=ListDictStrStr,
296
+ mode=None,
297
+ )
298
+
299
+ metadata = ToolOutputMetadata(
300
+ tool_name=tool_name, execution_time=perf_counter() - start
301
+ )
302
+ tool_output = ToolOutput(
303
+ result=operator_output.result,
304
+ logprobs=operator_output.logprobs,
305
+ analysis=operator_output.analysis,
306
+ metadata=metadata,
307
+ )
308
+
309
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
310
+ metadata = ToolOutputMetadata(tool_name=tool_name)
311
+ tool_output = ToolOutput(
312
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
313
+ )
314
+
315
+ return tool_output
316
+
317
+ def is_question(
318
+ self,
319
+ text: str,
320
+ with_analysis: bool = False,
321
+ user_prompt: str | None = None,
322
+ temperature: float | None = 0.0,
323
+ logprobs: bool = False,
324
+ top_logprobs: int = 3,
325
+ validator: Callable[[Any], bool] | None = None,
326
+ max_validation_retries: int | None = None,
327
+ priority: int | None = None,
328
+ ) -> ToolOutput:
329
+ """
330
+ Detect if the input is phrased as a question.
331
+
332
+ Arguments:
333
+ text: The input text
334
+ with_analysis: Whether to include detailed reasoning analysis
335
+ user_prompt: Additional instructions
336
+ temperature: Controls randomness (0.0 - 2.0)
337
+ logprobs: Whether to return token probability information
338
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
339
+ validator: Custom validation function to validate the output
340
+ max_validation_retries: Maximum number of retry attempts if validation fails
341
+ priority: Task execution priority (if enabled by vLLM and the model)
342
+
343
+ Returns:
344
+ ToolOutput
345
+ """
346
+ tool_name = sys._getframe().f_code.co_name
347
+ start = perf_counter()
348
+
349
+ try:
350
+ operator_output = self._operator.run(
351
+ # User parameters
352
+ text=text,
353
+ with_analysis=with_analysis,
354
+ user_prompt=user_prompt,
355
+ temperature=temperature,
356
+ logprobs=logprobs,
357
+ top_logprobs=top_logprobs,
358
+ validator=validator,
359
+ max_validation_retries=max_validation_retries,
360
+ priority=priority,
361
+ # Internal parameters
362
+ tool_name=tool_name,
363
+ output_model=Bool,
364
+ mode=None,
365
+ output_lang=None,
366
+ )
367
+
368
+ metadata = ToolOutputMetadata(
369
+ tool_name=tool_name, execution_time=perf_counter() - start
370
+ )
371
+ tool_output = ToolOutput(
372
+ result=operator_output.result,
373
+ logprobs=operator_output.logprobs,
374
+ analysis=operator_output.analysis,
375
+ metadata=metadata,
376
+ )
377
+
378
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
379
+ metadata = ToolOutputMetadata(tool_name=tool_name)
380
+ tool_output = ToolOutput(
381
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
382
+ )
383
+
384
+ return tool_output
385
+
386
+ def text_to_question(
387
+ self,
388
+ text: str,
389
+ number_of_questions: int,
390
+ with_analysis: bool = False,
391
+ output_lang: str | None = None,
392
+ user_prompt: str | None = None,
393
+ temperature: float | None = 0.0,
394
+ logprobs: bool = False,
395
+ top_logprobs: int = 3,
396
+ validator: Callable[[Any], bool] | None = None,
397
+ max_validation_retries: int | None = None,
398
+ priority: int | None = None,
399
+ ) -> ToolOutput:
400
+ """
401
+ Generate a single question from the given text.
402
+
403
+ Arguments:
404
+ text: The input text
405
+ number_of_questions: Number of questions to generate
406
+ with_analysis: Whether to include detailed reasoning analysis
407
+ output_lang: Language for the output
408
+ user_prompt: Additional instructions
409
+ temperature: Controls randomness (0.0 - 2.0)
410
+ logprobs: Whether to return token probability information
411
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
412
+ validator: Custom validation function to validate the output
413
+ max_validation_retries: Maximum number of retry attempts if validation fails
414
+ priority: Task execution priority (if enabled by vLLM and the model)
415
+
416
+ Returns:
417
+ ToolOutput
418
+ """
419
+ tool_name = sys._getframe().f_code.co_name
420
+ start = perf_counter()
421
+
422
+ try:
423
+ operator_output = self._operator.run(
424
+ # User parameters
425
+ text=text,
426
+ number_of_questions=number_of_questions,
427
+ with_analysis=with_analysis,
428
+ output_lang=output_lang,
429
+ user_prompt=user_prompt,
430
+ temperature=temperature,
431
+ logprobs=logprobs,
432
+ top_logprobs=top_logprobs,
433
+ validator=validator,
434
+ max_validation_retries=max_validation_retries,
435
+ priority=priority,
436
+ # Internal parameters
437
+ tool_name=tool_name,
438
+ output_model=ReasonListStr,
439
+ mode=None,
440
+ )
441
+
442
+ metadata = ToolOutputMetadata(
443
+ tool_name=tool_name, execution_time=perf_counter() - start
444
+ )
445
+ tool_output = ToolOutput(
446
+ result=operator_output.result,
447
+ logprobs=operator_output.logprobs,
448
+ analysis=operator_output.analysis,
449
+ metadata=metadata,
450
+ )
451
+
452
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
453
+ metadata = ToolOutputMetadata(tool_name=tool_name)
454
+ tool_output = ToolOutput(
455
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
456
+ )
457
+
458
+ return tool_output
459
+
460
+ def merge_questions(
461
+ self,
462
+ text: list[str],
463
+ with_analysis: bool = False,
464
+ output_lang: str | None = None,
465
+ user_prompt: str | None = None,
466
+ temperature: float | None = 0.0,
467
+ logprobs: bool = False,
468
+ top_logprobs: int = 3,
469
+ mode: Literal["default", "reason"] = "default",
470
+ validator: Callable[[Any], bool] | None = None,
471
+ max_validation_retries: int | None = None,
472
+ priority: int | None = None,
473
+ ) -> ToolOutput:
474
+ """
475
+ Merge multiple questions into a single unified question.
476
+
477
+ Arguments:
478
+ text: List of questions to merge
479
+ with_analysis: Whether to include detailed reasoning analysis
480
+ output_lang: Language for the output
481
+ user_prompt: Additional instructions
482
+ temperature: Controls randomness (0.0 - 2.0)
483
+ logprobs: Whether to return token probability information
484
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
485
+ validator: Custom validation function to validate the output
486
+ max_validation_retries: Maximum number of retry attempts if validation fails
487
+ priority: Task execution priority (if enabled by vLLM and the model)
488
+
489
+ Returns:
490
+ ToolOutput
491
+ """
492
+ tool_name = sys._getframe().f_code.co_name
493
+ start = perf_counter()
494
+
495
+ try:
496
+ text = ", ".join(text)
497
+ operator_output = self._operator.run(
498
+ # User parameters
499
+ text=text,
500
+ with_analysis=with_analysis,
501
+ output_lang=output_lang,
502
+ user_prompt=user_prompt,
503
+ temperature=temperature,
504
+ logprobs=logprobs,
505
+ top_logprobs=top_logprobs,
506
+ validator=validator,
507
+ max_validation_retries=max_validation_retries,
508
+ priority=priority,
509
+ # Internal parameters
510
+ tool_name=tool_name,
511
+ output_model=Str,
512
+ mode=mode,
513
+ )
514
+
515
+ metadata = ToolOutputMetadata(
516
+ tool_name=tool_name, execution_time=perf_counter() - start
517
+ )
518
+ tool_output = ToolOutput(
519
+ result=operator_output.result,
520
+ logprobs=operator_output.logprobs,
521
+ analysis=operator_output.analysis,
522
+ metadata=metadata,
523
+ )
524
+
525
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
526
+ metadata = ToolOutputMetadata(tool_name=tool_name)
527
+ tool_output = ToolOutput(
528
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
529
+ )
530
+
531
+ return tool_output
532
+
533
+ def rewrite(
534
+ self,
535
+ text: str,
536
+ with_analysis: bool = False,
537
+ output_lang: str | None = None,
538
+ user_prompt: str | None = None,
539
+ temperature: float | None = 0.0,
540
+ logprobs: bool = False,
541
+ top_logprobs: int = 3,
542
+ mode: Literal["positive", "negative", "hard_negative"] = "positive",
543
+ validator: Callable[[Any], bool] | None = None,
544
+ max_validation_retries: int | None = None,
545
+ priority: int | None = None,
546
+ ) -> ToolOutput:
547
+ """
548
+ Rewrite a text with different modes.
549
+
550
+ Arguments:
551
+ text: The input text
552
+ with_analysis: Whether to include detailed reasoning analysis
553
+ output_lang: Language for the output
554
+ user_prompt: Additional instructions
555
+ temperature: Controls randomness (0.0 - 2.0)
556
+ logprobs: Whether to return token probability information
557
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
558
+ validator: Custom validation function to validate the output
559
+ max_validation_retries: Maximum number of retry attempts if validation fails
560
+ priority: Task execution priority (if enabled by vLLM and the model)
561
+
562
+ Returns:
563
+ ToolOutput
564
+ """
565
+ tool_name = sys._getframe().f_code.co_name
566
+ start = perf_counter()
567
+
568
+ try:
569
+ operator_output = self._operator.run(
570
+ # User parameters
571
+ text=text,
572
+ with_analysis=with_analysis,
573
+ output_lang=output_lang,
574
+ user_prompt=user_prompt,
575
+ temperature=temperature,
576
+ logprobs=logprobs,
577
+ top_logprobs=top_logprobs,
578
+ validator=validator,
579
+ max_validation_retries=max_validation_retries,
580
+ priority=priority,
581
+ # Internal parameters
582
+ tool_name=tool_name,
583
+ output_model=Str,
584
+ mode=mode,
585
+ )
586
+
587
+ metadata = ToolOutputMetadata(
588
+ tool_name=tool_name, execution_time=perf_counter() - start
589
+ )
590
+ tool_output = ToolOutput(
591
+ result=operator_output.result,
592
+ logprobs=operator_output.logprobs,
593
+ analysis=operator_output.analysis,
594
+ metadata=metadata,
595
+ )
596
+
597
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
598
+ metadata = ToolOutputMetadata(tool_name=tool_name)
599
+ tool_output = ToolOutput(
600
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
601
+ )
602
+
603
+ return tool_output
604
+
605
+ def subject_to_question(
606
+ self,
607
+ text: str,
608
+ number_of_questions: int,
609
+ with_analysis: bool = False,
610
+ output_lang: str | None = None,
611
+ user_prompt: str | None = None,
612
+ temperature: float | None = 0.0,
613
+ logprobs: bool = False,
614
+ top_logprobs: int = 3,
615
+ validator: Callable[[Any], bool] | None = None,
616
+ max_validation_retries: int | None = None,
617
+ priority: int | None = None,
618
+ ) -> ToolOutput:
619
+ """
620
+ Generate a list of questions about a subject.
621
+
622
+ Arguments:
623
+ text: The subject text to generate questions about
624
+ number_of_questions: Number of questions to generate
625
+ with_analysis: Whether to include detailed reasoning analysis
626
+ output_lang: Language for the output
627
+ user_prompt: Additional instructions
628
+ temperature: Controls randomness (0.0 - 2.0)
629
+ logprobs: Whether to return token probability information
630
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
631
+ validator: Custom validation function to validate the output
632
+ max_validation_retries: Maximum number of retry attempts if validation fails
633
+ priority: Task execution priority (if enabled by vLLM and the model)
634
+
635
+ Returns:
636
+ ToolOutput
637
+ """
638
+ tool_name = sys._getframe().f_code.co_name
639
+ start = perf_counter()
640
+
641
+ try:
642
+ operator_output = self._operator.run(
643
+ # User parameters
644
+ text=text,
645
+ number_of_questions=number_of_questions,
646
+ with_analysis=with_analysis,
647
+ output_lang=output_lang,
648
+ user_prompt=user_prompt,
649
+ temperature=temperature,
650
+ logprobs=logprobs,
651
+ top_logprobs=top_logprobs,
652
+ validator=validator,
653
+ max_validation_retries=max_validation_retries,
654
+ priority=priority,
655
+ # Internal parameters
656
+ tool_name=tool_name,
657
+ output_model=ReasonListStr,
658
+ mode=None,
659
+ )
660
+
661
+ metadata = ToolOutputMetadata(
662
+ tool_name=tool_name, execution_time=perf_counter() - start
663
+ )
664
+ tool_output = ToolOutput(
665
+ result=operator_output.result,
666
+ logprobs=operator_output.logprobs,
667
+ analysis=operator_output.analysis,
668
+ metadata=metadata,
669
+ )
670
+
671
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
672
+ metadata = ToolOutputMetadata(tool_name=tool_name)
673
+ tool_output = ToolOutput(
674
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
675
+ )
676
+
677
+ return tool_output
678
+
679
+ def summarize(
680
+ self,
681
+ text: str,
682
+ with_analysis: bool = False,
683
+ output_lang: str | None = None,
684
+ user_prompt: str | None = None,
685
+ temperature: float | None = 0.0,
686
+ logprobs: bool = False,
687
+ top_logprobs: int = 3,
688
+ validator: Callable[[Any], bool] | None = None,
689
+ max_validation_retries: int | None = None,
690
+ priority: int | None = None,
691
+ ) -> ToolOutput:
692
+ """
693
+ Summarize the given subject text.
694
+
695
+ Arguments:
696
+ text: The input text
697
+ with_analysis: Whether to include detailed reasoning analysis
698
+ output_lang: Language for the output
699
+ user_prompt: Additional instructions
700
+ temperature: Controls randomness (0.0 - 2.0)
701
+ logprobs: Whether to return token probability information
702
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
703
+ validator: Custom validation function to validate the output
704
+ max_validation_retries: Maximum number of retry attempts if validation fails
705
+ priority: Task execution priority (if enabled by vLLM and the model)
706
+
707
+ Returns:
708
+ ToolOutput
709
+ """
710
+ tool_name = sys._getframe().f_code.co_name
711
+ start = perf_counter()
712
+
713
+ try:
714
+ operator_output = self._operator.run(
715
+ # User parameters
716
+ text=text,
717
+ with_analysis=with_analysis,
718
+ output_lang=output_lang,
719
+ user_prompt=user_prompt,
720
+ temperature=temperature,
721
+ logprobs=logprobs,
722
+ top_logprobs=top_logprobs,
723
+ validator=validator,
724
+ max_validation_retries=max_validation_retries,
725
+ priority=priority,
726
+ # Internal parameters
727
+ tool_name=tool_name,
728
+ output_model=Str,
729
+ mode=None,
730
+ )
731
+
732
+ metadata = ToolOutputMetadata(
733
+ tool_name=tool_name, execution_time=perf_counter() - start
734
+ )
735
+ tool_output = ToolOutput(
736
+ result=operator_output.result,
737
+ logprobs=operator_output.logprobs,
738
+ analysis=operator_output.analysis,
739
+ metadata=metadata,
740
+ )
741
+
742
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
743
+ metadata = ToolOutputMetadata(tool_name=tool_name)
744
+ tool_output = ToolOutput(
745
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
746
+ )
747
+
748
+ return tool_output
749
+
750
+ def translate(
751
+ self,
752
+ text: str,
753
+ target_language: str,
754
+ use_chunker: bool = True,
755
+ with_analysis: bool = False,
756
+ user_prompt: str | None = None,
757
+ temperature: float | None = 0.0,
758
+ logprobs: bool = False,
759
+ top_logprobs: int = 3,
760
+ validator: Callable[[Any], bool] | None = None,
761
+ max_validation_retries: int | None = None,
762
+ priority: int | None = None,
763
+ ) -> ToolOutput:
764
+ """
765
+ Translate text between languages.
766
+
767
+ Important Note: This tool is EXPERIMENTAL, you can use it but it isn't reliable.
768
+
769
+ Arguments:
770
+ text: The input text
771
+ target_language: The target language for translation
772
+ use_chunker: Whether to use text chunker for text length bigger than 1500
773
+ with_analysis: Whether to include detailed reasoning analysis
774
+ user_prompt: Additional instructions
775
+ temperature: Controls randomness (0.0 - 2.0)
776
+ logprobs: Whether to return token probability information
777
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
778
+ validator: Custom validation function to validate the output
779
+ max_validation_retries: Maximum number of retry attempts if validation fails
780
+ priority: Task execution priority (if enabled by vLLM and the model)
781
+
782
+ Returns:
783
+ ToolOutput
784
+ """
785
+ tool_name = sys._getframe().f_code.co_name
786
+ start = perf_counter()
787
+
788
+ try:
789
+ if len(text.split(" ")) > 1500 and use_chunker:
790
+ chunks = text_to_chunks(text, 1200, 0)
791
+ translation = ""
792
+ analysis = ""
793
+ logprobs_list = []
794
+
795
+ for chunk in chunks:
796
+ chunk_operator_output = self._operator.run(
797
+ # User parameters
798
+ text=chunk,
799
+ target_language=target_language,
800
+ with_analysis=with_analysis,
801
+ user_prompt=user_prompt,
802
+ temperature=temperature,
803
+ logprobs=logprobs,
804
+ top_logprobs=top_logprobs,
805
+ validator=validator,
806
+ max_validation_retries=max_validation_retries,
807
+ priority=priority,
808
+ # Internal parameters
809
+ tool_name=tool_name,
810
+ output_model=Str,
811
+ mode=None,
812
+ output_lang=None,
813
+ )
814
+
815
+ translation += chunk_operator_output.result + "\n"
816
+
817
+ if with_analysis:
818
+ analysis += chunk_operator_output.analysis
819
+ if logprobs:
820
+ logprobs_list.extend(chunk_operator_output.logprobs)
821
+
822
+ metadata = ToolOutputMetadata(
823
+ tool_name=tool_name, execution_time=perf_counter() - start
824
+ )
825
+ tool_output = ToolOutput(
826
+ result=translation,
827
+ logprobs=logprobs_list,
828
+ analysis=analysis,
829
+ metadata=metadata,
830
+ )
831
+
832
+ else:
833
+ operator_output = self._operator.run(
834
+ # User parameters
835
+ text=text,
836
+ target_language=target_language,
837
+ with_analysis=with_analysis,
838
+ user_prompt=user_prompt,
839
+ temperature=temperature,
840
+ logprobs=logprobs,
841
+ top_logprobs=top_logprobs,
842
+ validator=validator,
843
+ max_validation_retries=max_validation_retries,
844
+ priority=priority,
845
+ # Internal parameters
846
+ tool_name=tool_name,
847
+ output_model=Str,
848
+ mode=None,
849
+ output_lang=None,
850
+ )
851
+
852
+ metadata = ToolOutputMetadata(
853
+ tool_name=tool_name, execution_time=perf_counter() - start
854
+ )
855
+ tool_output = ToolOutput(
856
+ result=operator_output.result,
857
+ logprobs=operator_output.logprobs,
858
+ analysis=operator_output.analysis,
859
+ metadata=metadata,
860
+ )
861
+
862
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
863
+ metadata = ToolOutputMetadata(tool_name=tool_name)
864
+ tool_output = ToolOutput(
865
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
866
+ )
867
+
868
+ return tool_output
869
+
870
+ def propositionize(
871
+ self,
872
+ text: str,
873
+ with_analysis: bool = False,
874
+ output_lang: str | None = None,
875
+ user_prompt: str | None = None,
876
+ temperature: float | None = 0.0,
877
+ logprobs: bool = False,
878
+ top_logprobs: int = 3,
879
+ validator: Callable[[Any], bool] | None = None,
880
+ max_validation_retries: int | None = None,
881
+ priority: int | None = None,
882
+ ) -> ToolOutput:
883
+ """
884
+ Proposition input text to meaningful sentences.
885
+
886
+ Important Note: This tool is EXPERIMENTAL, you can use it but it isn't reliable.
887
+
888
+ Arguments:
889
+ text: The input text
890
+ with_analysis: Whether to include detailed reasoning analysis
891
+ output_lang: Language for the output
892
+ user_prompt: Additional instructions
893
+ temperature: Controls randomness (0.0 - 2.0)
894
+ logprobs: Whether to return token probability information
895
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
896
+ validator: Custom validation function to validate the output
897
+ max_validation_retries: Maximum number of retry attempts if validation fails
898
+ priority: Task execution priority (if enabled by vLLM and the model)
899
+
900
+ Returns:
901
+ ToolOutput
902
+ """
903
+ tool_name = sys._getframe().f_code.co_name
904
+ start = perf_counter()
905
+
906
+ try:
907
+ operator_output = self._operator.run(
908
+ # User parameters
909
+ text=text,
910
+ with_analysis=with_analysis,
911
+ output_lang=output_lang,
912
+ user_prompt=user_prompt,
913
+ temperature=temperature,
914
+ logprobs=logprobs,
915
+ top_logprobs=top_logprobs,
916
+ validator=validator,
917
+ max_validation_retries=max_validation_retries,
918
+ priority=priority,
919
+ # Internal parameters
920
+ tool_name=tool_name,
921
+ output_model=ListStr,
922
+ mode=None,
923
+ )
924
+
925
+ metadata = ToolOutputMetadata(
926
+ tool_name=tool_name, execution_time=perf_counter() - start
927
+ )
928
+ tool_output = ToolOutput(
929
+ result=operator_output.result,
930
+ logprobs=operator_output.logprobs,
931
+ analysis=operator_output.analysis,
932
+ metadata=metadata,
933
+ )
934
+
935
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
936
+ metadata = ToolOutputMetadata(tool_name=tool_name)
937
+ tool_output = ToolOutput(
938
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
939
+ )
940
+
941
+ return tool_output
942
+
943
+ def check_fact(
944
+ self,
945
+ text: str,
946
+ source_text: str,
947
+ with_analysis: bool = False,
948
+ output_lang: str | None = None,
949
+ user_prompt: str | None = None,
950
+ temperature: float | None = 0.0,
951
+ logprobs: bool = False,
952
+ top_logprobs: int = 3,
953
+ validator: Callable[[Any], bool] | None = None,
954
+ max_validation_retries: int | None = None,
955
+ priority: int | None = None,
956
+ ) -> ToolOutput:
957
+ """
958
+ Checks wheather a statement is relevant to the source text or not.
959
+
960
+ Important Note: This tool is EXPERIMENTAL, you can use it but it isn't reliable.
961
+
962
+ Arguments:
963
+ text: The input text
964
+ source_text: the source text that we want to check relation of text to it
965
+ with_analysis: Whether to include detailed reasoning analysis
966
+ output_lang: Language for the output
967
+ user_prompt: Additional instructions
968
+ temperature: Controls randomness (0.0 - 2.0)
969
+ logprobs: Whether to return token probability information
970
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
971
+ validator: Custom validation function to validate the output
972
+ max_validation_retries: Maximum number of retry attempts if validation fails
973
+ priority: Task execution priority (if enabled by vLLM and the model)
974
+
975
+ Returns:
976
+ ToolOutput
977
+ """
978
+ tool_name = sys._getframe().f_code.co_name
979
+ start = perf_counter()
980
+
981
+ try:
982
+ operator_output = self._operator.run(
983
+ # User parameters
984
+ text=text,
985
+ with_analysis=with_analysis,
986
+ output_lang=output_lang,
987
+ user_prompt=user_prompt,
988
+ temperature=temperature,
989
+ logprobs=logprobs,
990
+ top_logprobs=top_logprobs,
991
+ validator=validator,
992
+ max_validation_retries=max_validation_retries,
993
+ priority=priority,
994
+ # Internal parameters
995
+ tool_name=tool_name,
996
+ output_model=Bool,
997
+ mode=None,
998
+ source_text=source_text,
999
+ )
1000
+
1001
+ metadata = ToolOutputMetadata(
1002
+ tool_name=tool_name, execution_time=perf_counter() - start
1003
+ )
1004
+ tool_output = ToolOutput(
1005
+ result=operator_output.result,
1006
+ logprobs=operator_output.logprobs,
1007
+ analysis=operator_output.analysis,
1008
+ metadata=metadata,
1009
+ )
1010
+
1011
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
1012
+ metadata = ToolOutputMetadata(tool_name=tool_name)
1013
+ tool_output = ToolOutput(
1014
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
1015
+ )
1016
+
1017
+ return tool_output
1018
+
1019
+ def run_custom(
1020
+ self,
1021
+ prompt: str,
1022
+ output_model: Any,
1023
+ with_analysis: bool = False,
1024
+ analyze_template: str | None = None,
1025
+ output_lang: str | None = None,
1026
+ temperature: float | None = None,
1027
+ logprobs: bool | None = None,
1028
+ top_logprobs: int = 3,
1029
+ validator: Callable[[Any], bool] | None = None,
1030
+ max_validation_retries: int | None = None,
1031
+ priority: int | None = None,
1032
+ ) -> ToolOutput:
1033
+ """
1034
+ Custom tool that can do almost anything!
1035
+
1036
+ Arguments:
1037
+ prompt: The user prompt
1038
+ output_model: Pydantic BaseModel used for structured output
1039
+ with_analysis: Whether to include detailed reasoning analysis
1040
+ analyze_template: The analyze template used for reasoning analysis
1041
+ output_lang: Language for the output
1042
+ temperature: Controls randomness (0.0 - 2.0)
1043
+ logprobs: Whether to return token probability information
1044
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
1045
+ validator: Custom validation function to validate the output
1046
+ max_validation_retries: Maximum number of retry attempts if validation fails
1047
+ priority: Task execution priority (if enabled by vLLM and the model)
1048
+
1049
+ Returns:
1050
+ ToolOutput
1051
+ """
1052
+ tool_name = sys._getframe().f_code.co_name
1053
+ start = perf_counter()
1054
+
1055
+ try:
1056
+ operator_output = self._operator.run(
1057
+ # User paramaeters
1058
+ text=prompt,
1059
+ output_model=output_model,
1060
+ with_analysis=with_analysis,
1061
+ analyze_template=analyze_template,
1062
+ output_model_str=output_model.model_json_schema(),
1063
+ output_lang=output_lang,
1064
+ temperature=temperature,
1065
+ logprobs=logprobs,
1066
+ top_logprobs=top_logprobs,
1067
+ validator=validator,
1068
+ max_validation_retries=max_validation_retries,
1069
+ priority=priority,
1070
+ # Internal parameters
1071
+ tool_name=tool_name,
1072
+ user_prompt=None,
1073
+ mode=None,
1074
+ )
1075
+
1076
+ metadata = ToolOutputMetadata(
1077
+ tool_name=tool_name, execution_time=perf_counter() - start
1078
+ )
1079
+ tool_output = ToolOutput(
1080
+ result=operator_output.result,
1081
+ logprobs=operator_output.logprobs,
1082
+ analysis=operator_output.analysis,
1083
+ metadata=metadata,
1084
+ )
1085
+
1086
+ except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
1087
+ metadata = ToolOutputMetadata(tool_name=tool_name)
1088
+ tool_output = ToolOutput(
1089
+ errors=[f"{type(e).__name__}: {e}"], metadata=metadata
1090
+ )
1091
+
1092
+ return tool_output