hamtaa-texttools 1.0.5__py3-none-any.whl → 1.1.16__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 (43) hide show
  1. hamtaa_texttools-1.1.16.dist-info/METADATA +255 -0
  2. hamtaa_texttools-1.1.16.dist-info/RECORD +31 -0
  3. texttools/__init__.py +6 -8
  4. texttools/batch/batch_config.py +26 -0
  5. texttools/batch/batch_runner.py +144 -139
  6. texttools/batch/{batch_manager.py → internals/batch_manager.py} +42 -54
  7. texttools/batch/internals/utils.py +16 -0
  8. texttools/prompts/README.md +8 -4
  9. texttools/prompts/categorize.yaml +77 -0
  10. texttools/prompts/detect_entity.yaml +22 -0
  11. texttools/prompts/extract_keywords.yaml +68 -0
  12. texttools/prompts/{question_merger.yaml → merge_questions.yaml} +5 -5
  13. texttools/tools/async_tools.py +804 -0
  14. texttools/tools/internals/async_operator.py +139 -236
  15. texttools/tools/internals/formatters.py +24 -0
  16. texttools/tools/internals/models.py +183 -0
  17. texttools/tools/internals/operator_utils.py +54 -0
  18. texttools/tools/internals/prompt_loader.py +23 -43
  19. texttools/tools/internals/sync_operator.py +201 -0
  20. texttools/tools/sync_tools.py +804 -0
  21. hamtaa_texttools-1.0.5.dist-info/METADATA +0 -192
  22. hamtaa_texttools-1.0.5.dist-info/RECORD +0 -30
  23. texttools/batch/__init__.py +0 -4
  24. texttools/formatters/base_formatter.py +0 -33
  25. texttools/formatters/user_merge_formatter.py +0 -30
  26. texttools/prompts/categorizer.yaml +0 -28
  27. texttools/prompts/keyword_extractor.yaml +0 -18
  28. texttools/tools/__init__.py +0 -4
  29. texttools/tools/async_the_tool.py +0 -277
  30. texttools/tools/internals/operator.py +0 -295
  31. texttools/tools/internals/output_models.py +0 -52
  32. texttools/tools/the_tool.py +0 -501
  33. {hamtaa_texttools-1.0.5.dist-info → hamtaa_texttools-1.1.16.dist-info}/WHEEL +0 -0
  34. {hamtaa_texttools-1.0.5.dist-info → hamtaa_texttools-1.1.16.dist-info}/licenses/LICENSE +0 -0
  35. {hamtaa_texttools-1.0.5.dist-info → hamtaa_texttools-1.1.16.dist-info}/top_level.txt +0 -0
  36. /texttools/prompts/{ner_extractor.yaml → extract_entities.yaml} +0 -0
  37. /texttools/prompts/{question_detector.yaml → is_question.yaml} +0 -0
  38. /texttools/prompts/{rewriter.yaml → rewrite.yaml} +0 -0
  39. /texttools/prompts/{custom_tool.yaml → run_custom.yaml} +0 -0
  40. /texttools/prompts/{subject_question_generator.yaml → subject_to_question.yaml} +0 -0
  41. /texttools/prompts/{summarizer.yaml → summarize.yaml} +0 -0
  42. /texttools/prompts/{question_generator.yaml → text_to_question.yaml} +0 -0
  43. /texttools/prompts/{translator.yaml → translate.yaml} +0 -0
@@ -1,295 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import math
4
- import re
5
- from typing import Any, TypeVar, Type, Literal, Optional
6
- import json
7
- import logging
8
-
9
- from openai import OpenAI
10
- from pydantic import BaseModel
11
-
12
- from texttools.formatters.user_merge_formatter import (
13
- UserMergeFormatter,
14
- )
15
- from texttools.tools.internals.prompt_loader import PromptLoader
16
-
17
- # Base Model type for output models
18
- T = TypeVar("T", bound=BaseModel)
19
-
20
- # Configure logger
21
- logger = logging.getLogger("operator")
22
- logger.setLevel(logging.INFO)
23
-
24
-
25
- class Operator:
26
- """
27
- Core engine for running text-processing operations with an LLM.
28
-
29
- It wires together:
30
- - `PromptLoader` → loads YAML prompt templates.
31
- - `UserMergeFormatter` → applies formatting to messages (e.g., merging).
32
- - OpenAI client → executes completions/parsed completions.
33
-
34
- Workflow inside `run()`:
35
- 1. Load prompt templates (`main_template` [+ `analyze_template` if enabled]).
36
- 2. Optionally generate an "analysis" step via `_analyze()`.
37
- 3. Build messages for the LLM.
38
- 4. Call `.beta.chat.completions.parse()` to parse the result into the
39
- configured `OUTPUT_MODEL` (a Pydantic schema).
40
- 5. Return results as a dict (always `{"result": ...}`, plus `analysis`
41
- if analysis was enabled).
42
-
43
- Attributes configured dynamically by `TheTool`:
44
- - PROMPT_FILE: str → YAML filename
45
- - OUTPUT_MODEL: Pydantic model class
46
- - WITH_ANALYSIS: bool → whether to run an analysis phase first
47
- - USE_MODES: bool → whether to select prompts by mode
48
- - MODE: str → which mode to use if modes are enabled
49
- - RESP_FORMAT: str → "vllm" or "parse"
50
- """
51
-
52
- def __init__(self, client: OpenAI):
53
- self.client: OpenAI = client
54
-
55
- def _build_user_message(self, prompt: str) -> dict[str, str]:
56
- return {"role": "user", "content": prompt}
57
-
58
- def _analysis_completion(
59
- self,
60
- analyze_message: list[dict[str, str]],
61
- model: str,
62
- temperature: float,
63
- ) -> str:
64
- completion = self.client.chat.completions.create(
65
- model=model,
66
- messages=analyze_message,
67
- temperature=temperature,
68
- )
69
- analysis = completion.choices[0].message.content.strip()
70
- return analysis
71
-
72
- def _analyze(
73
- self,
74
- prompt_configs: dict[str, str],
75
- model: str,
76
- temperature: float,
77
- ) -> str:
78
- analyze_prompt = prompt_configs["analyze_template"]
79
- analyze_message = [self._build_user_message(analyze_prompt)]
80
- analysis = self._analysis_completion(analyze_message, model, temperature)
81
- return analysis
82
-
83
- def _parse_completion(
84
- self,
85
- message: list[dict[str, str]],
86
- output_model: Type[T],
87
- model: str,
88
- temperature: float,
89
- logprobs: bool = False,
90
- top_logprobs: int = 3,
91
- ) -> tuple[Type[T], Any]:
92
- request_kwargs = {
93
- "model": model,
94
- "messages": message,
95
- "response_format": output_model,
96
- "temperature": temperature,
97
- }
98
- if logprobs:
99
- request_kwargs["logprobs"] = True
100
- request_kwargs["top_logprobs"] = top_logprobs
101
-
102
- completion = self.client.beta.chat.completions.parse(**request_kwargs)
103
- parsed = completion.choices[0].message.parsed
104
- return parsed, completion
105
-
106
- def _clean_json_response(self, response: str) -> str:
107
- """
108
- Clean JSON response by removing code block markers and whitespace.
109
- Handles cases like:
110
- - ```json{"result": "value"}```
111
- """
112
- stripped = response.strip()
113
- cleaned = re.sub(r"^```(?:json)?\s*", "", stripped)
114
- cleaned = re.sub(r"\s*```$", "", cleaned)
115
-
116
- return cleaned.strip()
117
-
118
- def _convert_to_output_model(
119
- self, response_string: str, output_model: Type[T]
120
- ) -> Type[T]:
121
- """
122
- Convert a JSON response string to output model.
123
-
124
- Args:
125
- response_string: The JSON string (may contain code block markers)
126
- output_model: Your Pydantic output model class (e.g., StrOutput, ListStrOutput)
127
-
128
- Returns:
129
- Instance of your output model
130
- """
131
- # Clean the response string
132
- cleaned_json = self._clean_json_response(response_string)
133
-
134
- # Fix Python-style booleans
135
- cleaned_json = cleaned_json.replace("False", "false").replace("True", "true")
136
-
137
- # Convert string to Python dictionary
138
- response_dict = json.loads(cleaned_json)
139
-
140
- # Convert dictionary to output model
141
- return output_model(**response_dict)
142
-
143
- def _vllm_completion(
144
- self,
145
- message: list[dict[str, str]],
146
- output_model: Type[T],
147
- model: str,
148
- temperature: float,
149
- logprobs: bool = False,
150
- top_logprobs: int = 3,
151
- ) -> tuple[Type[T], Any]:
152
- json_schema = output_model.model_json_schema()
153
-
154
- # Build kwargs dynamically
155
- request_kwargs = {
156
- "model": model,
157
- "messages": message,
158
- "extra_body": {"guided_json": json_schema},
159
- "temperature": temperature,
160
- }
161
-
162
- if logprobs:
163
- request_kwargs["logprobs"] = True
164
- request_kwargs["top_logprobs"] = top_logprobs
165
-
166
- completion = self.client.chat.completions.create(**request_kwargs)
167
- response = completion.choices[0].message.content
168
-
169
- # Convert the string response to output model
170
- parsed = self._convert_to_output_model(response, output_model)
171
- return parsed, completion
172
-
173
- def _extract_logprobs(self, completion: dict):
174
- logprobs_data = []
175
- ignore_pattern = re.compile(r'^(result|[\s\[\]\{\}",:]+)$')
176
-
177
- for choice in completion.choices:
178
- if not getattr(choice, "logprobs", None):
179
- logger.info("No logprobs found.")
180
- continue
181
-
182
- for logprob_item in choice.logprobs.content:
183
- if ignore_pattern.match(logprob_item.token):
184
- continue
185
- token_entry = {
186
- "token": logprob_item.token,
187
- "prob": round(math.exp(logprob_item.logprob), 8),
188
- "top_alternatives": [],
189
- }
190
- for alt in logprob_item.top_logprobs:
191
- if ignore_pattern.match(alt.token):
192
- continue
193
- token_entry["top_alternatives"].append(
194
- {
195
- "token": alt.token,
196
- "prob": round(math.exp(alt.logprob), 8),
197
- }
198
- )
199
- logprobs_data.append(token_entry)
200
-
201
- return logprobs_data
202
-
203
- def run(
204
- self,
205
- text: str,
206
- # User parameters
207
- model: str,
208
- with_analysis: bool,
209
- temperature: float,
210
- logprobs: bool,
211
- top_logprobs: int,
212
- user_prompt: str | None,
213
- output_lang: str | None,
214
- # Each tool's parameters
215
- prompt_file: str,
216
- output_model: Type[T],
217
- resp_format: Literal["vllm", "parse"] = "parse",
218
- mode: str | None = None,
219
- **extra_kwargs,
220
- ) -> dict[str, Any]:
221
- """
222
- Execute the LLM pipeline with the given input text.
223
-
224
- Args:
225
- text: The text to process (will be stripped of whitespace)
226
- **extra_kwargs: Additional variables to inject into prompt templates
227
-
228
- Returns:
229
- Dictionary containing the parsed result and optional analysis
230
- """
231
- prompt_loader = PromptLoader()
232
- formatter = UserMergeFormatter()
233
-
234
- try:
235
- cleaned_text = text.strip()
236
-
237
- prompt_configs = prompt_loader.load(
238
- prompt_file=prompt_file,
239
- text=cleaned_text,
240
- mode=mode,
241
- **extra_kwargs,
242
- )
243
-
244
- messages: list[dict[str, str]] = []
245
-
246
- if with_analysis:
247
- analysis = self._analyze(prompt_configs, model, temperature)
248
- messages.append(
249
- self._build_user_message(f"Based on this analysis: {analysis}")
250
- )
251
-
252
- if output_lang:
253
- messages.append(
254
- self._build_user_message(
255
- f"Respond only in the {output_lang} language."
256
- )
257
- )
258
-
259
- if user_prompt:
260
- messages.append(
261
- self._build_user_message(f"Consider this instruction {user_prompt}")
262
- )
263
-
264
- messages.append(self._build_user_message(prompt_configs["main_template"]))
265
-
266
- messages = formatter.format(messages)
267
-
268
- if resp_format == "vllm":
269
- parsed, completion = self._vllm_completion(
270
- messages, output_model, model, temperature, logprobs, top_logprobs
271
- )
272
- elif resp_format == "parse":
273
- parsed, completion = self._parse_completion(
274
- messages, output_model, model, temperature, logprobs, top_logprobs
275
- )
276
-
277
- # Ensure output_model has a `result` field
278
- if not hasattr(parsed, "result"):
279
- logger.error(
280
- "The provided output_model must define a field named 'result'"
281
- )
282
-
283
- results = {"result": parsed.result}
284
-
285
- if logprobs:
286
- results["logprobs"] = self._extract_logprobs(completion)
287
-
288
- if with_analysis:
289
- results["analysis"] = analysis
290
-
291
- return results
292
-
293
- except Exception as e:
294
- logger.error(f"Operation failed: {e}")
295
- return {"Error": str(e), "result": ""}
@@ -1,52 +0,0 @@
1
- from typing import Literal
2
-
3
- from pydantic import BaseModel, Field
4
-
5
-
6
- class StrOutput(BaseModel):
7
- result: str = Field(..., description="The output string")
8
-
9
-
10
- class BoolOutput(BaseModel):
11
- result: bool = Field(
12
- ..., description="Boolean indicating the output state", example=True
13
- )
14
-
15
-
16
- class ListStrOutput(BaseModel):
17
- result: list[str] = Field(
18
- ..., description="The output list of strings", example=["text_1", "text_2"]
19
- )
20
-
21
-
22
- class ListDictStrStrOutput(BaseModel):
23
- result: list[dict[str, str]] = Field(
24
- ...,
25
- description="List of dictionaries containing string key-value pairs",
26
- example=[{"text": "Mohammad", "type": "PER"}],
27
- )
28
-
29
-
30
- class ReasonListStrOutput(BaseModel):
31
- reason: str = Field(..., description="Thinking process that led to the output")
32
- result: list[str] = Field(..., description="The output list of strings")
33
-
34
-
35
- class CategorizerOutput(BaseModel):
36
- reason: str = Field(
37
- ..., description="Explanation of why the input belongs to the category"
38
- )
39
- result: Literal[
40
- "باورهای دینی",
41
- "اخلاق اسلامی",
42
- "احکام و فقه",
43
- "تاریخ اسلام و شخصیت ها",
44
- "منابع دینی",
45
- "دین و جامعه/سیاست",
46
- "عرفان و معنویت",
47
- "هیچکدام",
48
- ] = Field(
49
- ...,
50
- description="Predicted category label",
51
- example="اخلاق اسلامی",
52
- )