langtrace-python-sdk 2.2.2__py3-none-any.whl → 2.2.3__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,8 +1,8 @@
1
+ import contextvars
1
2
  import dspy
2
3
  from dspy.datasets.gsm8k import GSM8K, gsm8k_metric
3
4
  from dspy.teleprompt import BootstrapFewShot
4
5
  from concurrent.futures import ThreadPoolExecutor
5
- from opentelemetry.context import get_current, attach, detach
6
6
 
7
7
  # flake8: noqa
8
8
  from langtrace_python_sdk import langtrace, with_langtrace_root_span
@@ -22,7 +22,8 @@ class CoT(dspy.Module):
22
22
  self.prog = dspy.ChainOfThought("question -> answer")
23
23
 
24
24
  def forward(self, question):
25
- return self.prog(question=question)
25
+ result = inject_additional_attributes(lambda: self.prog(question=question), {'langtrace.span.name': 'MathProblemsCotParallel'})
26
+ return result
26
27
 
27
28
  @with_langtrace_root_span(name="parallel_example")
28
29
  def example():
@@ -34,21 +35,12 @@ def example():
34
35
  optimized_cot = teleprompter.compile(CoT(), trainset=gsm8k_trainset)
35
36
 
36
37
  questions = [
37
- "What is the cosine of 0?",
38
- "What is the tangent of 0?",
38
+ "What is the sine of 0?",
39
+ "What is the tangent of 100?",
39
40
  ]
40
41
 
41
- current_context = get_current()
42
-
43
- def run_with_context(context, func, *args, **kwargs):
44
- token = attach(context)
45
- try:
46
- return func(*args, **kwargs)
47
- finally:
48
- detach(token)
49
-
50
42
  with ThreadPoolExecutor(max_workers=2) as executor:
51
- futures = [executor.submit(run_with_context, current_context, optimized_cot, question=q) for q in questions]
43
+ futures = [executor.submit(contextvars.copy_context().run, optimized_cot, question=q) for q in questions]
52
44
 
53
45
  for future in futures:
54
46
  ans = future.result()
@@ -0,0 +1,73 @@
1
+ # Example taken from https://platform.openai.com/docs/guides/function-calling
2
+ import json
3
+
4
+ from dotenv import find_dotenv, load_dotenv
5
+ from openai import OpenAI
6
+
7
+ from langtrace_python_sdk import langtrace
8
+
9
+ client = OpenAI()
10
+
11
+ _ = load_dotenv(find_dotenv())
12
+
13
+ langtrace.init(
14
+ write_spans_to_console=True,
15
+ )
16
+
17
+
18
+ # Example dummy function hard coded to return the same weather
19
+ # In production, this could be your backend API or an external API
20
+ def get_current_weather(location, unit="fahrenheit"):
21
+ """Get the current weather in a given location"""
22
+ if "tokyo" in location.lower():
23
+ return json.dumps({"location": "Tokyo", "temperature": "10", "unit": unit})
24
+ elif "san francisco" in location.lower():
25
+ return json.dumps(
26
+ {"location": "San Francisco", "temperature": "72", "unit": unit}
27
+ )
28
+ elif "paris" in location.lower():
29
+ return json.dumps({"location": "Paris", "temperature": "22", "unit": unit})
30
+ else:
31
+ return json.dumps({"location": location, "temperature": "unknown"})
32
+
33
+
34
+ def run_conversation():
35
+ # Step 1: send the conversation and available functions to the model
36
+ messages = [
37
+ {
38
+ "role": "user",
39
+ "content": "What's the weather like in San Francisco, Tokyo, and Paris?",
40
+ }
41
+ ]
42
+ tools = [
43
+ {
44
+ "type": "function",
45
+ "function": {
46
+ "name": "get_current_weather",
47
+ "description": "Get the current weather in a given location",
48
+ "parameters": {
49
+ "type": "object",
50
+ "properties": {
51
+ "location": {
52
+ "type": "string",
53
+ "description": "The city and state, e.g. San Francisco, CA",
54
+ },
55
+ "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
56
+ },
57
+ "required": ["location"],
58
+ },
59
+ },
60
+ }
61
+ ]
62
+ response = client.chat.completions.create(
63
+ model="gpt-4o",
64
+ messages=messages,
65
+ tools=tools,
66
+ tool_choice="required", # auto is default, but we'll be explicit
67
+ )
68
+ response_message = response.choices[0].message
69
+ tool_calls = response_message.tool_calls
70
+ print(tool_calls)
71
+
72
+
73
+ print(run_conversation())
@@ -48,7 +48,9 @@ def messages_create(original_method, version, tracer):
48
48
  prompts = kwargs.get("messages", [])
49
49
  system = kwargs.get("system")
50
50
  if system:
51
- prompts = [{"role": "system", "content": system}] + kwargs.get("messages", [])
51
+ prompts = [{"role": "system", "content": system}] + kwargs.get(
52
+ "messages", []
53
+ )
52
54
 
53
55
  span_attributes = {
54
56
  **get_langtrace_attributes(version, service_provider),
@@ -61,8 +61,14 @@ def patch_bootstrapfewshot_optimizer(operation_name, version, tracer):
61
61
  if config and len(config) > 0:
62
62
  span_attributes["dspy.optimizer.config"] = json.dumps(config)
63
63
 
64
+ # passed operation name
65
+ opname = operation_name
66
+ if extra_attributes is not None and "langtrace.span.name" in extra_attributes:
67
+ # append the operation name to the span name
68
+ opname = f"{operation_name}-{extra_attributes['langtrace.span.name']}"
69
+
64
70
  attributes = FrameworkSpanAttributes(**span_attributes)
65
- with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span:
71
+ with tracer.start_as_current_span(opname, kind=SpanKind.CLIENT) as span:
66
72
  _set_input_attributes(span, kwargs, attributes)
67
73
 
68
74
  try:
@@ -100,6 +106,12 @@ def patch_signature(operation_name, version, tracer):
100
106
  **(extra_attributes if extra_attributes is not None else {}),
101
107
  }
102
108
 
109
+ # passed operation name
110
+ opname = operation_name
111
+ if extra_attributes is not None and "langtrace.span.name" in extra_attributes:
112
+ # append the operation name to the span name
113
+ opname = f"{operation_name}-{extra_attributes['langtrace.span.name']}"
114
+
103
115
  if instance.__class__.__name__:
104
116
  span_attributes["dspy.signature.name"] = instance.__class__.__name__
105
117
  span_attributes["dspy.signature"] = str(instance)
@@ -108,7 +120,7 @@ def patch_signature(operation_name, version, tracer):
108
120
  span_attributes["dspy.signature.args"] = str(kwargs)
109
121
 
110
122
  attributes = FrameworkSpanAttributes(**span_attributes)
111
- with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span:
123
+ with tracer.start_as_current_span(opname, kind=SpanKind.CLIENT) as span:
112
124
  _set_input_attributes(span, kwargs, attributes)
113
125
 
114
126
  try:
@@ -147,6 +159,12 @@ def patch_evaluate(operation_name, version, tracer):
147
159
  **(extra_attributes if extra_attributes is not None else {}),
148
160
  }
149
161
 
162
+ # passed operation name
163
+ opname = operation_name
164
+ if extra_attributes is not None and "langtrace.span.name" in extra_attributes:
165
+ # append the operation name to the span name
166
+ opname = f"{operation_name}-{extra_attributes['langtrace.span.name']}"
167
+
150
168
  if hasattr(instance, "devset"):
151
169
  span_attributes["dspy.evaluate.devset"] = str(getattr(instance, "devset"))
152
170
  if hasattr(instance, "trainset"):
@@ -175,7 +193,7 @@ def patch_evaluate(operation_name, version, tracer):
175
193
  span_attributes["dspy.evaluate.args"] = str(args)
176
194
 
177
195
  attributes = FrameworkSpanAttributes(**span_attributes)
178
- with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span:
196
+ with tracer.start_as_current_span(opname, kind=SpanKind.CLIENT) as span:
179
197
  _set_input_attributes(span, kwargs, attributes)
180
198
 
181
199
  try:
@@ -110,7 +110,10 @@ def get_llm_model(instance):
110
110
 
111
111
  def serialize_prompts(args, kwargs, instance):
112
112
  prompts = []
113
- if hasattr(instance, "_system_instruction") and instance._system_instruction is not None:
113
+ if (
114
+ hasattr(instance, "_system_instruction")
115
+ and instance._system_instruction is not None
116
+ ):
114
117
  system_prompt = {
115
118
  "role": "system",
116
119
  "content": instance._system_instruction.__dict__["_pb"].parts[0].text,
@@ -123,6 +123,7 @@ def get_llm_request_attributes(kwargs, prompts=None, model=None):
123
123
  SpanAttributes.LLM_FREQUENCY_PENALTY: kwargs.get("frequency_penalty"),
124
124
  SpanAttributes.LLM_REQUEST_SEED: kwargs.get("seed"),
125
125
  SpanAttributes.LLM_TOOLS: json.dumps(tools) if tools else None,
126
+ SpanAttributes.LLM_TOOL_CHOICE: kwargs.get("tool_choice"),
126
127
  SpanAttributes.LLM_REQUEST_LOGPROPS: kwargs.get("logprobs"),
127
128
  SpanAttributes.LLM_REQUEST_LOGITBIAS: kwargs.get("logit_bias"),
128
129
  SpanAttributes.LLM_REQUEST_TOP_LOGPROPS: kwargs.get("top_logprobs"),
@@ -1 +1 @@
1
- __version__ = "2.2.2"
1
+ __version__ = "2.2.3"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: langtrace-python-sdk
3
- Version: 2.2.2
3
+ Version: 2.2.3
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>
@@ -11,7 +11,7 @@ examples/cohere_example/rerank.py,sha256=e7OU0A2FzfiQDuOmCy3Kg5LLNYGRmRIK5LqeLnT
11
11
  examples/cohere_example/tools.py,sha256=a5uvS058tcwU6PJbF9EDO6LPVmPj2LoW4Vn8Web3Iq8,1656
12
12
  examples/crewai_example/basic.py,sha256=PBu4f8yQfZO1L_22UDm_ReU9lnEcycjZcGuy5UpgDJM,1948
13
13
  examples/dspy_example/math_problems_cot.py,sha256=Z98nB6myt8WJse2dWS6Ap7CFUhC27lBNb37R1Gg80VQ,1282
14
- examples/dspy_example/math_problems_cot_parallel.py,sha256=mrv6CrzgOZzplibAxZCYDKdtvbnj-suZKC24QR4RiBM,1891
14
+ examples/dspy_example/math_problems_cot_parallel.py,sha256=s3jdEjhwZD-Dd2-EqYFrA_dc3D3vm5bhMwFRYAjCCJw,1734
15
15
  examples/dspy_example/program_of_thought_basic.py,sha256=oEbtJdeKENMUbex25-zyStWwurRWW6OdP0KDs-jUkko,984
16
16
  examples/dspy_example/quiz_gen.py,sha256=OyGhepeX8meKOtLdmlYUjMD2ECk-ZQuQXUZif1hFQY4,3371
17
17
  examples/dspy_example/react.py,sha256=APAnHqgy9w-qY5jnPD_WbBx6bwo9C-DhPnUuhL-t7sg,1376
@@ -37,6 +37,7 @@ examples/openai_example/__init__.py,sha256=MU4CELvhe2EU6d4Okg-bTfjvfGxQO7PNzqMw1
37
37
  examples/openai_example/async_tool_calling_nonstreaming.py,sha256=H1-CrNfNDfqAkB5wEipITXlW2OsYL7XD5uQb6k3C6ps,3865
38
38
  examples/openai_example/async_tool_calling_streaming.py,sha256=LaSKmn_Unv55eTHXYdEmKjo39eNuB3ASOBV-m8U1HfU,7136
39
39
  examples/openai_example/chat_completion.py,sha256=HPFdM0lA01yo5VvZRRdgczyPSa-eurnALP723laOv6M,1192
40
+ examples/openai_example/chat_completion_tool_choice.py,sha256=rkOjbFnIJ5hWWHWg-aTSek41UN2PBfufGpdaFhkWYj8,2356
40
41
  examples/openai_example/embeddings_create.py,sha256=kcOZpl5nhHo_NC-3n2yKX5W8mAzNfut43mSy1BmQJUI,555
41
42
  examples/openai_example/function_calling.py,sha256=zz-JdCcpP7uCXG21EYXF1Y39IKj6gYt2fOP5N_ywpnc,2338
42
43
  examples/openai_example/images_edit.py,sha256=6dSKA40V39swSs1mWdWXSa0reK4tyNBkK9MM7V3IEPw,939
@@ -57,7 +58,7 @@ examples/weaviate_example/__init__.py,sha256=8JMDBsRSEV10HfTd-YC7xb4txBjD3la56sn
57
58
  examples/weaviate_example/query_text.py,sha256=sG8O-bXQpflBAiYpgE_M2X7GcHUlZNgl_wJW8_h-W6Q,127024
58
59
  langtrace_python_sdk/__init__.py,sha256=VZM6i71NR7pBQK6XvJWRelknuTYUhqwqE7PlicKa5Wg,1166
59
60
  langtrace_python_sdk/langtrace.py,sha256=1L0IjME-pzEYht92QfwByPZr3H1MClTrqQdoN1KyKJY,7689
60
- langtrace_python_sdk/version.py,sha256=toAYzE_ok1SiBE0AqAVdW0O8YCXCwcx0w4JATYQuJOg,22
61
+ langtrace_python_sdk/version.py,sha256=3pvG1dVJuHEcdu38cc8mGwpb57ZUjsnbAe_LVURnTPQ,22
61
62
  langtrace_python_sdk/constants/__init__.py,sha256=P8QvYwt5czUNDZsKS64vxm9Dc41ptGbuF1TFtAF6nv4,44
62
63
  langtrace_python_sdk/constants/exporter/langtrace_exporter.py,sha256=5MNjnAOg-4am78J3gVMH6FSwq5N8TOj72ugkhsw4vi0,46
63
64
  langtrace_python_sdk/constants/instrumentation/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -79,7 +80,7 @@ langtrace_python_sdk/extensions/langtrace_filesystem.py,sha256=qpnkpkuTZ2yhGgpBK
79
80
  langtrace_python_sdk/instrumentation/__init__.py,sha256=yJd3aGu4kPfm2h6oe6kiCWvzTF9awpC1UztjXF9WSO4,1391
80
81
  langtrace_python_sdk/instrumentation/anthropic/__init__.py,sha256=donrurJAGYlxrSRA3BIf76jGeUcAx9Tq8CVpah68S0Y,101
81
82
  langtrace_python_sdk/instrumentation/anthropic/instrumentation.py,sha256=-srgE8qumAn0ulQYZxMa8ch-9IBH0XgBW_rfEnGk6LI,1684
82
- langtrace_python_sdk/instrumentation/anthropic/patch.py,sha256=AdsVFDoU0u_4NTjcIDe6CBbK-Bqhlkw4iLwN4sAr2K8,6884
83
+ langtrace_python_sdk/instrumentation/anthropic/patch.py,sha256=Ol0U6vi5UG55ZC_Y7RBmuW87x0FCerAIbio0GSNjOqk,6914
83
84
  langtrace_python_sdk/instrumentation/chroma/__init__.py,sha256=pNZ5UO8Q-d5VkXSobBf79reB6AmEl_usnnTp5Itv818,95
84
85
  langtrace_python_sdk/instrumentation/chroma/instrumentation.py,sha256=nT6PS6bsrIOO9kLV5GuUeRjMe6THHHAZGvqWBP1dYog,1807
85
86
  langtrace_python_sdk/instrumentation/chroma/patch.py,sha256=JfFc8SDfwkEyIwTd1yM6jwa1vu5hZH6IXyxAEcQQQOs,9010
@@ -91,10 +92,10 @@ langtrace_python_sdk/instrumentation/crewai/instrumentation.py,sha256=W8PLTLzgEd
91
92
  langtrace_python_sdk/instrumentation/crewai/patch.py,sha256=Vnpip9Pbk4UFbTFHoUrHtAnDgsaihwSvZBgtUeOtLr8,6109
92
93
  langtrace_python_sdk/instrumentation/dspy/__init__.py,sha256=tM1srfi_QgyCzrde4izojMrRq2Wm7Dj5QUvVQXIJzkk,84
93
94
  langtrace_python_sdk/instrumentation/dspy/instrumentation.py,sha256=o8URiDvCbZ8LL0I-4xKHkn_Ms2sETBRpn-gOliv3xzQ,2929
94
- langtrace_python_sdk/instrumentation/dspy/patch.py,sha256=86oTIUcM45INKnGs6Q5I-1NQ0jvgDQuwGlOg-sbCCYs,9017
95
+ langtrace_python_sdk/instrumentation/dspy/patch.py,sha256=E2P3MJBQ71or4M6BsvZOwYFtJK1UdTsYkdxVj9fSWPs,9869
95
96
  langtrace_python_sdk/instrumentation/gemini/__init__.py,sha256=ilWmKA4Li-g3DX6R10WQ4v-51VljxToEnJpOQoQB5uQ,88
96
97
  langtrace_python_sdk/instrumentation/gemini/instrumentation.py,sha256=rhGOt1YsTmTSzdFXkuTcFJFUQSgNBnqKvYURcS3yGHU,1308
97
- langtrace_python_sdk/instrumentation/gemini/patch.py,sha256=XyJT6Zl5NSlHOPxKvfZDtLiWV_G5aiOeZqAvlIQf0j0,6291
98
+ langtrace_python_sdk/instrumentation/gemini/patch.py,sha256=XtsxD9vbe4lv4o8K8iRb9K1-U8DrjtUG6rWCYkCdJuc,6315
98
99
  langtrace_python_sdk/instrumentation/groq/__init__.py,sha256=ZXeq_nrej6Lm_uoMFEg8wbSejhjB2UJ5IoHQBPc2-C0,91
99
100
  langtrace_python_sdk/instrumentation/groq/instrumentation.py,sha256=Ttf07XVKhdYY1_fqJc7QWiSdmgEhEVyQB_3Az2_wqYo,1832
100
101
  langtrace_python_sdk/instrumentation/groq/patch.py,sha256=zvQ0WPSHXLYQwzST-9-hRvDyjPG3vkY6FX0LeGV-Cgg,23863
@@ -134,7 +135,7 @@ langtrace_python_sdk/instrumentation/weaviate/patch.py,sha256=rRD6WfQcNGYpw9teoC
134
135
  langtrace_python_sdk/types/__init__.py,sha256=KDW6S74FDxpeBa9xoH5zVEYfmRjccCCHzlW7lTJg1TA,3194
135
136
  langtrace_python_sdk/utils/__init__.py,sha256=QPF7SMuiz_003fLCHkRrgNb9NjqErDQ5cQr6pkJReKc,724
136
137
  langtrace_python_sdk/utils/langtrace_sampler.py,sha256=BupNndHbU9IL_wGleKetz8FdcveqHMBVz1bfKTTW80w,1753
137
- langtrace_python_sdk/utils/llm.py,sha256=UUjzsQ2Howo43LPib0_T_LB_d-L7Rk7kddX_QWE1UrA,12781
138
+ langtrace_python_sdk/utils/llm.py,sha256=49oiL4leIB_KvO6oVzC2qEurl_7k5lhet4PpUESyn5E,12848
138
139
  langtrace_python_sdk/utils/misc.py,sha256=CD9NWRLxLpFd0YwlHJqzlpFNedXVWtAKGOjQWnDCo8k,838
139
140
  langtrace_python_sdk/utils/prompt_registry.py,sha256=n5dQMVLBw8aJZY8Utvf67bncc25ELf6AH9BYw8_hSzo,2619
140
141
  langtrace_python_sdk/utils/sdk_version_checker.py,sha256=FzjIWZjn53cX0LEVPdipQd1fO9lG8iGVUEVUs9Hyk6M,1713
@@ -183,8 +184,8 @@ tests/pinecone/cassettes/test_query.yaml,sha256=b5v9G3ssUy00oG63PlFUR3JErF2Js-5A
183
184
  tests/pinecone/cassettes/test_upsert.yaml,sha256=neWmQ1v3d03V8WoLl8FoFeeCYImb8pxlJBWnFd_lITU,38607
184
185
  tests/qdrant/conftest.py,sha256=9n0uHxxIjWk9fbYc4bx-uP8lSAgLBVx-cV9UjnsyCHM,381
185
186
  tests/qdrant/test_qdrant.py,sha256=pzjAjVY2kmsmGfrI2Gs2xrolfuaNHz7l1fqGQCjp5_o,3353
186
- langtrace_python_sdk-2.2.2.dist-info/METADATA,sha256=4fYMOzc9y_HLZ-M9IxzgcEesxm8ylDZiTKlCnXsjQUw,14471
187
- langtrace_python_sdk-2.2.2.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
188
- langtrace_python_sdk-2.2.2.dist-info/entry_points.txt,sha256=1_b9-qvf2fE7uQNZcbUei9vLpFZBbbh9LrtGw95ssAo,70
189
- langtrace_python_sdk-2.2.2.dist-info/licenses/LICENSE,sha256=QwcOLU5TJoTeUhuIXzhdCEEDDvorGiC6-3YTOl4TecE,11356
190
- langtrace_python_sdk-2.2.2.dist-info/RECORD,,
187
+ langtrace_python_sdk-2.2.3.dist-info/METADATA,sha256=ZKVh06M9VHfn7ESym1J4vECVp8-aM9aaDqqDpjsOVHE,14471
188
+ langtrace_python_sdk-2.2.3.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
189
+ langtrace_python_sdk-2.2.3.dist-info/entry_points.txt,sha256=1_b9-qvf2fE7uQNZcbUei9vLpFZBbbh9LrtGw95ssAo,70
190
+ langtrace_python_sdk-2.2.3.dist-info/licenses/LICENSE,sha256=QwcOLU5TJoTeUhuIXzhdCEEDDvorGiC6-3YTOl4TecE,11356
191
+ langtrace_python_sdk-2.2.3.dist-info/RECORD,,