langtrace-python-sdk 2.1.17__py3-none-any.whl → 2.1.19__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/inspect_ai_example/basic_eval.py +3 -7
- examples/langchain_example/basic.py +23 -8
- examples/llamaindex_example/agent.py +1 -1
- examples/llamaindex_example/basic.py +5 -1
- examples/ollama_example/__init__.py +14 -0
- examples/ollama_example/basic.py +50 -0
- examples/openai_example/chat_completion.py +1 -1
- examples/pinecone_example/basic.py +1 -0
- langtrace_python_sdk/__init__.py +6 -4
- langtrace_python_sdk/constants/instrumentation/common.py +1 -0
- langtrace_python_sdk/constants/instrumentation/ollama.py +7 -0
- langtrace_python_sdk/constants/instrumentation/pinecone.py +1 -1
- langtrace_python_sdk/extensions/langtrace_filesystem.py +31 -15
- langtrace_python_sdk/instrumentation/__init__.py +2 -0
- langtrace_python_sdk/instrumentation/chroma/patch.py +7 -3
- langtrace_python_sdk/instrumentation/groq/patch.py +5 -2
- langtrace_python_sdk/instrumentation/langchain/patch.py +8 -2
- langtrace_python_sdk/instrumentation/langchain_community/patch.py +8 -2
- langtrace_python_sdk/instrumentation/langchain_core/patch.py +13 -3
- langtrace_python_sdk/instrumentation/langgraph/patch.py +7 -2
- langtrace_python_sdk/instrumentation/llamaindex/patch.py +7 -3
- langtrace_python_sdk/instrumentation/ollama/__init__.py +3 -0
- langtrace_python_sdk/instrumentation/ollama/instrumentation.py +58 -0
- langtrace_python_sdk/instrumentation/ollama/patch.py +215 -0
- langtrace_python_sdk/instrumentation/openai/patch.py +20 -8
- langtrace_python_sdk/instrumentation/pinecone/instrumentation.py +0 -1
- langtrace_python_sdk/instrumentation/pinecone/patch.py +7 -3
- langtrace_python_sdk/instrumentation/qdrant/patch.py +7 -2
- langtrace_python_sdk/instrumentation/weaviate/patch.py +7 -3
- langtrace_python_sdk/langtrace.py +14 -2
- langtrace_python_sdk/types/__init__.py +77 -1
- langtrace_python_sdk/utils/langtrace_sampler.py +56 -0
- langtrace_python_sdk/utils/with_root_span.py +11 -2
- langtrace_python_sdk/version.py +1 -1
- {langtrace_python_sdk-2.1.17.dist-info → langtrace_python_sdk-2.1.19.dist-info}/METADATA +2 -1
- {langtrace_python_sdk-2.1.17.dist-info → langtrace_python_sdk-2.1.19.dist-info}/RECORD +39 -32
- {langtrace_python_sdk-2.1.17.dist-info → langtrace_python_sdk-2.1.19.dist-info}/WHEEL +0 -0
- {langtrace_python_sdk-2.1.17.dist-info → langtrace_python_sdk-2.1.19.dist-info}/entry_points.txt +0 -0
- {langtrace_python_sdk-2.1.17.dist-info → langtrace_python_sdk-2.1.19.dist-info}/licenses/LICENSE +0 -0
|
@@ -5,8 +5,7 @@ from inspect_ai.dataset import csv_dataset
|
|
|
5
5
|
from inspect_ai.scorer import model_graded_qa
|
|
6
6
|
from inspect_ai.solver import chain_of_thought, generate, self_critique
|
|
7
7
|
|
|
8
|
-
from langtrace_python_sdk.extensions.langtrace_filesystem import
|
|
9
|
-
LangTraceFileSystem
|
|
8
|
+
from langtrace_python_sdk.extensions.langtrace_filesystem import LangTraceFileSystem
|
|
10
9
|
|
|
11
10
|
# from langtrace_python_sdk import langtrace
|
|
12
11
|
|
|
@@ -20,9 +19,6 @@ fsspec.register_implementation(LangTraceFileSystem.protocol, LangTraceFileSystem
|
|
|
20
19
|
def security_guide():
|
|
21
20
|
return Task(
|
|
22
21
|
dataset=csv_dataset("langtracefs://clxc2mxu6000lpc7ntsvcjvp9"),
|
|
23
|
-
plan=[
|
|
24
|
-
|
|
25
|
-
self_critique()
|
|
26
|
-
],
|
|
27
|
-
scorer=model_graded_qa()
|
|
22
|
+
plan=[chain_of_thought(), self_critique()],
|
|
23
|
+
scorer=model_graded_qa(),
|
|
28
24
|
)
|
|
@@ -8,14 +8,15 @@ 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
|
|
12
|
-
|
|
13
|
-
with_additional_attributes,
|
|
14
|
-
)
|
|
11
|
+
from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
|
|
12
|
+
from openai import OpenAI
|
|
15
13
|
|
|
16
14
|
_ = load_dotenv(find_dotenv())
|
|
17
15
|
|
|
18
|
-
langtrace.init(
|
|
16
|
+
langtrace.init(
|
|
17
|
+
write_spans_to_console=False,
|
|
18
|
+
disable_tracing_for_functions={"langchain": ["RunnableSequence.invoke"]},
|
|
19
|
+
)
|
|
19
20
|
|
|
20
21
|
|
|
21
22
|
def api_call_1():
|
|
@@ -29,7 +30,8 @@ def api_call_1():
|
|
|
29
30
|
output_parser = StrOutputParser()
|
|
30
31
|
chain = prompt | llm | output_parser
|
|
31
32
|
res = chain.invoke({"input": "how can langsmith help with testing?"})
|
|
32
|
-
print(res)
|
|
33
|
+
# print(res)
|
|
34
|
+
return res
|
|
33
35
|
|
|
34
36
|
|
|
35
37
|
def api_call_2():
|
|
@@ -43,13 +45,26 @@ def api_call_2():
|
|
|
43
45
|
output_parser = StrOutputParser()
|
|
44
46
|
chain = prompt | llm | output_parser
|
|
45
47
|
res = chain.invoke({"input": "how can langsmith help with testing?"})
|
|
46
|
-
print(res)
|
|
48
|
+
# print(res)
|
|
49
|
+
return res
|
|
47
50
|
|
|
48
51
|
|
|
49
52
|
@with_langtrace_root_span()
|
|
50
53
|
def basic_app():
|
|
51
54
|
api_call_1()
|
|
52
|
-
api_call_2()
|
|
55
|
+
# api_call_2()
|
|
56
|
+
client = OpenAI()
|
|
57
|
+
response = client.chat.completions.create(
|
|
58
|
+
model="gpt-4",
|
|
59
|
+
messages=[
|
|
60
|
+
{"role": "system", "content": "Talk like a pirate"},
|
|
61
|
+
{"role": "user", "content": "Tell me a story in 3 sentences or less."},
|
|
62
|
+
],
|
|
63
|
+
# stream=True,
|
|
64
|
+
stream=False,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
return response
|
|
53
68
|
|
|
54
69
|
|
|
55
70
|
@with_langtrace_root_span()
|
|
@@ -7,7 +7,11 @@ from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
|
|
|
7
7
|
_ = load_dotenv(find_dotenv())
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
langtrace.init(
|
|
10
|
+
langtrace.init(
|
|
11
|
+
disable_tracing_for_functions={
|
|
12
|
+
"open_ai": ["openai.chat.completions.create"],
|
|
13
|
+
}
|
|
14
|
+
)
|
|
11
15
|
|
|
12
16
|
|
|
13
17
|
@with_langtrace_root_span()
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from .basic import chat, async_chat, async_generate, generate, embed, async_embed
|
|
2
|
+
from langtrace_python_sdk import with_langtrace_root_span
|
|
3
|
+
import asyncio
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class OllamaRunner:
|
|
7
|
+
@with_langtrace_root_span("OllamaRunner")
|
|
8
|
+
def run(self):
|
|
9
|
+
chat()
|
|
10
|
+
generate()
|
|
11
|
+
embed()
|
|
12
|
+
asyncio.run(async_chat())
|
|
13
|
+
asyncio.run(async_generate())
|
|
14
|
+
asyncio.run(async_embed())
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from langtrace_python_sdk import langtrace, with_langtrace_root_span
|
|
2
|
+
import ollama
|
|
3
|
+
from ollama import AsyncClient
|
|
4
|
+
from dotenv import load_dotenv
|
|
5
|
+
|
|
6
|
+
load_dotenv()
|
|
7
|
+
|
|
8
|
+
langtrace.init(write_spans_to_console=False)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def chat():
|
|
12
|
+
response = ollama.chat(
|
|
13
|
+
model="llama3",
|
|
14
|
+
messages=[
|
|
15
|
+
{
|
|
16
|
+
"role": "user",
|
|
17
|
+
"content": "hi",
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
stream=True,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
return response
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
async def async_chat():
|
|
27
|
+
message = {"role": "user", "content": "Why is the sky blue?"}
|
|
28
|
+
return await AsyncClient().chat(model="llama3", messages=[message])
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def generate():
|
|
32
|
+
return ollama.generate(model="llama3", prompt="Why is the sky blue?")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def async_generate():
|
|
36
|
+
return AsyncClient().generate(model="llama3", prompt="Why is the sky blue?")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def embed():
|
|
40
|
+
return ollama.embeddings(
|
|
41
|
+
model="llama3",
|
|
42
|
+
prompt="cat",
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
async def async_embed():
|
|
47
|
+
return await AsyncClient().embeddings(
|
|
48
|
+
model="llama3",
|
|
49
|
+
prompt="cat",
|
|
50
|
+
)
|
|
@@ -13,6 +13,7 @@ from langtrace_python_sdk import (
|
|
|
13
13
|
with_additional_attributes,
|
|
14
14
|
)
|
|
15
15
|
from langtrace_python_sdk.utils.with_root_span import SendUserFeedback
|
|
16
|
+
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
|
|
16
17
|
|
|
17
18
|
_ = load_dotenv(find_dotenv())
|
|
18
19
|
langtrace.init()
|
langtrace_python_sdk/__init__.py
CHANGED
|
@@ -15,12 +15,14 @@ limitations under the License.
|
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
17
|
from langtrace_python_sdk import langtrace
|
|
18
|
-
from langtrace_python_sdk.extensions.langtrace_filesystem import
|
|
19
|
-
LangTraceFileSystem
|
|
18
|
+
from langtrace_python_sdk.extensions.langtrace_filesystem import LangTraceFileSystem
|
|
20
19
|
from langtrace_python_sdk.utils.prompt_registry import get_prompt_from_registry
|
|
21
20
|
from langtrace_python_sdk.utils.with_root_span import (
|
|
22
|
-
SendUserFeedback,
|
|
23
|
-
|
|
21
|
+
SendUserFeedback,
|
|
22
|
+
inject_additional_attributes,
|
|
23
|
+
with_additional_attributes,
|
|
24
|
+
with_langtrace_root_span,
|
|
25
|
+
)
|
|
24
26
|
|
|
25
27
|
__all__ = [
|
|
26
28
|
"langtrace",
|
|
@@ -15,7 +15,7 @@ OpenBinaryMode = Literal["rb", "ab", "wb"]
|
|
|
15
15
|
|
|
16
16
|
class OpenMode(str):
|
|
17
17
|
def __init_subclass__(cls, **kwargs):
|
|
18
|
-
allowed_values = set(OpenTextMode.__args__)
|
|
18
|
+
allowed_values = Union[set(OpenTextMode.__args__), set(OpenBinaryMode.__args__)]
|
|
19
19
|
super().__init_subclass__(**kwargs)
|
|
20
20
|
|
|
21
21
|
def __new__(cls, value):
|
|
@@ -50,13 +50,17 @@ class LangTraceFile(io.BytesIO):
|
|
|
50
50
|
log = file_data.decode("utf-8")
|
|
51
51
|
eval_log = json.loads(log)
|
|
52
52
|
data = {
|
|
53
|
-
"runId": eval_log[
|
|
54
|
-
"taskId": eval_log[
|
|
53
|
+
"runId": eval_log["eval"]["run_id"],
|
|
54
|
+
"taskId": eval_log["eval"]["task_id"],
|
|
55
55
|
"log": log,
|
|
56
56
|
}
|
|
57
57
|
if self.path is not None:
|
|
58
58
|
dataset_id = self.path.split("/")[0]
|
|
59
|
-
print(
|
|
59
|
+
print(
|
|
60
|
+
Fore.GREEN
|
|
61
|
+
+ f"Sending results to Langtrace for dataset: {dataset_id}"
|
|
62
|
+
+ Fore.RESET
|
|
63
|
+
)
|
|
60
64
|
data["datasetId"] = dataset_id
|
|
61
65
|
else:
|
|
62
66
|
print(Fore.GREEN + "Sending results to Langtrace" + Fore.RESET)
|
|
@@ -65,14 +69,12 @@ class LangTraceFile(io.BytesIO):
|
|
|
65
69
|
data=json.dumps(data),
|
|
66
70
|
headers={
|
|
67
71
|
"Content-Type": "application/json",
|
|
68
|
-
"x-api-key": os.environ.get("LANGTRACE_API_KEY")
|
|
72
|
+
"x-api-key": os.environ.get("LANGTRACE_API_KEY"),
|
|
69
73
|
},
|
|
70
74
|
timeout=20,
|
|
71
75
|
)
|
|
72
76
|
response.raise_for_status()
|
|
73
|
-
print(
|
|
74
|
-
Fore.GREEN + "Results sent to Langtrace successfully." + Fore.RESET
|
|
75
|
-
)
|
|
77
|
+
print(Fore.GREEN + "Results sent to Langtrace successfully." + Fore.RESET)
|
|
76
78
|
except requests.exceptions.RequestException as error:
|
|
77
79
|
print(Fore.RED + f"Error reporting results: {error}" + Fore.RESET)
|
|
78
80
|
|
|
@@ -89,9 +91,9 @@ class LangTraceFileSystem(AbstractFileSystem):
|
|
|
89
91
|
def open(
|
|
90
92
|
self,
|
|
91
93
|
path: str,
|
|
92
|
-
mode: OpenTextMode
|
|
94
|
+
mode: Union[OpenTextMode, OpenBinaryMode] = "rb",
|
|
93
95
|
**kwargs,
|
|
94
|
-
) -> Iterator[LangTraceFile
|
|
96
|
+
) -> Iterator[Union[LangTraceFile, io.BytesIO]]:
|
|
95
97
|
if "r" in mode:
|
|
96
98
|
dataset_id = path
|
|
97
99
|
# Fetch file from API and return a BytesIO object
|
|
@@ -104,21 +106,33 @@ class LangTraceFileSystem(AbstractFileSystem):
|
|
|
104
106
|
|
|
105
107
|
def fetch_file_from_api(self, dataset_id: str) -> bytes:
|
|
106
108
|
try:
|
|
107
|
-
print(
|
|
109
|
+
print(
|
|
110
|
+
Fore.GREEN
|
|
111
|
+
+ f"Fetching dataset with id: {dataset_id} from Langtrace"
|
|
112
|
+
+ Fore.RESET
|
|
113
|
+
)
|
|
108
114
|
response = requests.get(
|
|
109
115
|
url=f"{LANGTRACE_REMOTE_URL}/api/dataset/download?id={dataset_id}",
|
|
110
116
|
headers={
|
|
111
117
|
"Content-Type": "application/json",
|
|
112
|
-
"x-api-key": os.environ.get("LANGTRACE_API_KEY")
|
|
118
|
+
"x-api-key": os.environ.get("LANGTRACE_API_KEY"),
|
|
113
119
|
},
|
|
114
120
|
timeout=20,
|
|
115
121
|
)
|
|
116
|
-
print(
|
|
122
|
+
print(
|
|
123
|
+
Fore.GREEN
|
|
124
|
+
+ f"Successfully fetched dataset with id: {dataset_id} from Langtrace"
|
|
125
|
+
+ Fore.RESET
|
|
126
|
+
)
|
|
117
127
|
response.raise_for_status()
|
|
118
128
|
file_data = response.content
|
|
119
129
|
return file_data
|
|
120
130
|
except requests.exceptions.RequestException as error:
|
|
121
|
-
print(
|
|
131
|
+
print(
|
|
132
|
+
Fore.RED
|
|
133
|
+
+ f"Error fetching dataset with id: {dataset_id} from Langtrace: {error}"
|
|
134
|
+
+ Fore.RESET
|
|
135
|
+
)
|
|
122
136
|
return b""
|
|
123
137
|
|
|
124
138
|
def makedirs(self, path: str, exist_ok: bool = False) -> None:
|
|
@@ -167,7 +181,9 @@ class LangTraceFileSystem(AbstractFileSystem):
|
|
|
167
181
|
if path in self.dirs:
|
|
168
182
|
dirs = [d for d in self.dirs if d.startswith(path + self.sep)]
|
|
169
183
|
files = [f for f in self.files if f.startswith(path + self.sep)]
|
|
170
|
-
yield path, [d.split(self.sep)[-1] for d in dirs], [
|
|
184
|
+
yield path, [d.split(self.sep)[-1] for d in dirs], [
|
|
185
|
+
f.split(self.sep)[-1] for f in files
|
|
186
|
+
]
|
|
171
187
|
for d in dirs:
|
|
172
188
|
yield from self._walk(d)
|
|
173
189
|
|
|
@@ -11,6 +11,7 @@ from .openai import OpenAIInstrumentation
|
|
|
11
11
|
from .pinecone import PineconeInstrumentation
|
|
12
12
|
from .qdrant import QdrantInstrumentation
|
|
13
13
|
from .weaviate import WeaviateInstrumentation
|
|
14
|
+
from .ollama import OllamaInstrumentor
|
|
14
15
|
|
|
15
16
|
__all__ = [
|
|
16
17
|
"AnthropicInstrumentation",
|
|
@@ -26,4 +27,5 @@ __all__ = [
|
|
|
26
27
|
"PineconeInstrumentation",
|
|
27
28
|
"QdrantInstrumentation",
|
|
28
29
|
"WeaviateInstrumentation",
|
|
30
|
+
"OllamaInstrumentor",
|
|
29
31
|
]
|
|
@@ -17,10 +17,10 @@ limitations under the License.
|
|
|
17
17
|
from langtrace.trace_attributes import DatabaseSpanAttributes
|
|
18
18
|
from langtrace_python_sdk.utils.llm import set_span_attributes
|
|
19
19
|
from langtrace_python_sdk.utils.silently_fail import silently_fail
|
|
20
|
-
from opentelemetry import baggage
|
|
20
|
+
from opentelemetry import baggage, trace
|
|
21
21
|
from opentelemetry.trace import SpanKind
|
|
22
22
|
from opentelemetry.trace.status import Status, StatusCode
|
|
23
|
-
|
|
23
|
+
from opentelemetry.trace.propagation import set_span_in_context
|
|
24
24
|
from langtrace_python_sdk.constants.instrumentation.chroma import APIS
|
|
25
25
|
from langtrace_python_sdk.constants.instrumentation.common import (
|
|
26
26
|
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY,
|
|
@@ -59,7 +59,11 @@ def collection_patch(method, version, tracer):
|
|
|
59
59
|
|
|
60
60
|
attributes = DatabaseSpanAttributes(**span_attributes)
|
|
61
61
|
|
|
62
|
-
with tracer.start_as_current_span(
|
|
62
|
+
with tracer.start_as_current_span(
|
|
63
|
+
api["METHOD"],
|
|
64
|
+
kind=SpanKind.CLIENT,
|
|
65
|
+
context=set_span_in_context(trace.get_current_span()),
|
|
66
|
+
) as span:
|
|
63
67
|
for field, value in attributes.model_dump(by_alias=True).items():
|
|
64
68
|
if value is not None:
|
|
65
69
|
span.set_attribute(field, value)
|
|
@@ -17,7 +17,8 @@ limitations under the License.
|
|
|
17
17
|
import json
|
|
18
18
|
|
|
19
19
|
from langtrace.trace_attributes import Event, LLMSpanAttributes
|
|
20
|
-
from opentelemetry import baggage
|
|
20
|
+
from opentelemetry import baggage, trace
|
|
21
|
+
from opentelemetry.trace.propagation import set_span_in_context
|
|
21
22
|
from opentelemetry.trace import SpanKind
|
|
22
23
|
from opentelemetry.trace.status import Status, StatusCode
|
|
23
24
|
|
|
@@ -112,7 +113,9 @@ def chat_completions_create(original_method, version, tracer):
|
|
|
112
113
|
# with tracer.start_as_current_span(APIS["CHAT_COMPLETION"]["METHOD"],
|
|
113
114
|
# kind=SpanKind.CLIENT) as span:
|
|
114
115
|
span = tracer.start_span(
|
|
115
|
-
APIS["CHAT_COMPLETION"]["METHOD"],
|
|
116
|
+
APIS["CHAT_COMPLETION"]["METHOD"],
|
|
117
|
+
kind=SpanKind.CLIENT,
|
|
118
|
+
context=set_span_in_context(trace.get_current_span()),
|
|
116
119
|
)
|
|
117
120
|
for field, value in attributes.model_dump(by_alias=True).items():
|
|
118
121
|
if value is not None:
|
|
@@ -17,7 +17,9 @@ limitations under the License.
|
|
|
17
17
|
import json
|
|
18
18
|
|
|
19
19
|
from langtrace.trace_attributes import FrameworkSpanAttributes
|
|
20
|
-
from
|
|
20
|
+
from langtrace_python_sdk.constants import LANGTRACE_SDK_NAME
|
|
21
|
+
from opentelemetry import baggage, trace
|
|
22
|
+
from opentelemetry.trace.propagation import set_span_in_context
|
|
21
23
|
from opentelemetry.trace import SpanKind, StatusCode
|
|
22
24
|
from opentelemetry.trace.status import Status
|
|
23
25
|
|
|
@@ -54,7 +56,11 @@ def generic_patch(
|
|
|
54
56
|
|
|
55
57
|
attributes = FrameworkSpanAttributes(**span_attributes)
|
|
56
58
|
|
|
57
|
-
with tracer.start_as_current_span(
|
|
59
|
+
with tracer.start_as_current_span(
|
|
60
|
+
method_name,
|
|
61
|
+
kind=SpanKind.CLIENT,
|
|
62
|
+
context=set_span_in_context(trace.get_current_span()),
|
|
63
|
+
) as span:
|
|
58
64
|
for field, value in attributes.model_dump(by_alias=True).items():
|
|
59
65
|
if value is not None:
|
|
60
66
|
span.set_attribute(field, value)
|
|
@@ -17,7 +17,9 @@ limitations under the License.
|
|
|
17
17
|
import json
|
|
18
18
|
|
|
19
19
|
from langtrace.trace_attributes import FrameworkSpanAttributes
|
|
20
|
-
from opentelemetry import baggage
|
|
20
|
+
from opentelemetry import baggage, trace
|
|
21
|
+
from opentelemetry.trace.propagation import set_span_in_context
|
|
22
|
+
|
|
21
23
|
from opentelemetry.trace import SpanKind
|
|
22
24
|
from opentelemetry.trace.status import Status, StatusCode
|
|
23
25
|
|
|
@@ -52,7 +54,11 @@ def generic_patch(
|
|
|
52
54
|
|
|
53
55
|
attributes = FrameworkSpanAttributes(**span_attributes)
|
|
54
56
|
|
|
55
|
-
with tracer.start_as_current_span(
|
|
57
|
+
with tracer.start_as_current_span(
|
|
58
|
+
method_name,
|
|
59
|
+
kind=SpanKind.CLIENT,
|
|
60
|
+
context=set_span_in_context(trace.get_current_span()),
|
|
61
|
+
) as span:
|
|
56
62
|
for field, value in attributes.model_dump(by_alias=True).items():
|
|
57
63
|
if value is not None:
|
|
58
64
|
span.set_attribute(field, value)
|
|
@@ -17,9 +17,10 @@ limitations under the License.
|
|
|
17
17
|
import json
|
|
18
18
|
|
|
19
19
|
from langtrace.trace_attributes import FrameworkSpanAttributes
|
|
20
|
-
from opentelemetry import baggage
|
|
20
|
+
from opentelemetry import baggage, trace
|
|
21
21
|
from opentelemetry.trace import SpanKind, StatusCode
|
|
22
22
|
from opentelemetry.trace.status import Status
|
|
23
|
+
from opentelemetry.trace.propagation import set_span_in_context
|
|
23
24
|
|
|
24
25
|
from langtrace_python_sdk.constants.instrumentation.common import (
|
|
25
26
|
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY,
|
|
@@ -73,7 +74,12 @@ def generic_patch(
|
|
|
73
74
|
|
|
74
75
|
attributes = FrameworkSpanAttributes(**span_attributes)
|
|
75
76
|
|
|
76
|
-
with tracer.start_as_current_span(
|
|
77
|
+
with tracer.start_as_current_span(
|
|
78
|
+
method_name,
|
|
79
|
+
kind=SpanKind.CLIENT,
|
|
80
|
+
context=set_span_in_context(trace.get_current_span()),
|
|
81
|
+
) as span:
|
|
82
|
+
|
|
77
83
|
for field, value in attributes.model_dump(by_alias=True).items():
|
|
78
84
|
if value is not None:
|
|
79
85
|
span.set_attribute(field, value)
|
|
@@ -147,7 +153,11 @@ def runnable_patch(
|
|
|
147
153
|
|
|
148
154
|
attributes = FrameworkSpanAttributes(**span_attributes)
|
|
149
155
|
|
|
150
|
-
with tracer.start_as_current_span(
|
|
156
|
+
with tracer.start_as_current_span(
|
|
157
|
+
method_name,
|
|
158
|
+
kind=SpanKind.CLIENT,
|
|
159
|
+
context=set_span_in_context(trace.get_current_span()),
|
|
160
|
+
) as span:
|
|
151
161
|
for field, value in attributes.model_dump(by_alias=True).items():
|
|
152
162
|
if value is not None:
|
|
153
163
|
span.set_attribute(field, value)
|
|
@@ -15,9 +15,10 @@ limitations under the License.
|
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
17
|
import json
|
|
18
|
+
from opentelemetry.trace.propagation import set_span_in_context
|
|
18
19
|
|
|
19
20
|
from langtrace.trace_attributes import FrameworkSpanAttributes
|
|
20
|
-
from opentelemetry import baggage
|
|
21
|
+
from opentelemetry import baggage, trace
|
|
21
22
|
from opentelemetry.trace import SpanKind
|
|
22
23
|
from opentelemetry.trace.status import Status, StatusCode
|
|
23
24
|
|
|
@@ -50,7 +51,11 @@ def patch_graph_methods(method_name, tracer, version):
|
|
|
50
51
|
|
|
51
52
|
attributes = FrameworkSpanAttributes(**span_attributes)
|
|
52
53
|
|
|
53
|
-
with tracer.start_as_current_span(
|
|
54
|
+
with tracer.start_as_current_span(
|
|
55
|
+
method_name,
|
|
56
|
+
kind=SpanKind.CLIENT,
|
|
57
|
+
context=set_span_in_context(trace.get_current_span()),
|
|
58
|
+
) as span:
|
|
54
59
|
for field, value in attributes.model_dump(by_alias=True).items():
|
|
55
60
|
if value is not None:
|
|
56
61
|
span.set_attribute(field, value)
|
|
@@ -15,10 +15,10 @@ limitations under the License.
|
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
17
|
from langtrace.trace_attributes import FrameworkSpanAttributes
|
|
18
|
-
from opentelemetry import baggage
|
|
18
|
+
from opentelemetry import baggage, trace
|
|
19
19
|
from opentelemetry.trace import SpanKind
|
|
20
20
|
from opentelemetry.trace.status import Status, StatusCode
|
|
21
|
-
|
|
21
|
+
from opentelemetry.trace.propagation import set_span_in_context
|
|
22
22
|
from langtrace_python_sdk.constants.instrumentation.common import (
|
|
23
23
|
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY,
|
|
24
24
|
SERVICE_PROVIDERS,
|
|
@@ -48,7 +48,11 @@ def generic_patch(method, task, tracer, version):
|
|
|
48
48
|
|
|
49
49
|
attributes = FrameworkSpanAttributes(**span_attributes)
|
|
50
50
|
|
|
51
|
-
with tracer.start_as_current_span(
|
|
51
|
+
with tracer.start_as_current_span(
|
|
52
|
+
method,
|
|
53
|
+
kind=SpanKind.CLIENT,
|
|
54
|
+
context=set_span_in_context(trace.get_current_span()),
|
|
55
|
+
) as span:
|
|
52
56
|
for field, value in attributes.model_dump(by_alias=True).items():
|
|
53
57
|
if value is not None:
|
|
54
58
|
span.set_attribute(field, value)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2024 Scale3 Labs
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
|
|
18
|
+
from opentelemetry.trace import get_tracer
|
|
19
|
+
from wrapt import wrap_function_wrapper as _W
|
|
20
|
+
from typing import Collection
|
|
21
|
+
from importlib_metadata import version as v
|
|
22
|
+
from langtrace_python_sdk.constants.instrumentation.ollama import APIS
|
|
23
|
+
from .patch import generic_patch, ageneric_patch
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class OllamaInstrumentor(BaseInstrumentor):
|
|
27
|
+
"""
|
|
28
|
+
The OllamaInstrumentor class represents the Ollama instrumentation"""
|
|
29
|
+
|
|
30
|
+
def instrumentation_dependencies(self) -> Collection[str]:
|
|
31
|
+
return ["ollama >= 0.2.0, < 1"]
|
|
32
|
+
|
|
33
|
+
def _instrument(self, **kwargs):
|
|
34
|
+
tracer_provider = kwargs.get("tracer_provider")
|
|
35
|
+
tracer = get_tracer(__name__, "", tracer_provider)
|
|
36
|
+
version = v("ollama")
|
|
37
|
+
for operation_name, details in APIS.items():
|
|
38
|
+
operation = details["METHOD"]
|
|
39
|
+
# Dynamically creating the patching call
|
|
40
|
+
_W(
|
|
41
|
+
"ollama._client",
|
|
42
|
+
f"Client.{operation}",
|
|
43
|
+
generic_patch(operation_name, version, tracer),
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
_W(
|
|
47
|
+
"ollama._client",
|
|
48
|
+
f"AsyncClient.{operation}",
|
|
49
|
+
ageneric_patch(operation_name, version, tracer),
|
|
50
|
+
)
|
|
51
|
+
_W(
|
|
52
|
+
"ollama",
|
|
53
|
+
f"{operation}",
|
|
54
|
+
generic_patch(operation_name, version, tracer),
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
def _uninstrument(self, **kwargs):
|
|
58
|
+
pass
|