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.
- hamtaa_texttools-1.2.0.dist-info/METADATA +212 -0
- hamtaa_texttools-1.2.0.dist-info/RECORD +34 -0
- texttools/__init__.py +6 -8
- texttools/batch/__init__.py +0 -4
- texttools/batch/config.py +40 -0
- texttools/batch/{batch_manager.py → manager.py} +41 -42
- texttools/batch/runner.py +228 -0
- texttools/core/__init__.py +0 -0
- texttools/core/engine.py +254 -0
- texttools/core/exceptions.py +22 -0
- texttools/core/internal_models.py +58 -0
- texttools/core/operators/async_operator.py +194 -0
- texttools/core/operators/sync_operator.py +192 -0
- texttools/models.py +88 -0
- texttools/prompts/categorize.yaml +36 -0
- texttools/prompts/check_fact.yaml +24 -0
- texttools/prompts/extract_entities.yaml +7 -3
- texttools/prompts/extract_keywords.yaml +80 -18
- texttools/prompts/is_question.yaml +6 -2
- texttools/prompts/merge_questions.yaml +12 -5
- texttools/prompts/propositionize.yaml +24 -0
- texttools/prompts/rewrite.yaml +9 -10
- texttools/prompts/run_custom.yaml +2 -2
- texttools/prompts/subject_to_question.yaml +7 -3
- texttools/prompts/summarize.yaml +6 -2
- texttools/prompts/text_to_question.yaml +12 -6
- texttools/prompts/translate.yaml +7 -2
- texttools/py.typed +0 -0
- texttools/tools/__init__.py +0 -4
- texttools/tools/async_tools.py +1093 -0
- texttools/tools/sync_tools.py +1092 -0
- hamtaa_texttools-1.1.1.dist-info/METADATA +0 -183
- hamtaa_texttools-1.1.1.dist-info/RECORD +0 -30
- texttools/batch/batch_runner.py +0 -263
- texttools/prompts/README.md +0 -35
- texttools/prompts/categorizer.yaml +0 -28
- texttools/tools/async_the_tool.py +0 -414
- texttools/tools/internals/async_operator.py +0 -179
- texttools/tools/internals/base_operator.py +0 -91
- texttools/tools/internals/formatters.py +0 -24
- texttools/tools/internals/operator.py +0 -179
- texttools/tools/internals/output_models.py +0 -59
- texttools/tools/internals/prompt_loader.py +0 -57
- texttools/tools/the_tool.py +0 -412
- {hamtaa_texttools-1.1.1.dist-info → hamtaa_texttools-1.2.0.dist-info}/WHEEL +0 -0
- {hamtaa_texttools-1.1.1.dist-info → hamtaa_texttools-1.2.0.dist-info}/licenses/LICENSE +0 -0
- {hamtaa_texttools-1.1.1.dist-info → hamtaa_texttools-1.2.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,1093 @@
|
|
|
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 AsyncOpenAI
|
|
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.async_operator import AsyncOperator
|
|
19
|
+
from ..models import CategoryTree, ToolOutput, ToolOutputMetadata
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class AsyncTheTool:
|
|
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: AsyncOpenAI,
|
|
31
|
+
model: str,
|
|
32
|
+
):
|
|
33
|
+
self._operator = AsyncOperator(client=client, model=model)
|
|
34
|
+
|
|
35
|
+
async 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 = await 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 = await 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
|
+
async 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 = await 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
|
+
async 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 (Optional)
|
|
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 = await 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
|
+
async 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
|
+
|
|
348
|
+
start = perf_counter()
|
|
349
|
+
|
|
350
|
+
try:
|
|
351
|
+
operator_output = await self._operator.run(
|
|
352
|
+
# User parameters
|
|
353
|
+
text=text,
|
|
354
|
+
with_analysis=with_analysis,
|
|
355
|
+
user_prompt=user_prompt,
|
|
356
|
+
temperature=temperature,
|
|
357
|
+
logprobs=logprobs,
|
|
358
|
+
top_logprobs=top_logprobs,
|
|
359
|
+
validator=validator,
|
|
360
|
+
max_validation_retries=max_validation_retries,
|
|
361
|
+
priority=priority,
|
|
362
|
+
# Internal parameters
|
|
363
|
+
tool_name=tool_name,
|
|
364
|
+
output_model=Bool,
|
|
365
|
+
mode=None,
|
|
366
|
+
output_lang=None,
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
metadata = ToolOutputMetadata(
|
|
370
|
+
tool_name=tool_name, execution_time=perf_counter() - start
|
|
371
|
+
)
|
|
372
|
+
tool_output = ToolOutput(
|
|
373
|
+
result=operator_output.result,
|
|
374
|
+
logprobs=operator_output.logprobs,
|
|
375
|
+
analysis=operator_output.analysis,
|
|
376
|
+
metadata=metadata,
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
|
|
380
|
+
metadata = ToolOutputMetadata(tool_name=tool_name)
|
|
381
|
+
tool_output = ToolOutput(
|
|
382
|
+
errors=[f"{type(e).__name__}: {e}"], metadata=metadata
|
|
383
|
+
)
|
|
384
|
+
|
|
385
|
+
return tool_output
|
|
386
|
+
|
|
387
|
+
async def text_to_question(
|
|
388
|
+
self,
|
|
389
|
+
text: str,
|
|
390
|
+
number_of_questions: int,
|
|
391
|
+
with_analysis: bool = False,
|
|
392
|
+
output_lang: str | None = None,
|
|
393
|
+
user_prompt: str | None = None,
|
|
394
|
+
temperature: float | None = 0.0,
|
|
395
|
+
logprobs: bool = False,
|
|
396
|
+
top_logprobs: int = 3,
|
|
397
|
+
validator: Callable[[Any], bool] | None = None,
|
|
398
|
+
max_validation_retries: int | None = None,
|
|
399
|
+
priority: int | None = None,
|
|
400
|
+
) -> ToolOutput:
|
|
401
|
+
"""
|
|
402
|
+
Generate a single question from the given text.
|
|
403
|
+
|
|
404
|
+
Arguments:
|
|
405
|
+
text: The input text
|
|
406
|
+
number_of_questions: Number of questions to generate
|
|
407
|
+
with_analysis: Whether to include detailed reasoning analysis
|
|
408
|
+
output_lang: Language for the output
|
|
409
|
+
user_prompt: Additional instructions
|
|
410
|
+
temperature: Controls randomness (0.0 - 2.0)
|
|
411
|
+
logprobs: Whether to return token probability information
|
|
412
|
+
top_logprobs: Number of top token alternatives to return if logprobs enabled
|
|
413
|
+
validator: Custom validation function to validate the output
|
|
414
|
+
max_validation_retries: Maximum number of retry attempts if validation fails
|
|
415
|
+
priority: Task execution priority (if enabled by vLLM and the model)
|
|
416
|
+
|
|
417
|
+
Returns:
|
|
418
|
+
ToolOutput
|
|
419
|
+
"""
|
|
420
|
+
tool_name = sys._getframe().f_code.co_name
|
|
421
|
+
start = perf_counter()
|
|
422
|
+
|
|
423
|
+
try:
|
|
424
|
+
operator_output = await self._operator.run(
|
|
425
|
+
# User parameters
|
|
426
|
+
text=text,
|
|
427
|
+
number_of_questions=number_of_questions,
|
|
428
|
+
with_analysis=with_analysis,
|
|
429
|
+
output_lang=output_lang,
|
|
430
|
+
user_prompt=user_prompt,
|
|
431
|
+
temperature=temperature,
|
|
432
|
+
logprobs=logprobs,
|
|
433
|
+
top_logprobs=top_logprobs,
|
|
434
|
+
validator=validator,
|
|
435
|
+
max_validation_retries=max_validation_retries,
|
|
436
|
+
priority=priority,
|
|
437
|
+
# Internal parameters
|
|
438
|
+
tool_name=tool_name,
|
|
439
|
+
output_model=ReasonListStr,
|
|
440
|
+
mode=None,
|
|
441
|
+
)
|
|
442
|
+
|
|
443
|
+
metadata = ToolOutputMetadata(
|
|
444
|
+
tool_name=tool_name, execution_time=perf_counter() - start
|
|
445
|
+
)
|
|
446
|
+
tool_output = ToolOutput(
|
|
447
|
+
result=operator_output.result,
|
|
448
|
+
logprobs=operator_output.logprobs,
|
|
449
|
+
analysis=operator_output.analysis,
|
|
450
|
+
metadata=metadata,
|
|
451
|
+
)
|
|
452
|
+
|
|
453
|
+
except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
|
|
454
|
+
metadata = ToolOutputMetadata(tool_name=tool_name)
|
|
455
|
+
tool_output = ToolOutput(
|
|
456
|
+
errors=[f"{type(e).__name__}: {e}"], metadata=metadata
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
return tool_output
|
|
460
|
+
|
|
461
|
+
async def merge_questions(
|
|
462
|
+
self,
|
|
463
|
+
text: list[str],
|
|
464
|
+
with_analysis: bool = False,
|
|
465
|
+
output_lang: str | None = None,
|
|
466
|
+
user_prompt: str | None = None,
|
|
467
|
+
temperature: float | None = 0.0,
|
|
468
|
+
logprobs: bool = False,
|
|
469
|
+
top_logprobs: int = 3,
|
|
470
|
+
mode: Literal["default", "reason"] = "default",
|
|
471
|
+
validator: Callable[[Any], bool] | None = None,
|
|
472
|
+
max_validation_retries: int | None = None,
|
|
473
|
+
priority: int | None = None,
|
|
474
|
+
) -> ToolOutput:
|
|
475
|
+
"""
|
|
476
|
+
Merge multiple questions into a single unified question.
|
|
477
|
+
|
|
478
|
+
Arguments:
|
|
479
|
+
text: List of questions to merge
|
|
480
|
+
with_analysis: Whether to include detailed reasoning analysis
|
|
481
|
+
output_lang: Language for the output
|
|
482
|
+
user_prompt: Additional instructions
|
|
483
|
+
temperature: Controls randomness (0.0 - 2.0)
|
|
484
|
+
logprobs: Whether to return token probability information
|
|
485
|
+
top_logprobs: Number of top token alternatives to return if logprobs enabled
|
|
486
|
+
validator: Custom validation function to validate the output
|
|
487
|
+
max_validation_retries: Maximum number of retry attempts if validation fails
|
|
488
|
+
priority: Task execution priority (if enabled by vLLM and the model)
|
|
489
|
+
|
|
490
|
+
Returns:
|
|
491
|
+
ToolOutput
|
|
492
|
+
"""
|
|
493
|
+
tool_name = sys._getframe().f_code.co_name
|
|
494
|
+
start = perf_counter()
|
|
495
|
+
|
|
496
|
+
try:
|
|
497
|
+
text = ", ".join(text)
|
|
498
|
+
operator_output = await self._operator.run(
|
|
499
|
+
# User parameters
|
|
500
|
+
text=text,
|
|
501
|
+
with_analysis=with_analysis,
|
|
502
|
+
output_lang=output_lang,
|
|
503
|
+
user_prompt=user_prompt,
|
|
504
|
+
temperature=temperature,
|
|
505
|
+
logprobs=logprobs,
|
|
506
|
+
top_logprobs=top_logprobs,
|
|
507
|
+
validator=validator,
|
|
508
|
+
max_validation_retries=max_validation_retries,
|
|
509
|
+
priority=priority,
|
|
510
|
+
# Internal parameters
|
|
511
|
+
tool_name=tool_name,
|
|
512
|
+
output_model=Str,
|
|
513
|
+
mode=mode,
|
|
514
|
+
)
|
|
515
|
+
|
|
516
|
+
metadata = ToolOutputMetadata(
|
|
517
|
+
tool_name=tool_name, execution_time=perf_counter() - start
|
|
518
|
+
)
|
|
519
|
+
tool_output = ToolOutput(
|
|
520
|
+
result=operator_output.result,
|
|
521
|
+
logprobs=operator_output.logprobs,
|
|
522
|
+
analysis=operator_output.analysis,
|
|
523
|
+
metadata=metadata,
|
|
524
|
+
)
|
|
525
|
+
|
|
526
|
+
except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
|
|
527
|
+
metadata = ToolOutputMetadata(tool_name=tool_name)
|
|
528
|
+
tool_output = ToolOutput(
|
|
529
|
+
errors=[f"{type(e).__name__}: {e}"], metadata=metadata
|
|
530
|
+
)
|
|
531
|
+
|
|
532
|
+
return tool_output
|
|
533
|
+
|
|
534
|
+
async def rewrite(
|
|
535
|
+
self,
|
|
536
|
+
text: str,
|
|
537
|
+
with_analysis: bool = False,
|
|
538
|
+
output_lang: str | None = None,
|
|
539
|
+
user_prompt: str | None = None,
|
|
540
|
+
temperature: float | None = 0.0,
|
|
541
|
+
logprobs: bool = False,
|
|
542
|
+
top_logprobs: int = 3,
|
|
543
|
+
mode: Literal["positive", "negative", "hard_negative"] = "positive",
|
|
544
|
+
validator: Callable[[Any], bool] | None = None,
|
|
545
|
+
max_validation_retries: int | None = None,
|
|
546
|
+
priority: int | None = None,
|
|
547
|
+
) -> ToolOutput:
|
|
548
|
+
"""
|
|
549
|
+
Rewrite a text with different modes.
|
|
550
|
+
|
|
551
|
+
Arguments:
|
|
552
|
+
text: The input text
|
|
553
|
+
with_analysis: Whether to include detailed reasoning analysis
|
|
554
|
+
output_lang: Language for the output
|
|
555
|
+
user_prompt: Additional instructions
|
|
556
|
+
temperature: Controls randomness (0.0 - 2.0)
|
|
557
|
+
logprobs: Whether to return token probability information
|
|
558
|
+
top_logprobs: Number of top token alternatives to return if logprobs enabled
|
|
559
|
+
validator: Custom validation function to validate the output
|
|
560
|
+
max_validation_retries: Maximum number of retry attempts if validation fails
|
|
561
|
+
priority: Task execution priority (if enabled by vLLM and the model)
|
|
562
|
+
|
|
563
|
+
Returns:
|
|
564
|
+
ToolOutput
|
|
565
|
+
"""
|
|
566
|
+
tool_name = sys._getframe().f_code.co_name
|
|
567
|
+
start = perf_counter()
|
|
568
|
+
|
|
569
|
+
try:
|
|
570
|
+
operator_output = await self._operator.run(
|
|
571
|
+
# User parameters
|
|
572
|
+
text=text,
|
|
573
|
+
with_analysis=with_analysis,
|
|
574
|
+
output_lang=output_lang,
|
|
575
|
+
user_prompt=user_prompt,
|
|
576
|
+
temperature=temperature,
|
|
577
|
+
logprobs=logprobs,
|
|
578
|
+
top_logprobs=top_logprobs,
|
|
579
|
+
validator=validator,
|
|
580
|
+
max_validation_retries=max_validation_retries,
|
|
581
|
+
priority=priority,
|
|
582
|
+
# Internal parameters
|
|
583
|
+
tool_name=tool_name,
|
|
584
|
+
output_model=Str,
|
|
585
|
+
mode=mode,
|
|
586
|
+
)
|
|
587
|
+
|
|
588
|
+
metadata = ToolOutputMetadata(
|
|
589
|
+
tool_name=tool_name, execution_time=perf_counter() - start
|
|
590
|
+
)
|
|
591
|
+
tool_output = ToolOutput(
|
|
592
|
+
result=operator_output.result,
|
|
593
|
+
logprobs=operator_output.logprobs,
|
|
594
|
+
analysis=operator_output.analysis,
|
|
595
|
+
metadata=metadata,
|
|
596
|
+
)
|
|
597
|
+
|
|
598
|
+
except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
|
|
599
|
+
metadata = ToolOutputMetadata(tool_name=tool_name)
|
|
600
|
+
tool_output = ToolOutput(
|
|
601
|
+
errors=[f"{type(e).__name__}: {e}"], metadata=metadata
|
|
602
|
+
)
|
|
603
|
+
|
|
604
|
+
return tool_output
|
|
605
|
+
|
|
606
|
+
async def subject_to_question(
|
|
607
|
+
self,
|
|
608
|
+
text: str,
|
|
609
|
+
number_of_questions: int,
|
|
610
|
+
with_analysis: bool = False,
|
|
611
|
+
output_lang: str | None = None,
|
|
612
|
+
user_prompt: str | None = None,
|
|
613
|
+
temperature: float | None = 0.0,
|
|
614
|
+
logprobs: bool = False,
|
|
615
|
+
top_logprobs: int = 3,
|
|
616
|
+
validator: Callable[[Any], bool] | None = None,
|
|
617
|
+
max_validation_retries: int | None = None,
|
|
618
|
+
priority: int | None = None,
|
|
619
|
+
) -> ToolOutput:
|
|
620
|
+
"""
|
|
621
|
+
Generate a list of questions about a subject.
|
|
622
|
+
|
|
623
|
+
Arguments:
|
|
624
|
+
text: The subject text to generate questions about
|
|
625
|
+
number_of_questions: Number of questions to generate
|
|
626
|
+
with_analysis: Whether to include detailed reasoning analysis
|
|
627
|
+
output_lang: Language for the output
|
|
628
|
+
user_prompt: Additional instructions
|
|
629
|
+
temperature: Controls randomness (0.0 - 2.0)
|
|
630
|
+
logprobs: Whether to return token probability information
|
|
631
|
+
top_logprobs: Number of top token alternatives to return if logprobs enabled
|
|
632
|
+
validator: Custom validation function to validate the output
|
|
633
|
+
max_validation_retries: Maximum number of retry attempts if validation fails
|
|
634
|
+
priority: Task execution priority (if enabled by vLLM and the model)
|
|
635
|
+
|
|
636
|
+
Returns:
|
|
637
|
+
ToolOutput
|
|
638
|
+
"""
|
|
639
|
+
tool_name = sys._getframe().f_code.co_name
|
|
640
|
+
start = perf_counter()
|
|
641
|
+
|
|
642
|
+
try:
|
|
643
|
+
operator_output = await self._operator.run(
|
|
644
|
+
# User parameters
|
|
645
|
+
text=text,
|
|
646
|
+
number_of_questions=number_of_questions,
|
|
647
|
+
with_analysis=with_analysis,
|
|
648
|
+
output_lang=output_lang,
|
|
649
|
+
user_prompt=user_prompt,
|
|
650
|
+
temperature=temperature,
|
|
651
|
+
logprobs=logprobs,
|
|
652
|
+
top_logprobs=top_logprobs,
|
|
653
|
+
validator=validator,
|
|
654
|
+
max_validation_retries=max_validation_retries,
|
|
655
|
+
priority=priority,
|
|
656
|
+
# Internal parameters
|
|
657
|
+
tool_name=tool_name,
|
|
658
|
+
output_model=ReasonListStr,
|
|
659
|
+
mode=None,
|
|
660
|
+
)
|
|
661
|
+
|
|
662
|
+
metadata = ToolOutputMetadata(
|
|
663
|
+
tool_name=tool_name, execution_time=perf_counter() - start
|
|
664
|
+
)
|
|
665
|
+
tool_output = ToolOutput(
|
|
666
|
+
result=operator_output.result,
|
|
667
|
+
logprobs=operator_output.logprobs,
|
|
668
|
+
analysis=operator_output.analysis,
|
|
669
|
+
metadata=metadata,
|
|
670
|
+
)
|
|
671
|
+
|
|
672
|
+
except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
|
|
673
|
+
metadata = ToolOutputMetadata(tool_name=tool_name)
|
|
674
|
+
tool_output = ToolOutput(
|
|
675
|
+
errors=[f"{type(e).__name__}: {e}"], metadata=metadata
|
|
676
|
+
)
|
|
677
|
+
|
|
678
|
+
return tool_output
|
|
679
|
+
|
|
680
|
+
async def summarize(
|
|
681
|
+
self,
|
|
682
|
+
text: str,
|
|
683
|
+
with_analysis: bool = False,
|
|
684
|
+
output_lang: str | None = None,
|
|
685
|
+
user_prompt: str | None = None,
|
|
686
|
+
temperature: float | None = 0.0,
|
|
687
|
+
logprobs: bool = False,
|
|
688
|
+
top_logprobs: int = 3,
|
|
689
|
+
validator: Callable[[Any], bool] | None = None,
|
|
690
|
+
max_validation_retries: int | None = None,
|
|
691
|
+
priority: int | None = None,
|
|
692
|
+
) -> ToolOutput:
|
|
693
|
+
"""
|
|
694
|
+
Summarize the given subject text.
|
|
695
|
+
|
|
696
|
+
Arguments:
|
|
697
|
+
text: The input text
|
|
698
|
+
with_analysis: Whether to include detailed reasoning analysis
|
|
699
|
+
output_lang: Language for the output
|
|
700
|
+
user_prompt: Additional instructions
|
|
701
|
+
temperature: Controls randomness (0.0 - 2.0)
|
|
702
|
+
logprobs: Whether to return token probability information
|
|
703
|
+
top_logprobs: Number of top token alternatives to return if logprobs enabled
|
|
704
|
+
validator: Custom validation function to validate the output
|
|
705
|
+
max_validation_retries: Maximum number of retry attempts if validation fails
|
|
706
|
+
priority: Task execution priority (if enabled by vLLM and the model)
|
|
707
|
+
|
|
708
|
+
Returns:
|
|
709
|
+
ToolOutput
|
|
710
|
+
"""
|
|
711
|
+
tool_name = sys._getframe().f_code.co_name
|
|
712
|
+
start = perf_counter()
|
|
713
|
+
|
|
714
|
+
try:
|
|
715
|
+
operator_output = await self._operator.run(
|
|
716
|
+
# User parameters
|
|
717
|
+
text=text,
|
|
718
|
+
with_analysis=with_analysis,
|
|
719
|
+
output_lang=output_lang,
|
|
720
|
+
user_prompt=user_prompt,
|
|
721
|
+
temperature=temperature,
|
|
722
|
+
logprobs=logprobs,
|
|
723
|
+
top_logprobs=top_logprobs,
|
|
724
|
+
validator=validator,
|
|
725
|
+
max_validation_retries=max_validation_retries,
|
|
726
|
+
priority=priority,
|
|
727
|
+
# Internal parameters
|
|
728
|
+
tool_name=tool_name,
|
|
729
|
+
output_model=Str,
|
|
730
|
+
mode=None,
|
|
731
|
+
)
|
|
732
|
+
|
|
733
|
+
metadata = ToolOutputMetadata(
|
|
734
|
+
tool_name=tool_name, execution_time=perf_counter() - start
|
|
735
|
+
)
|
|
736
|
+
tool_output = ToolOutput(
|
|
737
|
+
result=operator_output.result,
|
|
738
|
+
logprobs=operator_output.logprobs,
|
|
739
|
+
analysis=operator_output.analysis,
|
|
740
|
+
metadata=metadata,
|
|
741
|
+
)
|
|
742
|
+
|
|
743
|
+
except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
|
|
744
|
+
metadata = ToolOutputMetadata(tool_name=tool_name)
|
|
745
|
+
tool_output = ToolOutput(
|
|
746
|
+
errors=[f"{type(e).__name__}: {e}"], metadata=metadata
|
|
747
|
+
)
|
|
748
|
+
|
|
749
|
+
return tool_output
|
|
750
|
+
|
|
751
|
+
async def translate(
|
|
752
|
+
self,
|
|
753
|
+
text: str,
|
|
754
|
+
target_language: str,
|
|
755
|
+
use_chunker: bool = True,
|
|
756
|
+
with_analysis: bool = False,
|
|
757
|
+
user_prompt: str | None = None,
|
|
758
|
+
temperature: float | None = 0.0,
|
|
759
|
+
logprobs: bool = False,
|
|
760
|
+
top_logprobs: int = 3,
|
|
761
|
+
validator: Callable[[Any], bool] | None = None,
|
|
762
|
+
max_validation_retries: int | None = None,
|
|
763
|
+
priority: int | None = None,
|
|
764
|
+
) -> ToolOutput:
|
|
765
|
+
"""
|
|
766
|
+
Translate text between languages.
|
|
767
|
+
|
|
768
|
+
Important Note: This tool is EXPERIMENTAL, you can use it but it isn't reliable.
|
|
769
|
+
|
|
770
|
+
Arguments:
|
|
771
|
+
text: The input text
|
|
772
|
+
target_language: The target language for translation
|
|
773
|
+
use_chunker: Whether to use text chunker for text length bigger than 1500
|
|
774
|
+
with_analysis: Whether to include detailed reasoning analysis
|
|
775
|
+
user_prompt: Additional instructions
|
|
776
|
+
temperature: Controls randomness (0.0 - 2.0)
|
|
777
|
+
logprobs: Whether to return token probability information
|
|
778
|
+
top_logprobs: Number of top token alternatives to return if logprobs enabled
|
|
779
|
+
validator: Custom validation function to validate the output
|
|
780
|
+
max_validation_retries: Maximum number of retry attempts if validation fails
|
|
781
|
+
priority: Task execution priority (if enabled by vLLM and the model)
|
|
782
|
+
|
|
783
|
+
Returns:
|
|
784
|
+
ToolOutput
|
|
785
|
+
"""
|
|
786
|
+
tool_name = sys._getframe().f_code.co_name
|
|
787
|
+
start = perf_counter()
|
|
788
|
+
|
|
789
|
+
try:
|
|
790
|
+
if len(text.split(" ")) > 1500 and use_chunker:
|
|
791
|
+
chunks = text_to_chunks(text, 1200, 0)
|
|
792
|
+
translation = ""
|
|
793
|
+
analysis = ""
|
|
794
|
+
logprobs_list = []
|
|
795
|
+
|
|
796
|
+
for chunk in chunks:
|
|
797
|
+
chunk_operator_output = await self._operator.run(
|
|
798
|
+
# User parameters
|
|
799
|
+
text=chunk,
|
|
800
|
+
target_language=target_language,
|
|
801
|
+
with_analysis=with_analysis,
|
|
802
|
+
user_prompt=user_prompt,
|
|
803
|
+
temperature=temperature,
|
|
804
|
+
logprobs=logprobs,
|
|
805
|
+
top_logprobs=top_logprobs,
|
|
806
|
+
validator=validator,
|
|
807
|
+
max_validation_retries=max_validation_retries,
|
|
808
|
+
priority=priority,
|
|
809
|
+
# Internal parameters
|
|
810
|
+
tool_name=tool_name,
|
|
811
|
+
output_model=Str,
|
|
812
|
+
mode=None,
|
|
813
|
+
output_lang=None,
|
|
814
|
+
)
|
|
815
|
+
|
|
816
|
+
translation += chunk_operator_output.result + "\n"
|
|
817
|
+
|
|
818
|
+
if with_analysis:
|
|
819
|
+
analysis += chunk_operator_output.analysis
|
|
820
|
+
if logprobs:
|
|
821
|
+
logprobs_list.extend(chunk_operator_output.logprobs)
|
|
822
|
+
|
|
823
|
+
metadata = ToolOutputMetadata(
|
|
824
|
+
tool_name=tool_name, execution_time=perf_counter() - start
|
|
825
|
+
)
|
|
826
|
+
tool_output = ToolOutput(
|
|
827
|
+
result=translation,
|
|
828
|
+
logprobs=logprobs_list,
|
|
829
|
+
analysis=analysis,
|
|
830
|
+
metadata=metadata,
|
|
831
|
+
)
|
|
832
|
+
|
|
833
|
+
else:
|
|
834
|
+
operator_output = await self._operator.run(
|
|
835
|
+
# User parameters
|
|
836
|
+
text=text,
|
|
837
|
+
target_language=target_language,
|
|
838
|
+
with_analysis=with_analysis,
|
|
839
|
+
user_prompt=user_prompt,
|
|
840
|
+
temperature=temperature,
|
|
841
|
+
logprobs=logprobs,
|
|
842
|
+
top_logprobs=top_logprobs,
|
|
843
|
+
validator=validator,
|
|
844
|
+
max_validation_retries=max_validation_retries,
|
|
845
|
+
priority=priority,
|
|
846
|
+
# Internal parameters
|
|
847
|
+
tool_name=tool_name,
|
|
848
|
+
output_model=Str,
|
|
849
|
+
mode=None,
|
|
850
|
+
output_lang=None,
|
|
851
|
+
)
|
|
852
|
+
|
|
853
|
+
metadata = ToolOutputMetadata(
|
|
854
|
+
tool_name=tool_name, execution_time=perf_counter() - start
|
|
855
|
+
)
|
|
856
|
+
tool_output = ToolOutput(
|
|
857
|
+
result=operator_output.result,
|
|
858
|
+
logprobs=operator_output.logprobs,
|
|
859
|
+
analysis=operator_output.analysis,
|
|
860
|
+
metadata=metadata,
|
|
861
|
+
)
|
|
862
|
+
|
|
863
|
+
except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
|
|
864
|
+
metadata = ToolOutputMetadata(tool_name=tool_name)
|
|
865
|
+
tool_output = ToolOutput(
|
|
866
|
+
errors=[f"{type(e).__name__}: {e}"], metadata=metadata
|
|
867
|
+
)
|
|
868
|
+
|
|
869
|
+
return tool_output
|
|
870
|
+
|
|
871
|
+
async def propositionize(
|
|
872
|
+
self,
|
|
873
|
+
text: str,
|
|
874
|
+
with_analysis: bool = False,
|
|
875
|
+
output_lang: str | None = None,
|
|
876
|
+
user_prompt: str | None = None,
|
|
877
|
+
temperature: float | None = 0.0,
|
|
878
|
+
logprobs: bool = False,
|
|
879
|
+
top_logprobs: int = 3,
|
|
880
|
+
validator: Callable[[Any], bool] | None = None,
|
|
881
|
+
max_validation_retries: int | None = None,
|
|
882
|
+
priority: int | None = None,
|
|
883
|
+
) -> ToolOutput:
|
|
884
|
+
"""
|
|
885
|
+
Proposition input text to meaningful sentences.
|
|
886
|
+
|
|
887
|
+
Important Note: This tool is EXPERIMENTAL, you can use it but it isn't reliable.
|
|
888
|
+
|
|
889
|
+
Arguments:
|
|
890
|
+
text: The input text
|
|
891
|
+
with_analysis: Whether to include detailed reasoning analysis
|
|
892
|
+
output_lang: Language for the output
|
|
893
|
+
user_prompt: Additional instructions
|
|
894
|
+
temperature: Controls randomness (0.0 - 2.0)
|
|
895
|
+
logprobs: Whether to return token probability information
|
|
896
|
+
top_logprobs: Number of top token alternatives to return if logprobs enabled
|
|
897
|
+
validator: Custom validation function to validate the output
|
|
898
|
+
max_validation_retries: Maximum number of retry attempts if validation fails
|
|
899
|
+
priority: Task execution priority (if enabled by vLLM and the model)
|
|
900
|
+
|
|
901
|
+
Returns:
|
|
902
|
+
ToolOutput
|
|
903
|
+
"""
|
|
904
|
+
tool_name = sys._getframe().f_code.co_name
|
|
905
|
+
start = perf_counter()
|
|
906
|
+
|
|
907
|
+
try:
|
|
908
|
+
operator_output = await self._operator.run(
|
|
909
|
+
# User parameters
|
|
910
|
+
text=text,
|
|
911
|
+
with_analysis=with_analysis,
|
|
912
|
+
output_lang=output_lang,
|
|
913
|
+
user_prompt=user_prompt,
|
|
914
|
+
temperature=temperature,
|
|
915
|
+
logprobs=logprobs,
|
|
916
|
+
top_logprobs=top_logprobs,
|
|
917
|
+
validator=validator,
|
|
918
|
+
max_validation_retries=max_validation_retries,
|
|
919
|
+
priority=priority,
|
|
920
|
+
# Internal parameters
|
|
921
|
+
tool_name=tool_name,
|
|
922
|
+
output_model=ListStr,
|
|
923
|
+
mode=None,
|
|
924
|
+
)
|
|
925
|
+
|
|
926
|
+
metadata = ToolOutputMetadata(
|
|
927
|
+
tool_name=tool_name, execution_time=perf_counter() - start
|
|
928
|
+
)
|
|
929
|
+
tool_output = ToolOutput(
|
|
930
|
+
result=operator_output.result,
|
|
931
|
+
logprobs=operator_output.logprobs,
|
|
932
|
+
analysis=operator_output.analysis,
|
|
933
|
+
metadata=metadata,
|
|
934
|
+
)
|
|
935
|
+
|
|
936
|
+
except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
|
|
937
|
+
metadata = ToolOutputMetadata(tool_name=tool_name)
|
|
938
|
+
tool_output = ToolOutput(
|
|
939
|
+
errors=[f"{type(e).__name__}: {e}"], metadata=metadata
|
|
940
|
+
)
|
|
941
|
+
|
|
942
|
+
return tool_output
|
|
943
|
+
|
|
944
|
+
async def check_fact(
|
|
945
|
+
self,
|
|
946
|
+
text: str,
|
|
947
|
+
source_text: str,
|
|
948
|
+
with_analysis: bool = False,
|
|
949
|
+
output_lang: str | None = None,
|
|
950
|
+
user_prompt: str | None = None,
|
|
951
|
+
temperature: float | None = 0.0,
|
|
952
|
+
logprobs: bool = False,
|
|
953
|
+
top_logprobs: int = 3,
|
|
954
|
+
validator: Callable[[Any], bool] | None = None,
|
|
955
|
+
max_validation_retries: int | None = None,
|
|
956
|
+
priority: int | None = None,
|
|
957
|
+
) -> ToolOutput:
|
|
958
|
+
"""
|
|
959
|
+
Checks wheather a statement is relevant to the source text or not.
|
|
960
|
+
|
|
961
|
+
Important Note: This tool is EXPERIMENTAL, you can use it but it isn't reliable.
|
|
962
|
+
|
|
963
|
+
Arguments:
|
|
964
|
+
text: The input text
|
|
965
|
+
source_text: the source text that we want to check relation of text to it
|
|
966
|
+
with_analysis: Whether to include detailed reasoning analysis
|
|
967
|
+
output_lang: Language for the output
|
|
968
|
+
user_prompt: Additional instructions
|
|
969
|
+
temperature: Controls randomness (0.0 - 2.0)
|
|
970
|
+
logprobs: Whether to return token probability information
|
|
971
|
+
top_logprobs: Number of top token alternatives to return if logprobs enabled
|
|
972
|
+
validator: Custom validation function to validate the output
|
|
973
|
+
max_validation_retries: Maximum number of retry attempts if validation fails
|
|
974
|
+
priority: Task execution priority (if enabled by vLLM and the model)
|
|
975
|
+
|
|
976
|
+
Returns:
|
|
977
|
+
ToolOutput
|
|
978
|
+
"""
|
|
979
|
+
tool_name = sys._getframe().f_code.co_name
|
|
980
|
+
start = perf_counter()
|
|
981
|
+
|
|
982
|
+
try:
|
|
983
|
+
operator_output = await self._operator.run(
|
|
984
|
+
# User parameters
|
|
985
|
+
text=text,
|
|
986
|
+
with_analysis=with_analysis,
|
|
987
|
+
output_lang=output_lang,
|
|
988
|
+
user_prompt=user_prompt,
|
|
989
|
+
temperature=temperature,
|
|
990
|
+
logprobs=logprobs,
|
|
991
|
+
top_logprobs=top_logprobs,
|
|
992
|
+
validator=validator,
|
|
993
|
+
max_validation_retries=max_validation_retries,
|
|
994
|
+
priority=priority,
|
|
995
|
+
# Internal parameters
|
|
996
|
+
tool_name=tool_name,
|
|
997
|
+
output_model=Bool,
|
|
998
|
+
mode=None,
|
|
999
|
+
source_text=source_text,
|
|
1000
|
+
)
|
|
1001
|
+
|
|
1002
|
+
metadata = ToolOutputMetadata(
|
|
1003
|
+
tool_name=tool_name, execution_time=perf_counter() - start
|
|
1004
|
+
)
|
|
1005
|
+
tool_output = ToolOutput(
|
|
1006
|
+
result=operator_output.result,
|
|
1007
|
+
logprobs=operator_output.logprobs,
|
|
1008
|
+
analysis=operator_output.analysis,
|
|
1009
|
+
metadata=metadata,
|
|
1010
|
+
)
|
|
1011
|
+
|
|
1012
|
+
except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
|
|
1013
|
+
metadata = ToolOutputMetadata(tool_name=tool_name)
|
|
1014
|
+
tool_output = ToolOutput(
|
|
1015
|
+
errors=[f"{type(e).__name__}: {e}"], metadata=metadata
|
|
1016
|
+
)
|
|
1017
|
+
|
|
1018
|
+
return tool_output
|
|
1019
|
+
|
|
1020
|
+
async def run_custom(
|
|
1021
|
+
self,
|
|
1022
|
+
prompt: str,
|
|
1023
|
+
output_model: Any,
|
|
1024
|
+
with_analysis: bool = False,
|
|
1025
|
+
analyze_template: str | None = None,
|
|
1026
|
+
output_lang: str | None = None,
|
|
1027
|
+
temperature: float | None = None,
|
|
1028
|
+
logprobs: bool | None = None,
|
|
1029
|
+
top_logprobs: int = 3,
|
|
1030
|
+
validator: Callable[[Any], bool] | None = None,
|
|
1031
|
+
max_validation_retries: int | None = None,
|
|
1032
|
+
priority: int | None = None,
|
|
1033
|
+
) -> ToolOutput:
|
|
1034
|
+
"""
|
|
1035
|
+
Custom tool that can do almost anything!
|
|
1036
|
+
|
|
1037
|
+
Arguments:
|
|
1038
|
+
prompt: The user prompt
|
|
1039
|
+
output_model: Pydantic BaseModel used for structured output
|
|
1040
|
+
with_analysis: Whether to include detailed reasoning analysis
|
|
1041
|
+
analyze_template: The analyze template used for reasoning analysis
|
|
1042
|
+
output_lang: Language for the output
|
|
1043
|
+
temperature: Controls randomness (0.0 - 2.0)
|
|
1044
|
+
logprobs: Whether to return token probability information
|
|
1045
|
+
top_logprobs: Number of top token alternatives to return if logprobs enabled
|
|
1046
|
+
validator: Custom validation function to validate the output
|
|
1047
|
+
max_validation_retries: Maximum number of retry attempts if validation fails
|
|
1048
|
+
priority: Task execution priority (if enabled by vLLM and the model)
|
|
1049
|
+
|
|
1050
|
+
Returns:
|
|
1051
|
+
ToolOutput
|
|
1052
|
+
"""
|
|
1053
|
+
tool_name = sys._getframe().f_code.co_name
|
|
1054
|
+
start = perf_counter()
|
|
1055
|
+
|
|
1056
|
+
try:
|
|
1057
|
+
operator_output = await self._operator.run(
|
|
1058
|
+
# User paramaeters
|
|
1059
|
+
text=prompt,
|
|
1060
|
+
output_model=output_model,
|
|
1061
|
+
with_analysis=with_analysis,
|
|
1062
|
+
analyze_template=analyze_template,
|
|
1063
|
+
output_model_str=output_model.model_json_schema(),
|
|
1064
|
+
output_lang=output_lang,
|
|
1065
|
+
temperature=temperature,
|
|
1066
|
+
logprobs=logprobs,
|
|
1067
|
+
top_logprobs=top_logprobs,
|
|
1068
|
+
validator=validator,
|
|
1069
|
+
max_validation_retries=max_validation_retries,
|
|
1070
|
+
priority=priority,
|
|
1071
|
+
# Internal parameters
|
|
1072
|
+
tool_name=tool_name,
|
|
1073
|
+
user_prompt=None,
|
|
1074
|
+
mode=None,
|
|
1075
|
+
)
|
|
1076
|
+
|
|
1077
|
+
metadata = ToolOutputMetadata(
|
|
1078
|
+
tool_name=tool_name, execution_time=perf_counter() - start
|
|
1079
|
+
)
|
|
1080
|
+
tool_output = ToolOutput(
|
|
1081
|
+
result=operator_output.result,
|
|
1082
|
+
logprobs=operator_output.logprobs,
|
|
1083
|
+
analysis=operator_output.analysis,
|
|
1084
|
+
metadata=metadata,
|
|
1085
|
+
)
|
|
1086
|
+
|
|
1087
|
+
except (PromptError, LLMError, ValidationError, TextToolsError, Exception) as e:
|
|
1088
|
+
metadata = ToolOutputMetadata(tool_name=tool_name)
|
|
1089
|
+
tool_output = ToolOutput(
|
|
1090
|
+
errors=[f"{type(e).__name__}: {e}"], metadata=metadata
|
|
1091
|
+
)
|
|
1092
|
+
|
|
1093
|
+
return tool_output
|