ai-pipeline-core 0.1.3__py3-none-any.whl → 0.1.4__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.
@@ -1,4 +1,5 @@
1
1
  import base64
2
+ import hashlib
2
3
  import json
3
4
 
4
5
  from openai.types.chat import (
@@ -59,6 +60,11 @@ class AIMessages(list[AIMessageType]):
59
60
  messages.append(message)
60
61
  return messages
61
62
 
63
+ def get_prompt_cache_key(self, system_prompt: str | None = None) -> str:
64
+ if not system_prompt:
65
+ system_prompt = ""
66
+ return hashlib.sha256((system_prompt + json.dumps(self.to_prompt())).encode()).hexdigest()
67
+
62
68
  @staticmethod
63
69
  def document_to_prompt(document: Document) -> list[ChatCompletionContentPartParam]:
64
70
  """
@@ -48,15 +48,13 @@ def _process_messages(
48
48
  # Use AIMessages.to_prompt() for context
49
49
  context_messages = context.to_prompt()
50
50
 
51
- # Apply caching to context messages
52
- for msg in context_messages:
53
- if msg.get("role") == "user":
54
- # Add cache control to user messages in context
55
- msg["cache_control"] = { # type: ignore
56
- "type": "ephemeral",
57
- "ttl": "120s", # Cache for 2m
58
- }
59
- processed_messages.append(msg)
51
+ # Apply caching to last context message
52
+ context_messages[-1]["cache_control"] = { # type: ignore
53
+ "type": "ephemeral",
54
+ "ttl": "120s", # Cache for 2m
55
+ }
56
+
57
+ processed_messages.extend(context_messages)
60
58
 
61
59
  # Process regular messages without caching
62
60
  if messages:
@@ -108,9 +106,14 @@ async def _generate_with_retry(
108
106
  **options.to_openai_completion_kwargs(),
109
107
  }
110
108
 
109
+ if context:
110
+ completion_kwargs["prompt_cache_key"] = context.get_prompt_cache_key(options.system_prompt)
111
+
111
112
  for attempt in range(options.retries):
112
113
  try:
113
- with Laminar.start_as_current_span(model, span_type="LLM", input=messages) as span:
114
+ with Laminar.start_as_current_span(
115
+ model, span_type="LLM", input=processed_messages
116
+ ) as span:
114
117
  response = await _generate(model, processed_messages, completion_kwargs)
115
118
  span.set_attributes(response.get_laminar_metadata())
116
119
  Laminar.set_span_output(response.content)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ai-pipeline-core
3
- Version: 0.1.3
3
+ Version: 0.1.4
4
4
  Summary: Core utilities for AI-powered processing pipelines using prefect
5
5
  Project-URL: Homepage, https://github.com/bbarwik/ai-pipeline-core
6
6
  Project-URL: Repository, https://github.com/bbarwik/ai-pipeline-core
@@ -14,8 +14,8 @@ ai_pipeline_core/documents/utils.py,sha256=BdE4taSl1vrBhxnFbOP5nDA7lXIcvY__AMRTH
14
14
  ai_pipeline_core/flow/__init__.py,sha256=_Sji2yY1ICkvVX6QiiGWKzqIXtg9UAiuvhjHSK_gdO8,57
15
15
  ai_pipeline_core/flow/config.py,sha256=crbe_OvNE6qulIKv1D8yKoe8xrEsIlvICyxjhqHHBxQ,2266
16
16
  ai_pipeline_core/llm/__init__.py,sha256=3XVK-bSJdOe0s6KmmO7PDbsXHfjlcZEG1MVBmaz3EeU,442
17
- ai_pipeline_core/llm/ai_messages.py,sha256=lzzp4t6xDU9ULhwbmORFTQbdl0BbsqxD5YLzlHstQwU,4333
18
- ai_pipeline_core/llm/client.py,sha256=vVzgJXmWcWZ1limRvq_PGkFAhrfSNp18UECSiJtGhYI,7690
17
+ ai_pipeline_core/llm/ai_messages.py,sha256=DwJJe05BtYdnMZeHbBbyEbDCqrW63SRvprxptoJUCn4,4586
18
+ ai_pipeline_core/llm/client.py,sha256=IOcyjwyAKQWlqnwC5p2Hl4FeRCzOJAHC5Yqr_oCBQ8s,7703
19
19
  ai_pipeline_core/llm/model_options.py,sha256=TvAAlDFZN-TP9-J-RZBuU_dpSocskf6paaQMw1XY9UE,1321
20
20
  ai_pipeline_core/llm/model_response.py,sha256=fIWueaemgo0cMruvToMZyKsRPzKwL6IlvUJN7DLG710,5558
21
21
  ai_pipeline_core/llm/model_types.py,sha256=rIwY6voT8-xdfsKPDC0Gkdl2iTp9Q2LuvWGSRU9Mp3k,342
@@ -23,7 +23,7 @@ ai_pipeline_core/logging/__init__.py,sha256=DOO6ckgnMVXl29Sy7q6jhO-iW96h54pCHQDz
23
23
  ai_pipeline_core/logging/logging.yml,sha256=YTW48keO_K5bkkb-KXGM7ZuaYKiquLsjsURei8Ql0V4,1353
24
24
  ai_pipeline_core/logging/logging_config.py,sha256=6MBz9nnVNvqiLDoyy9-R3sWkn6927Re5hdz4hwTptpI,4903
25
25
  ai_pipeline_core/logging/logging_mixin.py,sha256=RDaR2ju2-vKTJRzXGa0DquGPT8_UxahWjvKJnaD0IV8,7810
26
- ai_pipeline_core-0.1.3.dist-info/METADATA,sha256=0TBoEep5C9LSmlOKA3koR2vfKSiIba49SLYLnVLiaWc,15869
27
- ai_pipeline_core-0.1.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
28
- ai_pipeline_core-0.1.3.dist-info/licenses/LICENSE,sha256=kKj8mfbdWwkyG3U6n7ztB3bAZlEwShTkAsvaY657i3I,1074
29
- ai_pipeline_core-0.1.3.dist-info/RECORD,,
26
+ ai_pipeline_core-0.1.4.dist-info/METADATA,sha256=oB5vtkmCTKTlJKiTetHT8Lt8PKgYEAihOGIlKsD8tSQ,15869
27
+ ai_pipeline_core-0.1.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
28
+ ai_pipeline_core-0.1.4.dist-info/licenses/LICENSE,sha256=kKj8mfbdWwkyG3U6n7ztB3bAZlEwShTkAsvaY657i3I,1074
29
+ ai_pipeline_core-0.1.4.dist-info/RECORD,,