promptlayer 1.0.61__tar.gz → 1.0.62__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.
Potentially problematic release.
This version of promptlayer might be problematic. Click here for more details.
- {promptlayer-1.0.61 → promptlayer-1.0.62}/PKG-INFO +1 -1
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/__init__.py +1 -1
- promptlayer-1.0.62/promptlayer/streaming/stream_processor.py +77 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/utils.py +6 -2
- {promptlayer-1.0.61 → promptlayer-1.0.62}/pyproject.toml +1 -1
- promptlayer-1.0.61/promptlayer/streaming/stream_processor.py +0 -100
- {promptlayer-1.0.61 → promptlayer-1.0.62}/LICENSE +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/README.md +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/groups/__init__.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/groups/groups.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/promptlayer.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/promptlayer_base.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/promptlayer_mixins.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/span_exporter.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/streaming/__init__.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/streaming/blueprint_builder.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/streaming/response_handlers.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/templates.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/track/__init__.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/track/track.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/types/__init__.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/types/prompt_template.py +0 -0
- {promptlayer-1.0.61 → promptlayer-1.0.62}/promptlayer/types/request_log.py +0 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
from typing import Any, AsyncGenerator, AsyncIterable, Callable, Dict, Generator
|
|
2
|
+
|
|
3
|
+
from .blueprint_builder import (
|
|
4
|
+
build_prompt_blueprint_from_anthropic_event,
|
|
5
|
+
build_prompt_blueprint_from_google_event,
|
|
6
|
+
build_prompt_blueprint_from_openai_chunk,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _build_stream_blueprint(result: Any, metadata: Dict) -> Any:
|
|
11
|
+
model_info = metadata.get("model", {}) if metadata else {}
|
|
12
|
+
provider = model_info.get("provider", "")
|
|
13
|
+
model_name = model_info.get("name", "")
|
|
14
|
+
|
|
15
|
+
if provider == "openai" or provider == "openai.azure":
|
|
16
|
+
return build_prompt_blueprint_from_openai_chunk(result, metadata)
|
|
17
|
+
|
|
18
|
+
elif provider == "google" or (provider == "vertexai" and model_name.startswith("gemini")):
|
|
19
|
+
return build_prompt_blueprint_from_google_event(result, metadata)
|
|
20
|
+
|
|
21
|
+
elif provider == "anthropic" or (provider == "vertexai" and model_name.startswith("claude")):
|
|
22
|
+
return build_prompt_blueprint_from_anthropic_event(result, metadata)
|
|
23
|
+
|
|
24
|
+
elif provider == "mistral":
|
|
25
|
+
return build_prompt_blueprint_from_openai_chunk(result.data, metadata)
|
|
26
|
+
|
|
27
|
+
return None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _build_stream_data(result: Any, stream_blueprint: Any, request_id: Any = None) -> Dict[str, Any]:
|
|
31
|
+
return {
|
|
32
|
+
"request_id": request_id,
|
|
33
|
+
"raw_response": result,
|
|
34
|
+
"prompt_blueprint": stream_blueprint,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def stream_response(*, generator: Generator, after_stream: Callable, map_results: Callable, metadata: Dict):
|
|
39
|
+
results = []
|
|
40
|
+
for result in generator:
|
|
41
|
+
results.append(result)
|
|
42
|
+
|
|
43
|
+
stream_blueprint = _build_stream_blueprint(result, metadata)
|
|
44
|
+
data = _build_stream_data(result, stream_blueprint)
|
|
45
|
+
yield data
|
|
46
|
+
|
|
47
|
+
request_response = map_results(results)
|
|
48
|
+
response = after_stream(request_response=request_response.model_dump(mode="json"))
|
|
49
|
+
data["request_id"] = response.get("request_id")
|
|
50
|
+
data["prompt_blueprint"] = response.get("prompt_blueprint")
|
|
51
|
+
yield data
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
async def astream_response(
|
|
55
|
+
generator: AsyncIterable[Any],
|
|
56
|
+
after_stream: Callable[..., Any],
|
|
57
|
+
map_results: Callable[[Any], Any],
|
|
58
|
+
metadata: Dict[str, Any] = None,
|
|
59
|
+
) -> AsyncGenerator[Dict[str, Any], None]:
|
|
60
|
+
results = []
|
|
61
|
+
|
|
62
|
+
async for result in generator:
|
|
63
|
+
results.append(result)
|
|
64
|
+
|
|
65
|
+
stream_blueprint = _build_stream_blueprint(result, metadata)
|
|
66
|
+
data = _build_stream_data(result, stream_blueprint)
|
|
67
|
+
yield data
|
|
68
|
+
|
|
69
|
+
async def async_generator_from_list(lst):
|
|
70
|
+
for item in lst:
|
|
71
|
+
yield item
|
|
72
|
+
|
|
73
|
+
request_response = await map_results(async_generator_from_list(results))
|
|
74
|
+
after_stream_response = await after_stream(request_response=request_response.model_dump(mode="json"))
|
|
75
|
+
data["request_id"] = after_stream_response.get("request_id")
|
|
76
|
+
data["prompt_blueprint"] = after_stream_response.get("prompt_blueprint")
|
|
77
|
+
yield data
|
|
@@ -59,6 +59,10 @@ def _make_httpx_client():
|
|
|
59
59
|
return httpx.AsyncClient(timeout=_get_http_timeout())
|
|
60
60
|
|
|
61
61
|
|
|
62
|
+
def _make_simple_httpx_client():
|
|
63
|
+
return httpx.Client(timeout=_get_http_timeout())
|
|
64
|
+
|
|
65
|
+
|
|
62
66
|
def _get_workflow_workflow_id_or_name(workflow_id_or_name, workflow_name):
|
|
63
67
|
# This is backward compatibility code
|
|
64
68
|
if (workflow_id_or_name := workflow_name if workflow_id_or_name is None else workflow_id_or_name) is None:
|
|
@@ -1396,7 +1400,7 @@ async def autil_log_request(api_key: str, **kwargs) -> Union[RequestLog, None]:
|
|
|
1396
1400
|
def mistral_request(prompt_blueprint: GetPromptTemplateResponse, client_kwargs: dict, function_kwargs: dict):
|
|
1397
1401
|
from mistralai import Mistral
|
|
1398
1402
|
|
|
1399
|
-
client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY"))
|
|
1403
|
+
client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY"), client=_make_simple_httpx_client())
|
|
1400
1404
|
if "stream" in function_kwargs and function_kwargs["stream"]:
|
|
1401
1405
|
function_kwargs.pop("stream")
|
|
1402
1406
|
return client.chat.stream(**function_kwargs)
|
|
@@ -1412,7 +1416,7 @@ async def amistral_request(
|
|
|
1412
1416
|
):
|
|
1413
1417
|
from mistralai import Mistral
|
|
1414
1418
|
|
|
1415
|
-
client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY"))
|
|
1419
|
+
client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY"), async_client=_make_httpx_client())
|
|
1416
1420
|
if "stream" in function_kwargs and function_kwargs["stream"]:
|
|
1417
1421
|
return await client.chat.stream_async(**function_kwargs)
|
|
1418
1422
|
return await client.chat.complete_async(**function_kwargs)
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Stream processors for handling streaming responses
|
|
3
|
-
|
|
4
|
-
This module contains the main streaming logic that processes streaming responses
|
|
5
|
-
from various LLM providers and builds progressive prompt blueprints.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
from typing import Any, AsyncGenerator, AsyncIterable, Callable, Dict, Generator
|
|
9
|
-
|
|
10
|
-
from .blueprint_builder import (
|
|
11
|
-
build_prompt_blueprint_from_anthropic_event,
|
|
12
|
-
build_prompt_blueprint_from_google_event,
|
|
13
|
-
build_prompt_blueprint_from_openai_chunk,
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def stream_response(*, generator: Generator, after_stream: Callable, map_results: Callable, metadata: Dict):
|
|
18
|
-
"""
|
|
19
|
-
Process streaming responses and build progressive prompt blueprints
|
|
20
|
-
|
|
21
|
-
Supports OpenAI, Anthropic, and Google (Gemini) streaming formats, building blueprints
|
|
22
|
-
progressively as the stream progresses.
|
|
23
|
-
"""
|
|
24
|
-
results = []
|
|
25
|
-
stream_blueprint = None
|
|
26
|
-
for result in generator:
|
|
27
|
-
results.append(result)
|
|
28
|
-
|
|
29
|
-
# Handle OpenAI streaming format - process each chunk individually
|
|
30
|
-
if hasattr(result, "choices"):
|
|
31
|
-
stream_blueprint = build_prompt_blueprint_from_openai_chunk(result, metadata)
|
|
32
|
-
|
|
33
|
-
# Handle Google streaming format (Gemini) - GenerateContentResponse objects
|
|
34
|
-
elif hasattr(result, "candidates"):
|
|
35
|
-
stream_blueprint = build_prompt_blueprint_from_google_event(result, metadata)
|
|
36
|
-
|
|
37
|
-
# Handle Anthropic streaming format - process each event individually
|
|
38
|
-
elif hasattr(result, "type"):
|
|
39
|
-
stream_blueprint = build_prompt_blueprint_from_anthropic_event(result, metadata)
|
|
40
|
-
|
|
41
|
-
data = {
|
|
42
|
-
"request_id": None,
|
|
43
|
-
"raw_response": result,
|
|
44
|
-
"prompt_blueprint": stream_blueprint,
|
|
45
|
-
}
|
|
46
|
-
yield data
|
|
47
|
-
|
|
48
|
-
request_response = map_results(results)
|
|
49
|
-
response = after_stream(request_response=request_response.model_dump(mode="json"))
|
|
50
|
-
data["request_id"] = response.get("request_id")
|
|
51
|
-
data["prompt_blueprint"] = response.get("prompt_blueprint")
|
|
52
|
-
yield data
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
async def astream_response(
|
|
56
|
-
generator: AsyncIterable[Any],
|
|
57
|
-
after_stream: Callable[..., Any],
|
|
58
|
-
map_results: Callable[[Any], Any],
|
|
59
|
-
metadata: Dict[str, Any] = None,
|
|
60
|
-
) -> AsyncGenerator[Dict[str, Any], None]:
|
|
61
|
-
"""
|
|
62
|
-
Async version of stream_response
|
|
63
|
-
|
|
64
|
-
Process streaming responses asynchronously and build progressive prompt blueprints
|
|
65
|
-
Supports OpenAI, Anthropic, and Google (Gemini) streaming formats.
|
|
66
|
-
"""
|
|
67
|
-
results = []
|
|
68
|
-
stream_blueprint = None
|
|
69
|
-
|
|
70
|
-
async for result in generator:
|
|
71
|
-
results.append(result)
|
|
72
|
-
|
|
73
|
-
# Handle OpenAI streaming format - process each chunk individually
|
|
74
|
-
if hasattr(result, "choices"):
|
|
75
|
-
stream_blueprint = build_prompt_blueprint_from_openai_chunk(result, metadata)
|
|
76
|
-
|
|
77
|
-
# Handle Google streaming format (Gemini) - GenerateContentResponse objects
|
|
78
|
-
elif hasattr(result, "candidates"):
|
|
79
|
-
stream_blueprint = build_prompt_blueprint_from_google_event(result, metadata)
|
|
80
|
-
|
|
81
|
-
# Handle Anthropic streaming format - process each event individually
|
|
82
|
-
elif hasattr(result, "type"):
|
|
83
|
-
stream_blueprint = build_prompt_blueprint_from_anthropic_event(result, metadata)
|
|
84
|
-
|
|
85
|
-
data = {
|
|
86
|
-
"request_id": None,
|
|
87
|
-
"raw_response": result,
|
|
88
|
-
"prompt_blueprint": stream_blueprint,
|
|
89
|
-
}
|
|
90
|
-
yield data
|
|
91
|
-
|
|
92
|
-
async def async_generator_from_list(lst):
|
|
93
|
-
for item in lst:
|
|
94
|
-
yield item
|
|
95
|
-
|
|
96
|
-
request_response = await map_results(async_generator_from_list(results))
|
|
97
|
-
after_stream_response = await after_stream(request_response=request_response.model_dump(mode="json"))
|
|
98
|
-
data["request_id"] = after_stream_response.get("request_id")
|
|
99
|
-
data["prompt_blueprint"] = after_stream_response.get("prompt_blueprint")
|
|
100
|
-
yield data
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|