langroid 0.59.9__py3-none-any.whl → 0.59.11__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.
@@ -463,12 +463,12 @@ class LlamaCppServerEmbeddings(EmbeddingModel):
463
463
  response = requests.post(self.embedding_url, json=data)
464
464
 
465
465
  if response.status_code == 200:
466
- embeddings = response.json()["embedding"]
466
+ embeddings = self._extract_embedding(response.json())
467
467
  if not (
468
468
  isinstance(embeddings, list) and isinstance(embeddings[0], (int, float))
469
469
  ):
470
470
  raise ValueError(
471
- """Embedding endpoint has not returned the correct format.
471
+ """Embedding endpoint has not returned the correct format.
472
472
  Is the URL correct?
473
473
  """
474
474
  )
@@ -480,6 +480,71 @@ class LlamaCppServerEmbeddings(EmbeddingModel):
480
480
  "Failed to connect to embedding provider",
481
481
  )
482
482
 
483
+ def _extract_embedding(
484
+ self, response_json: dict[str, Any] | list[Any]
485
+ ) -> List[int | float]:
486
+ """
487
+ Extract embedding vector from llama.cpp response.
488
+
489
+ Handles multiple response formats:
490
+ 1. Native /embedding: {"embedding": [floats]}
491
+ 2. Array format: [{"embedding": [floats]}]
492
+ 3. Double-nested: [{"embedding": [[floats]]}]
493
+ 4. OpenAI /v1/embeddings: {"data": [{"embedding": [floats]}]}
494
+ 5. Nested in dict: {"embedding": [[floats]]}
495
+
496
+ Args:
497
+ response_json: The JSON response from llama.cpp server
498
+
499
+ Returns:
500
+ List of floats representing the embedding vector
501
+
502
+ Raises:
503
+ ValueError: If response format is not recognized
504
+ """
505
+ import json
506
+
507
+ # Try native format first: {"embedding": [floats]}
508
+ if isinstance(response_json, dict) and "embedding" in response_json:
509
+ embeddings = response_json["embedding"]
510
+ # Check if it's [floats]
511
+ if isinstance(embeddings, list) and len(embeddings) > 0:
512
+ if isinstance(embeddings[0], (int, float)):
513
+ return embeddings
514
+ # Might be nested: {"embedding": [[floats]]}
515
+ if isinstance(embeddings[0], list) and len(embeddings[0]) > 0:
516
+ if isinstance(embeddings[0][0], (int, float)):
517
+ return embeddings[0]
518
+
519
+ # Try OpenAI format: {"data": [{"embedding": [floats]}]}
520
+ if isinstance(response_json, dict) and "data" in response_json:
521
+ data = response_json["data"]
522
+ if isinstance(data, list) and len(data) > 0:
523
+ if isinstance(data[0], dict) and "embedding" in data[0]:
524
+ embeddings = data[0]["embedding"]
525
+ if isinstance(embeddings, list) and len(embeddings) > 0:
526
+ if isinstance(embeddings[0], (int, float)):
527
+ return embeddings
528
+
529
+ # Try array format: [{"embedding": [floats]}] or [{"embedding": [[floats]]}]
530
+ if isinstance(response_json, list) and len(response_json) > 0:
531
+ first_item = response_json[0]
532
+ if isinstance(first_item, dict) and "embedding" in first_item:
533
+ embeddings = first_item["embedding"]
534
+ # Check if it's [floats]
535
+ if isinstance(embeddings, list) and len(embeddings) > 0:
536
+ if isinstance(embeddings[0], (int, float)):
537
+ return embeddings
538
+ # Check if it's [[floats]]
539
+ if isinstance(embeddings[0], list) and len(embeddings[0]) > 0:
540
+ if isinstance(embeddings[0][0], (int, float)):
541
+ return embeddings[0]
542
+
543
+ raise ValueError(
544
+ f"Unsupported embedding response format from {self.embedding_url}. "
545
+ f"Response: {json.dumps(response_json)[:500]}"
546
+ )
547
+
483
548
  def embedding_fn(self) -> Callable[[List[str]], Embeddings]:
484
549
  return EmbeddingFunctionCallable(self, self.config.batch_size)
485
550
 
@@ -135,15 +135,32 @@ def top_level_json_field(s: str, f: str) -> Any:
135
135
  Returns:
136
136
  str: The value of the field f in the top-level JSON object, if any.
137
137
  Otherwise, return an empty string.
138
- """
139
138
 
140
- jsons = extract_top_level_json(s)
141
- if len(jsons) == 0:
142
- return ""
143
- for j in jsons:
144
- json_data = json.loads(j)
145
- if f in json_data:
146
- return json_data[f]
139
+ Note:
140
+ This function is designed to never crash. If any exception occurs during
141
+ JSON parsing or field extraction, it gracefully returns an empty string.
142
+ """
143
+ try:
144
+ jsons = extract_top_level_json(s)
145
+ if len(jsons) == 0:
146
+ return ""
147
+ for j in jsons:
148
+ try:
149
+ json_data = json.loads(j)
150
+ if isinstance(json_data, dict):
151
+ if f in json_data:
152
+ return json_data[f]
153
+ elif isinstance(json_data, list):
154
+ # Some responses wrap candidate JSON objects in a list; scan them.
155
+ for item in json_data:
156
+ if isinstance(item, dict) and f in item:
157
+ return item[f]
158
+ except (json.JSONDecodeError, TypeError, KeyError):
159
+ # If this specific JSON fails to parse, continue to next candidate
160
+ continue
161
+ except Exception:
162
+ # Catch any unexpected errors to ensure we never crash
163
+ pass
147
164
 
148
165
  return ""
149
166
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langroid
3
- Version: 0.59.9
3
+ Version: 0.59.11
4
4
  Summary: Harness LLMs with Multi-Agent Programming
5
5
  Author-email: Prasad Chalasani <pchalasani@gmail.com>
6
6
  License: MIT
@@ -64,7 +64,7 @@ langroid/cachedb/base.py,sha256=b104RrL1Og7K2mWFy3sWc4Er3z9zWMtY9dxQVhwnm2E,1351
64
64
  langroid/cachedb/redis_cachedb.py,sha256=7kgnbf4b5CKsCrlL97mHWKvdvlLt8zgn7lc528jEpiE,5141
65
65
  langroid/embedding_models/__init__.py,sha256=KyYxR3jDFUCfYjSuCL86qjAmrq6mXXjOT4lFNOKVj6Y,955
66
66
  langroid/embedding_models/base.py,sha256=F65Vlj3RugkcntWOoKm-0b7h4T_Les6m4e7Qto_-Otg,2564
67
- langroid/embedding_models/models.py,sha256=Cwlq3ZsXXRblh7v0iABJ1QDZorJ2l3Hyks-T9g4JtnE,20649
67
+ langroid/embedding_models/models.py,sha256=SmUK23iX6ypisjD71ElzVizZpYmZxwQOlDpAcyXioK4,23613
68
68
  langroid/embedding_models/remote_embeds.py,sha256=6_kjXByVbqhY9cGwl9R83ZcYC2km-nGieNNAo1McHaY,5151
69
69
  langroid/embedding_models/protoc/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
70
70
  langroid/embedding_models/protoc/embeddings.proto,sha256=_O-SgFpTaylQeOTgSpxhEJ7CUw7PeCQQJLaPqpPYKJg,321
@@ -94,7 +94,7 @@ langroid/parsing/document_parser.py,sha256=cUcp4JKS_LpsjX7OqnGBhHorDHx7FG5pvKGjR
94
94
  langroid/parsing/file_attachment.py,sha256=f-MBRCI58XsCqJDH2HwTWwTQxLbYsDrOLgjrM1kw3XE,7350
95
95
  langroid/parsing/md_parser.py,sha256=8LX9RDRWV1dZSYa-uBD8-whC_L6UYco-AQUxIuviqEk,21656
96
96
  langroid/parsing/para_sentence_split.py,sha256=AJBzZojP3zpB-_IMiiHismhqcvkrVBQ3ZINoQyx_bE4,2000
97
- langroid/parsing/parse_json.py,sha256=aADo38bAHQhC8on4aWZZzVzSDy-dK35vRLZsFI2ewh8,4756
97
+ langroid/parsing/parse_json.py,sha256=NxECxPndhIpyVdSYFEe7QpoIQktMPrFjo1i5P139dNA,5608
98
98
  langroid/parsing/parser.py,sha256=IcwmVLlAae5LiKZ9OFhrnVOoHxcnsV7feFSHQiFfoi4,16112
99
99
  langroid/parsing/pdf_utils.py,sha256=QogxU_B1N3WSLyZ9PEcJDaJoZShKs7CPQRVyF1V2DiE,3143
100
100
  langroid/parsing/repo_loader.py,sha256=oB0TNifWCaqvlj7C0U76C4NZT7b94BbGkVX_-mrcH_4,30220
@@ -139,7 +139,7 @@ langroid/vector_store/pineconedb.py,sha256=7V0Bkt4ZrOR3V90tdXvdFmyNGuww7SFdyPq7-
139
139
  langroid/vector_store/postgres.py,sha256=TY_VshimwFZglYgKYm7Qn1F-dCSL8GsXRTgmh7VTe9c,16110
140
140
  langroid/vector_store/qdrantdb.py,sha256=mqxMOrcLAQpl0opuL8vXhdIt6ppv2zYyAvddHZoEW0Y,19184
141
141
  langroid/vector_store/weaviatedb.py,sha256=BS95bxVKNYfQc9VPb85a1HlcgnXfAkgMzjydnjCgRHc,11853
142
- langroid-0.59.9.dist-info/METADATA,sha256=wJlApwHFS887eho2UVRilBDaM4UHzKdzh4xK5P3WvO4,66517
143
- langroid-0.59.9.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
144
- langroid-0.59.9.dist-info/licenses/LICENSE,sha256=EgVbvA6VSYgUlvC3RvPKehSg7MFaxWDsFuzLOsPPfJg,1065
145
- langroid-0.59.9.dist-info/RECORD,,
142
+ langroid-0.59.11.dist-info/METADATA,sha256=bAKWmjVR0qVyzuBz89HWXyID4WEZ-FQeS9I4tHg4lMM,66518
143
+ langroid-0.59.11.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
144
+ langroid-0.59.11.dist-info/licenses/LICENSE,sha256=EgVbvA6VSYgUlvC3RvPKehSg7MFaxWDsFuzLOsPPfJg,1065
145
+ langroid-0.59.11.dist-info/RECORD,,