nucliadb 6.5.1.post4504__py3-none-any.whl → 6.5.1.post4510__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.
@@ -100,33 +100,58 @@ class CappedPromptContext:
100
100
  self.output: PromptContext = {}
101
101
  self.images: PromptContextImages = {}
102
102
  self.max_size = max_size
103
- self._size = 0
104
103
 
105
104
  def __setitem__(self, key: str, value: str) -> None:
106
- prev_value_len = len(self.output.get(key, ""))
107
- if self.max_size is None:
108
- # Unbounded size context
109
- to_add = value
110
- else:
111
- # Make sure we don't exceed the max size
112
- size_available = max(self.max_size - self._size + prev_value_len, 0)
113
- to_add = value[:size_available]
114
- self.output[key] = to_add
115
- self._size = self._size - prev_value_len + len(to_add)
105
+ self.output.__setitem__(key, value)
116
106
 
117
107
  def __getitem__(self, key: str) -> str:
118
108
  return self.output.__getitem__(key)
119
109
 
120
110
  def __delitem__(self, key: str) -> None:
121
- value = self.output.pop(key, "")
122
- self._size -= len(value)
111
+ try:
112
+ self.output.__delitem__(key)
113
+ except KeyError:
114
+ pass
123
115
 
124
116
  def text_block_ids(self) -> list[str]:
125
117
  return list(self.output.keys())
126
118
 
127
119
  @property
128
120
  def size(self) -> int:
129
- return self._size
121
+ """
122
+ Returns the total size of the context in characters.
123
+ """
124
+ return sum(len(text) for text in self.output.values())
125
+
126
+ def cap(self) -> dict[str, str]:
127
+ """
128
+ This method will trim the context to the maximum size if it exceeds it.
129
+ It will remove text from the most recent entries first, until the size is below the limit.
130
+ """
131
+ if self.max_size is None:
132
+ return self.output
133
+
134
+ if self.size <= self.max_size:
135
+ return self.output
136
+
137
+ logger.info("Removing text from context to fit within the max size limit")
138
+ # Iterate the dictionary in reverse order of insertion
139
+ for key in reversed(list(self.output.keys())):
140
+ current_size = self.size
141
+ if current_size <= self.max_size:
142
+ break
143
+ # Remove text from the value
144
+ text = self.output[key]
145
+ # If removing the whole text still keeps the total size above the limit, remove it
146
+ if current_size - len(text) >= self.max_size:
147
+ del self.output[key]
148
+ else:
149
+ # Otherwise, trim the text to fit within the limit
150
+ excess_size = current_size - self.max_size
151
+ if excess_size > 0:
152
+ trimmed_text = text[:-excess_size]
153
+ self.output[key] = trimmed_text
154
+ return self.output
130
155
 
131
156
 
132
157
  async def get_next_conversation_messages(
@@ -1001,12 +1026,11 @@ class PromptContextBuilder:
1001
1026
  self,
1002
1027
  ) -> tuple[PromptContext, PromptContextOrder, PromptContextImages, AugmentedContext]:
1003
1028
  ccontext = CappedPromptContext(max_size=self.max_context_characters)
1004
- print(".......................")
1005
1029
  self.prepend_user_context(ccontext)
1006
1030
  await self._build_context(ccontext)
1007
1031
  if self.visual_llm:
1008
1032
  await self._build_context_images(ccontext)
1009
- context = ccontext.output
1033
+ context = ccontext.cap()
1010
1034
  context_images = ccontext.images
1011
1035
  context_order = {text_block_id: order for order, text_block_id in enumerate(context.keys())}
1012
1036
  return context, context_order, context_images, self.augmented_context
@@ -171,14 +171,13 @@ async def chat_streaming_generator(
171
171
  user_query: str,
172
172
  is_json: bool,
173
173
  ):
174
- stream = predict_response.content.iter_any()
175
174
  first = True
176
175
  status_code = AnswerStatusCode.ERROR.value
177
176
  text_answer = ""
178
177
  json_object = None
179
178
  metrics = AskMetrics()
180
179
  with metrics.time(PREDICT_ANSWER_METRIC):
181
- async for chunk in stream:
180
+ async for chunk in predict_response.content:
182
181
  if first:
183
182
  metrics.record_first_chunk_yielded()
184
183
  first = False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nucliadb
3
- Version: 6.5.1.post4504
3
+ Version: 6.5.1.post4510
4
4
  Summary: NucliaDB
5
5
  Author-email: Nuclia <nucliadb@nuclia.com>
6
6
  License-Expression: AGPL-3.0-or-later
@@ -19,11 +19,11 @@ Classifier: Programming Language :: Python :: 3.12
19
19
  Classifier: Programming Language :: Python :: 3 :: Only
20
20
  Requires-Python: <4,>=3.9
21
21
  Description-Content-Type: text/markdown
22
- Requires-Dist: nucliadb-telemetry[all]>=6.5.1.post4504
23
- Requires-Dist: nucliadb-utils[cache,fastapi,storages]>=6.5.1.post4504
24
- Requires-Dist: nucliadb-protos>=6.5.1.post4504
25
- Requires-Dist: nucliadb-models>=6.5.1.post4504
26
- Requires-Dist: nidx-protos>=6.5.1.post4504
22
+ Requires-Dist: nucliadb-telemetry[all]>=6.5.1.post4510
23
+ Requires-Dist: nucliadb-utils[cache,fastapi,storages]>=6.5.1.post4510
24
+ Requires-Dist: nucliadb-protos>=6.5.1.post4510
25
+ Requires-Dist: nucliadb-models>=6.5.1.post4510
26
+ Requires-Dist: nidx-protos>=6.5.1.post4510
27
27
  Requires-Dist: nucliadb-admin-assets>=1.0.0.post1224
28
28
  Requires-Dist: nuclia-models>=0.24.2
29
29
  Requires-Dist: uvicorn[standard]
@@ -255,7 +255,7 @@ nucliadb/search/search/merge.py,sha256=XiRBsxhYPshPV7lZXD-9E259KZOPIf4I2tKosY0lP
255
255
  nucliadb/search/search/metrics.py,sha256=3I6IN0qDSmqIvUaWJmT3rt-Jyjs6LcvnKI8ZqCiuJPY,3501
256
256
  nucliadb/search/search/paragraphs.py,sha256=pNAEiYqJGGUVcEf7xf-PFMVqz0PX4Qb-WNG-_zPGN2o,7799
257
257
  nucliadb/search/search/pgcatalog.py,sha256=O_nRjSJf1Qc-XorVwcNlsDOftzy_zQLLfagkjU4YmSA,16718
258
- nucliadb/search/search/predict_proxy.py,sha256=cuD_sfM3RLdEoQaanRz0CflO6nKVGGKPzoFA17shb_w,8647
258
+ nucliadb/search/search/predict_proxy.py,sha256=rbclJeMBW7Yd6PpSAhp9fiH6YpPrVnUb-qgIGQTUB90,8616
259
259
  nucliadb/search/search/query.py,sha256=0qIQdt548L3jtKOyKo06aGJ73SLBxAW3N38_Hc1M3Uw,11528
260
260
  nucliadb/search/search/rank_fusion.py,sha256=xZtXhbmKb_56gs73u6KkFm2efvTATOSMmpOV2wrAIqE,9613
261
261
  nucliadb/search/search/rerankers.py,sha256=E2J1QdKAojqbhHM3KAyaOXKf6tJyETUxKs4tf_BEyqk,7472
@@ -266,7 +266,7 @@ nucliadb/search/search/chat/__init__.py,sha256=cp15ZcFnHvpcu_5-aK2A4uUyvuZVV_MJn
266
266
  nucliadb/search/search/chat/ask.py,sha256=0sgfiCbNaCZrTvYaRGtf5xL6VnzRgzofINiEP4IvhWs,38278
267
267
  nucliadb/search/search/chat/exceptions.py,sha256=Siy4GXW2L7oPhIR86H3WHBhE9lkV4A4YaAszuGGUf54,1356
268
268
  nucliadb/search/search/chat/images.py,sha256=PA8VWxT5_HUGfW1ULhKTK46UBsVyINtWWqEM1ulzX1E,3095
269
- nucliadb/search/search/chat/prompt.py,sha256=TKyIblYvZ3EiyfwVFbEeYJ66ZNDtbeJySSa45HptCOk,51837
269
+ nucliadb/search/search/chat/prompt.py,sha256=SNsCtB9mZTODjnUMAH8YfPxn05Kjl2d5xTIteNxyVcI,52783
270
270
  nucliadb/search/search/chat/query.py,sha256=3jMPNbiFEOoS0ydMOPYkSx1qVlvAv51npzadWXDwkMs,16650
271
271
  nucliadb/search/search/query_parser/__init__.py,sha256=cp15ZcFnHvpcu_5-aK2A4uUyvuZVV_MJn4bIXMa20ks,835
272
272
  nucliadb/search/search/query_parser/exceptions.py,sha256=sVl9gRNzhE-s480LBBVkiXzNRbKhYRQN5F3it5tNNp8,939
@@ -375,8 +375,8 @@ nucliadb/writer/tus/local.py,sha256=7jYa_w9b-N90jWgN2sQKkNcomqn6JMVBOVeDOVYJHto,
375
375
  nucliadb/writer/tus/s3.py,sha256=vF0NkFTXiXhXq3bCVXXVV-ED38ECVoUeeYViP8uMqcU,8357
376
376
  nucliadb/writer/tus/storage.py,sha256=ToqwjoYnjI4oIcwzkhha_MPxi-k4Jk3Lt55zRwaC1SM,2903
377
377
  nucliadb/writer/tus/utils.py,sha256=MSdVbRsRSZVdkaum69_0wku7X3p5wlZf4nr6E0GMKbw,2556
378
- nucliadb-6.5.1.post4504.dist-info/METADATA,sha256=NgbKcJmI6-SK9h2awP4bpcbndCDv-XWCqmh6rGtBREg,4158
379
- nucliadb-6.5.1.post4504.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
380
- nucliadb-6.5.1.post4504.dist-info/entry_points.txt,sha256=XqGfgFDuY3zXQc8ewXM2TRVjTModIq851zOsgrmaXx4,1268
381
- nucliadb-6.5.1.post4504.dist-info/top_level.txt,sha256=hwYhTVnX7jkQ9gJCkVrbqEG1M4lT2F_iPQND1fCzF80,20
382
- nucliadb-6.5.1.post4504.dist-info/RECORD,,
378
+ nucliadb-6.5.1.post4510.dist-info/METADATA,sha256=lLAN-xpgGXv6qlz-HyosNkSlKRX5qV6Be8-Xj59N8F0,4158
379
+ nucliadb-6.5.1.post4510.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
380
+ nucliadb-6.5.1.post4510.dist-info/entry_points.txt,sha256=XqGfgFDuY3zXQc8ewXM2TRVjTModIq851zOsgrmaXx4,1268
381
+ nucliadb-6.5.1.post4510.dist-info/top_level.txt,sha256=hwYhTVnX7jkQ9gJCkVrbqEG1M4lT2F_iPQND1fCzF80,20
382
+ nucliadb-6.5.1.post4510.dist-info/RECORD,,