langtrace-python-sdk 1.0.9__tar.gz → 1.0.10__tar.gz
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.
- {langtrace-python-sdk-1.0.9/src/langtrace_python_sdk.egg-info → langtrace-python-sdk-1.0.10}/PKG-INFO +1 -1
- langtrace-python-sdk-1.0.10/README.md +22 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/setup.py +1 -1
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/chroma_example/basic.py +1 -1
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/langchain_example/basic.py +1 -1
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/langchain_example/tool.py +2 -2
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/llamaindex_example/basic.py +1 -1
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/openai/chat_completion.py +16 -3
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/openai/embeddings_create.py +1 -1
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/openai/images_generate.py +1 -1
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/pinecone_example/basic.py +1 -1
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/chroma/instrumentation.py +10 -4
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/chroma/patch.py +8 -2
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/constants.py +4 -9
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/langchain_core/instrumentation.py +11 -5
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/langchain_core/patch.py +24 -8
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/llamaindex/instrumentation.py +6 -4
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/llamaindex/patch.py +6 -1
- {langtrace-python-sdk-1.0.9/src/instrumentation/openai/lib → langtrace-python-sdk-1.0.10/src/instrumentation/openai}/apis.py +3 -0
- {langtrace-python-sdk-1.0.9/src/instrumentation/openai/lib → langtrace-python-sdk-1.0.10/src/instrumentation/openai}/constants.py +13 -0
- langtrace-python-sdk-1.0.10/src/instrumentation/openai/patch.py +268 -0
- langtrace-python-sdk-1.0.10/src/instrumentation/openai/token_estimation.py +48 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/pinecone/instrumentation.py +8 -2
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/pinecone/patch.py +6 -4
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10/src/langtrace_python_sdk.egg-info}/PKG-INFO +1 -1
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/langtrace_python_sdk.egg-info/SOURCES.txt +5 -6
- langtrace-python-sdk-1.0.9/README.md +0 -3
- langtrace-python-sdk-1.0.9/src/instrumentation/openai/patch.py +0 -209
- langtrace-python-sdk-1.0.9/src/instrumentation/setup.py +0 -50
- langtrace-python-sdk-1.0.9/src/instrumentation/utils.py +0 -27
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/LICENSE +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/setup.cfg +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/chroma_example/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/langchain_example/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/llamaindex_example/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/openai/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/pinecone_example/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/chroma/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9/src/instrumentation/chroma/lib → langtrace-python-sdk-1.0.10/src/instrumentation/chroma}/apis.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/chroma/lib/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/langchain/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/langchain/instrumentation.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/langchain/patch.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/langchain_community/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/langchain_community/instrumentation.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/langchain_community/patch.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/langchain_core/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/llamaindex/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/openai/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/openai/instrumentation.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/openai/lib/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/pinecone/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9/src/instrumentation/pinecone/lib → langtrace-python-sdk-1.0.10/src/instrumentation/pinecone}/apis.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/pinecone/lib/__init__.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/with_root_span.py +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/langtrace_python_sdk.egg-info/dependency_links.txt +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/langtrace_python_sdk.egg-info/requires.txt +0 -0
- {langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/langtrace_python_sdk.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# langtrace-python-sdk
|
|
2
|
+
|
|
3
|
+
export PYTHONPATH="/Users/karthikkalyanaraman/work/langtrace/python-sdk:$PYTHONPATH"
|
|
4
|
+
|
|
5
|
+
## Steps to run
|
|
6
|
+
|
|
7
|
+
1. From your root directory, create a virtualenv for installing your dependencies
|
|
8
|
+
```
|
|
9
|
+
python -m venv pysdk
|
|
10
|
+
```
|
|
11
|
+
2. Activate the virtualenv
|
|
12
|
+
```
|
|
13
|
+
source pysdk/bin/activate
|
|
14
|
+
```
|
|
15
|
+
3. Install the dependencies
|
|
16
|
+
```
|
|
17
|
+
pip install -r requirements.txt
|
|
18
|
+
```
|
|
19
|
+
4. Run the example and see the traces on the terminal
|
|
20
|
+
```
|
|
21
|
+
python src/entrypoint.py
|
|
22
|
+
```
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/chroma_example/basic.py
RENAMED
|
@@ -2,7 +2,7 @@ import chromadb
|
|
|
2
2
|
from chromadb.utils import embedding_functions
|
|
3
3
|
from dotenv import find_dotenv, load_dotenv
|
|
4
4
|
|
|
5
|
-
from
|
|
5
|
+
from examples.setup import setup_instrumentation
|
|
6
6
|
from instrumentation.with_root_span import with_langtrace_root_span
|
|
7
7
|
|
|
8
8
|
_ = load_dotenv(find_dotenv())
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/langchain_example/basic.py
RENAMED
|
@@ -7,7 +7,7 @@ from langchain_core.prompts.chat import ChatPromptTemplate
|
|
|
7
7
|
from langchain_core.runnables import RunnablePassthrough
|
|
8
8
|
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
|
9
9
|
|
|
10
|
-
from
|
|
10
|
+
from examples.setup import setup_instrumentation
|
|
11
11
|
from instrumentation.with_root_span import with_langtrace_root_span
|
|
12
12
|
|
|
13
13
|
_ = load_dotenv(find_dotenv())
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/langchain_example/tool.py
RENAMED
|
@@ -6,7 +6,7 @@ from langchain_core.pydantic_v1 import BaseModel, Field
|
|
|
6
6
|
from langchain_core.tools import Tool
|
|
7
7
|
from langchain_openai import ChatOpenAI
|
|
8
8
|
|
|
9
|
-
from
|
|
9
|
+
from examples.setup import setup_instrumentation
|
|
10
10
|
from instrumentation.with_root_span import with_langtrace_root_span
|
|
11
11
|
|
|
12
12
|
_ = load_dotenv(find_dotenv())
|
|
@@ -45,7 +45,7 @@ async def aget_prime(n: int, primes: dict = primes) -> str:
|
|
|
45
45
|
return str(primes.get(int(n)))
|
|
46
46
|
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
@with_langtrace_root_span()
|
|
49
49
|
def tool_example():
|
|
50
50
|
|
|
51
51
|
tools = [
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/llamaindex_example/basic.py
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from dotenv import find_dotenv, load_dotenv
|
|
2
2
|
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
|
|
3
3
|
|
|
4
|
-
from
|
|
4
|
+
from examples.setup import setup_instrumentation
|
|
5
5
|
from instrumentation.with_root_span import with_langtrace_root_span
|
|
6
6
|
|
|
7
7
|
_ = load_dotenv(find_dotenv())
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/openai/chat_completion.py
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from dotenv import find_dotenv, load_dotenv
|
|
2
2
|
from openai import OpenAI
|
|
3
3
|
|
|
4
|
-
from
|
|
4
|
+
from examples.setup import setup_instrumentation
|
|
5
5
|
from instrumentation.with_root_span import with_langtrace_root_span
|
|
6
6
|
|
|
7
7
|
_ = load_dotenv(find_dotenv())
|
|
@@ -13,14 +13,27 @@ client = OpenAI()
|
|
|
13
13
|
|
|
14
14
|
@with_langtrace_root_span()
|
|
15
15
|
def chat_completion():
|
|
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=True,
|
|
20
20
|
)
|
|
21
|
+
# print(stream)
|
|
21
22
|
# stream = client.chat.completions.create(
|
|
22
23
|
# model="gpt-4",
|
|
23
24
|
# 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"},
|
|
24
25
|
# {"role": "user", "content": "Say this is a mock 4 times"}],
|
|
25
26
|
# stream=False,
|
|
26
27
|
# )
|
|
28
|
+
|
|
29
|
+
result = []
|
|
30
|
+
for chunk in response:
|
|
31
|
+
if chunk.choices[0].delta.function_call is not None:
|
|
32
|
+
content = [
|
|
33
|
+
choice.delta.function_call.arguments if choice.delta.function_call and
|
|
34
|
+
choice.delta.function_call.arguments else ""
|
|
35
|
+
for choice in chunk.choices]
|
|
36
|
+
result.append(
|
|
37
|
+
content[0] if len(content) > 0 else "")
|
|
38
|
+
|
|
39
|
+
print("".join(result))
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/openai/embeddings_create.py
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from dotenv import find_dotenv, load_dotenv
|
|
2
2
|
from openai import OpenAI
|
|
3
3
|
|
|
4
|
-
from
|
|
4
|
+
from examples.setup import setup_instrumentation
|
|
5
5
|
from instrumentation.with_root_span import with_langtrace_root_span
|
|
6
6
|
|
|
7
7
|
_ = load_dotenv(find_dotenv())
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/openai/images_generate.py
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from dotenv import find_dotenv, load_dotenv
|
|
2
2
|
from openai import OpenAI
|
|
3
3
|
|
|
4
|
-
from
|
|
4
|
+
from examples.setup import setup_instrumentation
|
|
5
5
|
from instrumentation.with_root_span import with_langtrace_root_span
|
|
6
6
|
|
|
7
7
|
_ = load_dotenv(find_dotenv())
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/pinecone_example/basic.py
RENAMED
|
@@ -2,7 +2,7 @@ from dotenv import find_dotenv, load_dotenv
|
|
|
2
2
|
from openai import OpenAI
|
|
3
3
|
from pinecone import Pinecone
|
|
4
4
|
|
|
5
|
-
from
|
|
5
|
+
from examples.setup import setup_instrumentation
|
|
6
6
|
from instrumentation.with_root_span import with_langtrace_root_span
|
|
7
7
|
|
|
8
8
|
_ = load_dotenv(find_dotenv())
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Instrumentation for ChromaDB
|
|
3
|
+
"""
|
|
1
4
|
import importlib.metadata
|
|
2
5
|
from typing import Collection
|
|
3
6
|
|
|
@@ -5,11 +8,14 @@ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
|
|
|
5
8
|
from opentelemetry.trace import get_tracer
|
|
6
9
|
from wrapt import wrap_function_wrapper
|
|
7
10
|
|
|
8
|
-
from instrumentation.chroma.
|
|
11
|
+
from instrumentation.chroma.apis import APIS
|
|
9
12
|
from instrumentation.chroma.patch import collection_patch
|
|
10
13
|
|
|
11
14
|
|
|
12
15
|
class ChromaInstrumentation(BaseInstrumentor):
|
|
16
|
+
"""
|
|
17
|
+
The ChromaInstrumentation class represents the ChromaDB instrumentation
|
|
18
|
+
"""
|
|
13
19
|
|
|
14
20
|
def instrumentation_dependencies(self) -> Collection[str]:
|
|
15
21
|
return ["chromadb >= 0.4.23"]
|
|
@@ -19,7 +25,7 @@ class ChromaInstrumentation(BaseInstrumentor):
|
|
|
19
25
|
tracer = get_tracer(__name__, "", tracer_provider)
|
|
20
26
|
version = importlib.metadata.version('chromadb')
|
|
21
27
|
|
|
22
|
-
for operation,
|
|
28
|
+
for operation, _ in APIS.items():
|
|
23
29
|
wrap_function_wrapper(
|
|
24
30
|
'chromadb.api.models.Collection',
|
|
25
31
|
f'Collection.{operation.lower()}',
|
|
@@ -27,7 +33,7 @@ class ChromaInstrumentation(BaseInstrumentor):
|
|
|
27
33
|
)
|
|
28
34
|
|
|
29
35
|
def _instrument_module(self, module_name):
|
|
30
|
-
|
|
36
|
+
pass
|
|
31
37
|
|
|
32
38
|
def _uninstrument(self, **kwargs):
|
|
33
|
-
|
|
39
|
+
pass
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/chroma/patch.py
RENAMED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module contains the patching logic for the Chroma client.
|
|
3
|
+
"""
|
|
1
4
|
from langtrace.trace_attributes import DatabaseSpanAttributes
|
|
2
|
-
from opentelemetry.trace import SpanKind
|
|
5
|
+
from opentelemetry.trace import SpanKind
|
|
3
6
|
from opentelemetry.trace.status import Status, StatusCode
|
|
4
7
|
|
|
8
|
+
from instrumentation.chroma.apis import APIS
|
|
5
9
|
from instrumentation.constants import SERVICE_PROVIDERS
|
|
6
|
-
from instrumentation.chroma.lib.apis import APIS
|
|
7
10
|
|
|
8
11
|
|
|
9
12
|
def collection_patch(method, version, tracer):
|
|
13
|
+
"""
|
|
14
|
+
A generic patch method that wraps a function with a span
|
|
15
|
+
"""
|
|
10
16
|
def traced_method(wrapped, instance, args, kwargs):
|
|
11
17
|
api = APIS[method]
|
|
12
18
|
service_provider = SERVICE_PROVIDERS['CHROMA']
|
|
@@ -1,16 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
"LANGCHAIN": "Langtrace Langchain SDK",
|
|
5
|
-
"PINECONE": "Langtrace Pinecone SDK",
|
|
6
|
-
"LLAMAINDEX": "Langtrace LlamaIndex SDK",
|
|
7
|
-
"CHROMA": "Langtrace Chroma SDK",
|
|
8
|
-
}
|
|
9
|
-
|
|
1
|
+
"""
|
|
2
|
+
This file contains the constants used in the project.
|
|
3
|
+
"""
|
|
10
4
|
SERVICE_PROVIDERS = {
|
|
11
5
|
"OPENAI": "OpenAI",
|
|
12
6
|
"AZURE": "Azure",
|
|
13
7
|
"LANGCHAIN": "Langchain",
|
|
8
|
+
"LANGCHAIN_CORE": "Langchain Core",
|
|
14
9
|
"LANGCHAIN_COMMUNITY": "Langchain Community",
|
|
15
10
|
"PINECONE": "Pinecone",
|
|
16
11
|
"LLAMAINDEX": "LlamaIndex",
|
|
@@ -71,15 +71,21 @@ class LangchainCoreInstrumentation(BaseInstrumentor):
|
|
|
71
71
|
version = importlib.metadata.version('langchain-core')
|
|
72
72
|
|
|
73
73
|
exclude_methods = ['get_name', 'get_output_schema',
|
|
74
|
-
'get_input_schema', 'get_graph', 'to_json'
|
|
75
|
-
|
|
74
|
+
'get_input_schema', 'get_graph', 'to_json',
|
|
75
|
+
'to_json_not_implemented', 'bind', 'dict',
|
|
76
|
+
'format', 'format_messages', 'format_prompt']
|
|
77
|
+
exclude_classes = ['BaseChatPromptTemplate', 'Runnable', 'RunnableBinding',
|
|
78
|
+
'RunnableBindingBase', 'RunnableEach', 'RunnableEachBase',
|
|
79
|
+
'RunnableGenerator', 'RunnablePick', 'RunnableMap',
|
|
80
|
+
'RunnableSerializable']
|
|
81
|
+
|
|
76
82
|
modules_to_patch = [
|
|
77
83
|
('langchain_core.retrievers', 'retriever',
|
|
78
84
|
generic_patch, True, True),
|
|
79
|
-
('langchain_core.prompts.chat', '
|
|
80
|
-
generic_patch, True,
|
|
85
|
+
('langchain_core.prompts.chat', 'prompt',
|
|
86
|
+
generic_patch, True, True),
|
|
81
87
|
('langchain_core.runnables.base',
|
|
82
|
-
'
|
|
88
|
+
'runnable', runnable_patch, True, True),
|
|
83
89
|
('langchain_core.runnables.passthrough',
|
|
84
90
|
'runnablepassthrough', runnable_patch, True, True),
|
|
85
91
|
('langchain_core.output_parsers.string',
|
|
@@ -22,7 +22,7 @@ def generic_patch(method_name, task, tracer, version, trace_output=True, trace_i
|
|
|
22
22
|
"""
|
|
23
23
|
|
|
24
24
|
def traced_method(wrapped, instance, args, kwargs):
|
|
25
|
-
service_provider = SERVICE_PROVIDERS['
|
|
25
|
+
service_provider = SERVICE_PROVIDERS['LANGCHAIN_CORE']
|
|
26
26
|
span_attributes = {
|
|
27
27
|
'langtrace.service.name': service_provider,
|
|
28
28
|
'langtrace.service.type': 'framework',
|
|
@@ -32,7 +32,18 @@ def generic_patch(method_name, task, tracer, version, trace_output=True, trace_i
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
if len(args) > 0 and trace_input:
|
|
35
|
-
|
|
35
|
+
inputs = {}
|
|
36
|
+
for arg in args:
|
|
37
|
+
if isinstance(arg, dict):
|
|
38
|
+
for key, value in arg.items():
|
|
39
|
+
if isinstance(value, list):
|
|
40
|
+
for item in value:
|
|
41
|
+
inputs[key] = item.__class__.__name__
|
|
42
|
+
elif isinstance(value, str):
|
|
43
|
+
inputs[key] = value
|
|
44
|
+
elif isinstance(arg, str):
|
|
45
|
+
inputs['input'] = arg
|
|
46
|
+
span_attributes['langchain.inputs'] = to_json_string(inputs)
|
|
36
47
|
|
|
37
48
|
attributes = FrameworkSpanAttributes(**span_attributes)
|
|
38
49
|
|
|
@@ -73,7 +84,7 @@ def runnable_patch(method_name, task, tracer, version, trace_output=True, trace_
|
|
|
73
84
|
trace_input: Whether to trace the input of the patched methods.
|
|
74
85
|
"""
|
|
75
86
|
def traced_method(wrapped, instance, args, kwargs):
|
|
76
|
-
service_provider = SERVICE_PROVIDERS['
|
|
87
|
+
service_provider = SERVICE_PROVIDERS['LANGCHAIN_CORE']
|
|
77
88
|
span_attributes = {
|
|
78
89
|
'langtrace.service.name': service_provider,
|
|
79
90
|
'langtrace.service.type': 'framework',
|
|
@@ -84,12 +95,17 @@ def runnable_patch(method_name, task, tracer, version, trace_output=True, trace_
|
|
|
84
95
|
|
|
85
96
|
if trace_input:
|
|
86
97
|
inputs = {}
|
|
87
|
-
args_list = []
|
|
88
98
|
if len(args) > 0:
|
|
89
|
-
for
|
|
90
|
-
if isinstance(
|
|
91
|
-
|
|
92
|
-
|
|
99
|
+
for arg in args:
|
|
100
|
+
if isinstance(arg, dict):
|
|
101
|
+
for key, value in arg.items():
|
|
102
|
+
if isinstance(value, list):
|
|
103
|
+
for item in value:
|
|
104
|
+
inputs[key] = item.__class__.__name__
|
|
105
|
+
elif isinstance(value, str):
|
|
106
|
+
inputs[key] = value
|
|
107
|
+
elif isinstance(arg, str):
|
|
108
|
+
inputs['input'] = arg
|
|
93
109
|
|
|
94
110
|
for field, value in instance.steps.items() if hasattr(instance, "steps") and \
|
|
95
111
|
isinstance(instance.steps, dict) else {}:
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The LlamaindexInstrumentation class represents the LlamaIndex instrumentation
|
|
3
|
+
"""
|
|
1
4
|
import importlib.metadata
|
|
2
5
|
from typing import Collection
|
|
3
6
|
|
|
@@ -8,12 +11,11 @@ from wrapt import wrap_function_wrapper
|
|
|
8
11
|
|
|
9
12
|
from instrumentation.llamaindex.patch import generic_patch
|
|
10
13
|
|
|
11
|
-
MODULES = [
|
|
12
|
-
"llama_index.core.query_pipeline.query",
|
|
13
|
-
]
|
|
14
|
-
|
|
15
14
|
|
|
16
15
|
class LlamaindexInstrumentation(BaseInstrumentor):
|
|
16
|
+
"""
|
|
17
|
+
The LlamaindexInstrumentation class represents the LlamaIndex instrumentation
|
|
18
|
+
"""
|
|
17
19
|
|
|
18
20
|
def instrumentation_dependencies(self) -> Collection[str]:
|
|
19
21
|
return ["llama-index >= 0.10.0"]
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/llamaindex/patch.py
RENAMED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module contains a generic patch method that wraps a function with a span.
|
|
3
|
+
"""
|
|
1
4
|
from langtrace.trace_attributes import FrameworkSpanAttributes
|
|
2
|
-
from opentelemetry.trace import SpanKind
|
|
5
|
+
from opentelemetry.trace import SpanKind
|
|
3
6
|
from opentelemetry.trace.status import Status, StatusCode
|
|
4
7
|
|
|
5
8
|
from instrumentation.constants import SERVICE_PROVIDERS
|
|
6
9
|
|
|
7
10
|
|
|
8
11
|
def generic_patch(method, task, tracer, version):
|
|
12
|
+
"""
|
|
13
|
+
A generic patch method that wraps a function with a span"""
|
|
9
14
|
def traced_method(wrapped, instance, args, kwargs):
|
|
10
15
|
service_provider = SERVICE_PROVIDERS['LLAMAINDEX']
|
|
11
16
|
span_attributes = {
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Constants for OpenAI API"""
|
|
3
|
+
|
|
1
4
|
OPENAI_COST_TABLE = {
|
|
2
5
|
"gpt-4-0125-preview": {
|
|
3
6
|
"input": 0.01,
|
|
@@ -28,3 +31,13 @@ OPENAI_COST_TABLE = {
|
|
|
28
31
|
"output": 0.002,
|
|
29
32
|
},
|
|
30
33
|
}
|
|
34
|
+
|
|
35
|
+
# TODO: Add more models
|
|
36
|
+
# https://github.com/dqbd/tiktoken/blob/74c147e19584a3a1acea0c8e0da4d39415cd33e0/wasm/src/lib.rs#L328
|
|
37
|
+
TIKTOKEN_MODEL_MAPPING = {
|
|
38
|
+
"gpt-4": "cl100k_base",
|
|
39
|
+
"gpt-4-32k": "cl100k_base",
|
|
40
|
+
"gpt-4-0125-preview": "cl100k_base",
|
|
41
|
+
"gpt-4-1106-preview": "cl100k_base",
|
|
42
|
+
"gpt-4-1106-vision-preview": "cl100k_base",
|
|
43
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module contains the patching logic for the OpenAI library."""
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
from langtrace.trace_attributes import Event, LLMSpanAttributes
|
|
6
|
+
from opentelemetry.trace import SpanKind
|
|
7
|
+
from opentelemetry.trace.status import Status, StatusCode
|
|
8
|
+
|
|
9
|
+
from instrumentation.constants import SERVICE_PROVIDERS
|
|
10
|
+
from instrumentation.openai.apis import APIS
|
|
11
|
+
from instrumentation.openai.token_estimation import (calculate_prompt_tokens,
|
|
12
|
+
estimate_tokens)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def images_generate(original_method, version, tracer):
|
|
16
|
+
"""
|
|
17
|
+
Wrap the `generate` method of the `Images` class to trace it.
|
|
18
|
+
"""
|
|
19
|
+
def traced_method(wrapped, instance, args, kwargs):
|
|
20
|
+
base_url = str(instance._client._base_url) if hasattr(
|
|
21
|
+
instance, '_client') and hasattr(instance._client, '_base_url') else ""
|
|
22
|
+
service_provider = SERVICE_PROVIDERS['OPENAI']
|
|
23
|
+
span_attributes = {
|
|
24
|
+
"langtrace.service.name": service_provider,
|
|
25
|
+
"langtrace.service.type": "llm",
|
|
26
|
+
"langtrace.service.version": version,
|
|
27
|
+
"langtrace.version": "1.0.0",
|
|
28
|
+
"url.full": base_url,
|
|
29
|
+
"llm.api": APIS["IMAGES_GENERATION"]["ENDPOINT"],
|
|
30
|
+
"llm.model": kwargs.get('model'),
|
|
31
|
+
"llm.stream": kwargs.get('stream'),
|
|
32
|
+
"llm.prompts": json.dumps([kwargs.get('prompt', [])])
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
attributes = LLMSpanAttributes(**span_attributes)
|
|
36
|
+
|
|
37
|
+
with tracer.start_as_current_span(APIS["IMAGES_GENERATION"]["METHOD"],
|
|
38
|
+
kind=SpanKind.CLIENT) as span:
|
|
39
|
+
for field, value in attributes.model_dump(by_alias=True).items():
|
|
40
|
+
if value is not None:
|
|
41
|
+
span.set_attribute(field, value)
|
|
42
|
+
try:
|
|
43
|
+
# Attempt to call the original method
|
|
44
|
+
result = original_method(*args, **kwargs)
|
|
45
|
+
if kwargs.get('stream') is False or kwargs.get('stream') is None:
|
|
46
|
+
data = result.data[0] if hasattr(
|
|
47
|
+
result, 'data') and len(result.data) > 0 else {}
|
|
48
|
+
response = [{
|
|
49
|
+
"url": data.url if hasattr(data, 'url') else "",
|
|
50
|
+
"revised_prompt": data.revised_prompt if
|
|
51
|
+
hasattr(data, 'revised_prompt') else "",
|
|
52
|
+
}]
|
|
53
|
+
span.set_attribute(
|
|
54
|
+
"llm.responses", json.dumps(response))
|
|
55
|
+
|
|
56
|
+
span.set_status(StatusCode.OK)
|
|
57
|
+
return result
|
|
58
|
+
except Exception as e:
|
|
59
|
+
# Record the exception in the span
|
|
60
|
+
span.record_exception(e)
|
|
61
|
+
|
|
62
|
+
# Set the span status to indicate an error
|
|
63
|
+
span.set_status(Status(StatusCode.ERROR, str(e)))
|
|
64
|
+
|
|
65
|
+
# Reraise the exception to ensure it's not swallowed
|
|
66
|
+
raise
|
|
67
|
+
|
|
68
|
+
return traced_method
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def chat_completions_create(original_method, version, tracer):
|
|
72
|
+
"""Wrap the `create` method of the `ChatCompletion` class to trace it."""
|
|
73
|
+
def traced_method(wrapped, instance, args, kwargs):
|
|
74
|
+
base_url = str(instance._client._base_url) if hasattr(
|
|
75
|
+
instance, '_client') and hasattr(instance._client, '_base_url') else ""
|
|
76
|
+
service_provider = SERVICE_PROVIDERS['OPENAI']
|
|
77
|
+
span_attributes = {
|
|
78
|
+
"langtrace.service.name": service_provider,
|
|
79
|
+
"langtrace.service.type": "llm",
|
|
80
|
+
"langtrace.service.version": version,
|
|
81
|
+
"langtrace.version": "1.0.0",
|
|
82
|
+
"url.full": base_url,
|
|
83
|
+
"llm.api": APIS["CHAT_COMPLETION"]["ENDPOINT"],
|
|
84
|
+
"llm.model": kwargs.get('model'),
|
|
85
|
+
"llm.prompts": json.dumps(kwargs.get('messages', [])),
|
|
86
|
+
"llm.stream": kwargs.get('stream'),
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
attributes = LLMSpanAttributes(**span_attributes)
|
|
90
|
+
|
|
91
|
+
if kwargs.get('temperature') is not None:
|
|
92
|
+
attributes.llm_temperature = kwargs.get('temperature')
|
|
93
|
+
if kwargs.get('top_p') is not None:
|
|
94
|
+
attributes.llm_top_p = kwargs.get('top_p')
|
|
95
|
+
if kwargs.get('user') is not None:
|
|
96
|
+
attributes.llm_user = kwargs.get('user')
|
|
97
|
+
if kwargs.get('functions') is not None:
|
|
98
|
+
attributes.llm_function_prompts = json.dumps(
|
|
99
|
+
kwargs.get('functions'))
|
|
100
|
+
|
|
101
|
+
# TODO(Karthik): Gotta figure out how to handle streaming with context
|
|
102
|
+
# with tracer.start_as_current_span(APIS["CHAT_COMPLETION"]["METHOD"],
|
|
103
|
+
# kind=SpanKind.CLIENT) as span:
|
|
104
|
+
span = tracer.start_span(
|
|
105
|
+
APIS["CHAT_COMPLETION"]["METHOD"], kind=SpanKind.CLIENT)
|
|
106
|
+
for field, value in attributes.model_dump(by_alias=True).items():
|
|
107
|
+
if value is not None:
|
|
108
|
+
span.set_attribute(field, value)
|
|
109
|
+
try:
|
|
110
|
+
# Attempt to call the original method
|
|
111
|
+
result = original_method(*args, **kwargs)
|
|
112
|
+
if kwargs.get('stream') is False:
|
|
113
|
+
if hasattr(result, 'choices') and result.choices is not None:
|
|
114
|
+
responses = [
|
|
115
|
+
{
|
|
116
|
+
"message": choice.message.content if choice.message and
|
|
117
|
+
choice.message.content else choice.message.function_call.arguments
|
|
118
|
+
if choice.message and
|
|
119
|
+
choice.message.function_call.arguments else "",
|
|
120
|
+
**({"content_filter_results": choice["content_filter_results"]}
|
|
121
|
+
if "content_filter_results" in choice else {})
|
|
122
|
+
}
|
|
123
|
+
for choice in result.choices
|
|
124
|
+
]
|
|
125
|
+
span.set_attribute(
|
|
126
|
+
"llm.responses", json.dumps(responses))
|
|
127
|
+
else:
|
|
128
|
+
responses = []
|
|
129
|
+
span.set_attribute(
|
|
130
|
+
"llm.responses", json.dumps(responses))
|
|
131
|
+
if hasattr(result, 'system_fingerprint') and \
|
|
132
|
+
result.system_fingerprint is not None:
|
|
133
|
+
span.set_attribute(
|
|
134
|
+
"llm.system.fingerprint", result.system_fingerprint)
|
|
135
|
+
# Get the usage
|
|
136
|
+
if hasattr(result, 'usage') and result.usage is not None:
|
|
137
|
+
usage = result.usage
|
|
138
|
+
if usage is not None:
|
|
139
|
+
usage_dict = {
|
|
140
|
+
"prompt_tokens": result.usage.prompt_tokens,
|
|
141
|
+
"completion_tokens": usage.completion_tokens,
|
|
142
|
+
"total_tokens": usage.total_tokens
|
|
143
|
+
}
|
|
144
|
+
span.set_attribute(
|
|
145
|
+
"llm.token.counts", json.dumps(usage_dict))
|
|
146
|
+
span.set_status(StatusCode.OK)
|
|
147
|
+
span.end()
|
|
148
|
+
return result
|
|
149
|
+
else:
|
|
150
|
+
prompt_tokens = calculate_prompt_tokens(json.dumps(
|
|
151
|
+
kwargs.get('messages', {})[0]), kwargs.get('model'))
|
|
152
|
+
return handle_streaming_response(result, span, prompt_tokens,
|
|
153
|
+
function_call=kwargs.get(
|
|
154
|
+
'functions')
|
|
155
|
+
is not None)
|
|
156
|
+
except Exception as e:
|
|
157
|
+
# Record the exception in the span
|
|
158
|
+
span.record_exception(e)
|
|
159
|
+
# Set the span status to indicate an error
|
|
160
|
+
span.set_status(Status(StatusCode.ERROR, str(e)))
|
|
161
|
+
# Reraise the exception to ensure it's not swallowed
|
|
162
|
+
span.end()
|
|
163
|
+
raise
|
|
164
|
+
|
|
165
|
+
def handle_streaming_response(result, span, prompt_tokens, function_call=False):
|
|
166
|
+
"""Process and yield streaming response chunks."""
|
|
167
|
+
result_content = []
|
|
168
|
+
span.add_event(Event.STREAM_START.value)
|
|
169
|
+
completion_tokens = 0
|
|
170
|
+
try:
|
|
171
|
+
for chunk in result:
|
|
172
|
+
if hasattr(chunk, 'choices') and chunk.choices is not None:
|
|
173
|
+
token_counts = [
|
|
174
|
+
estimate_tokens(choice.delta.content) if choice.delta
|
|
175
|
+
and choice.delta.content
|
|
176
|
+
else estimate_tokens(choice.delta.function_call.arguments)
|
|
177
|
+
if choice.delta.function_call and
|
|
178
|
+
choice.delta.function_call.arguments else 0
|
|
179
|
+
for choice in chunk.choices
|
|
180
|
+
]
|
|
181
|
+
completion_tokens += sum(token_counts)
|
|
182
|
+
content = [
|
|
183
|
+
choice.delta.content if choice.delta and choice.delta.content
|
|
184
|
+
else choice.delta.function_call.arguments if choice.delta.function_call and
|
|
185
|
+
choice.delta.function_call.arguments else ""
|
|
186
|
+
for choice in chunk.choices
|
|
187
|
+
]
|
|
188
|
+
else:
|
|
189
|
+
content = []
|
|
190
|
+
span.add_event(Event.STREAM_OUTPUT.value, {
|
|
191
|
+
"response": "".join(content)
|
|
192
|
+
})
|
|
193
|
+
result_content.append(
|
|
194
|
+
content[0] if len(content) > 0 else "")
|
|
195
|
+
yield chunk
|
|
196
|
+
finally:
|
|
197
|
+
|
|
198
|
+
# Finalize span after processing all chunks
|
|
199
|
+
span.add_event(Event.STREAM_END.value)
|
|
200
|
+
span.set_attribute("llm.token.counts", json.dumps({
|
|
201
|
+
"prompt_tokens": prompt_tokens,
|
|
202
|
+
"completion_tokens": completion_tokens,
|
|
203
|
+
"total_tokens": prompt_tokens + completion_tokens
|
|
204
|
+
}))
|
|
205
|
+
if function_call is False:
|
|
206
|
+
span.set_attribute("llm.responses", json.dumps(
|
|
207
|
+
{"message": {"role": "assistant", "content": "".join(result_content)}}))
|
|
208
|
+
else:
|
|
209
|
+
span.set_attribute("llm.responses", json.dumps(
|
|
210
|
+
{"message": {"role": "assistant", "function_call": "".join(result_content)}}))
|
|
211
|
+
span.set_status(StatusCode.OK)
|
|
212
|
+
span.end()
|
|
213
|
+
|
|
214
|
+
# return the wrapped method
|
|
215
|
+
return traced_method
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def embeddings_create(original_method, version, tracer):
|
|
219
|
+
"""
|
|
220
|
+
Wrap the `create` method of the `Embeddings` class to trace it.
|
|
221
|
+
"""
|
|
222
|
+
def traced_method(wrapped, instance, args, kwargs):
|
|
223
|
+
base_url = str(instance._client._base_url) if hasattr(
|
|
224
|
+
instance, '_client') and hasattr(instance._client, '_base_url') else ""
|
|
225
|
+
|
|
226
|
+
service_provider = SERVICE_PROVIDERS['OPENAI']
|
|
227
|
+
span_attributes = {
|
|
228
|
+
"langtrace.service.name": service_provider,
|
|
229
|
+
"langtrace.service.type": "llm",
|
|
230
|
+
"langtrace.service.version": version,
|
|
231
|
+
"langtrace.version": "1.0.0",
|
|
232
|
+
"url.full": base_url,
|
|
233
|
+
"llm.api": APIS["EMBEDDINGS_CREATE"]["ENDPOINT"],
|
|
234
|
+
"llm.model": kwargs.get('model'),
|
|
235
|
+
"llm.prompts": "",
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
attributes = LLMSpanAttributes(**span_attributes)
|
|
239
|
+
kwargs.get('encoding_format')
|
|
240
|
+
|
|
241
|
+
if kwargs.get('encoding_format') is not None:
|
|
242
|
+
attributes.llm_encoding_format = kwargs.get('encoding_format')
|
|
243
|
+
if kwargs.get('dimensions') is not None:
|
|
244
|
+
attributes["llm.dimensions"] = kwargs.get('dimensions')
|
|
245
|
+
if kwargs.get('user') is not None:
|
|
246
|
+
attributes["llm.user"] = kwargs.get('user')
|
|
247
|
+
|
|
248
|
+
with tracer.start_as_current_span(APIS["EMBEDDINGS_CREATE"]["METHOD"],
|
|
249
|
+
kind=SpanKind.CLIENT) as span:
|
|
250
|
+
for field, value in attributes.model_dump(by_alias=True).items():
|
|
251
|
+
if value is not None:
|
|
252
|
+
span.set_attribute(field, value)
|
|
253
|
+
try:
|
|
254
|
+
# Attempt to call the original method
|
|
255
|
+
result = original_method(*args, **kwargs)
|
|
256
|
+
span.set_status(StatusCode.OK)
|
|
257
|
+
return result
|
|
258
|
+
except Exception as e:
|
|
259
|
+
# Record the exception in the span
|
|
260
|
+
span.record_exception(e)
|
|
261
|
+
|
|
262
|
+
# Set the span status to indicate an error
|
|
263
|
+
span.set_status(Status(StatusCode.ERROR, str(e)))
|
|
264
|
+
|
|
265
|
+
# Reraise the exception to ensure it's not swallowed
|
|
266
|
+
raise
|
|
267
|
+
|
|
268
|
+
return traced_method
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module contains functions to estimate the number of tokens in a prompt and
|
|
3
|
+
to calculate the price of a model based on its usage.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from tiktoken import get_encoding
|
|
7
|
+
|
|
8
|
+
from instrumentation.openai.constants import (OPENAI_COST_TABLE,
|
|
9
|
+
TIKTOKEN_MODEL_MAPPING)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def estimate_tokens(prompt):
|
|
13
|
+
"""
|
|
14
|
+
Estimate the number of tokens in a prompt."""
|
|
15
|
+
if prompt and len(prompt) > 0:
|
|
16
|
+
# Simplified token estimation: count the words.
|
|
17
|
+
return len([word for word in prompt.split() if word])
|
|
18
|
+
return 0
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def estimate_tokens_using_tiktoken(prompt, model):
|
|
22
|
+
"""
|
|
23
|
+
Estimate the number of tokens in a prompt using tiktoken."""
|
|
24
|
+
encoding = get_encoding(model)
|
|
25
|
+
tokens = encoding.encode(prompt)
|
|
26
|
+
return len(tokens)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def calculate_prompt_tokens(prompt_content, model):
|
|
30
|
+
"""
|
|
31
|
+
Calculate the number of tokens in a prompt. If the model is supported by tiktoken, use it for the estimation."""
|
|
32
|
+
try:
|
|
33
|
+
tiktoken_model = TIKTOKEN_MODEL_MAPPING[model]
|
|
34
|
+
return estimate_tokens_using_tiktoken(prompt_content, tiktoken_model)
|
|
35
|
+
except Exception:
|
|
36
|
+
return estimate_tokens(prompt_content) # Fallback method
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def calculate_price_from_usage(model, usage):
|
|
40
|
+
"""
|
|
41
|
+
Calculate the price of a model based on its usage."""
|
|
42
|
+
cost_table = OPENAI_COST_TABLE.get(model)
|
|
43
|
+
if cost_table:
|
|
44
|
+
return (
|
|
45
|
+
(cost_table['input'] * usage['prompt_tokens'] +
|
|
46
|
+
cost_table['output'] * usage['completion_tokens']) / 1000
|
|
47
|
+
)
|
|
48
|
+
return 0
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Pinecone instrumentation
|
|
3
|
+
"""
|
|
4
|
+
|
|
1
5
|
import importlib.metadata
|
|
2
6
|
from typing import Collection
|
|
3
7
|
|
|
@@ -7,11 +11,13 @@ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
|
|
|
7
11
|
from opentelemetry.trace import get_tracer
|
|
8
12
|
from wrapt import wrap_function_wrapper
|
|
9
13
|
|
|
10
|
-
from instrumentation.pinecone.
|
|
14
|
+
from instrumentation.pinecone.apis import APIS
|
|
11
15
|
from instrumentation.pinecone.patch import generic_patch
|
|
12
16
|
|
|
13
17
|
|
|
14
18
|
class PineconeInstrumentation(BaseInstrumentor):
|
|
19
|
+
"""
|
|
20
|
+
The PineconeInstrumentation class represents the Pinecone instrumentation"""
|
|
15
21
|
|
|
16
22
|
def instrumentation_dependencies(self) -> Collection[str]:
|
|
17
23
|
return ["pinecone-client >= 3.1.0"]
|
|
@@ -40,4 +46,4 @@ class PineconeInstrumentation(BaseInstrumentor):
|
|
|
40
46
|
)
|
|
41
47
|
|
|
42
48
|
def _uninstrument(self, **kwargs):
|
|
43
|
-
|
|
49
|
+
pass
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/pinecone/patch.py
RENAMED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"""
|
|
2
|
+
This module contains the patching logic for the Pinecone client."""
|
|
3
3
|
from langtrace.trace_attributes import DatabaseSpanAttributes
|
|
4
|
-
from opentelemetry.trace import SpanKind
|
|
4
|
+
from opentelemetry.trace import SpanKind
|
|
5
5
|
from opentelemetry.trace.status import Status, StatusCode
|
|
6
6
|
|
|
7
7
|
from instrumentation.constants import SERVICE_PROVIDERS
|
|
8
|
-
from instrumentation.pinecone.
|
|
8
|
+
from instrumentation.pinecone.apis import APIS
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
def generic_patch(original_method, method, version, tracer):
|
|
12
|
+
"""
|
|
13
|
+
A generic patch method that wraps a function with a span"""
|
|
12
14
|
def traced_method(wrapped, instance, args, kwargs):
|
|
13
15
|
api = APIS[method]
|
|
14
16
|
service_provider = SERVICE_PROVIDERS['PINECONE']
|
|
@@ -17,14 +17,12 @@ src/examples/pinecone_example/__init__.py
|
|
|
17
17
|
src/examples/pinecone_example/basic.py
|
|
18
18
|
src/instrumentation/__init__.py
|
|
19
19
|
src/instrumentation/constants.py
|
|
20
|
-
src/instrumentation/setup.py
|
|
21
|
-
src/instrumentation/utils.py
|
|
22
20
|
src/instrumentation/with_root_span.py
|
|
23
21
|
src/instrumentation/chroma/__init__.py
|
|
22
|
+
src/instrumentation/chroma/apis.py
|
|
24
23
|
src/instrumentation/chroma/instrumentation.py
|
|
25
24
|
src/instrumentation/chroma/patch.py
|
|
26
25
|
src/instrumentation/chroma/lib/__init__.py
|
|
27
|
-
src/instrumentation/chroma/lib/apis.py
|
|
28
26
|
src/instrumentation/langchain/__init__.py
|
|
29
27
|
src/instrumentation/langchain/instrumentation.py
|
|
30
28
|
src/instrumentation/langchain/patch.py
|
|
@@ -38,16 +36,17 @@ src/instrumentation/llamaindex/__init__.py
|
|
|
38
36
|
src/instrumentation/llamaindex/instrumentation.py
|
|
39
37
|
src/instrumentation/llamaindex/patch.py
|
|
40
38
|
src/instrumentation/openai/__init__.py
|
|
39
|
+
src/instrumentation/openai/apis.py
|
|
40
|
+
src/instrumentation/openai/constants.py
|
|
41
41
|
src/instrumentation/openai/instrumentation.py
|
|
42
42
|
src/instrumentation/openai/patch.py
|
|
43
|
+
src/instrumentation/openai/token_estimation.py
|
|
43
44
|
src/instrumentation/openai/lib/__init__.py
|
|
44
|
-
src/instrumentation/openai/lib/apis.py
|
|
45
|
-
src/instrumentation/openai/lib/constants.py
|
|
46
45
|
src/instrumentation/pinecone/__init__.py
|
|
46
|
+
src/instrumentation/pinecone/apis.py
|
|
47
47
|
src/instrumentation/pinecone/instrumentation.py
|
|
48
48
|
src/instrumentation/pinecone/patch.py
|
|
49
49
|
src/instrumentation/pinecone/lib/__init__.py
|
|
50
|
-
src/instrumentation/pinecone/lib/apis.py
|
|
51
50
|
src/langtrace_python_sdk.egg-info/PKG-INFO
|
|
52
51
|
src/langtrace_python_sdk.egg-info/SOURCES.txt
|
|
53
52
|
src/langtrace_python_sdk.egg-info/dependency_links.txt
|
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
import json
|
|
2
|
-
|
|
3
|
-
from langtrace.trace_attributes import Event, LLMSpanAttributes
|
|
4
|
-
from opentelemetry.trace import SpanKind, StatusCode
|
|
5
|
-
from opentelemetry.trace.status import Status, StatusCode
|
|
6
|
-
|
|
7
|
-
from instrumentation.constants import SERVICE_PROVIDERS
|
|
8
|
-
from instrumentation.openai.lib.apis import APIS
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def images_generate(original_method, version, tracer):
|
|
12
|
-
def traced_method(wrapped, instance, args, kwargs):
|
|
13
|
-
base_url = str(instance._client._base_url) if hasattr(
|
|
14
|
-
instance, '_client') and hasattr(instance._client, '_base_url') else ""
|
|
15
|
-
service_provider = SERVICE_PROVIDERS['OPENAI']
|
|
16
|
-
span_attributes = {
|
|
17
|
-
"langtrace.service.name": service_provider,
|
|
18
|
-
"langtrace.service.type": "llm",
|
|
19
|
-
"langtrace.service.version": version,
|
|
20
|
-
"langtrace.version": "1.0.0",
|
|
21
|
-
"url.full": base_url,
|
|
22
|
-
"llm.api": APIS["IMAGES_GENERATION"]["ENDPOINT"],
|
|
23
|
-
"llm.model": kwargs.get('model'),
|
|
24
|
-
"llm.stream": kwargs.get('stream'),
|
|
25
|
-
"llm.prompts": json.dumps([kwargs.get('prompt', [])])
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
attributes = LLMSpanAttributes(**span_attributes)
|
|
29
|
-
|
|
30
|
-
with tracer.start_as_current_span(APIS["IMAGES_GENERATION"]["METHOD"], kind=SpanKind.CLIENT) as span:
|
|
31
|
-
for field, value in attributes.model_dump(by_alias=True).items():
|
|
32
|
-
if value is not None:
|
|
33
|
-
span.set_attribute(field, value)
|
|
34
|
-
try:
|
|
35
|
-
# Attempt to call the original method
|
|
36
|
-
result = original_method(*args, **kwargs)
|
|
37
|
-
if kwargs.get('stream') is False or kwargs.get('stream') is None:
|
|
38
|
-
data = result.data[0] if hasattr(
|
|
39
|
-
result, 'data') and len(result.data) > 0 else {}
|
|
40
|
-
response = [{
|
|
41
|
-
"url": data.url if hasattr(data, 'url') else "",
|
|
42
|
-
"revised_prompt": data.revised_prompt if hasattr(data, 'revised_prompt') else "",
|
|
43
|
-
}]
|
|
44
|
-
span.set_attribute(
|
|
45
|
-
"llm.responses", json.dumps(response))
|
|
46
|
-
|
|
47
|
-
span.set_status(StatusCode.OK)
|
|
48
|
-
return result
|
|
49
|
-
except Exception as e:
|
|
50
|
-
# Record the exception in the span
|
|
51
|
-
span.record_exception(e)
|
|
52
|
-
|
|
53
|
-
# Set the span status to indicate an error
|
|
54
|
-
span.set_status(Status(StatusCode.ERROR, str(e)))
|
|
55
|
-
|
|
56
|
-
# Reraise the exception to ensure it's not swallowed
|
|
57
|
-
raise
|
|
58
|
-
|
|
59
|
-
return traced_method
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
def chat_completions_create(original_method, version, tracer):
|
|
63
|
-
def traced_method(wrapped, instance, args, kwargs):
|
|
64
|
-
base_url = str(instance._client._base_url) if hasattr(
|
|
65
|
-
instance, '_client') and hasattr(instance._client, '_base_url') else ""
|
|
66
|
-
service_provider = SERVICE_PROVIDERS['OPENAI']
|
|
67
|
-
span_attributes = {
|
|
68
|
-
"langtrace.service.name": service_provider,
|
|
69
|
-
"langtrace.service.type": "llm",
|
|
70
|
-
"langtrace.service.version": version,
|
|
71
|
-
"langtrace.version": "1.0.0",
|
|
72
|
-
"url.full": base_url,
|
|
73
|
-
"llm.api": APIS["CHAT_COMPLETION"]["ENDPOINT"],
|
|
74
|
-
"llm.model": kwargs.get('model'),
|
|
75
|
-
"llm.prompts": json.dumps(kwargs.get('messages', [])),
|
|
76
|
-
"llm.stream": kwargs.get('stream'),
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
attributes = LLMSpanAttributes(**span_attributes)
|
|
80
|
-
|
|
81
|
-
if kwargs.get('temperature') is not None:
|
|
82
|
-
attributes.llm_temperature = kwargs.get('temperature')
|
|
83
|
-
if kwargs.get('top_p') is not None:
|
|
84
|
-
attributes.llm_top_p = kwargs.get('top_p')
|
|
85
|
-
if kwargs.get('user') is not None:
|
|
86
|
-
attributes.llm_user = kwargs.get('user')
|
|
87
|
-
|
|
88
|
-
with tracer.start_as_current_span(APIS["CHAT_COMPLETION"]["METHOD"], kind=SpanKind.CLIENT) as span:
|
|
89
|
-
for field, value in attributes.model_dump(by_alias=True).items():
|
|
90
|
-
if value is not None:
|
|
91
|
-
span.set_attribute(field, value)
|
|
92
|
-
try:
|
|
93
|
-
# Attempt to call the original method
|
|
94
|
-
result = original_method(*args, **kwargs)
|
|
95
|
-
if kwargs.get('stream') is False:
|
|
96
|
-
if hasattr(result, 'choices') and result.choices is not None:
|
|
97
|
-
responses = [
|
|
98
|
-
{
|
|
99
|
-
"message": choice.message.content if choice.message and choice.message.content else "",
|
|
100
|
-
**({"content_filter_results": choice["content_filter_results"]} if "content_filter_results" in choice else {})
|
|
101
|
-
}
|
|
102
|
-
for choice in result.choices
|
|
103
|
-
]
|
|
104
|
-
else:
|
|
105
|
-
responses = []
|
|
106
|
-
span.set_attribute("llm.responses", json.dumps(responses))
|
|
107
|
-
|
|
108
|
-
if hasattr(result, 'system_fingerprint') and result.system_fingerprint is not None:
|
|
109
|
-
span.set_attribute(
|
|
110
|
-
"llm.system.fingerprint", result.system_fingerprint)
|
|
111
|
-
|
|
112
|
-
# Get the usage
|
|
113
|
-
if hasattr(result, 'usage') and result.usage is not None:
|
|
114
|
-
usage = result.usage
|
|
115
|
-
if usage is not None:
|
|
116
|
-
usage_dict = {
|
|
117
|
-
"prompt_tokens": result.usage.prompt_tokens,
|
|
118
|
-
"completion_tokens": usage.completion_tokens,
|
|
119
|
-
"total_tokens": usage.total_tokens
|
|
120
|
-
}
|
|
121
|
-
span.set_attribute(
|
|
122
|
-
"llm.token.counts", json.dumps(usage_dict))
|
|
123
|
-
|
|
124
|
-
span.set_status(StatusCode.OK)
|
|
125
|
-
return result
|
|
126
|
-
else:
|
|
127
|
-
result_content = []
|
|
128
|
-
span.add_event(Event.STREAM_START.value)
|
|
129
|
-
|
|
130
|
-
for chunk in result:
|
|
131
|
-
# Assuming `chunk` has a structure similar to what OpenAI might return,
|
|
132
|
-
# adjust the access accordingly based on actual response structure.
|
|
133
|
-
if hasattr(chunk, 'choices') and chunk.choices is not None:
|
|
134
|
-
content = [
|
|
135
|
-
choice.delta.content if choice.delta and choice.delta.content else ""
|
|
136
|
-
for choice in chunk.choices
|
|
137
|
-
]
|
|
138
|
-
else:
|
|
139
|
-
content = []
|
|
140
|
-
span.add_event(Event.STREAM_OUTPUT.value, {
|
|
141
|
-
"response": "".join(content)
|
|
142
|
-
})
|
|
143
|
-
result_content.append(
|
|
144
|
-
content[0] if len(content) > 0 else "")
|
|
145
|
-
span.add_event(Event.STREAM_END.value)
|
|
146
|
-
span.set_attribute("llm.responses", json.dumps(
|
|
147
|
-
{"message": {"role": "assistant", "content": "".join(result_content)}}))
|
|
148
|
-
|
|
149
|
-
except Exception as e:
|
|
150
|
-
# Record the exception in the span
|
|
151
|
-
span.record_exception(e)
|
|
152
|
-
|
|
153
|
-
# Set the span status to indicate an error
|
|
154
|
-
span.set_status(Status(StatusCode.ERROR, str(e)))
|
|
155
|
-
|
|
156
|
-
# Reraise the exception to ensure it's not swallowed
|
|
157
|
-
raise
|
|
158
|
-
|
|
159
|
-
# return the wrapped method
|
|
160
|
-
return traced_method
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
def embeddings_create(original_method, version, tracer):
|
|
164
|
-
def traced_method(wrapped, instance, args, kwargs):
|
|
165
|
-
base_url = str(instance._client._base_url) if hasattr(
|
|
166
|
-
instance, '_client') and hasattr(instance._client, '_base_url') else ""
|
|
167
|
-
|
|
168
|
-
service_provider = SERVICE_PROVIDERS['OPENAI']
|
|
169
|
-
span_attributes = {
|
|
170
|
-
"langtrace.service.name": service_provider,
|
|
171
|
-
"langtrace.service.type": "llm",
|
|
172
|
-
"langtrace.service.version": version,
|
|
173
|
-
"langtrace.version": "1.0.0",
|
|
174
|
-
"url.full": base_url,
|
|
175
|
-
"llm.api": APIS["EMBEDDINGS_CREATE"]["ENDPOINT"],
|
|
176
|
-
"llm.model": kwargs.get('model'),
|
|
177
|
-
"llm.prompts": "",
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
attributes = LLMSpanAttributes(**span_attributes)
|
|
181
|
-
kwargs.get('encoding_format')
|
|
182
|
-
|
|
183
|
-
if kwargs.get('encoding_format') is not None:
|
|
184
|
-
attributes.llm_encoding_format = kwargs.get('encoding_format')
|
|
185
|
-
if kwargs.get('dimensions') is not None:
|
|
186
|
-
attributes["llm.dimensions"] = kwargs.get('dimensions')
|
|
187
|
-
if kwargs.get('user') is not None:
|
|
188
|
-
attributes["llm.user"] = kwargs.get('user')
|
|
189
|
-
|
|
190
|
-
with tracer.start_as_current_span(APIS["EMBEDDINGS_CREATE"]["METHOD"], kind=SpanKind.CLIENT) as span:
|
|
191
|
-
for field, value in attributes.model_dump(by_alias=True).items():
|
|
192
|
-
if value is not None:
|
|
193
|
-
span.set_attribute(field, value)
|
|
194
|
-
try:
|
|
195
|
-
# Attempt to call the original method
|
|
196
|
-
result = original_method(*args, **kwargs)
|
|
197
|
-
span.set_status(StatusCode.OK)
|
|
198
|
-
return result
|
|
199
|
-
except Exception as e:
|
|
200
|
-
# Record the exception in the span
|
|
201
|
-
span.record_exception(e)
|
|
202
|
-
|
|
203
|
-
# Set the span status to indicate an error
|
|
204
|
-
span.set_status(Status(StatusCode.ERROR, str(e)))
|
|
205
|
-
|
|
206
|
-
# Reraise the exception to ensure it's not swallowed
|
|
207
|
-
raise
|
|
208
|
-
|
|
209
|
-
return traced_method
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
from opentelemetry import trace
|
|
3
|
-
from opentelemetry.sdk.trace import TracerProvider
|
|
4
|
-
from opentelemetry.sdk.trace.export import (ConsoleSpanExporter,
|
|
5
|
-
SimpleSpanProcessor)
|
|
6
|
-
|
|
7
|
-
from instrumentation.chroma.instrumentation import ChromaInstrumentation
|
|
8
|
-
from instrumentation.langchain.instrumentation import LangchainInstrumentation
|
|
9
|
-
from instrumentation.langchain_community.instrumentation import \
|
|
10
|
-
LangchainCommunityInstrumentation
|
|
11
|
-
from instrumentation.langchain_core.instrumentation import \
|
|
12
|
-
LangchainCoreInstrumentation
|
|
13
|
-
from instrumentation.llamaindex.instrumentation import \
|
|
14
|
-
LlamaindexInstrumentation
|
|
15
|
-
from instrumentation.openai.instrumentation import OpenAIInstrumentation
|
|
16
|
-
from instrumentation.pinecone.instrumentation import PineconeInstrumentation
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def setup_instrumentation():
|
|
20
|
-
|
|
21
|
-
# Set up OpenTelemetry tracing
|
|
22
|
-
tracer_provider = TracerProvider()
|
|
23
|
-
|
|
24
|
-
# Use the ConsoleSpanExporter to print traces to the console
|
|
25
|
-
console_exporter = ConsoleSpanExporter()
|
|
26
|
-
tracer_provider.add_span_processor(SimpleSpanProcessor(console_exporter))
|
|
27
|
-
|
|
28
|
-
# Initialize tracer
|
|
29
|
-
trace.set_tracer_provider(tracer_provider)
|
|
30
|
-
|
|
31
|
-
# Initialize and enable your custom OpenAI instrumentation
|
|
32
|
-
# Create an instance of OpenAIInstrumentation
|
|
33
|
-
openai_instrumentation = OpenAIInstrumentation()
|
|
34
|
-
pinecone_instrumentation = PineconeInstrumentation()
|
|
35
|
-
llamaindex_instrumentation = LlamaindexInstrumentation()
|
|
36
|
-
chroma_instrumentation = ChromaInstrumentation()
|
|
37
|
-
langchain_instrumentation = LangchainInstrumentation()
|
|
38
|
-
langchain_core_instrumentation = LangchainCoreInstrumentation()
|
|
39
|
-
langchain_community_instrumentation = LangchainCommunityInstrumentation()
|
|
40
|
-
|
|
41
|
-
# Call the instrument method with some arguments
|
|
42
|
-
openai_instrumentation.instrument()
|
|
43
|
-
pinecone_instrumentation.instrument()
|
|
44
|
-
llamaindex_instrumentation.instrument()
|
|
45
|
-
chroma_instrumentation.instrument()
|
|
46
|
-
langchain_instrumentation.instrument()
|
|
47
|
-
langchain_core_instrumentation.instrument()
|
|
48
|
-
langchain_community_instrumentation.instrument()
|
|
49
|
-
|
|
50
|
-
print("setup complete")
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
from tiktoken import TiktokenEncoding, get_encoding
|
|
2
|
-
from .constants import TIKTOKEN_MODEL_MAPPING, OPENAI_COST_TABLE
|
|
3
|
-
|
|
4
|
-
def estimate_tokens(prompt: str) -> int:
|
|
5
|
-
if prompt and len(prompt) > 0:
|
|
6
|
-
# Simplified token estimation: count the words.
|
|
7
|
-
return len(prompt.split())
|
|
8
|
-
return 0
|
|
9
|
-
|
|
10
|
-
def estimate_tokens_using_tiktoken(prompt: str, model: TiktokenEncoding) -> int:
|
|
11
|
-
encoding = get_encoding(model)
|
|
12
|
-
tokens = encoding.encode(prompt)
|
|
13
|
-
return len(tokens)
|
|
14
|
-
|
|
15
|
-
def calculate_prompt_tokens(prompt_content: str, model: str) -> int:
|
|
16
|
-
try:
|
|
17
|
-
tiktoken_model = TIKTOKEN_MODEL_MAPPING[model]
|
|
18
|
-
return estimate_tokens_using_tiktoken(prompt_content, tiktoken_model)
|
|
19
|
-
except KeyError:
|
|
20
|
-
return estimate_tokens(prompt_content) # Fallback method
|
|
21
|
-
|
|
22
|
-
def calculate_price_from_usage(model: str, usage: dict) -> float:
|
|
23
|
-
cost_table = OPENAI_COST_TABLE.get(model)
|
|
24
|
-
if cost_table:
|
|
25
|
-
return ((cost_table['input'] * usage['prompt_tokens'] +
|
|
26
|
-
cost_table['output'] * usage['completion_tokens']) / 1000)
|
|
27
|
-
return 0
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/chroma_example/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/examples/pinecone_example/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/chroma/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/langchain/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/langchain/patch.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/openai/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/pinecone/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{langtrace-python-sdk-1.0.9 → langtrace-python-sdk-1.0.10}/src/instrumentation/with_root_span.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|