langroid 0.59.8__py3-none-any.whl → 0.59.10__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.
@@ -14,7 +14,6 @@ from langroid.agent.chat_document import ChatDocument
14
14
  from langroid.agent.tools.segment_extract_tool import SegmentExtractTool
15
15
  from langroid.language_models.base import LLMConfig
16
16
  from langroid.language_models.openai_gpt import OpenAIGPTConfig
17
- from langroid.mytypes import Entity
18
17
  from langroid.parsing.utils import extract_numbered_segments, number_segments
19
18
  from langroid.utils.constants import DONE, NO_ANSWER
20
19
 
@@ -26,12 +25,19 @@ class RelevanceExtractorAgentConfig(ChatAgentConfig):
26
25
  llm: LLMConfig | None = OpenAIGPTConfig()
27
26
  segment_length: int = 1 # number of sentences per segment
28
27
  query: str = "" # query for relevance extraction
28
+ handle_llm_no_tool: str = """
29
+ You FORGOT to use the `extract_segments` tool!
30
+ Remember that your response MUST be a JSON-formatted string
31
+ starting with `{"request": "extract_segments", ...}`
32
+ """
29
33
  system_message: str = """
30
34
  The user will give you a PASSAGE containing segments numbered as
31
35
  <#1#>, <#2#>, <#3#>, etc.,
32
36
  followed by a QUERY. Extract ONLY the segment-numbers from
33
37
  the PASSAGE that are RELEVANT to the QUERY.
34
38
  Present the extracted segment-numbers using the `extract_segments` tool/function.
39
+ Note that your response MUST be a JSON-formatted string
40
+ starting with `{"request": "extract_segments", ...}`
35
41
  """
36
42
 
37
43
 
@@ -63,13 +69,22 @@ class RelevanceExtractorAgent(ChatAgent):
63
69
  self.numbered_passage = number_segments(message_str, self.config.segment_length)
64
70
  # compose prompt
65
71
  prompt = f"""
72
+ <Instructions>
73
+ Given the PASSAGE below with NUMBERED segments, and the QUERY,
74
+ extract ONLY the segment-numbers that are RELEVANT to the QUERY,
75
+ and present them using the `extract_segments` tool/function,
76
+ i.e. your response MUST be a JSON-formatted string starting with
77
+ `{{"request": "extract_segments", ...}}`
78
+ </Instructions>
79
+
66
80
  PASSAGE:
67
81
  {self.numbered_passage}
68
82
 
69
83
  QUERY: {self.config.query}
70
84
  """
71
85
  # send to LLM
72
- return super().llm_response(prompt)
86
+ response = super().llm_response(prompt)
87
+ return response
73
88
 
74
89
  @no_type_check
75
90
  async def llm_response_async(
@@ -99,7 +114,8 @@ class RelevanceExtractorAgent(ChatAgent):
99
114
  QUERY: {self.config.query}
100
115
  """
101
116
  # send to LLM
102
- return await super().llm_response_async(prompt)
117
+ response = await super().llm_response_async(prompt)
118
+ return response
103
119
 
104
120
  def extract_segments(self, msg: SegmentExtractTool) -> str:
105
121
  """Method to handle a segmentExtractTool message from LLM"""
@@ -116,12 +132,3 @@ class RelevanceExtractorAgent(ChatAgent):
116
132
  return DONE + " " + NO_ANSWER
117
133
  # this response ends the task by saying DONE
118
134
  return DONE + " " + extracts
119
-
120
- def handle_message_fallback(
121
- self, msg: str | ChatDocument
122
- ) -> str | ChatDocument | None:
123
- """Handle case where LLM forgets to use SegmentExtractTool"""
124
- if isinstance(msg, ChatDocument) and msg.metadata.sender == Entity.LLM:
125
- return DONE + " " + NO_ANSWER
126
- else:
127
- return None
@@ -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
 
@@ -142,8 +142,14 @@ def top_level_json_field(s: str, f: str) -> Any:
142
142
  return ""
143
143
  for j in jsons:
144
144
  json_data = json.loads(j)
145
- if f in json_data:
146
- return json_data[f]
145
+ if isinstance(json_data, dict):
146
+ if f in json_data:
147
+ return json_data[f]
148
+ elif isinstance(json_data, list):
149
+ # Some responses wrap candidate JSON objects in a list; scan them.
150
+ for item in json_data:
151
+ if isinstance(item, dict) and f in item:
152
+ return item[f]
147
153
 
148
154
  return ""
149
155
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langroid
3
- Version: 0.59.8
3
+ Version: 0.59.10
4
4
  Summary: Harness LLMs with Multi-Agent Programming
5
5
  Author-email: Prasad Chalasani <pchalasani@gmail.com>
6
6
  License: MIT
@@ -19,7 +19,7 @@ langroid/agent/special/doc_chat_agent.py,sha256=tUr4qex3OjqF32zeyvTOnNgUP1wdpe5h
19
19
  langroid/agent/special/doc_chat_task.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
20
20
  langroid/agent/special/lance_doc_chat_agent.py,sha256=6pIqi2DF-MvYYN3-blsdUgulYnOBTl7I21T7wPAt1zM,10413
21
21
  langroid/agent/special/lance_tools.py,sha256=3j7Hsyf3-H9ccTXjyNOcnMnpJ7r1lXnqDLSMQgFa7ZI,2114
22
- langroid/agent/special/relevance_extractor_agent.py,sha256=Wa65UReGaNIB5MkXugzc4X9ci3c21-PwDrN7zNX-iVQ,4801
22
+ langroid/agent/special/relevance_extractor_agent.py,sha256=EgFDgxHZmnpzwh17vrzdncoRn2yPBsiRweN9wr85czc,5206
23
23
  langroid/agent/special/retriever_agent.py,sha256=o2UfqiCGME0t85SZ6qjK041_WZYqXSuV1SeH_3KtVuc,1931
24
24
  langroid/agent/special/table_chat_agent.py,sha256=T2YMFpOnW4YV-QXvB34MbaBGXBPiWeCiqO1bVKFykbg,10943
25
25
  langroid/agent/special/arangodb/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -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=W_8dMD1SFohcQjbOBvRR1NrdO-F6xdhNVDmUX1nNdsY,5052
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.8.dist-info/METADATA,sha256=URy5sRVmD5E7DQdVg-TjodaingDUvId0hsgsATNQF5A,66517
143
- langroid-0.59.8.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
144
- langroid-0.59.8.dist-info/licenses/LICENSE,sha256=EgVbvA6VSYgUlvC3RvPKehSg7MFaxWDsFuzLOsPPfJg,1065
145
- langroid-0.59.8.dist-info/RECORD,,
142
+ langroid-0.59.10.dist-info/METADATA,sha256=LnA1YOq6VYfYsfnBONaSbzMPti5M_2IySKSNSBbdOIA,66518
143
+ langroid-0.59.10.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
144
+ langroid-0.59.10.dist-info/licenses/LICENSE,sha256=EgVbvA6VSYgUlvC3RvPKehSg7MFaxWDsFuzLOsPPfJg,1065
145
+ langroid-0.59.10.dist-info/RECORD,,