langtrace-python-sdk 2.1.9__py3-none-any.whl → 2.1.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.
@@ -7,7 +7,7 @@ from openai import OpenAI
7
7
  from pinecone import Pinecone, ServerlessSpec
8
8
 
9
9
  from langtrace_python_sdk import langtrace, with_langtrace_root_span
10
- from langtrace_python_sdk.utils.with_root_span import send_user_feedback
10
+ from langtrace_python_sdk.utils.with_root_span import SendUserFeedback
11
11
 
12
12
  _ = load_dotenv(find_dotenv())
13
13
 
@@ -48,7 +48,7 @@ def basic(span_id=None, trace_id=None):
48
48
  resp = index.query(
49
49
  vector=embedding, top_k=1, include_values=False, namespace="test-namespace"
50
50
  )
51
- send_user_feedback(
51
+ SendUserFeedback().evaluate(
52
52
  {"spanId": span_id, "traceId": trace_id, "userScore": 1, "userId": "123"}
53
53
  )
54
54
  return [res, resp]
@@ -17,11 +17,11 @@ limitations under the License.
17
17
  from langtrace_python_sdk import langtrace
18
18
  from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
19
19
  from langtrace_python_sdk.utils.prompt_registry import get_prompt_from_registry
20
- from langtrace_python_sdk.utils.with_root_span import send_user_feedback
20
+ from langtrace_python_sdk.utils.with_root_span import SendUserFeedback
21
21
 
22
22
  __all__ = [
23
23
  "langtrace",
24
24
  "with_langtrace_root_span",
25
25
  "get_prompt_from_registry",
26
- "send_user_feedback",
26
+ "SendUserFeedback",
27
27
  ]
@@ -34,6 +34,7 @@ from langtrace_python_sdk.utils.types import (
34
34
  LangTraceApiError,
35
35
  LangTraceEvaluation,
36
36
  )
37
+ from colorama import Fore
37
38
 
38
39
 
39
40
  def with_langtrace_root_span(
@@ -112,63 +113,78 @@ def with_additional_attributes(attributes={}):
112
113
  return decorator
113
114
 
114
115
 
115
- def send_user_feedback(data: EvaluationAPIData) -> None:
116
- try:
117
- evaluation = _get_evaluation(data["spanId"])
118
- headers = {"x-api-key": os.environ["LANGTRACE_API_KEY"]}
119
- if evaluation is not None:
120
- # Make a PUT request to update the evaluation
121
- response = requests.put(
122
- f"{LANGTRACE_REMOTE_URL}/api/evaluation",
123
- json=data,
124
- params={"spanId": data["spanId"]},
125
- headers=headers,
126
- timeout=None,
127
- )
128
- response.raise_for_status()
116
+ class SendUserFeedback:
117
+ _langtrace_host: str
118
+ _langtrace_api_key: str
129
119
 
130
- else:
131
- # Make a POST request to create a new evaluation
132
- response = requests.post(
133
- f"{LANGTRACE_REMOTE_URL}/api/evaluation",
134
- json=data,
135
- params={"spanId": data["spanId"]},
136
- headers=headers,
120
+ def __init__(self):
121
+ self._langtrace_host = os.environ.get(
122
+ "LANGTRACE_API_HOST", LANGTRACE_REMOTE_URL
123
+ )
124
+ self._langtrace_api_key = os.environ.get("LANGTRACE_API_KEY", None)
125
+
126
+ def evaluate(self, data: EvaluationAPIData) -> None:
127
+ try:
128
+ if self._langtrace_api_key is None:
129
+ print(Fore.RED)
130
+ print(
131
+ f"Missing Langtrace API key, proceed to {self._langtrace_host} to create one"
132
+ )
133
+ print("Set the API key as an environment variable LANGTRACE_API_KEY")
134
+ print(Fore.RESET)
135
+ return
136
+ evaluation = self.get_evaluation(data["spanId"])
137
+ headers = {"x-api-key": self._langtrace_api_key}
138
+ if evaluation is not None:
139
+ # Make a PUT request to update the evaluation
140
+ response = requests.put(
141
+ f"{self._langtrace_host}/api/evaluation",
142
+ json=data,
143
+ params={"spanId": data["spanId"]},
144
+ headers=headers,
145
+ timeout=None,
146
+ )
147
+ response.raise_for_status()
148
+
149
+ else:
150
+ # Make a POST request to create a new evaluation
151
+ response = requests.post(
152
+ f"{self._langtrace_host}/api/evaluation",
153
+ json=data,
154
+ params={"spanId": data["spanId"]},
155
+ headers=headers,
156
+ timeout=None,
157
+ )
158
+ response.raise_for_status()
159
+
160
+ except requests.RequestException as err:
161
+ if err.response is not None:
162
+ message = (
163
+ err.response.json().get("message", "")
164
+ if err.response.json().get("message", "")
165
+ else err.response.text if err.response.text else str(err)
166
+ )
167
+ raise LangTraceApiError(message, err.response.status_code)
168
+ raise LangTraceApiError(str(err), 500)
169
+
170
+ def get_evaluation(self, span_id: str) -> Optional[LangTraceEvaluation]:
171
+ try:
172
+ response = requests.get(
173
+ f"{self._langtrace_host}/api/evaluation",
174
+ params={"spanId": span_id},
175
+ headers={"x-api-key": self._langtrace_api_key},
137
176
  timeout=None,
138
177
  )
178
+ evaluations = response.json().get("evaluations", [])
139
179
  response.raise_for_status()
140
-
141
- except requests.RequestException as err:
142
- # Handle specific HTTP errors or general request exceptions
143
- error_msg = str(err)
144
- if err.response:
145
- try:
146
- # Try to extract server-provided error message
147
- error_msg = err.response.json().get("error", error_msg)
148
- except ValueError:
149
- # Fallback if response is not JSON
150
- error_msg = err.response.text
151
- raise Exception(f"API error: {error_msg}") from err
152
-
153
-
154
- def _get_evaluation(span_id: str) -> Optional[LangTraceEvaluation]:
155
- try:
156
- response = requests.get(
157
- f"{LANGTRACE_REMOTE_URL}/api/evaluation",
158
- params={"spanId": span_id},
159
- headers={"x-api-key": os.environ["LANGTRACE_API_KEY"]},
160
- timeout=None,
161
- )
162
- evaluations = response.json().get("evaluations", [])
163
- response.raise_for_status()
164
- return None if not evaluations else evaluations[0]
165
-
166
- except requests.RequestException as err:
167
- if err.response is not None:
168
- message = (
169
- err.response.json().get("message", "")
170
- if err.response.json().get("message", "")
171
- else err.response.text if err.response.text else str(err)
172
- )
173
- raise LangTraceApiError(message, err.response.status_code)
174
- raise LangTraceApiError(str(err), 500)
180
+ return None if not evaluations else evaluations[0]
181
+
182
+ except requests.RequestException as err:
183
+ if err.response is not None:
184
+ message = (
185
+ err.response.json().get("message", "")
186
+ if err.response.json().get("message", "")
187
+ else err.response.text if err.response.text else str(err)
188
+ )
189
+ raise LangTraceApiError(message, err.response.status_code)
190
+ raise LangTraceApiError(str(err), 500)
@@ -1 +1 @@
1
- __version__ = "2.1.9"
1
+ __version__ = "2.1.10"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: langtrace-python-sdk
3
- Version: 2.1.9
3
+ Version: 2.1.10
4
4
  Summary: Python SDK for LangTrace
5
5
  Project-URL: Homepage, https://github.com/Scale3-Labs/langtrace-python-sdk
6
6
  Author-email: Scale3 Labs <engineering@scale3labs.com>
@@ -32,13 +32,13 @@ examples/openai_example/tool_calling_nonstreaming.py,sha256=Yc848IooZRXNynHL6z0k
32
32
  examples/openai_example/tool_calling_streaming.py,sha256=mV1RbyAoVhumGRPpqPWQ6PMhnJyeifrlELd2-K1qJ_w,7015
33
33
  examples/perplexity_example/basic.py,sha256=bp7n27gaugJkaFVyt8pjaEfi66lYcqP6eFFjPewUShY,668
34
34
  examples/pinecone_example/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
35
- examples/pinecone_example/basic.py,sha256=b-5-cSf3_N6Gk4zGVecGF47mBcFY9ZrC5ztrIZVgTQo,1430
35
+ examples/pinecone_example/basic.py,sha256=gkb1uQwKFLnWmRuM2KGvAeTpT1AYlHmz2kRey5_-Dqs,1437
36
36
  examples/qdrant_example/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
37
37
  examples/qdrant_example/basic.py,sha256=DCMjHSuBZKkhEjCkwy5d5La9WMyW0lCWqtcZWiFCEm4,1425
38
38
  examples/weaviate_example/query_text.py,sha256=iE9OiHsibjsprbCGzabE03eZsGN06e6ym2iS1A9P3ig,64650
39
- langtrace_python_sdk/__init__.py,sha256=R4dv73QARUw4WRfCdSKkKzZTLDumF9jcSdwtVrzWhb4,962
39
+ langtrace_python_sdk/__init__.py,sha256=FuvyRuStRe_N2wo2SB2_ZQ0w7LGNIjV0lLi6S1IgGwY,958
40
40
  langtrace_python_sdk/langtrace.py,sha256=_qJsbLo-4GqhXd8apg3W7A_l3ZnFYEX1wOA-_rg-TZA,6655
41
- langtrace_python_sdk/version.py,sha256=Gf0zUMmdRLkPmvwnoP7oCpWn7Ep93jlNIlBD6nifugI,22
41
+ langtrace_python_sdk/version.py,sha256=zBzBCTHx_lpM20-2wHeccRHskIUls_oSQ1tzAOiGGrU,23
42
42
  langtrace_python_sdk/constants/__init__.py,sha256=P8QvYwt5czUNDZsKS64vxm9Dc41ptGbuF1TFtAF6nv4,44
43
43
  langtrace_python_sdk/constants/exporter/langtrace_exporter.py,sha256=5MNjnAOg-4am78J3gVMH6FSwq5N8TOj72ugkhsw4vi0,46
44
44
  langtrace_python_sdk/constants/instrumentation/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -101,7 +101,7 @@ langtrace_python_sdk/utils/prompt_registry.py,sha256=7FFB4Pj0414qgf02h5zL5vXBZgN
101
101
  langtrace_python_sdk/utils/sdk_version_checker.py,sha256=FzjIWZjn53cX0LEVPdipQd1fO9lG8iGVUEVUs9Hyk6M,1713
102
102
  langtrace_python_sdk/utils/silently_fail.py,sha256=F_9EteXCO9Cyq-8MA1OT2Zy_dx8n06nt31I7t7ui24E,478
103
103
  langtrace_python_sdk/utils/types.py,sha256=l-N6o7cnWUyrD6dBvW7W3Pf5CkPo5QaoT__k1XLbrQg,383
104
- langtrace_python_sdk/utils/with_root_span.py,sha256=rSPrTnKndJo6cFUWL4907IkJFbtLwqMS87paADUxD6A,5957
104
+ langtrace_python_sdk/utils/with_root_span.py,sha256=wORIk9N4BOWEP9zoDMXpD3qX4r6P6sDMUn9bzl8BbvM,6754
105
105
  tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
106
106
  tests/conftest.py,sha256=0Jo6iCZTXbdvyJVhG9UpYGkLabL75378oauCzmt-Sa8,603
107
107
  tests/utils.py,sha256=hP8sTH-M8WO6zlLkSFHPf6483ZcKEcnxul6JIIb1pLM,1396
@@ -138,7 +138,7 @@ tests/pinecone/cassettes/test_query.yaml,sha256=b5v9G3ssUy00oG63PlFUR3JErF2Js-5A
138
138
  tests/pinecone/cassettes/test_upsert.yaml,sha256=neWmQ1v3d03V8WoLl8FoFeeCYImb8pxlJBWnFd_lITU,38607
139
139
  tests/qdrant/conftest.py,sha256=9n0uHxxIjWk9fbYc4bx-uP8lSAgLBVx-cV9UjnsyCHM,381
140
140
  tests/qdrant/test_qdrant.py,sha256=pzjAjVY2kmsmGfrI2Gs2xrolfuaNHz7l1fqGQCjp5_o,3353
141
- langtrace_python_sdk-2.1.9.dist-info/METADATA,sha256=z55Vy4oeKQROK5MlKoeYpJfzHxqMvhlfEXjr2PSXAZc,11859
142
- langtrace_python_sdk-2.1.9.dist-info/WHEEL,sha256=zEMcRr9Kr03x1ozGwg5v9NQBKn3kndp6LSoSlVg-jhU,87
143
- langtrace_python_sdk-2.1.9.dist-info/licenses/LICENSE,sha256=QwcOLU5TJoTeUhuIXzhdCEEDDvorGiC6-3YTOl4TecE,11356
144
- langtrace_python_sdk-2.1.9.dist-info/RECORD,,
141
+ langtrace_python_sdk-2.1.10.dist-info/METADATA,sha256=2k5jRe5pbkZ1IJ6iTSx67R_vg6_97O4_W9eSr5_f8Zs,11860
142
+ langtrace_python_sdk-2.1.10.dist-info/WHEEL,sha256=zEMcRr9Kr03x1ozGwg5v9NQBKn3kndp6LSoSlVg-jhU,87
143
+ langtrace_python_sdk-2.1.10.dist-info/licenses/LICENSE,sha256=QwcOLU5TJoTeUhuIXzhdCEEDDvorGiC6-3YTOl4TecE,11356
144
+ langtrace_python_sdk-2.1.10.dist-info/RECORD,,