ws-bom-robot-app 0.0.101__py3-none-any.whl → 0.0.102__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.
@@ -22,13 +22,9 @@ class AgentLcel:
22
22
  self.embeddings = llm.get_embeddings()
23
23
  self.memory_key: str = "chat_history"
24
24
  self.__llm_with_tools = llm.get_llm().bind_tools(self.__tools) if len(self.__tools) > 0 else llm.get_llm()
25
-
26
- # Configura l'LLM in base alla modalità
27
25
  if self.json_schema:
28
- # JSON mode: usa with_structured_output per output strutturato mantenendo i tool
29
26
  self.__pydantic_schema = self.__create_pydantic_schema()
30
27
  else:
31
- # Agent mode standard
32
28
  self.__pydantic_schema = None
33
29
 
34
30
  self.executor = self.__create_agent()
@@ -68,13 +64,7 @@ class AgentLcel:
68
64
  return create_model('JsonSchema', **fields)
69
65
 
70
66
  def __get_output_parser(self):
71
- """Restituisce il parser appropriato in base alla modalità."""
72
- if self.json_schema and self.__pydantic_schema:
73
- # In modalità JSON, usa with_structured_output direttamente nell'agent
74
- # Il parser qui serve solo per compatibilità, la conversione avviene prima
75
- return self.__llm.get_parser()
76
- else:
77
- return self.__llm.get_parser()
67
+ return self.__llm.get_parser()
78
68
 
79
69
  async def __create_prompt(self, input: dict) -> ChatPromptTemplate:
80
70
  from langchain_core.messages import SystemMessage
@@ -82,9 +72,14 @@ class AgentLcel:
82
72
  rules_prompt = await get_rules(self.embeddings, self.rules, message.content) if self.rules else ""
83
73
  system = default_prompt + (tool_prompt(render_text_description(self.__tools)) if len(self.__tools)>0 else "") + self.sys_message + rules_prompt
84
74
 
85
- # Aggiungere istruzioni per output strutturato se è presente json_schema
75
+ # Aggiungi istruzioni per output JSON strutturato se necessario
86
76
  if self.json_schema and self.__pydantic_schema:
87
- system += f"\n\nYou must respond with a JSON object matching this schema: {self.__pydantic_schema.schema_json()}"
77
+ json_instructions = f"\n\nIMPORTANT: You must format your final response as a JSON object with the following structure:\n"
78
+ for field_name, field_info in self.__pydantic_schema.model_fields.items():
79
+ field_type = field_info.annotation.__name__ if hasattr(field_info.annotation, '__name__') else str(field_info.annotation)
80
+ json_instructions += f"- {field_name}: {field_type}\n"
81
+ json_instructions += "\nProvide ONLY the JSON object in your response, no additional text."
82
+ system += json_instructions
88
83
 
89
84
  messages = [
90
85
  SystemMessage(content=system),
@@ -98,14 +93,15 @@ class AgentLcel:
98
93
  )
99
94
  return prompt
100
95
 
101
- def __create_agent(self) -> AgentExecutor:
102
- agent: Any = (
103
- {
104
- "agent_scratchpad": lambda x: self.__llm.get_formatter(x["intermediate_steps"]),
105
- str(self.memory_key): lambda x: x[self.memory_key],
106
- }
107
- | RunnableLambda(self.__create_prompt)
108
- | self.__llm_with_tools
109
- | self.__get_output_parser()
110
- )
111
- return AgentExecutor(agent=agent,tools=self.__tools,verbose=False)
96
+ def __create_agent(self):
97
+ # Un solo AgentExecutor per entrambe le modalità
98
+ agent = (
99
+ {
100
+ "agent_scratchpad": lambda x: self.__llm.get_formatter(x["intermediate_steps"]),
101
+ self.memory_key: lambda x: x[self.memory_key],
102
+ }
103
+ | RunnableLambda(self.__create_prompt)
104
+ | self.__llm_with_tools
105
+ | self.__get_output_parser()
106
+ )
107
+ return AgentExecutor(agent=agent, tools=self.__tools, verbose=False)
@@ -1,5 +1,5 @@
1
1
  from asyncio import Queue
2
- import aiohttp
2
+ import aiohttp, re
3
3
  from typing import Optional, Type, Callable
4
4
  from ws_bom_robot_app.config import config
5
5
  from ws_bom_robot_app.llm.models.api import LlmApp,LlmAppTool
@@ -200,10 +200,21 @@ class ToolManager:
200
200
  else: # default
201
201
  try:
202
202
  from ws_bom_robot_app.llm.main import stream
203
+ import json
203
204
  chunks = []
204
205
  async for chunk in stream(rq=app.rq, ctx=None, formatted=False):
205
206
  chunks.append(chunk)
206
207
  rs = ''.join(chunks) if chunks else None
208
+
209
+ # if the app has output_structure, parse the JSON and return dict
210
+ if rs and app.rq.output_structure:
211
+ try:
212
+ cleaned_rs = re.sub(r'^```(?:json)?\s*\n?', '', rs.strip())
213
+ cleaned_rs = re.sub(r'\n?```\s*$', '', cleaned_rs)
214
+ return json.loads(cleaned_rs)
215
+ except json.JSONDecodeError:
216
+ print(f"[!] Failed to parse JSON output from proxy_app_chat: {rs}")
217
+ return rs
207
218
  return rs
208
219
  except Exception as e:
209
220
  print(f"[!] Error in proxy_app_chat: {e}")
@@ -37,6 +37,14 @@ async def get_apps() -> list[CmsApp]:
37
37
  if obj is None:
38
38
  break
39
39
  return obj
40
+ def __to_dict(obj):
41
+ """Converts DictObject to dict recursively"""
42
+ if isinstance(obj, DictObject):
43
+ return {k: __to_dict(v) for k, v in obj.__dict__.items()}
44
+ elif isinstance(obj, list):
45
+ return [__to_dict(item) for item in obj]
46
+ else:
47
+ return obj
40
48
  host = config.robot_cms_host
41
49
  if host:
42
50
  url = f"{host}/api/llmApp?depth=1&pagination=false&locale=it"
@@ -77,7 +85,8 @@ async def get_apps() -> list[CmsApp]:
77
85
  ) if __attr(_cms_app_dict.settings,'rules','vectorDbFile','filename') else None,
78
86
  #fine_tuned_model=__attr(_cms_app_dict.settings,'llmConfig','fineTunedModel'),
79
87
  lang_chain_tracing= __attr(_cms_app_dict.settings,'llmConfig','langChainTracing', default=False),
80
- lang_chain_project= __attr(_cms_app_dict.settings,'llmConfig','langChainProject', default='')
88
+ lang_chain_project= __attr(_cms_app_dict.settings,'llmConfig','langChainProject', default=''),
89
+ output_structure= __to_dict(__attr(_cms_app_dict.settings,'llmConfig','outputStructure')) if __attr(_cms_app_dict.settings,'llmConfig','outputStructure') else None
81
90
  ))
82
91
  except Exception as e:
83
92
  import traceback
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ws_bom_robot_app
3
- Version: 0.0.101
3
+ Version: 0.0.102
4
4
  Summary: A FastAPI application serving ws bom/robot/llm platform ai.
5
5
  Home-page: https://github.com/websolutespa/bom
6
6
  Author: Websolute Spa
@@ -10,7 +10,7 @@ ws_bom_robot_app/llm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hS
10
10
  ws_bom_robot_app/llm/agent_context.py,sha256=uatHJ8wcRly6h0S762BgfzDMpmcwCHwNzwo37aWjeE0,1305
11
11
  ws_bom_robot_app/llm/agent_description.py,sha256=5IP0qFSJvaE3zjGS7f0W1DuiegP0RHXRMBoDC5pCofA,4779
12
12
  ws_bom_robot_app/llm/agent_handler.py,sha256=HAg3qmj-QY_k7P-GfAKna1tKdmZaTHrZbNWJc0eol3A,7858
13
- ws_bom_robot_app/llm/agent_lcel.py,sha256=4eOEXCgPX3p6IE9CmfaAAsPJNeGvWA_xF9OZV03Kxo0,4741
13
+ ws_bom_robot_app/llm/agent_lcel.py,sha256=Bkz3JAAR_NibV27H9zfHlleklps93J0mQL53enFoqRo,4694
14
14
  ws_bom_robot_app/llm/api.py,sha256=jMoiKiD5HNxGu6gTb5_qZ5UU8d2uJ7UVrdLseDStI6o,7634
15
15
  ws_bom_robot_app/llm/defaut_prompt.py,sha256=D9dn8yPveu0bVwGM1wQWLYftmBs5O76o0R_caLLll8w,1121
16
16
  ws_bom_robot_app/llm/evaluator.py,sha256=tUyPX1oGZEjSiO4JixwNlgv6BI9cUHSmcAsTCpBnIn4,13322
@@ -27,7 +27,7 @@ ws_bom_robot_app/llm/providers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5
27
27
  ws_bom_robot_app/llm/providers/llm_manager.py,sha256=oVeEmZUnR1ysV-BI_zpwQ-gpXqmhSzjKFQQAHtaFGSI,16596
28
28
  ws_bom_robot_app/llm/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
29
29
  ws_bom_robot_app/llm/tools/tool_builder.py,sha256=CtZwJ94aj0YGA3yVWkyCUxNE7WgU2zWjhl_tEfEskxw,3432
30
- ws_bom_robot_app/llm/tools/tool_manager.py,sha256=avoFERE0v9MFQ3pUBMug8eGYIXbIYl7NqkP1kjNee7s,15439
30
+ ws_bom_robot_app/llm/tools/tool_manager.py,sha256=ThiaBCDOn192NOaLHsxbNZXYs5fptqlfgCHW-9h2eVY,15989
31
31
  ws_bom_robot_app/llm/tools/utils.py,sha256=Ba7ScFZPVJ3ke8KLO8ik1wyR2f_zC99Bikqx0OGnKoI,1924
32
32
  ws_bom_robot_app/llm/tools/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
33
33
  ws_bom_robot_app/llm/tools/models/main.py,sha256=1hICqHs-KS2heenkH7b2eH0N2GrPaaNGBrn64cl_A40,827
@@ -35,7 +35,7 @@ ws_bom_robot_app/llm/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJ
35
35
  ws_bom_robot_app/llm/utils/agent.py,sha256=uFuSfYMfGIE2WCKGNSKL-T2SDFn-tUKvbAYbGTPIw6g,1445
36
36
  ws_bom_robot_app/llm/utils/chunker.py,sha256=zVXjRMloc3KbNEqiDcycYzy4N0Ey1g8XYeq6ftyvkyg,857
37
37
  ws_bom_robot_app/llm/utils/cleanup.py,sha256=ARLZTX4mLbkLCEnMdIWYDYEAPOjzfy1laLGkYnxZe30,3063
38
- ws_bom_robot_app/llm/utils/cms.py,sha256=5TBDDlTsE4O8_bGvlqFOkkK13WFEoOvYRp_FOEXUuKY,6466
38
+ ws_bom_robot_app/llm/utils/cms.py,sha256=gfIXvY3DxgbgDf0LCzyekWitaduxKGLHfV6gbRmh8zk,6960
39
39
  ws_bom_robot_app/llm/utils/download.py,sha256=rvc88E63UGHnFVlJJeMb05Z2FcBYIITqKnIE3ldEu6I,7293
40
40
  ws_bom_robot_app/llm/utils/print.py,sha256=HK3zhZOd4cEyXZ8QcudLtTIfqqtMOERce_yTofS8NXo,803
41
41
  ws_bom_robot_app/llm/utils/secrets.py,sha256=-HtqLIDVIJrpvGC5YhPAVyLsq8P4ChVM5g3GOfdwqVk,878
@@ -70,7 +70,7 @@ ws_bom_robot_app/llm/vector_store/loader/__init__.py,sha256=47DEQpj8HBSa-_TImW-5
70
70
  ws_bom_robot_app/llm/vector_store/loader/base.py,sha256=InpRwKPxp0tuM4drezBvxxAWHe3XTmu60MGvFsT7RPE,7176
71
71
  ws_bom_robot_app/llm/vector_store/loader/docling.py,sha256=dLvpOi4EH0jyx06IrHoanfLRPFXLJi9BU2BWYcaw-U4,5000
72
72
  ws_bom_robot_app/llm/vector_store/loader/json_loader.py,sha256=LDppW0ZATo4_1hh-KlsAM3TLawBvwBxva_a7k5Oz1sc,858
73
- ws_bom_robot_app-0.0.101.dist-info/METADATA,sha256=FlworyfxD6hsx_BlbG3OC2piu5HXFj5rwSQC3dosCtk,11025
74
- ws_bom_robot_app-0.0.101.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
75
- ws_bom_robot_app-0.0.101.dist-info/top_level.txt,sha256=Yl0akyHVbynsBX_N7wx3H3ZTkcMLjYyLJs5zBMDAKcM,17
76
- ws_bom_robot_app-0.0.101.dist-info/RECORD,,
73
+ ws_bom_robot_app-0.0.102.dist-info/METADATA,sha256=MOViAkxOwrXz2ynceG8p6KjRYdBI3XPzId3CdZ4SgaI,11025
74
+ ws_bom_robot_app-0.0.102.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
75
+ ws_bom_robot_app-0.0.102.dist-info/top_level.txt,sha256=Yl0akyHVbynsBX_N7wx3H3ZTkcMLjYyLJs5zBMDAKcM,17
76
+ ws_bom_robot_app-0.0.102.dist-info/RECORD,,