Skip to content

Commit 9981f89

Browse files
committed
mongodb kickstart + openai embedding enrichment
1 parent b2e7773 commit 9981f89

File tree

7 files changed

+139
-0
lines changed

7 files changed

+139
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import pymongo
2+
import os
3+
from dotenv import load_dotenv
4+
from openai import OpenAI
5+
from langtrace_python_sdk import langtrace
6+
7+
load_dotenv()
8+
langtrace.init()
9+
MODEL = "text-embedding-ada-002"
10+
openai_client = OpenAI()
11+
client = pymongo.MongoClient(os.environ["MONGO_URI"])
12+
13+
14+
# Define a function to generate embeddings
15+
def get_embedding(text):
16+
"""Generates vector embeddings for the given text."""
17+
embedding = (
18+
openai_client.embeddings.create(input=[text], model=MODEL).data[0].embedding
19+
)
20+
return embedding
21+
22+
23+
def vector_query():
24+
db = client["sample_mflix"]
25+
26+
embedded_movies_collection = db["embedded_movies"]
27+
28+
# define pipeline
29+
pipeline = [
30+
{
31+
"$vectorSearch": {
32+
"index": "vector_index",
33+
"path": "plot_embedding",
34+
"queryVector": get_embedding(
35+
"A movie about a hacker that had a really rough childhood and been trying to convince his father otherwise."
36+
),
37+
# "numCandidates": 150,
38+
"exact": True,
39+
"limit": 10,
40+
}
41+
},
42+
{
43+
"$project": {
44+
"_id": 0,
45+
"plot": 1,
46+
"title": 1,
47+
"score": {"$meta": "vectorSearchScore"},
48+
}
49+
},
50+
]
51+
52+
result = embedded_movies_collection.aggregate(pipeline)
53+
for doc in result:
54+
print(doc)
55+
56+
57+
if __name__ == "__main__":
58+
try:
59+
vector_query()
60+
except Exception as e:
61+
print(e)
62+
finally:
63+
client.close()

src/langtrace_python_sdk/instrumentation/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .mistral import MistralInstrumentation
2121
from .embedchain import EmbedchainInstrumentation
2222
from .litellm import LiteLLMInstrumentation
23+
from .pymongo import PyMongoInstrumentation
2324

2425
__all__ = [
2526
"AnthropicInstrumentation",
@@ -44,4 +45,5 @@
4445
"VertexAIInstrumentation",
4546
"GeminiInstrumentation",
4647
"MistralInstrumentation",
48+
"PyMongoInstrumentation",
4749
]

src/langtrace_python_sdk/instrumentation/openai/patch.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
set_event_completion,
2828
StreamWrapper,
2929
set_span_attributes,
30+
set_usage_attributes,
3031
)
3132
from langtrace_python_sdk.types import NOT_GIVEN
3233

@@ -446,6 +447,14 @@ def traced_method(
446447
span_attributes[SpanAttributes.LLM_REQUEST_EMBEDDING_INPUTS] = json.dumps(
447448
[kwargs.get("input", "")]
448449
)
450+
span_attributes[SpanAttributes.LLM_PROMPTS] = json.dumps(
451+
[
452+
{
453+
"role": "user",
454+
"content": kwargs.get("input"),
455+
}
456+
]
457+
)
449458

450459
attributes = LLMSpanAttributes(**filter_valid_attributes(span_attributes))
451460

@@ -459,6 +468,11 @@ def traced_method(
459468
try:
460469
# Attempt to call the original method
461470
result = wrapped(*args, **kwargs)
471+
usage = getattr(result, "usage", None)
472+
if usage:
473+
set_usage_attributes(
474+
span, {"prompt_tokens": getattr(usage, "prompt_tokens", 0)}
475+
)
462476
span.set_status(StatusCode.OK)
463477
return result
464478
except Exception as err:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .instrumentation import PyMongoInstrumentation
2+
3+
__all__ = [
4+
"PyMongoInstrumentation",
5+
]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
20+
from typing import Collection
21+
from importlib_metadata import version as v
22+
from wrapt import wrap_function_wrapper as _W
23+
from .patch import generic_patch
24+
25+
26+
class PyMongoInstrumentation(BaseInstrumentor):
27+
"""
28+
The PyMongoInstrumentation class represents the PyMongo instrumentation
29+
"""
30+
31+
def instrumentation_dependencies(self) -> Collection[str]:
32+
return ["pymongo >= 4.0.0"]
33+
34+
def _instrument(self, **kwargs):
35+
tracer_provider = kwargs.get("tracer_provider")
36+
tracer = get_tracer(__name__, "", tracer_provider)
37+
version = v("pymongo")
38+
_W(
39+
module="pymongo.collection",
40+
name="Collection.find",
41+
wrapper=generic_patch(version, tracer),
42+
)
43+
44+
def _uninstrument(self, **kwargs):
45+
pass
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def generic_patch(version, tracer):
2+
def traced_method(wrapped, instance, args, kwargs):
3+
print("kwargs", kwargs)
4+
print("args", args)
5+
print("instance", instance)
6+
wrapped(*args, **kwargs)
7+
8+
return traced_method

src/langtrace_python_sdk/langtrace.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
AutogenInstrumentation,
5959
VertexAIInstrumentation,
6060
WeaviateInstrumentation,
61+
PyMongoInstrumentation,
6162
)
6263
from langtrace_python_sdk.types import DisableInstrumentations, InstrumentationMethods
6364
from langtrace_python_sdk.utils import (
@@ -230,6 +231,7 @@ def init(
230231
"google-generativeai": GeminiInstrumentation(),
231232
"mistralai": MistralInstrumentation(),
232233
"autogen": AutogenInstrumentation(),
234+
"pymongo": PyMongoInstrumentation(),
233235
}
234236

235237
init_instrumentations(config.disable_instrumentations, all_instrumentations)

0 commit comments

Comments
 (0)