sonika-langchain-bot 0.0.18__tar.gz → 0.0.19__tar.gz
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.
Potentially problematic release.
This version of sonika-langchain-bot might be problematic. Click here for more details.
- {sonika_langchain_bot-0.0.18/src/sonika_langchain_bot.egg-info → sonika_langchain_bot-0.0.19}/PKG-INFO +1 -1
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/setup.py +1 -1
- sonika_langchain_bot-0.0.19/src/sonika_langchain_bot/langchain_clasificator.py +66 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19/src/sonika_langchain_bot.egg-info}/PKG-INFO +1 -1
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/test/test.py +2 -2
- sonika_langchain_bot-0.0.18/src/sonika_langchain_bot/langchain_clasificator.py +0 -30
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/LICENSE +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/README.md +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/setup.cfg +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/src/sonika_langchain_bot/__init__.py +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/src/sonika_langchain_bot/document_processor.py +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/src/sonika_langchain_bot/langchain_bot_agent.py +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/src/sonika_langchain_bot/langchain_class.py +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/src/sonika_langchain_bot/langchain_files.py +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/src/sonika_langchain_bot/langchain_models.py +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/src/sonika_langchain_bot/langchain_tools.py +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/src/sonika_langchain_bot.egg-info/SOURCES.txt +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/src/sonika_langchain_bot.egg-info/dependency_links.txt +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/src/sonika_langchain_bot.egg-info/requires.txt +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/src/sonika_langchain_bot.egg-info/top_level.txt +0 -0
- {sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/test/test_document_processor.py +0 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from pydantic import BaseModel
|
|
2
|
+
from typing import Dict, Any, Type
|
|
3
|
+
from sonika_langchain_bot.langchain_class import ILanguageModel
|
|
4
|
+
|
|
5
|
+
class ClassificationResponse(BaseModel):
|
|
6
|
+
"""Respuesta de clasificación con tokens utilizados"""
|
|
7
|
+
input_tokens: int
|
|
8
|
+
output_tokens: int
|
|
9
|
+
result: Dict[str, Any]
|
|
10
|
+
|
|
11
|
+
class TextClassifier:
|
|
12
|
+
def __init__(self, validation_class: Type[BaseModel], llm: ILanguageModel):
|
|
13
|
+
self.llm = llm
|
|
14
|
+
self.validation_class = validation_class
|
|
15
|
+
# Guardamos ambas versiones del modelo
|
|
16
|
+
self.original_model = self.llm.model # Sin structured output
|
|
17
|
+
self.structured_model = self.llm.model.with_structured_output(validation_class)
|
|
18
|
+
|
|
19
|
+
def classify(self, text: str) -> ClassificationResponse:
|
|
20
|
+
"""
|
|
21
|
+
Clasifica el texto según la clase de validación.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
text: Texto a clasificar
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
ClassificationResponse: Objeto con result, input_tokens y output_tokens
|
|
28
|
+
"""
|
|
29
|
+
prompt = f"""
|
|
30
|
+
Classify the following text based on the properties defined in the validation class.
|
|
31
|
+
|
|
32
|
+
Text: {text}
|
|
33
|
+
|
|
34
|
+
Only extract the properties mentioned in the validation class.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
# Primero invocamos el modelo ORIGINAL para obtener metadata de tokens
|
|
38
|
+
raw_response = self.original_model.invoke(prompt)
|
|
39
|
+
|
|
40
|
+
# Extraer información de tokens del AIMessage original
|
|
41
|
+
input_tokens = 0
|
|
42
|
+
output_tokens = 0
|
|
43
|
+
|
|
44
|
+
if hasattr(raw_response, 'response_metadata'):
|
|
45
|
+
token_usage = raw_response.response_metadata.get('token_usage', {})
|
|
46
|
+
input_tokens = token_usage.get('prompt_tokens', 0)
|
|
47
|
+
output_tokens = token_usage.get('completion_tokens', 0)
|
|
48
|
+
|
|
49
|
+
# Ahora invocamos con structured output para obtener el objeto parseado
|
|
50
|
+
response = self.structured_model.invoke(prompt)
|
|
51
|
+
|
|
52
|
+
# Validar que el response es de la clase correcta
|
|
53
|
+
if isinstance(response, self.validation_class):
|
|
54
|
+
# Crear el resultado dinámicamente basado en los atributos
|
|
55
|
+
result_data = {
|
|
56
|
+
field: getattr(response, field)
|
|
57
|
+
for field in self.validation_class.__fields__.keys()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return ClassificationResponse(
|
|
61
|
+
input_tokens=input_tokens,
|
|
62
|
+
output_tokens=output_tokens,
|
|
63
|
+
result=result_data
|
|
64
|
+
)
|
|
65
|
+
else:
|
|
66
|
+
raise ValueError(f"The response is not of type '{self.validation_class.__name__}'")
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
from pydantic import BaseModel
|
|
2
|
-
from typing import Dict, Any, Type
|
|
3
|
-
from sonika_langchain_bot.langchain_class import ILanguageModel
|
|
4
|
-
|
|
5
|
-
# Clase para realizar la clasificación de texto
|
|
6
|
-
class TextClassifier:
|
|
7
|
-
def __init__(self, validation_class: Type[BaseModel], llm: ILanguageModel):
|
|
8
|
-
self.llm =llm
|
|
9
|
-
self.validation_class = validation_class
|
|
10
|
-
#configuramos el modelo para que tenga una estructura de salida
|
|
11
|
-
self.llm.model = self.llm.model.with_structured_output(validation_class)
|
|
12
|
-
|
|
13
|
-
def classify(self, text: str) -> Dict[str, Any]:
|
|
14
|
-
# Crear el template del prompt
|
|
15
|
-
prompt = f"""
|
|
16
|
-
Classify the following text based on the properties defined in the validation class.
|
|
17
|
-
|
|
18
|
-
Text: {text}
|
|
19
|
-
|
|
20
|
-
Only extract the properties mentioned in the validation class.
|
|
21
|
-
"""
|
|
22
|
-
response = self.llm.invoke(prompt=prompt)
|
|
23
|
-
|
|
24
|
-
# Asegurarse de que el `response` es de la clase de validación proporcionada
|
|
25
|
-
if isinstance(response, self.validation_class):
|
|
26
|
-
# Crear el resultado dinámicamente basado en los atributos de la clase de validación
|
|
27
|
-
result = {field: getattr(response, field) for field in self.validation_class.__fields__.keys()}
|
|
28
|
-
return result
|
|
29
|
-
else:
|
|
30
|
-
raise ValueError(f"The response is not of type '{self.validation_class.__name__}'")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{sonika_langchain_bot-0.0.18 → sonika_langchain_bot-0.0.19}/src/sonika_langchain_bot/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|