promptlayer 1.0.32__py3-none-any.whl → 1.0.34__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.

Potentially problematic release.


This version of promptlayer might be problematic. Click here for more details.

promptlayer/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
1
  from .promptlayer import AsyncPromptLayer, PromptLayer
2
2
 
3
- __version__ = "1.0.32"
3
+ __version__ = "1.0.34"
4
4
  __all__ = ["PromptLayer", "AsyncPromptLayer", "__version__"]
@@ -483,7 +483,7 @@ class AsyncPromptLayer(PromptLayerMixin):
483
483
  )
484
484
  return await atrack_request(**track_request_kwargs)
485
485
 
486
- return await _track_request
486
+ return _track_request
487
487
 
488
488
  async def _track_request_log(
489
489
  self,
@@ -547,15 +547,16 @@ class AsyncPromptLayer(PromptLayerMixin):
547
547
  )
548
548
 
549
549
  if stream:
550
+ track_request_callable = await self._create_track_request_callable(
551
+ request_params=llm_request_params,
552
+ tags=tags,
553
+ input_variables=input_variables,
554
+ group_id=group_id,
555
+ pl_run_span_id=pl_run_span_id,
556
+ )
550
557
  return astream_response(
551
558
  response,
552
- self._create_track_request_callable(
553
- request_params=llm_request_params,
554
- tags=tags,
555
- input_variables=input_variables,
556
- group_id=group_id,
557
- pl_run_span_id=pl_run_span_id,
558
- ),
559
+ track_request_callable,
559
560
  llm_request_params["stream_function"],
560
561
  )
561
562
 
@@ -15,6 +15,8 @@ from promptlayer.utils import (
15
15
  aanthropic_stream_completion,
16
16
  aanthropic_stream_message,
17
17
  aazure_openai_request,
18
+ amistral_request,
19
+ amistral_stream_chat,
18
20
  anthropic_request,
19
21
  anthropic_stream_completion,
20
22
  anthropic_stream_message,
@@ -22,6 +24,8 @@ from promptlayer.utils import (
22
24
  aopenai_stream_chat,
23
25
  aopenai_stream_completion,
24
26
  azure_openai_request,
27
+ mistral_request,
28
+ mistral_stream_chat,
25
29
  openai_request,
26
30
  openai_stream_chat,
27
31
  openai_stream_completion,
@@ -58,6 +62,16 @@ MAP_PROVIDER_TO_FUNCTION_NAME = {
58
62
  "stream_function": openai_stream_completion,
59
63
  },
60
64
  },
65
+ "mistral": {
66
+ "chat": {
67
+ "function_name": "mistral.client.chat",
68
+ "stream_function": mistral_stream_chat,
69
+ },
70
+ "completion": {
71
+ "function_name": None,
72
+ "stream_function": None,
73
+ },
74
+ },
61
75
  }
62
76
 
63
77
 
@@ -65,6 +79,7 @@ MAP_PROVIDER_TO_FUNCTION = {
65
79
  "openai": openai_request,
66
80
  "anthropic": anthropic_request,
67
81
  "openai.azure": azure_openai_request,
82
+ "mistral": mistral_request,
68
83
  }
69
84
 
70
85
  AMAP_PROVIDER_TO_FUNCTION_NAME = {
@@ -98,6 +113,16 @@ AMAP_PROVIDER_TO_FUNCTION_NAME = {
98
113
  "stream_function": aopenai_stream_completion,
99
114
  },
100
115
  },
116
+ "mistral": {
117
+ "chat": {
118
+ "function_name": "mistral.client.chat",
119
+ "stream_function": amistral_stream_chat,
120
+ },
121
+ "completion": {
122
+ "function_name": None,
123
+ "stream_function": None,
124
+ },
125
+ },
101
126
  }
102
127
 
103
128
 
@@ -105,6 +130,7 @@ AMAP_PROVIDER_TO_FUNCTION = {
105
130
  "openai": aopenai_request,
106
131
  "anthropic": aanthropic_request,
107
132
  "openai.azure": aazure_openai_request,
133
+ "mistral": amistral_request,
108
134
  }
109
135
 
110
136
 
promptlayer/utils.py CHANGED
@@ -2,7 +2,6 @@ import asyncio
2
2
  import contextvars
3
3
  import datetime
4
4
  import functools
5
- import inspect
6
5
  import json
7
6
  import os
8
7
  import sys
@@ -1449,15 +1448,17 @@ async def astream_response(
1449
1448
  results.append(result)
1450
1449
  data["raw_response"] = result
1451
1450
  yield data
1452
- request_response = await map_results(results)
1453
- if inspect.iscoroutinefunction(after_stream):
1454
- # after_stream is an async function
1455
- response = await after_stream(request_response=request_response.model_dump())
1456
- else:
1457
- # after_stream is synchronous
1458
- response = after_stream(request_response=request_response.model_dump())
1459
- data["request_id"] = response.get("request_id")
1460
- data["prompt_blueprint"] = response.get("prompt_blueprint")
1451
+
1452
+ async def async_generator_from_list(lst):
1453
+ for item in lst:
1454
+ yield item
1455
+
1456
+ request_response = await map_results(async_generator_from_list(results))
1457
+ after_stream_response = await after_stream(
1458
+ request_response=request_response.model_dump()
1459
+ )
1460
+ data["request_id"] = after_stream_response.get("request_id")
1461
+ data["prompt_blueprint"] = after_stream_response.get("prompt_blueprint")
1461
1462
  yield data
1462
1463
 
1463
1464
 
@@ -1631,3 +1632,165 @@ async def autil_log_request(api_key: str, **kwargs) -> Union[RequestLog, None]:
1631
1632
  file=sys.stderr,
1632
1633
  )
1633
1634
  return None
1635
+
1636
+
1637
+ def mistral_request(
1638
+ prompt_blueprint: GetPromptTemplateResponse,
1639
+ **kwargs,
1640
+ ):
1641
+ from mistralai import Mistral
1642
+
1643
+ client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY"))
1644
+ if "stream" in kwargs and kwargs["stream"]:
1645
+ kwargs.pop("stream")
1646
+ return client.chat.stream(**kwargs)
1647
+ if "stream" in kwargs:
1648
+ kwargs.pop("stream")
1649
+ return client.chat.complete(**kwargs)
1650
+
1651
+
1652
+ async def amistral_request(
1653
+ prompt_blueprint: GetPromptTemplateResponse,
1654
+ **kwargs,
1655
+ ):
1656
+ from mistralai import Mistral
1657
+
1658
+ client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY"))
1659
+ if "stream" in kwargs and kwargs["stream"]:
1660
+ return await client.chat.stream_async(**kwargs)
1661
+ return await client.chat.complete_async(**kwargs)
1662
+
1663
+
1664
+ def mistral_stream_chat(results: list):
1665
+ from openai.types.chat import (
1666
+ ChatCompletion,
1667
+ ChatCompletionMessage,
1668
+ ChatCompletionMessageToolCall,
1669
+ )
1670
+ from openai.types.chat.chat_completion import Choice
1671
+ from openai.types.chat.chat_completion_message_tool_call import Function
1672
+
1673
+ last_result = results[-1]
1674
+ response = ChatCompletion(
1675
+ id=last_result.data.id,
1676
+ object="chat.completion",
1677
+ choices=[
1678
+ Choice(
1679
+ finish_reason=last_result.data.choices[0].finish_reason or "stop",
1680
+ index=0,
1681
+ message=ChatCompletionMessage(role="assistant"),
1682
+ )
1683
+ ],
1684
+ created=last_result.data.created,
1685
+ model=last_result.data.model,
1686
+ )
1687
+
1688
+ content = ""
1689
+ tool_calls = None
1690
+
1691
+ for result in results:
1692
+ choices = result.data.choices
1693
+ if len(choices) == 0:
1694
+ continue
1695
+
1696
+ delta = choices[0].delta
1697
+ if delta.content is not None:
1698
+ content = f"{content}{delta.content}"
1699
+
1700
+ if delta.tool_calls:
1701
+ tool_calls = tool_calls or []
1702
+ for tool_call in delta.tool_calls:
1703
+ if len(tool_calls) == 0 or tool_call.id:
1704
+ tool_calls.append(
1705
+ ChatCompletionMessageToolCall(
1706
+ id=tool_call.id or "",
1707
+ function=Function(
1708
+ name=tool_call.function.name,
1709
+ arguments=tool_call.function.arguments,
1710
+ ),
1711
+ type="function",
1712
+ )
1713
+ )
1714
+ else:
1715
+ last_tool_call = tool_calls[-1]
1716
+ if tool_call.function.name:
1717
+ last_tool_call.function.name = (
1718
+ f"{last_tool_call.function.name}{tool_call.function.name}"
1719
+ )
1720
+ if tool_call.function.arguments:
1721
+ last_tool_call.function.arguments = f"{last_tool_call.function.arguments}{tool_call.function.arguments}"
1722
+
1723
+ response.choices[0].message.content = content
1724
+ response.choices[0].message.tool_calls = tool_calls
1725
+ response.usage = last_result.data.usage
1726
+ return response
1727
+
1728
+
1729
+ async def amistral_stream_chat(generator: AsyncIterable[Any]) -> Any:
1730
+ from openai.types.chat import (
1731
+ ChatCompletion,
1732
+ ChatCompletionMessage,
1733
+ ChatCompletionMessageToolCall,
1734
+ )
1735
+ from openai.types.chat.chat_completion import Choice
1736
+ from openai.types.chat.chat_completion_message_tool_call import Function
1737
+
1738
+ completion_chunks = []
1739
+ response = ChatCompletion(
1740
+ id="",
1741
+ object="chat.completion",
1742
+ choices=[
1743
+ Choice(
1744
+ finish_reason="stop",
1745
+ index=0,
1746
+ message=ChatCompletionMessage(role="assistant"),
1747
+ )
1748
+ ],
1749
+ created=0,
1750
+ model="",
1751
+ )
1752
+ content = ""
1753
+ tool_calls = None
1754
+
1755
+ async for result in generator:
1756
+ completion_chunks.append(result)
1757
+ choices = result.data.choices
1758
+ if len(choices) == 0:
1759
+ continue
1760
+ delta = choices[0].delta
1761
+ if delta.content is not None:
1762
+ content = f"{content}{delta.content}"
1763
+
1764
+ if delta.tool_calls:
1765
+ tool_calls = tool_calls or []
1766
+ for tool_call in delta.tool_calls:
1767
+ if len(tool_calls) == 0 or tool_call.id:
1768
+ tool_calls.append(
1769
+ ChatCompletionMessageToolCall(
1770
+ id=tool_call.id or "",
1771
+ function=Function(
1772
+ name=tool_call.function.name,
1773
+ arguments=tool_call.function.arguments,
1774
+ ),
1775
+ type="function",
1776
+ )
1777
+ )
1778
+ else:
1779
+ last_tool_call = tool_calls[-1]
1780
+ if tool_call.function.name:
1781
+ last_tool_call.function.name = (
1782
+ f"{last_tool_call.function.name}{tool_call.function.name}"
1783
+ )
1784
+ if tool_call.function.arguments:
1785
+ last_tool_call.function.arguments = f"{last_tool_call.function.arguments}{tool_call.function.arguments}"
1786
+
1787
+ if completion_chunks:
1788
+ last_result = completion_chunks[-1]
1789
+ response.id = last_result.data.id
1790
+ response.created = last_result.data.created
1791
+ response.model = last_result.data.model
1792
+ response.usage = last_result.data.usage
1793
+
1794
+ response.choices[0].message.content = content
1795
+ response.choices[0].message.tool_calls = tool_calls
1796
+ return response
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: promptlayer
3
- Version: 1.0.32
3
+ Version: 1.0.34
4
4
  Summary: PromptLayer is a platform for prompt engineering and tracks your LLM requests.
5
5
  License: Apache-2.0
6
6
  Author: Magniv
@@ -1,9 +1,9 @@
1
- promptlayer/__init__.py,sha256=ZWwec3lasIs6PTQi77sYwX746yWKAQc2p8Lg8fLZrO4,140
1
+ promptlayer/__init__.py,sha256=i7ms716ks4DoOJfFle-Me41ZwM58biK11-kQqjjH-Zo,140
2
2
  promptlayer/groups/__init__.py,sha256=xhOAolLUBkr76ZHvJr29OwjCIk1V9qKQXjZCuyTJUIY,429
3
3
  promptlayer/groups/groups.py,sha256=YPROicy-TzpkrpA8vOpZS2lwvJ6VRtlbQ1S2oT1N0vM,338
4
- promptlayer/promptlayer.py,sha256=ZzHLMwbF-qq9SXNb610tmAOnGzOEvugGjHp3MtGWMGA,20083
4
+ promptlayer/promptlayer.py,sha256=rMqH5c-NpfGo41gbaZdurXvUIOa6-QJWDAZ4W5ajJko,20119
5
5
  promptlayer/promptlayer_base.py,sha256=sev-EZehRXJSZSmJtMkqmAUK1345pqbDY_lNjPP5MYA,7158
6
- promptlayer/promptlayer_mixins.py,sha256=CZRX-kjd067JLzxd0qOWBnOCHdC5CZ4bXFDs7CDMplg,9572
6
+ promptlayer/promptlayer_mixins.py,sha256=_7LRo8Mj8FhkIQZByEh2gX_lS0GjB6Bo94k0QWFJVMM,10260
7
7
  promptlayer/span_exporter.py,sha256=zIJNsb3Fe6yb5wKLDmkoPF2wqFjk1p39E0jWHD2plzI,2658
8
8
  promptlayer/templates.py,sha256=bdX8ZxydWwF9QMF1UBD-qoYqYRPrUSTAt88r2D8ws7c,1193
9
9
  promptlayer/track/__init__.py,sha256=8J258daTXb_P8eHRbYR2Au1lJzTh_92UkOHf7q0NpKs,1757
@@ -11,8 +11,8 @@ promptlayer/track/track.py,sha256=UdkCxhWUvhvPdhsoHj4qmeiRq6xLcWmeIdYXrgZph04,32
11
11
  promptlayer/types/__init__.py,sha256=xJcvQuOk91ZBBePb40-1FDNDKYrZoH5lPE2q6_UhprM,111
12
12
  promptlayer/types/prompt_template.py,sha256=sv5iMV8Iix2LCMUSMTkfb8PohfKp-vR9hRMck8OXSVc,4728
13
13
  promptlayer/types/request_log.py,sha256=xU6bcxQar6GaBOJlgZTavXUV3FjE8sF_nSjPu4Ya_00,174
14
- promptlayer/utils.py,sha256=yPEzt3JZT7rbVch39Gv8Uk0-CR8Ih5Ym9iIfS0VEEH8,53960
15
- promptlayer-1.0.32.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
16
- promptlayer-1.0.32.dist-info/METADATA,sha256=5TUZRxITDb2RVq9j9xdHlK8BHTFzp6_HNJjPhK3uKk8,4824
17
- promptlayer-1.0.32.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
18
- promptlayer-1.0.32.dist-info/RECORD,,
14
+ promptlayer/utils.py,sha256=230OFFkG2DoaRCh-sgUZNoQxKEQT0RVKW3jJ6CciQKo,59465
15
+ promptlayer-1.0.34.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
16
+ promptlayer-1.0.34.dist-info/METADATA,sha256=SOTWiUCjpanIjd9YusTV9e3tb9Ihz9FBRanSqoY5aSQ,4824
17
+ promptlayer-1.0.34.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
18
+ promptlayer-1.0.34.dist-info/RECORD,,