Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ classifiers=[
"Operating System :: OS Independent",
]
dependencies = [
'trace-attributes>=6.0.3,<7.0.0',
'trace-attributes==7.0.0',
'opentelemetry-api>=1.25.0',
'opentelemetry-sdk>=1.25.0',
'opentelemetry-instrumentation>=0.46b0',
Expand Down
34 changes: 0 additions & 34 deletions src/examples/ollama_example/basic_example_2.py

This file was deleted.

59 changes: 59 additions & 0 deletions src/examples/otlp_example/otlp_with_langtrace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Instructions
# 1. Run the OpenTelemetry Collector with the OTLP receiver enabled
# Create otel-config.yaml with the following content:
# receivers:
# otlp:
# protocols:
# grpc:
# endpoint: "0.0.0.0:4317"
# http:
# endpoint: "0.0.0.0:4318"

# exporters:
# logging:
# loglevel: debug

# service:
# pipelines:
# traces:
# receivers: [otlp]
# exporters: [logging]
# docker pull otel/opentelemetry-collector:latest
# docker run --rm -p 4317:4317 -p 4318:4318 -v $(pwd)/otel-config.yaml:/otel-config.yaml otel/opentelemetry-collector --config otel-config.yaml
# 2. Run the following code

from langtrace_python_sdk import langtrace
from openai import OpenAI
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter


# Configure the OTLP exporter to use the correct endpoint and API key
otlp_endpoint = "http://localhost:4318/v1/traces"
otlp_exporter = OTLPSpanExporter(
endpoint=otlp_endpoint,
headers=(("Content-Type", "application/json"),))
langtrace.init(custom_remote_exporter=otlp_exporter, batch=False)


def chat_with_openai():
client = OpenAI()
messages = [
{
"role": "user",
"content": "Hello, I'm a human.",
},
]
chat_completion = client.chat.completions.create(
messages=messages,
stream=False,
model="gpt-3.5-turbo",
)
print(chat_completion.choices[0].message.content)


def main():
chat_with_openai()


if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions src/langtrace_python_sdk/instrumentation/cohere/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def traced_method(wrapped, instance, args, kwargs):

span_attributes = {
**get_langtrace_attributes(version, service_provider),
**get_llm_request_attributes(kwargs),
**get_llm_request_attributes(kwargs, operation_name="rerank"),
**get_llm_url(instance),
SpanAttributes.LLM_REQUEST_MODEL: kwargs.get("model") or "command-r-plus",
SpanAttributes.LLM_URL: APIS["RERANK"]["URL"],
Expand Down Expand Up @@ -121,7 +121,7 @@ def traced_method(wrapped, instance, args, kwargs):

span_attributes = {
**get_langtrace_attributes(version, service_provider),
**get_llm_request_attributes(kwargs),
**get_llm_request_attributes(kwargs, operation_name="embed"),
**get_llm_url(instance),
SpanAttributes.LLM_URL: APIS["EMBED"]["URL"],
SpanAttributes.LLM_PATH: APIS["EMBED"]["ENDPOINT"],
Expand Down
10 changes: 5 additions & 5 deletions src/langtrace_python_sdk/instrumentation/openai/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def traced_method(wrapped, instance, args, kwargs):
service_provider = SERVICE_PROVIDERS["OPENAI"]
span_attributes = {
**get_langtrace_attributes(version, service_provider, vendor_type="llm"),
**get_llm_request_attributes(kwargs),
**get_llm_request_attributes(kwargs, operation_name="images_generate"),
**get_llm_url(instance),
SpanAttributes.LLM_PATH: APIS["IMAGES_GENERATION"]["ENDPOINT"],
**get_extra_attributes(),
Expand Down Expand Up @@ -118,7 +118,7 @@ async def traced_method(wrapped, instance, args, kwargs):

span_attributes = {
**get_langtrace_attributes(version, service_provider, vendor_type="llm"),
**get_llm_request_attributes(kwargs),
**get_llm_request_attributes(kwargs, operation_name="images_generate"),
**get_llm_url(instance),
SpanAttributes.LLM_PATH: APIS["IMAGES_GENERATION"]["ENDPOINT"],
**get_extra_attributes(),
Expand Down Expand Up @@ -181,7 +181,7 @@ def traced_method(wrapped, instance, args, kwargs):

span_attributes = {
**get_langtrace_attributes(version, service_provider, vendor_type="llm"),
**get_llm_request_attributes(kwargs),
**get_llm_request_attributes(kwargs, operation_name="images_edit"),
**get_llm_url(instance),
SpanAttributes.LLM_PATH: APIS["IMAGES_EDIT"]["ENDPOINT"],
SpanAttributes.LLM_RESPONSE_FORMAT: kwargs.get("response_format"),
Expand Down Expand Up @@ -432,7 +432,7 @@ def traced_method(wrapped, instance, args, kwargs):

span_attributes = {
**get_langtrace_attributes(version, service_provider, vendor_type="llm"),
**get_llm_request_attributes(kwargs),
**get_llm_request_attributes(kwargs, operation_name="embed"),
**get_llm_url(instance),
SpanAttributes.LLM_PATH: APIS["EMBEDDINGS_CREATE"]["ENDPOINT"],
SpanAttributes.LLM_REQUEST_DIMENSIONS: kwargs.get("dimensions"),
Expand Down Expand Up @@ -490,7 +490,7 @@ async def traced_method(wrapped, instance, args, kwargs):

span_attributes = {
**get_langtrace_attributes(version, service_provider, vendor_type="llm"),
**get_llm_request_attributes(kwargs),
**get_llm_request_attributes(kwargs, operation_name="embed"),
SpanAttributes.LLM_PATH: APIS["EMBEDDINGS_CREATE"]["ENDPOINT"],
SpanAttributes.LLM_REQUEST_DIMENSIONS: kwargs.get("dimensions"),
**get_extra_attributes(),
Expand Down
3 changes: 2 additions & 1 deletion src/langtrace_python_sdk/utils/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_langtrace_attributes(version, service_provider, vendor_type="llm"):
}


def get_llm_request_attributes(kwargs, prompts=None, model=None):
def get_llm_request_attributes(kwargs, prompts=None, model=None, operation_name="chat"):

user = kwargs.get("user", None)
if prompts is None:
Expand All @@ -111,6 +111,7 @@ def get_llm_request_attributes(kwargs, prompts=None, model=None):
top_p = kwargs.get("p", None) or kwargs.get("top_p", None)
tools = kwargs.get("tools", None)
return {
SpanAttributes.LLM_OPERATION_NAME: operation_name,
SpanAttributes.LLM_REQUEST_MODEL: model or kwargs.get("model"),
SpanAttributes.LLM_IS_STREAMING: kwargs.get("stream"),
SpanAttributes.LLM_REQUEST_TEMPERATURE: kwargs.get("temperature"),
Expand Down
2 changes: 1 addition & 1 deletion src/langtrace_python_sdk/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.2.6"
__version__ = "2.2.7"