langchain-b12 0.1.4__tar.gz → 0.1.6__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langchain-b12
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: A reusable collection of tools and implementations for Langchain
5
5
  Author-email: Vincent Min <vincent.min@b12-consulting.com>
6
6
  Requires-Python: >=3.11
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "langchain-b12"
3
- version = "0.1.4"
3
+ version = "0.1.6"
4
4
  description = "A reusable collection of tools and implementations for Langchain"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -3,7 +3,6 @@ from collections.abc import Sequence
3
3
  from typing import Any, Literal, TypedDict
4
4
  from uuid import UUID
5
5
 
6
- from fuzzysearch import find_near_matches
7
6
  from langchain_core.callbacks import Callbacks
8
7
  from langchain_core.language_models import BaseChatModel
9
8
  from langchain_core.messages import AIMessage, BaseMessage, SystemMessage
@@ -155,6 +154,8 @@ def validate_citations(
155
154
  sentences: list[str],
156
155
  ) -> list[tuple[Citation, Match | None]]:
157
156
  """Validate the citations. Invalid citations are dropped."""
157
+ from fuzzysearch import find_near_matches
158
+
158
159
  n_sentences = len(sentences)
159
160
 
160
161
  all_text = "\n".join(
@@ -244,18 +245,25 @@ def create_citation_model(
244
245
  e.g. `<context key="abc">Today is a sunny day</context>`.
245
246
  The returned AIMessage will have the following structure:
246
247
  AIMessage(
247
- content= {
248
- "citations": [
249
- {
250
- "cited_text": "the color of the grass is green",
251
- "generated_cited_text": "the color of the grass is green",
252
- "key": "abc",
253
- "dist": 0,
254
- }
255
- ],
256
- "text": "The grass is green",
257
- "type": "text",
258
- },
248
+ content=[
249
+ {
250
+ "citations": [
251
+ {
252
+ "cited_text": "the color of the grass is green",
253
+ "generated_cited_text": "the color of the grass is green",
254
+ "key": "abc",
255
+ "dist": 0,
256
+ }
257
+ ],
258
+ "text": "The grass is green",
259
+ "type": "text",
260
+ },
261
+ {
262
+ "citations": None,
263
+ "text": "Is there anything else I can help you with?",
264
+ "type": "text",
265
+ }
266
+ ]
259
267
  )
260
268
 
261
269
  Args:
@@ -90,6 +90,8 @@ class ChatGenAI(BaseChatModel):
90
90
  HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
91
91
  }
92
92
  """ # noqa: E501
93
+ thinking_config: types.ThinkingConfig | None = None
94
+ "The thinking configuration to use for the model."
93
95
 
94
96
  model_config = ConfigDict(
95
97
  arbitrary_types_allowed=True,
@@ -208,6 +210,10 @@ class ChatGenAI(BaseChatModel):
208
210
  candidate_count=self.n,
209
211
  stop_sequences=stop or self.stop,
210
212
  safety_settings=self.safety_settings,
213
+ thinking_config=self.thinking_config,
214
+ automatic_function_calling=types.AutomaticFunctionCallingConfig(
215
+ disable=True,
216
+ ),
211
217
  **kwargs,
212
218
  ),
213
219
  )
@@ -240,6 +246,10 @@ class ChatGenAI(BaseChatModel):
240
246
  candidate_count=self.n,
241
247
  stop_sequences=stop or self.stop,
242
248
  safety_settings=self.safety_settings,
249
+ thinking_config=self.thinking_config,
250
+ automatic_function_calling=types.AutomaticFunctionCallingConfig(
251
+ disable=True,
252
+ ),
243
253
  **kwargs,
244
254
  ),
245
255
  )
@@ -362,6 +372,12 @@ class ChatGenAI(BaseChatModel):
362
372
  input_tokens=usage_metadata.prompt_token_count or 0,
363
373
  output_tokens=usage_metadata.candidates_token_count or 0,
364
374
  total_tokens=usage_metadata.total_token_count or 0,
375
+ input_token_details={
376
+ "cache_read": usage_metadata.cached_content_token_count or 0
377
+ },
378
+ output_token_details={
379
+ "reasoning": usage_metadata.thoughts_token_count or 0
380
+ },
365
381
  )
366
382
 
367
383
  total_lc_usage: UsageMetadata | None = (
@@ -392,7 +408,12 @@ class ChatGenAI(BaseChatModel):
392
408
  "finish_reason": top_candidate.finish_reason,
393
409
  "finish_message": top_candidate.finish_message,
394
410
  }
395
- message = parse_response_candidate(top_candidate)
411
+ try:
412
+ message = parse_response_candidate(top_candidate)
413
+ except Exception as e:
414
+ raise ValueError(
415
+ f"Failed to parse model response: {top_candidate.finish_message}"
416
+ ) from e
396
417
  if lc_usage:
397
418
  message.usage_metadata = lc_usage
398
419
  # add model name if final chunk
@@ -437,20 +437,9 @@ class TestCitationMixin:
437
437
  ]
438
438
  ]
439
439
 
440
- with (
441
- patch.object(
442
- CitationMixin.__bases__[0], "agenerate"
443
- ) as mock_parent_agenerate,
444
- patch(
445
- "langchain_b12.citations.citations.add_citations"
446
- ) as mock_add_citations,
447
- ):
448
-
449
- mock_parent_agenerate.return_value = LLMResult(
450
- generations=[
451
- [ChatGeneration(message=AIMessage(content="Test response"))]
452
- ]
453
- )
440
+ with patch(
441
+ "langchain_b12.citations.citations.add_citations"
442
+ ) as mock_add_citations:
454
443
 
455
444
  # Simulate an error in citation processing
456
445
  mock_add_citations.side_effect = RuntimeError("Citation error")
File without changes
File without changes
File without changes
File without changes