diff --git a/src/examples/dspy_example/math_problems_cot_parallel.py b/src/examples/dspy_example/math_problems_cot_parallel.py index b70890b5..690683e8 100644 --- a/src/examples/dspy_example/math_problems_cot_parallel.py +++ b/src/examples/dspy_example/math_problems_cot_parallel.py @@ -5,7 +5,7 @@ from concurrent.futures import ThreadPoolExecutor # flake8: noqa -from langtrace_python_sdk import langtrace, with_langtrace_root_span +from langtrace_python_sdk import langtrace, with_langtrace_root_span, inject_additional_attributes langtrace.init() diff --git a/src/examples/inspect_ai_example/basic_eval.py b/src/examples/inspect_ai_example/basic_eval.py index be509111..d2d1fe95 100644 --- a/src/examples/inspect_ai_example/basic_eval.py +++ b/src/examples/inspect_ai_example/basic_eval.py @@ -1,24 +1,32 @@ -# langtrace.init(write_spans_to_console=True) import fsspec from inspect_ai import Task, task -from inspect_ai.dataset import csv_dataset +from inspect_ai.dataset import csv_dataset, Sample from inspect_ai.scorer import model_graded_qa -from inspect_ai.solver import chain_of_thought, generate, self_critique - +from inspect_ai.solver import chain_of_thought, self_critique from langtrace_python_sdk.extensions.langtrace_filesystem import LangTraceFileSystem -# from langtrace_python_sdk import langtrace - # Manually register the filesystem with fsspec # Note: This is only necessary because the filesystem is not registered. fsspec.register_implementation(LangTraceFileSystem.protocol, LangTraceFileSystem) +question = "What is the price?" + + +def hydrate_with_question(record): + # add context to input + record["input"] = f"Context: {record['input']}\n question: {question}" + + return Sample( + input=record["input"], + target=record["target"], + ) + @task -def security_guide(): +def pricing_question(): return Task( - dataset=csv_dataset("langtracefs://clxc2mxu6000lpc7ntsvcjvp9"), + dataset=csv_dataset("langtracefs://clyythmcs0001145cuvi426zi", hydrate_with_question), plan=[chain_of_thought(), self_critique()], scorer=model_graded_qa(), ) diff --git a/src/examples/ollama_example/basic_example_2.py b/src/examples/ollama_example/basic_example_2.py new file mode 100644 index 00000000..e9ecb7df --- /dev/null +++ b/src/examples/ollama_example/basic_example_2.py @@ -0,0 +1,34 @@ +from langtrace_python_sdk import langtrace +from openai import OpenAI +from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter + +service_name = "langtrace-python-ollama" +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_ollama(): + # Use the OpenAI endpoint, not the Ollama API. + base_url = "http://localhost:11434/v1" + client = OpenAI(base_url=base_url, api_key="unused") + messages = [ + { + "role": "user", + "content": "Hello, I'm a human.", + }, + ] + chat_completion = client.chat.completions.create( + model="llama3", messages=messages + ) + print(chat_completion.choices[0].message.content) + + +def main(): + chat_with_ollama() + + +if __name__ == "__main__": + main() diff --git a/src/examples/otlp_example/otlp_basic.py b/src/examples/otlp_example/otlp_basic.py new file mode 100644 index 00000000..48da9129 --- /dev/null +++ b/src/examples/otlp_example/otlp_basic.py @@ -0,0 +1,43 @@ +# 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 opentelemetry import trace +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchSpanProcessor +from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter + +# Set up the tracer provider +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) + +# Set up the OTLP exporter +otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317") + +# Set up a span processor and add it to the tracer provider +span_processor = BatchSpanProcessor(otlp_exporter) +trace.get_tracer_provider().add_span_processor(span_processor) + +# Create a span +with tracer.start_as_current_span("example-span"): + print("Hello, World!") diff --git a/src/langtrace_python_sdk/instrumentation/groq/patch.py b/src/langtrace_python_sdk/instrumentation/groq/patch.py index 82f345e6..9e19e51e 100644 --- a/src/langtrace_python_sdk/instrumentation/groq/patch.py +++ b/src/langtrace_python_sdk/instrumentation/groq/patch.py @@ -104,10 +104,10 @@ def traced_method(wrapped, instance, args, kwargs): # TODO(Karthik): Gotta figure out how to handle streaming with context # with tracer.start_as_current_span(APIS["CHAT_COMPLETION"]["METHOD"], - # kind=SpanKind.CLIENT.value) as span: + # kind=SpanKind.CLIENT) as span: span = tracer.start_span( APIS["CHAT_COMPLETION"]["METHOD"], - kind=SpanKind.CLIENT.value, + kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) for field, value in attributes.model_dump(by_alias=True).items(): @@ -333,9 +333,9 @@ async def traced_method(wrapped, instance, args, kwargs): # TODO(Karthik): Gotta figure out how to handle streaming with context # with tracer.start_as_current_span(APIS["CHAT_COMPLETION"]["METHOD"], - # kind=SpanKind.CLIENT.value) as span: + # kind=SpanKind.CLIENT) as span: span = tracer.start_span( - APIS["CHAT_COMPLETION"]["METHOD"], kind=SpanKind.CLIENT.value + APIS["CHAT_COMPLETION"]["METHOD"], kind=SpanKind.CLIENT ) for field, value in attributes.model_dump(by_alias=True).items(): set_span_attribute(span, field, value) diff --git a/src/langtrace_python_sdk/instrumentation/openai/patch.py b/src/langtrace_python_sdk/instrumentation/openai/patch.py index e72e0441..b5d7ff52 100644 --- a/src/langtrace_python_sdk/instrumentation/openai/patch.py +++ b/src/langtrace_python_sdk/instrumentation/openai/patch.py @@ -65,7 +65,7 @@ def traced_method(wrapped, instance, args, kwargs): with tracer.start_as_current_span( APIS["IMAGES_GENERATION"]["METHOD"], - kind=SpanKind.CLIENT.value, + kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: set_span_attributes(span, attributes) @@ -128,7 +128,7 @@ async def traced_method(wrapped, instance, args, kwargs): with tracer.start_as_current_span( APIS["IMAGES_GENERATION"]["METHOD"], - kind=SpanKind.CLIENT.value, + kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: set_span_attributes(span, attributes) @@ -193,7 +193,7 @@ def traced_method(wrapped, instance, args, kwargs): with tracer.start_as_current_span( APIS["IMAGES_EDIT"]["METHOD"], - kind=SpanKind.CLIENT.value, + kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: set_span_attributes(span, attributes) @@ -283,7 +283,7 @@ def traced_method(wrapped, instance, args, kwargs): span = tracer.start_span( APIS["CHAT_COMPLETION"]["METHOD"], - kind=SpanKind.CLIENT.value, + kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) _set_input_attributes(span, kwargs, attributes) @@ -377,7 +377,7 @@ async def traced_method(wrapped, instance, args, kwargs): span = tracer.start_span( APIS["CHAT_COMPLETION"]["METHOD"], - kind=SpanKind.CLIENT.value, + kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) _set_input_attributes(span, kwargs, attributes) @@ -456,7 +456,7 @@ def traced_method(wrapped, instance, args, kwargs): with tracer.start_as_current_span( APIS["EMBEDDINGS_CREATE"]["METHOD"], - kind=SpanKind.CLIENT.value, + kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: @@ -513,7 +513,7 @@ async def traced_method(wrapped, instance, args, kwargs): with tracer.start_as_current_span( APIS["EMBEDDINGS_CREATE"]["METHOD"], - kind=SpanKind.CLIENT.value, + kind=SpanKind.CLIENT, context=set_span_in_context(trace.get_current_span()), ) as span: diff --git a/src/langtrace_python_sdk/version.py b/src/langtrace_python_sdk/version.py index 62fa04d7..f1e49f68 100644 --- a/src/langtrace_python_sdk/version.py +++ b/src/langtrace_python_sdk/version.py @@ -1 +1 @@ -__version__ = "2.2.4" +__version__ = "2.2.5"