hamtaa-texttools 2.1.0__py3-none-any.whl → 2.3.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.
@@ -0,0 +1,688 @@
1
+ import asyncio
2
+ from typing import Any, Callable, Literal
3
+
4
+ from openai import AsyncOpenAI
5
+
6
+ from ..models import CategoryTree, ToolOutput
7
+ from .async_tools import AsyncTheTool
8
+
9
+
10
+ class BatchTheTool:
11
+ def __init__(
12
+ self,
13
+ client: AsyncOpenAI,
14
+ model: str,
15
+ raise_on_error: bool = True,
16
+ max_concurrency: int = 5,
17
+ ):
18
+ self.tool = AsyncTheTool(client, model, raise_on_error)
19
+ self.semaphore = asyncio.Semaphore(max_concurrency)
20
+
21
+ async def categorize(
22
+ self,
23
+ texts: list[str],
24
+ categories: list[str] | CategoryTree,
25
+ with_analysis: bool = False,
26
+ user_prompt: str | None = None,
27
+ temperature: float | None = 0.0,
28
+ logprobs: bool = False,
29
+ top_logprobs: int = 3,
30
+ validator: Callable[[Any], bool] | None = None,
31
+ max_validation_retries: int | None = None,
32
+ priority: int | None = None,
33
+ timeout: float | None = None,
34
+ ) -> list[ToolOutput]:
35
+ """
36
+ Classify texts into given categories
37
+
38
+ Arguments:
39
+ texts: The input texts
40
+ categories: The category list / category tree
41
+ with_analysis: Adds a reasoning step before generating the final output. Note: This doubles token usage per call
42
+ user_prompt: Additional instructions
43
+ temperature: Controls randomness
44
+ logprobs: Whether to return token probability information
45
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
46
+ validator: Custom validation function to validate the output
47
+ max_validation_retries: Maximum number of retry attempts if validation fails
48
+ priority: Task execution priority (if enabled by vLLM and the model)
49
+ timeout: Maximum time in seconds to wait for the response before raising a timeout error
50
+
51
+ Returns:
52
+ list[ToolOutput]
53
+ """
54
+
55
+ async def _throttled_task(text: str) -> ToolOutput:
56
+ async with self.semaphore:
57
+ return await self.tool.categorize(
58
+ text=text,
59
+ categories=categories,
60
+ with_analysis=with_analysis,
61
+ user_prompt=user_prompt,
62
+ temperature=temperature,
63
+ logprobs=logprobs,
64
+ top_logprobs=top_logprobs,
65
+ validator=validator,
66
+ max_validation_retries=max_validation_retries,
67
+ priority=priority,
68
+ timeout=timeout,
69
+ )
70
+
71
+ tasks = [_throttled_task(t) for t in texts]
72
+ return await asyncio.gather(*tasks)
73
+
74
+ async def extract_keywords(
75
+ self,
76
+ texts: list[str],
77
+ mode: Literal["auto", "threshold", "count"],
78
+ number_of_keywords: int | None = None,
79
+ with_analysis: bool = False,
80
+ output_lang: str | None = None,
81
+ user_prompt: str | None = None,
82
+ temperature: float | None = 0.0,
83
+ logprobs: bool = False,
84
+ top_logprobs: int = 3,
85
+ validator: Callable[[Any], bool] | None = None,
86
+ max_validation_retries: int | None = None,
87
+ priority: int | None = None,
88
+ timeout: float | None = None,
89
+ ) -> list[ToolOutput]:
90
+ """
91
+ Extract keywords from the texts
92
+
93
+ Arguments:
94
+ texts: The input texts
95
+ mode: auto -> decide n of keywords automatically, threshold -> decide n of keywords by a threshold, count -> takes number of keywords as the parameter
96
+ number_of_keywords: Must be set only when using "count" mode
97
+ with_analysis: Adds a reasoning step before generating the final output. Note: This doubles token usage per call
98
+ output_lang: Forces the model to respond in a specific language
99
+ user_prompt: Additional instructions
100
+ temperature: Controls randomness
101
+ logprobs: Whether to return token probability information
102
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
103
+ validator: Custom validation function to validate the output
104
+ max_validation_retries: Maximum number of retry attempts if validation fails
105
+ priority: Task execution priority (if enabled by vLLM and the model)
106
+ timeout: Maximum time in seconds to wait for the response before raising a timeout error
107
+
108
+ Returns:
109
+ list[ToolOutput]
110
+ """
111
+
112
+ async def _throttled_task(text: str) -> ToolOutput:
113
+ async with self.semaphore:
114
+ return await self.tool.extract_keywords(
115
+ text=text,
116
+ mode=mode,
117
+ number_of_keywords=number_of_keywords,
118
+ with_analysis=with_analysis,
119
+ output_lang=output_lang,
120
+ user_prompt=user_prompt,
121
+ temperature=temperature,
122
+ logprobs=logprobs,
123
+ top_logprobs=top_logprobs,
124
+ validator=validator,
125
+ max_validation_retries=max_validation_retries,
126
+ priority=priority,
127
+ timeout=timeout,
128
+ )
129
+
130
+ tasks = [_throttled_task(t) for t in texts]
131
+ return await asyncio.gather(*tasks)
132
+
133
+ async def extract_entities(
134
+ self,
135
+ texts: list[str],
136
+ entities: list[str] = ["all named entities"],
137
+ with_analysis: bool = False,
138
+ output_lang: str | None = None,
139
+ user_prompt: str | None = None,
140
+ temperature: float | None = 0.0,
141
+ logprobs: bool = False,
142
+ top_logprobs: int = 3,
143
+ validator: Callable[[Any], bool] | None = None,
144
+ max_validation_retries: int | None = None,
145
+ priority: int | None = None,
146
+ timeout: float | None = None,
147
+ ) -> list[ToolOutput]:
148
+ """
149
+ Perform Named Entity Recognition (NER) on texts
150
+
151
+ Arguments:
152
+ texts: The input texts
153
+ entities: List of entities
154
+ with_analysis: Adds a reasoning step before generating the final output. Note: This doubles token usage per call
155
+ output_lang: Forces the model to respond in a specific language
156
+ user_prompt: Additional instructions
157
+ temperature: Controls randomness
158
+ logprobs: Whether to return token probability information
159
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
160
+ validator: Custom validation function to validate the output
161
+ max_validation_retries: Maximum number of retry attempts if validation fails
162
+ priority: Task execution priority (if enabled by vLLM and the model)
163
+ timeout: Maximum time in seconds to wait for the response before raising a timeout error
164
+
165
+ Returns:
166
+ list[ToolOutput]
167
+ """
168
+
169
+ async def _throttled_task(text: str) -> ToolOutput:
170
+ async with self.semaphore:
171
+ return await self.tool.extract_entities(
172
+ text=text,
173
+ entities=entities,
174
+ with_analysis=with_analysis,
175
+ output_lang=output_lang,
176
+ user_prompt=user_prompt,
177
+ temperature=temperature,
178
+ logprobs=logprobs,
179
+ top_logprobs=top_logprobs,
180
+ validator=validator,
181
+ max_validation_retries=max_validation_retries,
182
+ priority=priority,
183
+ timeout=timeout,
184
+ )
185
+
186
+ tasks = [_throttled_task(t) for t in texts]
187
+ return await asyncio.gather(*tasks)
188
+
189
+ async def is_question(
190
+ self,
191
+ texts: list[str],
192
+ with_analysis: bool = False,
193
+ user_prompt: str | None = None,
194
+ temperature: float | None = 0.0,
195
+ logprobs: bool = False,
196
+ top_logprobs: int = 3,
197
+ validator: Callable[[Any], bool] | None = None,
198
+ max_validation_retries: int | None = None,
199
+ priority: int | None = None,
200
+ timeout: float | None = None,
201
+ ) -> list[ToolOutput]:
202
+ """
203
+ Detect if the inputs are phrased as questions.
204
+
205
+ Arguments:
206
+ texts: The input texts
207
+ with_analysis: Adds a reasoning step before generating the final output. Note: This doubles token usage per call
208
+ user_prompt: Additional instructions
209
+ temperature: Controls randomness
210
+ logprobs: Whether to return token probability information
211
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
212
+ validator: Custom validation function to validate the output
213
+ max_validation_retries: Maximum number of retry attempts if validation fails
214
+ priority: Task execution priority (if enabled by vLLM and the model)
215
+ timeout: Maximum time in seconds to wait for the response before raising a timeout error
216
+
217
+ Returns:
218
+ list[ToolOutput]
219
+ """
220
+
221
+ async def _throttled_task(text: str) -> ToolOutput:
222
+ async with self.semaphore:
223
+ return await self.tool.is_question(
224
+ text=text,
225
+ with_analysis=with_analysis,
226
+ user_prompt=user_prompt,
227
+ temperature=temperature,
228
+ logprobs=logprobs,
229
+ top_logprobs=top_logprobs,
230
+ validator=validator,
231
+ max_validation_retries=max_validation_retries,
232
+ priority=priority,
233
+ timeout=timeout,
234
+ )
235
+
236
+ tasks = [_throttled_task(t) for t in texts]
237
+ return await asyncio.gather(*tasks)
238
+
239
+ async def to_question(
240
+ self,
241
+ texts: list[str],
242
+ number_of_questions: int,
243
+ mode: Literal["from_text", "from_subject"],
244
+ with_analysis: bool = False,
245
+ output_lang: str | None = None,
246
+ user_prompt: str | None = None,
247
+ temperature: float | None = 0.0,
248
+ logprobs: bool = False,
249
+ top_logprobs: int = 3,
250
+ validator: Callable[[Any], bool] | None = None,
251
+ max_validation_retries: int | None = None,
252
+ priority: int | None = None,
253
+ timeout: float | None = None,
254
+ ) -> list[ToolOutput]:
255
+ """
256
+ Generate questions from the given texts / subjects
257
+
258
+ Arguments:
259
+ texts: The input texts
260
+ mode: from_text -> generate questions from an answer, from_subject -> generate questions from a subject
261
+ number_of_questions: Number of questions to generate
262
+ with_analysis: Adds a reasoning step before generating the final output. Note: This doubles token usage per call
263
+ output_lang: Forces the model to respond in a specific language
264
+ user_prompt: Additional instructions
265
+ temperature: Controls randomness
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
+ timeout: Maximum time in seconds to wait for the response before raising a timeout error
272
+
273
+ Returns:
274
+ list[ToolOutput]
275
+ """
276
+
277
+ async def _throttled_task(text: str) -> ToolOutput:
278
+ async with self.semaphore:
279
+ return await self.tool.to_question(
280
+ text=text,
281
+ number_of_questions=number_of_questions,
282
+ mode=mode,
283
+ with_analysis=with_analysis,
284
+ output_lang=output_lang,
285
+ user_prompt=user_prompt,
286
+ temperature=temperature,
287
+ logprobs=logprobs,
288
+ top_logprobs=top_logprobs,
289
+ validator=validator,
290
+ max_validation_retries=max_validation_retries,
291
+ priority=priority,
292
+ timeout=timeout,
293
+ )
294
+
295
+ tasks = [_throttled_task(t) for t in texts]
296
+ return await asyncio.gather(*tasks)
297
+
298
+ async def merge_questions(
299
+ self,
300
+ texts_list: list[list[str]],
301
+ mode: Literal["simple", "stepwise"],
302
+ with_analysis: bool = False,
303
+ output_lang: str | None = None,
304
+ user_prompt: str | None = None,
305
+ temperature: float | None = 0.0,
306
+ logprobs: bool = False,
307
+ top_logprobs: int = 3,
308
+ validator: Callable[[Any], bool] | None = None,
309
+ max_validation_retries: int | None = None,
310
+ priority: int | None = None,
311
+ timeout: float | None = None,
312
+ ) -> list[ToolOutput]:
313
+ """
314
+ Merge multiple questions into a single unified question for each group
315
+
316
+ Arguments:
317
+ texts_list: List of groups of questions to merge
318
+ mode: simple -> regular question merging, stepwise -> merge questions in two steps
319
+ with_analysis: Adds a reasoning step before generating the final output. Note: This doubles token usage per call
320
+ output_lang: Forces the model to respond in a specific language
321
+ user_prompt: Additional instructions
322
+ temperature: Controls randomness
323
+ logprobs: Whether to return token probability information
324
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
325
+ validator: Custom validation function to validate the output
326
+ max_validation_retries: Maximum number of retry attempts if validation fails
327
+ priority: Task execution priority (if enabled by vLLM and the model)
328
+ timeout: Maximum time in seconds to wait for the response before raising a timeout error
329
+
330
+ Returns:
331
+ list[ToolOutput]
332
+ """
333
+
334
+ async def _throttled_task(texts: list[str]) -> ToolOutput:
335
+ async with self.semaphore:
336
+ return await self.tool.merge_questions(
337
+ text=texts,
338
+ mode=mode,
339
+ with_analysis=with_analysis,
340
+ output_lang=output_lang,
341
+ user_prompt=user_prompt,
342
+ temperature=temperature,
343
+ logprobs=logprobs,
344
+ top_logprobs=top_logprobs,
345
+ validator=validator,
346
+ max_validation_retries=max_validation_retries,
347
+ priority=priority,
348
+ timeout=timeout,
349
+ )
350
+
351
+ tasks = [_throttled_task(t) for t in texts_list]
352
+ return await asyncio.gather(*tasks)
353
+
354
+ async def augment(
355
+ self,
356
+ texts: list[str],
357
+ mode: Literal["positive", "negative", "hard_negative"],
358
+ with_analysis: bool = False,
359
+ output_lang: str | None = None,
360
+ user_prompt: str | None = None,
361
+ temperature: float | None = 0.0,
362
+ logprobs: bool = False,
363
+ top_logprobs: int = 3,
364
+ validator: Callable[[Any], bool] | None = None,
365
+ max_validation_retries: int | None = None,
366
+ priority: int | None = None,
367
+ timeout: float | None = None,
368
+ ) -> list[ToolOutput]:
369
+ """
370
+ Rewrite texts in different augmentations
371
+
372
+ Arguments:
373
+ texts: The input texts
374
+ mode: positive -> positive augmentation, negative -> negative augmentation, hard_negative -> hard negative augmentation
375
+ with_analysis: Adds a reasoning step before generating the final output. Note: This doubles token usage per call
376
+ output_lang: Forces the model to respond in a specific language
377
+ user_prompt: Additional instructions
378
+ temperature: Controls randomness
379
+ logprobs: Whether to return token probability information
380
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
381
+ validator: Custom validation function to validate the output
382
+ max_validation_retries: Maximum number of retry attempts if validation fails
383
+ priority: Task execution priority (if enabled by vLLM and the model)
384
+ timeout: Maximum time in seconds to wait for the response before raising a timeout error
385
+
386
+ Returns:
387
+ list[ToolOutput]
388
+ """
389
+
390
+ async def _throttled_task(text: str) -> ToolOutput:
391
+ async with self.semaphore:
392
+ return await self.tool.augment(
393
+ text=text,
394
+ mode=mode,
395
+ with_analysis=with_analysis,
396
+ output_lang=output_lang,
397
+ user_prompt=user_prompt,
398
+ temperature=temperature,
399
+ logprobs=logprobs,
400
+ top_logprobs=top_logprobs,
401
+ validator=validator,
402
+ max_validation_retries=max_validation_retries,
403
+ priority=priority,
404
+ timeout=timeout,
405
+ )
406
+
407
+ tasks = [_throttled_task(t) for t in texts]
408
+ return await asyncio.gather(*tasks)
409
+
410
+ async def summarize(
411
+ self,
412
+ texts: list[str],
413
+ with_analysis: bool = False,
414
+ output_lang: str | None = None,
415
+ user_prompt: str | None = None,
416
+ temperature: float | None = 0.0,
417
+ logprobs: bool = False,
418
+ top_logprobs: int = 3,
419
+ validator: Callable[[Any], bool] | None = None,
420
+ max_validation_retries: int | None = None,
421
+ priority: int | None = None,
422
+ timeout: float | None = None,
423
+ ) -> list[ToolOutput]:
424
+ """
425
+ Summarize the given texts
426
+
427
+ Arguments:
428
+ texts: The input texts
429
+ with_analysis: Adds a reasoning step before generating the final output. Note: This doubles token usage per call
430
+ output_lang: Forces the model to respond in a specific language
431
+ user_prompt: Additional instructions
432
+ temperature: Controls randomness
433
+ logprobs: Whether to return token probability information
434
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
435
+ validator: Custom validation function to validate the output
436
+ max_validation_retries: Maximum number of retry attempts if validation fails
437
+ priority: Task execution priority (if enabled by vLLM and the model)
438
+ timeout: Maximum time in seconds to wait for the response before raising a timeout error
439
+
440
+ Returns:
441
+ list[ToolOutput]
442
+ """
443
+
444
+ async def _throttled_task(text: str) -> ToolOutput:
445
+ async with self.semaphore:
446
+ return await self.tool.summarize(
447
+ text=text,
448
+ with_analysis=with_analysis,
449
+ output_lang=output_lang,
450
+ user_prompt=user_prompt,
451
+ temperature=temperature,
452
+ logprobs=logprobs,
453
+ top_logprobs=top_logprobs,
454
+ validator=validator,
455
+ max_validation_retries=max_validation_retries,
456
+ priority=priority,
457
+ timeout=timeout,
458
+ )
459
+
460
+ tasks = [_throttled_task(t) for t in texts]
461
+ return await asyncio.gather(*tasks)
462
+
463
+ async def translate(
464
+ self,
465
+ texts: list[str],
466
+ target_lang: str,
467
+ use_chunker: bool = True,
468
+ with_analysis: bool = False,
469
+ user_prompt: str | None = None,
470
+ temperature: float | None = 0.0,
471
+ logprobs: bool = False,
472
+ top_logprobs: int = 3,
473
+ validator: Callable[[Any], bool] | None = None,
474
+ max_validation_retries: int | None = None,
475
+ priority: int | None = None,
476
+ timeout: float | None = None,
477
+ ) -> list[ToolOutput]:
478
+ """
479
+ Translate texts between languages
480
+
481
+ Important Note: This tool is EXPERIMENTAL, you can use it but it isn't reliable.
482
+
483
+ Arguments:
484
+ texts: The input texts
485
+ target_lang: The target language for translation
486
+ use_chunker: Whether to use text chunker for large texts
487
+ with_analysis: Adds a reasoning step before generating the final output. Note: This doubles token usage per call
488
+ user_prompt: Additional instructions
489
+ temperature: Controls randomness
490
+ logprobs: Whether to return token probability information
491
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
492
+ validator: Custom validation function to validate the output
493
+ max_validation_retries: Maximum number of retry attempts if validation fails
494
+ priority: Task execution priority (if enabled by vLLM and the model)
495
+ timeout: Maximum time in seconds to wait for the response before raising a timeout error
496
+
497
+ Returns:
498
+ list[ToolOutput]
499
+ """
500
+
501
+ async def _throttled_task(text: str) -> ToolOutput:
502
+ async with self.semaphore:
503
+ return await self.tool.translate(
504
+ text=text,
505
+ target_lang=target_lang,
506
+ use_chunker=use_chunker,
507
+ with_analysis=with_analysis,
508
+ user_prompt=user_prompt,
509
+ temperature=temperature,
510
+ logprobs=logprobs,
511
+ top_logprobs=top_logprobs,
512
+ validator=validator,
513
+ max_validation_retries=max_validation_retries,
514
+ priority=priority,
515
+ timeout=timeout,
516
+ )
517
+
518
+ tasks = [_throttled_task(t) for t in texts]
519
+ return await asyncio.gather(*tasks)
520
+
521
+ async def propositionize(
522
+ self,
523
+ texts: list[str],
524
+ with_analysis: bool = False,
525
+ output_lang: str | None = None,
526
+ user_prompt: str | None = None,
527
+ temperature: float | None = 0.0,
528
+ logprobs: bool = False,
529
+ top_logprobs: int = 3,
530
+ validator: Callable[[Any], bool] | None = None,
531
+ max_validation_retries: int | None = None,
532
+ priority: int | None = None,
533
+ timeout: float | None = None,
534
+ ) -> list[ToolOutput]:
535
+ """
536
+ Convert texts into atomic, independent, meaningful sentences
537
+
538
+ Important Note: This tool is EXPERIMENTAL, you can use it but it isn't reliable.
539
+
540
+ Arguments:
541
+ texts: The input texts
542
+ with_analysis: Adds a reasoning step before generating the final output. Note: This doubles token usage per call
543
+ output_lang: Forces the model to respond in a specific language
544
+ user_prompt: Additional instructions
545
+ temperature: Controls randomness
546
+ logprobs: Whether to return token probability information
547
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
548
+ validator: Custom validation function to validate the output
549
+ max_validation_retries: Maximum number of retry attempts if validation fails
550
+ priority: Task execution priority (if enabled by vLLM and the model)
551
+ timeout: Maximum time in seconds to wait for the response before raising a timeout error
552
+
553
+ Returns:
554
+ list[ToolOutput]
555
+ """
556
+
557
+ async def _throttled_task(text: str) -> ToolOutput:
558
+ async with self.semaphore:
559
+ return await self.tool.propositionize(
560
+ text=text,
561
+ with_analysis=with_analysis,
562
+ output_lang=output_lang,
563
+ user_prompt=user_prompt,
564
+ temperature=temperature,
565
+ logprobs=logprobs,
566
+ top_logprobs=top_logprobs,
567
+ validator=validator,
568
+ max_validation_retries=max_validation_retries,
569
+ priority=priority,
570
+ timeout=timeout,
571
+ )
572
+
573
+ tasks = [_throttled_task(t) for t in texts]
574
+ return await asyncio.gather(*tasks)
575
+
576
+ async def is_fact(
577
+ self,
578
+ texts: list[str],
579
+ source_texts: list[str],
580
+ with_analysis: bool = False,
581
+ output_lang: str | None = None,
582
+ user_prompt: str | None = None,
583
+ temperature: float | None = 0.0,
584
+ logprobs: bool = False,
585
+ top_logprobs: int = 3,
586
+ validator: Callable[[Any], bool] | None = None,
587
+ max_validation_retries: int | None = None,
588
+ priority: int | None = None,
589
+ timeout: float | None = None,
590
+ ) -> list[ToolOutput]:
591
+ """
592
+ Check whether statements are facts based on source texts
593
+
594
+ Important Note: This tool is EXPERIMENTAL, you can use it but it isn't reliable.
595
+
596
+ Arguments:
597
+ texts: The input texts (statements to check)
598
+ source_texts: The source texts
599
+ with_analysis: Adds a reasoning step before generating the final output. Note: This doubles token usage per call
600
+ output_lang: Forces the model to respond in a specific language
601
+ user_prompt: Additional instructions
602
+ temperature: Controls randomness
603
+ logprobs: Whether to return token probability information
604
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
605
+ validator: Custom validation function to validate the output
606
+ max_validation_retries: Maximum number of retry attempts if validation fails
607
+ priority: Task execution priority (if enabled by vLLM and the model)
608
+ timeout: Maximum time in seconds to wait for the response before raising a timeout error
609
+
610
+ Returns:
611
+ list[ToolOutput]
612
+ """
613
+
614
+ async def _throttled_task(text: str, source_text: str) -> ToolOutput:
615
+ async with self.semaphore:
616
+ return await self.tool.is_fact(
617
+ text=text,
618
+ source_text=source_text,
619
+ with_analysis=with_analysis,
620
+ output_lang=output_lang,
621
+ user_prompt=user_prompt,
622
+ temperature=temperature,
623
+ logprobs=logprobs,
624
+ top_logprobs=top_logprobs,
625
+ validator=validator,
626
+ max_validation_retries=max_validation_retries,
627
+ priority=priority,
628
+ timeout=timeout,
629
+ )
630
+
631
+ tasks = [_throttled_task(t, s) for t, s in zip(texts, source_texts)]
632
+ return await asyncio.gather(*tasks)
633
+
634
+ async def run_custom(
635
+ self,
636
+ prompts: list[str],
637
+ output_model: Any,
638
+ with_analysis: bool = False,
639
+ analyze_template: str | None = None,
640
+ output_lang: str | None = None,
641
+ temperature: float | None = None,
642
+ logprobs: bool | None = None,
643
+ top_logprobs: int = 3,
644
+ validator: Callable[[Any], bool] | None = None,
645
+ max_validation_retries: int | None = None,
646
+ priority: int | None = None,
647
+ timeout: float | None = None,
648
+ ) -> list[ToolOutput]:
649
+ """
650
+ Custom tool that can do almost anything for multiple prompts
651
+
652
+ Arguments:
653
+ prompts: The user prompts
654
+ output_model: Pydantic BaseModel used for structured output
655
+ with_analysis: Adds a reasoning step before generating the final output. Note: This doubles token usage per call
656
+ analyze_template: The analyze template used for reasoning analysis
657
+ output_lang: Forces the model to respond in a specific language
658
+ temperature: Controls randomness
659
+ logprobs: Whether to return token probability information
660
+ top_logprobs: Number of top token alternatives to return if logprobs enabled
661
+ validator: Custom validation function to validate the output
662
+ max_validation_retries: Maximum number of retry attempts if validation fails
663
+ priority: Task execution priority (if enabled by vLLM and the model)
664
+ timeout: Maximum time in seconds to wait for the response before raising a timeout error
665
+
666
+ Returns:
667
+ list[ToolOutput]
668
+ """
669
+
670
+ async def _throttled_task(prompt: str) -> ToolOutput:
671
+ async with self.semaphore:
672
+ return await self.tool.run_custom(
673
+ prompt=prompt,
674
+ output_model=output_model,
675
+ with_analysis=with_analysis,
676
+ analyze_template=analyze_template,
677
+ output_lang=output_lang,
678
+ temperature=temperature,
679
+ logprobs=logprobs,
680
+ top_logprobs=top_logprobs,
681
+ validator=validator,
682
+ max_validation_retries=max_validation_retries,
683
+ priority=priority,
684
+ timeout=timeout,
685
+ )
686
+
687
+ tasks = [_throttled_task(p) for p in prompts]
688
+ return await asyncio.gather(*tasks)