langtrace-python-sdk 1.2.6__py3-none-any.whl → 1.2.8__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.
- examples/langchain_example/basic.py +28 -4
- examples/openai/chat_completion.py +45 -25
- langtrace_python_sdk/constants/exporter/langtrace_exporter.py +1 -0
- langtrace_python_sdk/constants/instrumentation/common.py +2 -0
- langtrace_python_sdk/extensions/langtrace_exporter.py +8 -14
- langtrace_python_sdk/instrumentation/anthropic/patch.py +5 -2
- langtrace_python_sdk/instrumentation/chroma/patch.py +6 -1
- langtrace_python_sdk/instrumentation/langchain/patch.py +6 -1
- langtrace_python_sdk/instrumentation/langchain_community/patch.py +6 -1
- langtrace_python_sdk/instrumentation/langchain_core/patch.py +6 -1
- langtrace_python_sdk/instrumentation/llamaindex/patch.py +6 -1
- langtrace_python_sdk/instrumentation/openai/patch.py +22 -5
- langtrace_python_sdk/instrumentation/pinecone/patch.py +6 -1
- langtrace_python_sdk/langtrace.py +7 -4
- langtrace_python_sdk/utils/with_root_span.py +37 -5
- langtrace_python_sdk/version.py +1 -1
- langtrace_python_sdk-1.2.8.dist-info/METADATA +130 -0
- {langtrace_python_sdk-1.2.6.dist-info → langtrace_python_sdk-1.2.8.dist-info}/RECORD +20 -19
- langtrace_python_sdk-1.2.8.dist-info/licenses/LICENSE +201 -0
- langtrace_python_sdk-1.2.6.dist-info/METADATA +0 -76
- langtrace_python_sdk-1.2.6.dist-info/licenses/LICENSE +0 -661
- {langtrace_python_sdk-1.2.6.dist-info → langtrace_python_sdk-1.2.8.dist-info}/WHEEL +0 -0
|
@@ -8,15 +8,18 @@ from langchain_core.runnables import RunnablePassthrough
|
|
|
8
8
|
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
|
9
9
|
|
|
10
10
|
from langtrace_python_sdk import langtrace
|
|
11
|
-
from langtrace_python_sdk.utils.with_root_span import
|
|
11
|
+
from langtrace_python_sdk.utils.with_root_span import (
|
|
12
|
+
with_langtrace_root_span,
|
|
13
|
+
with_additional_attributes,
|
|
14
|
+
)
|
|
12
15
|
|
|
13
16
|
_ = load_dotenv(find_dotenv())
|
|
14
17
|
|
|
15
|
-
langtrace.init(
|
|
18
|
+
langtrace.init()
|
|
16
19
|
|
|
17
20
|
|
|
18
|
-
@
|
|
19
|
-
def
|
|
21
|
+
@with_additional_attributes({"user.id": "1234", "user.feedback.rating": 1})
|
|
22
|
+
def api_call_1():
|
|
20
23
|
llm = ChatOpenAI()
|
|
21
24
|
prompt = ChatPromptTemplate.from_messages(
|
|
22
25
|
[
|
|
@@ -30,6 +33,27 @@ def basic():
|
|
|
30
33
|
print(res)
|
|
31
34
|
|
|
32
35
|
|
|
36
|
+
@with_additional_attributes({"user.id": "37373", "user.feedback.rating": 1})
|
|
37
|
+
def api_call_2():
|
|
38
|
+
llm = ChatOpenAI()
|
|
39
|
+
prompt = ChatPromptTemplate.from_messages(
|
|
40
|
+
[
|
|
41
|
+
("system", "You are world class technical documentation writer."),
|
|
42
|
+
("user", "{input}"),
|
|
43
|
+
]
|
|
44
|
+
)
|
|
45
|
+
output_parser = StrOutputParser()
|
|
46
|
+
chain = prompt | llm | output_parser
|
|
47
|
+
res = chain.invoke({"input": "how can langsmith help with testing?"})
|
|
48
|
+
print(res)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@with_langtrace_root_span()
|
|
52
|
+
def basic():
|
|
53
|
+
api_call_1()
|
|
54
|
+
api_call_2()
|
|
55
|
+
|
|
56
|
+
|
|
33
57
|
@with_langtrace_root_span()
|
|
34
58
|
def rag():
|
|
35
59
|
vectorstore = FAISS.from_texts(
|
|
@@ -2,38 +2,58 @@ from dotenv import find_dotenv, load_dotenv
|
|
|
2
2
|
from openai import OpenAI
|
|
3
3
|
|
|
4
4
|
from langtrace_python_sdk import langtrace
|
|
5
|
-
from langtrace_python_sdk.utils.with_root_span import
|
|
5
|
+
from langtrace_python_sdk.utils.with_root_span import (
|
|
6
|
+
with_additional_attributes, with_langtrace_root_span)
|
|
6
7
|
|
|
7
8
|
_ = load_dotenv(find_dotenv())
|
|
8
9
|
|
|
9
|
-
langtrace.init(
|
|
10
|
-
|
|
10
|
+
langtrace.init()
|
|
11
11
|
client = OpenAI()
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
@
|
|
15
|
-
def
|
|
14
|
+
@with_additional_attributes({"user.id": "1234", "user.feedback.rating": 1})
|
|
15
|
+
def api1():
|
|
16
16
|
response = client.chat.completions.create(
|
|
17
17
|
model="gpt-4",
|
|
18
18
|
messages=[{"role": "user", "content": "Say this is a test three times"}],
|
|
19
|
-
stream=
|
|
19
|
+
stream=False,
|
|
20
20
|
)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
21
|
+
return response
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@with_additional_attributes({"user.id": "5678", "user.feedback.rating": -1})
|
|
25
|
+
def api2():
|
|
26
|
+
response = client.chat.completions.create(
|
|
27
|
+
model="gpt-4",
|
|
28
|
+
messages=[{"role": "user", "content": "Say this is a test three times"}],
|
|
29
|
+
stream=False,
|
|
30
|
+
)
|
|
31
|
+
return response
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@with_langtrace_root_span()
|
|
35
|
+
def chat_completion():
|
|
36
|
+
response = api1()
|
|
37
|
+
response = api2()
|
|
38
|
+
return response
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# print(response)
|
|
42
|
+
# stream = client.chat.completions.create(
|
|
43
|
+
# model="gpt-4",
|
|
44
|
+
# messages=[{"role": "user", "content": "Say this is a test three times"}, {"role": "assistant", "content": "This is a test. This is a test. This is a test"},
|
|
45
|
+
# {"role": "user", "content": "Say this is a mock 4 times"}],
|
|
46
|
+
# stream=False,
|
|
47
|
+
# )
|
|
48
|
+
|
|
49
|
+
# result = []
|
|
50
|
+
# for chunk in response:
|
|
51
|
+
# if chunk.choices[0].delta.content is not None:
|
|
52
|
+
# content = [
|
|
53
|
+
# choice.delta.content if choice.delta and
|
|
54
|
+
# choice.delta.content else ""
|
|
55
|
+
# for choice in chunk.choices]
|
|
56
|
+
# result.append(
|
|
57
|
+
# content[0] if len(content) > 0 else "")
|
|
58
|
+
|
|
59
|
+
# print("".join(result))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
LANGTRACE_REMOTE_URL = "https://app.langtrace.ai/api/trace"
|
|
@@ -2,9 +2,11 @@ import json
|
|
|
2
2
|
import os
|
|
3
3
|
import typing
|
|
4
4
|
|
|
5
|
+
from langtrace_python_sdk.constants.exporter.langtrace_exporter import (
|
|
6
|
+
LANGTRACE_REMOTE_URL,
|
|
7
|
+
)
|
|
5
8
|
import requests
|
|
6
|
-
from opentelemetry.sdk.trace.export import
|
|
7
|
-
SpanExportResult)
|
|
9
|
+
from opentelemetry.sdk.trace.export import ReadableSpan, SpanExporter, SpanExportResult
|
|
8
10
|
from opentelemetry.trace.span import format_trace_id
|
|
9
11
|
|
|
10
12
|
|
|
@@ -22,7 +24,6 @@ class LangTraceExporter(SpanExporter):
|
|
|
22
24
|
**Attributes:**
|
|
23
25
|
|
|
24
26
|
* `api_key` (str): An API key to authenticate with the LangTrace collector (required).
|
|
25
|
-
* `url` (str): The URL of the LangTrace collector endpoint (required if `write_to_remote_url` is True).
|
|
26
27
|
* `write_to_remote_url` (bool): A flag indicating whether to send spans to the remote URL (defaults to False).
|
|
27
28
|
|
|
28
29
|
**Methods:**
|
|
@@ -46,22 +47,15 @@ class LangTraceExporter(SpanExporter):
|
|
|
46
47
|
"""
|
|
47
48
|
|
|
48
49
|
api_key: str
|
|
49
|
-
url: str
|
|
50
50
|
write_to_remote_url: bool
|
|
51
51
|
|
|
52
|
-
def __init__(
|
|
53
|
-
self
|
|
54
|
-
) -> None:
|
|
55
|
-
self.api_key = api_key if api_key else os.environ.get("API_KEY")
|
|
56
|
-
self.url = url if url else os.environ.get("URL")
|
|
52
|
+
def __init__(self, api_key: str = None, write_to_remote_url: bool = False) -> None:
|
|
53
|
+
self.api_key = api_key if api_key else os.environ.get("LANGTRACE_API_KEY")
|
|
57
54
|
self.write_to_remote_url = write_to_remote_url
|
|
58
55
|
|
|
59
56
|
if self.write_to_remote_url and not self.api_key:
|
|
60
57
|
raise ValueError("No API key provided")
|
|
61
58
|
|
|
62
|
-
if self.write_to_remote_url and not self.url:
|
|
63
|
-
raise ValueError("No URL provided")
|
|
64
|
-
|
|
65
59
|
def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult:
|
|
66
60
|
"""
|
|
67
61
|
Exports a batch of telemetry data.
|
|
@@ -92,11 +86,11 @@ class LangTraceExporter(SpanExporter):
|
|
|
92
86
|
# Send data to remote URL
|
|
93
87
|
try:
|
|
94
88
|
requests.post(
|
|
95
|
-
url=
|
|
89
|
+
url=LANGTRACE_REMOTE_URL,
|
|
96
90
|
data=json.dumps(data),
|
|
97
91
|
headers={"Content-Type": "application/json", "x-api-key": self.api_key},
|
|
98
92
|
)
|
|
99
|
-
print(f"Traces sent To {
|
|
93
|
+
print(f"Traces sent To {LANGTRACE_REMOTE_URL}")
|
|
100
94
|
return SpanExportResult.SUCCESS
|
|
101
95
|
except Exception as e:
|
|
102
96
|
print("Error sending data to remote URL", e)
|
|
@@ -4,12 +4,13 @@ This module contains the patching logic for the Anthropic library."""
|
|
|
4
4
|
import json
|
|
5
5
|
|
|
6
6
|
from langtrace.trace_attributes import Event, LLMSpanAttributes
|
|
7
|
+
from opentelemetry import baggage
|
|
7
8
|
from opentelemetry.trace import SpanKind
|
|
8
9
|
from opentelemetry.trace.status import Status, StatusCode
|
|
9
10
|
|
|
10
11
|
from langtrace_python_sdk.constants.instrumentation.anthropic import APIS
|
|
11
|
-
from langtrace_python_sdk.constants.instrumentation.common import
|
|
12
|
-
SERVICE_PROVIDERS
|
|
12
|
+
from langtrace_python_sdk.constants.instrumentation.common import (
|
|
13
|
+
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, SERVICE_PROVIDERS)
|
|
13
14
|
|
|
14
15
|
|
|
15
16
|
def messages_create(original_method, version, tracer):
|
|
@@ -31,6 +32,7 @@ def messages_create(original_method, version, tracer):
|
|
|
31
32
|
prompts = json.dumps(
|
|
32
33
|
[{"role": "system", "content": system}] + kwargs.get("messages", [])
|
|
33
34
|
)
|
|
35
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
34
36
|
|
|
35
37
|
span_attributes = {
|
|
36
38
|
"langtrace.service.name": service_provider,
|
|
@@ -42,6 +44,7 @@ def messages_create(original_method, version, tracer):
|
|
|
42
44
|
"llm.model": kwargs.get("model"),
|
|
43
45
|
"llm.prompts": prompts,
|
|
44
46
|
"llm.stream": kwargs.get("stream"),
|
|
47
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
attributes = LLMSpanAttributes(**span_attributes)
|
|
@@ -3,11 +3,13 @@ This module contains the patching logic for the Chroma client.
|
|
|
3
3
|
"""
|
|
4
4
|
|
|
5
5
|
from langtrace.trace_attributes import DatabaseSpanAttributes
|
|
6
|
+
from opentelemetry import baggage
|
|
6
7
|
from opentelemetry.trace import SpanKind
|
|
7
8
|
from opentelemetry.trace.status import Status, StatusCode
|
|
8
9
|
|
|
9
10
|
from langtrace_python_sdk.constants.instrumentation.chroma import APIS
|
|
10
|
-
from langtrace_python_sdk.constants.instrumentation.common import
|
|
11
|
+
from langtrace_python_sdk.constants.instrumentation.common import (
|
|
12
|
+
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, SERVICE_PROVIDERS)
|
|
11
13
|
|
|
12
14
|
|
|
13
15
|
def collection_patch(method, version, tracer):
|
|
@@ -18,6 +20,8 @@ def collection_patch(method, version, tracer):
|
|
|
18
20
|
def traced_method(wrapped, instance, args, kwargs):
|
|
19
21
|
api = APIS[method]
|
|
20
22
|
service_provider = SERVICE_PROVIDERS["CHROMA"]
|
|
23
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
24
|
+
|
|
21
25
|
span_attributes = {
|
|
22
26
|
"langtrace.service.name": service_provider,
|
|
23
27
|
"langtrace.service.type": "vectordb",
|
|
@@ -25,6 +29,7 @@ def collection_patch(method, version, tracer):
|
|
|
25
29
|
"langtrace.version": "1.0.0",
|
|
26
30
|
"db.system": "chromadb",
|
|
27
31
|
"db.operation": api["OPERATION"],
|
|
32
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
28
33
|
}
|
|
29
34
|
|
|
30
35
|
if hasattr(instance, "name") and instance.name is not None:
|
|
@@ -5,10 +5,12 @@ This module contains the patching logic for the langchain package.
|
|
|
5
5
|
import json
|
|
6
6
|
|
|
7
7
|
from langtrace.trace_attributes import FrameworkSpanAttributes
|
|
8
|
+
from opentelemetry import baggage
|
|
8
9
|
from opentelemetry.trace import SpanKind, StatusCode
|
|
9
10
|
from opentelemetry.trace.status import Status
|
|
10
11
|
|
|
11
|
-
from langtrace_python_sdk.constants.instrumentation.common import
|
|
12
|
+
from langtrace_python_sdk.constants.instrumentation.common import (
|
|
13
|
+
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, SERVICE_PROVIDERS)
|
|
12
14
|
|
|
13
15
|
|
|
14
16
|
def generic_patch(
|
|
@@ -20,12 +22,15 @@ def generic_patch(
|
|
|
20
22
|
|
|
21
23
|
def traced_method(wrapped, instance, args, kwargs):
|
|
22
24
|
service_provider = SERVICE_PROVIDERS["LANGCHAIN"]
|
|
25
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
26
|
+
|
|
23
27
|
span_attributes = {
|
|
24
28
|
"langtrace.service.name": service_provider,
|
|
25
29
|
"langtrace.service.type": "framework",
|
|
26
30
|
"langtrace.service.version": version,
|
|
27
31
|
"langtrace.version": "1.0.0",
|
|
28
32
|
"langchain.task.name": task,
|
|
33
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
29
34
|
}
|
|
30
35
|
|
|
31
36
|
if len(args) > 0 and trace_input:
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import json
|
|
2
2
|
|
|
3
3
|
from langtrace.trace_attributes import FrameworkSpanAttributes
|
|
4
|
+
from opentelemetry import baggage
|
|
4
5
|
from opentelemetry.trace import SpanKind
|
|
5
6
|
from opentelemetry.trace.status import Status, StatusCode
|
|
6
7
|
|
|
7
|
-
from langtrace_python_sdk.constants.instrumentation.common import
|
|
8
|
+
from langtrace_python_sdk.constants.instrumentation.common import (
|
|
9
|
+
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, SERVICE_PROVIDERS)
|
|
8
10
|
|
|
9
11
|
|
|
10
12
|
def generic_patch(
|
|
@@ -12,12 +14,15 @@ def generic_patch(
|
|
|
12
14
|
):
|
|
13
15
|
def traced_method(wrapped, instance, args, kwargs):
|
|
14
16
|
service_provider = SERVICE_PROVIDERS["LANGCHAIN_COMMUNITY"]
|
|
17
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
18
|
+
|
|
15
19
|
span_attributes = {
|
|
16
20
|
"langtrace.service.name": service_provider,
|
|
17
21
|
"langtrace.service.type": "framework",
|
|
18
22
|
"langtrace.service.version": version,
|
|
19
23
|
"langtrace.version": "1.0.0",
|
|
20
24
|
"langchain.task.name": task,
|
|
25
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
if trace_input and len(args) > 0:
|
|
@@ -5,10 +5,12 @@ This module contains the patching functions for the langchain_core package.
|
|
|
5
5
|
import json
|
|
6
6
|
|
|
7
7
|
from langtrace.trace_attributes import FrameworkSpanAttributes
|
|
8
|
+
from opentelemetry import baggage
|
|
8
9
|
from opentelemetry.trace import SpanKind, StatusCode
|
|
9
10
|
from opentelemetry.trace.status import Status
|
|
10
11
|
|
|
11
|
-
from langtrace_python_sdk.constants.instrumentation.common import
|
|
12
|
+
from langtrace_python_sdk.constants.instrumentation.common import (
|
|
13
|
+
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, SERVICE_PROVIDERS)
|
|
12
14
|
|
|
13
15
|
|
|
14
16
|
def generic_patch(
|
|
@@ -26,12 +28,15 @@ def generic_patch(
|
|
|
26
28
|
|
|
27
29
|
def traced_method(wrapped, instance, args, kwargs):
|
|
28
30
|
service_provider = SERVICE_PROVIDERS["LANGCHAIN_CORE"]
|
|
31
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
32
|
+
|
|
29
33
|
span_attributes = {
|
|
30
34
|
"langtrace.service.name": service_provider,
|
|
31
35
|
"langtrace.service.type": "framework",
|
|
32
36
|
"langtrace.service.version": version,
|
|
33
37
|
"langtrace.version": "1.0.0",
|
|
34
38
|
"langchain.task.name": task,
|
|
39
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
if len(args) > 0 and trace_input:
|
|
@@ -3,10 +3,12 @@ This module contains a generic patch method that wraps a function with a span.
|
|
|
3
3
|
"""
|
|
4
4
|
|
|
5
5
|
from langtrace.trace_attributes import FrameworkSpanAttributes
|
|
6
|
+
from opentelemetry import baggage
|
|
6
7
|
from opentelemetry.trace import SpanKind
|
|
7
8
|
from opentelemetry.trace.status import Status, StatusCode
|
|
8
9
|
|
|
9
|
-
from langtrace_python_sdk.constants.instrumentation.common import
|
|
10
|
+
from langtrace_python_sdk.constants.instrumentation.common import (
|
|
11
|
+
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, SERVICE_PROVIDERS)
|
|
10
12
|
|
|
11
13
|
|
|
12
14
|
def generic_patch(method, task, tracer, version):
|
|
@@ -15,12 +17,15 @@ def generic_patch(method, task, tracer, version):
|
|
|
15
17
|
|
|
16
18
|
def traced_method(wrapped, instance, args, kwargs):
|
|
17
19
|
service_provider = SERVICE_PROVIDERS["LLAMAINDEX"]
|
|
20
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
21
|
+
|
|
18
22
|
span_attributes = {
|
|
19
23
|
"langtrace.service.name": service_provider,
|
|
20
24
|
"langtrace.service.type": "framework",
|
|
21
25
|
"langtrace.service.version": version,
|
|
22
26
|
"langtrace.version": "1.0.0",
|
|
23
27
|
"llamaindex.task.name": task,
|
|
28
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
attributes = FrameworkSpanAttributes(**span_attributes)
|
|
@@ -4,12 +4,15 @@ This module contains the patching logic for the OpenAI library."""
|
|
|
4
4
|
import json
|
|
5
5
|
|
|
6
6
|
from langtrace.trace_attributes import Event, LLMSpanAttributes
|
|
7
|
+
from opentelemetry import baggage, trace
|
|
7
8
|
from opentelemetry.trace import SpanKind
|
|
8
9
|
from opentelemetry.trace.status import Status, StatusCode
|
|
9
10
|
|
|
10
|
-
from langtrace_python_sdk.constants.instrumentation.common import
|
|
11
|
+
from langtrace_python_sdk.constants.instrumentation.common import (
|
|
12
|
+
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, SERVICE_PROVIDERS)
|
|
11
13
|
from langtrace_python_sdk.constants.instrumentation.openai import APIS
|
|
12
|
-
from langtrace_python_sdk.utils.llm import calculate_prompt_tokens,
|
|
14
|
+
from langtrace_python_sdk.utils.llm import (calculate_prompt_tokens,
|
|
15
|
+
estimate_tokens)
|
|
13
16
|
|
|
14
17
|
|
|
15
18
|
def images_generate(original_method, version, tracer):
|
|
@@ -24,6 +27,8 @@ def images_generate(original_method, version, tracer):
|
|
|
24
27
|
else ""
|
|
25
28
|
)
|
|
26
29
|
service_provider = SERVICE_PROVIDERS["OPENAI"]
|
|
30
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
31
|
+
|
|
27
32
|
span_attributes = {
|
|
28
33
|
"langtrace.service.name": service_provider,
|
|
29
34
|
"langtrace.service.type": "llm",
|
|
@@ -34,6 +39,7 @@ def images_generate(original_method, version, tracer):
|
|
|
34
39
|
"llm.model": kwargs.get("model"),
|
|
35
40
|
"llm.stream": kwargs.get("stream"),
|
|
36
41
|
"llm.prompts": json.dumps([kwargs.get("prompt", [])]),
|
|
42
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
attributes = LLMSpanAttributes(**span_attributes)
|
|
@@ -90,6 +96,8 @@ def chat_completions_create(original_method, version, tracer):
|
|
|
90
96
|
else ""
|
|
91
97
|
)
|
|
92
98
|
service_provider = SERVICE_PROVIDERS["OPENAI"]
|
|
99
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
100
|
+
|
|
93
101
|
span_attributes = {
|
|
94
102
|
"langtrace.service.name": service_provider,
|
|
95
103
|
"langtrace.service.type": "llm",
|
|
@@ -99,6 +107,7 @@ def chat_completions_create(original_method, version, tracer):
|
|
|
99
107
|
"llm.api": APIS["CHAT_COMPLETION"]["ENDPOINT"],
|
|
100
108
|
"llm.prompts": json.dumps(kwargs.get("messages", [])),
|
|
101
109
|
"llm.stream": kwargs.get("stream"),
|
|
110
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
102
111
|
}
|
|
103
112
|
|
|
104
113
|
attributes = LLMSpanAttributes(**span_attributes)
|
|
@@ -130,10 +139,12 @@ def chat_completions_create(original_method, version, tracer):
|
|
|
130
139
|
responses = [
|
|
131
140
|
{
|
|
132
141
|
"message": {
|
|
133
|
-
|
|
142
|
+
"role": (
|
|
143
|
+
choice.message.role
|
|
134
144
|
if choice.message and choice.message.role
|
|
135
|
-
else "assistant"
|
|
136
|
-
|
|
145
|
+
else "assistant"
|
|
146
|
+
),
|
|
147
|
+
"content": (
|
|
137
148
|
choice.message.content
|
|
138
149
|
if choice.message and choice.message.content
|
|
139
150
|
else (
|
|
@@ -299,6 +310,8 @@ def embeddings_create(original_method, version, tracer):
|
|
|
299
310
|
)
|
|
300
311
|
|
|
301
312
|
service_provider = SERVICE_PROVIDERS["OPENAI"]
|
|
313
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
314
|
+
|
|
302
315
|
span_attributes = {
|
|
303
316
|
"langtrace.service.name": service_provider,
|
|
304
317
|
"langtrace.service.type": "llm",
|
|
@@ -308,6 +321,7 @@ def embeddings_create(original_method, version, tracer):
|
|
|
308
321
|
"llm.api": APIS["EMBEDDINGS_CREATE"]["ENDPOINT"],
|
|
309
322
|
"llm.model": kwargs.get("model"),
|
|
310
323
|
"llm.prompts": "",
|
|
324
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
311
325
|
}
|
|
312
326
|
|
|
313
327
|
attributes = LLMSpanAttributes(**span_attributes)
|
|
@@ -323,6 +337,9 @@ def embeddings_create(original_method, version, tracer):
|
|
|
323
337
|
with tracer.start_as_current_span(
|
|
324
338
|
APIS["EMBEDDINGS_CREATE"]["METHOD"], kind=SpanKind.CLIENT
|
|
325
339
|
) as span:
|
|
340
|
+
|
|
341
|
+
print("Inside embeddings_create", trace.get_current_span())
|
|
342
|
+
|
|
326
343
|
for field, value in attributes.model_dump(by_alias=True).items():
|
|
327
344
|
if value is not None:
|
|
328
345
|
span.set_attribute(field, value)
|
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
This module contains the patching logic for the Pinecone client."""
|
|
3
3
|
|
|
4
4
|
from langtrace.trace_attributes import DatabaseSpanAttributes
|
|
5
|
+
from opentelemetry import baggage
|
|
5
6
|
from opentelemetry.trace import SpanKind
|
|
6
7
|
from opentelemetry.trace.status import Status, StatusCode
|
|
7
8
|
|
|
8
|
-
from langtrace_python_sdk.constants.instrumentation.common import
|
|
9
|
+
from langtrace_python_sdk.constants.instrumentation.common import (
|
|
10
|
+
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, SERVICE_PROVIDERS)
|
|
9
11
|
from langtrace_python_sdk.constants.instrumentation.pinecone import APIS
|
|
10
12
|
|
|
11
13
|
|
|
@@ -16,6 +18,8 @@ def generic_patch(original_method, method, version, tracer):
|
|
|
16
18
|
def traced_method(wrapped, instance, args, kwargs):
|
|
17
19
|
api = APIS[method]
|
|
18
20
|
service_provider = SERVICE_PROVIDERS["PINECONE"]
|
|
21
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
22
|
+
|
|
19
23
|
span_attributes = {
|
|
20
24
|
"langtrace.service.name": service_provider,
|
|
21
25
|
"langtrace.service.type": "vectordb",
|
|
@@ -23,6 +27,7 @@ def generic_patch(original_method, method, version, tracer):
|
|
|
23
27
|
"langtrace.version": "1.0.0",
|
|
24
28
|
"db.system": "pinecone",
|
|
25
29
|
"db.operation": api["OPERATION"],
|
|
30
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
attributes = DatabaseSpanAttributes(**span_attributes)
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
|
|
1
2
|
from opentelemetry import trace
|
|
2
3
|
from opentelemetry.sdk.trace import TracerProvider
|
|
3
4
|
from opentelemetry.sdk.trace.export import (
|
|
@@ -37,17 +38,19 @@ def init(
|
|
|
37
38
|
api_key: str = None,
|
|
38
39
|
batch: bool = True,
|
|
39
40
|
write_to_langtrace_cloud: bool = True,
|
|
40
|
-
remote_url: str = None,
|
|
41
41
|
debug_log_to_console: bool = False,
|
|
42
|
-
custom_remote_exporter
|
|
42
|
+
custom_remote_exporter=None,
|
|
43
43
|
):
|
|
44
44
|
|
|
45
45
|
provider = TracerProvider()
|
|
46
46
|
|
|
47
|
-
remote_write_exporter =
|
|
47
|
+
remote_write_exporter = (
|
|
48
|
+
LangTraceExporter(api_key, write_to_langtrace_cloud)
|
|
49
|
+
if custom_remote_exporter is None
|
|
50
|
+
else custom_remote_exporter
|
|
51
|
+
)
|
|
48
52
|
console_exporter = ConsoleSpanExporter()
|
|
49
53
|
batch_processor_remote = BatchSpanProcessor(remote_write_exporter)
|
|
50
|
-
simple_processor_remote = SimpleSpanProcessor(remote_write_exporter)
|
|
51
54
|
batch_processor_console = BatchSpanProcessor(console_exporter)
|
|
52
55
|
simple_processor_console = SimpleSpanProcessor(console_exporter)
|
|
53
56
|
|
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
import asyncio
|
|
2
2
|
from functools import wraps
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
from langtrace_python_sdk.constants.instrumentation.common import (
|
|
6
|
+
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY,
|
|
7
|
+
)
|
|
5
8
|
from opentelemetry.trace import SpanKind
|
|
9
|
+
from opentelemetry import trace, context, baggage
|
|
6
10
|
|
|
7
11
|
|
|
8
12
|
def with_langtrace_root_span(
|
|
9
|
-
name="LangtraceRootSpan",
|
|
13
|
+
name="LangtraceRootSpan",
|
|
14
|
+
kind=SpanKind.INTERNAL,
|
|
10
15
|
):
|
|
16
|
+
|
|
11
17
|
def decorator(func):
|
|
12
18
|
@wraps(func)
|
|
13
19
|
def sync_wrapper(*args, **kwargs):
|
|
14
20
|
tracer = trace.get_tracer(__name__)
|
|
15
21
|
operation_name = name if name else func.__name__
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
|
|
23
|
+
with tracer.start_as_current_span(operation_name, kind=kind):
|
|
24
|
+
|
|
19
25
|
return func(*args, **kwargs)
|
|
20
26
|
|
|
21
27
|
@wraps(func)
|
|
@@ -31,3 +37,29 @@ def with_langtrace_root_span(
|
|
|
31
37
|
return sync_wrapper
|
|
32
38
|
|
|
33
39
|
return decorator
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def with_additional_attributes(attributes={}):
|
|
43
|
+
def decorator(func):
|
|
44
|
+
@wraps(func)
|
|
45
|
+
def sync_wrapper(*args, **kwargs):
|
|
46
|
+
new_ctx = baggage.set_baggage(
|
|
47
|
+
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, attributes
|
|
48
|
+
)
|
|
49
|
+
context.attach(new_ctx)
|
|
50
|
+
return func(*args, **kwargs)
|
|
51
|
+
|
|
52
|
+
@wraps(func)
|
|
53
|
+
async def async_wrapper(*args, **kwargs):
|
|
54
|
+
new_ctx = baggage.set_baggage(
|
|
55
|
+
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, attributes
|
|
56
|
+
)
|
|
57
|
+
context.attach(new_ctx)
|
|
58
|
+
return await func(*args, **kwargs)
|
|
59
|
+
|
|
60
|
+
if asyncio.iscoroutinefunction(func):
|
|
61
|
+
return async_wrapper
|
|
62
|
+
else:
|
|
63
|
+
return sync_wrapper
|
|
64
|
+
|
|
65
|
+
return decorator
|
langtrace_python_sdk/version.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__ = "1.2.
|
|
1
|
+
__version__ = "1.2.8"
|