Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

Openlit docs fixes (#188)

patcher912bde6a2f336
diff --git a/.github/workflows/pypi-release.yml b/.github/workflows/pypi-release.yml
index 762e6bc7..0a934aec 100644
--- a/.github/workflows/pypi-release.yml
+++ b/.github/workflows/pypi-release.yml
@@ -35,4 +35,4 @@ jobs:
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
- packages-dir: sdk/python
+ packages_dir: sdk/python/dist
diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml
index 210a0f23..d293ef5b 100644
--- a/.github/workflows/python-tests.yml
+++ b/.github/workflows/python-tests.yml
@@ -3,8 +3,12 @@ name: Python Tests
on:
push:
branches: [ "main" ]
+ paths:
+ - 'sdk/python/**'
pull_request:
branches: [ "main" ]
+ paths:
+ - 'sdk/python/**'
workflow_dispatch:
schedule:
- cron: '0 0 1-31/15 * *'
@@ -27,9 +31,6 @@ jobs:
steps:
- uses: actions/checkout@v4
- - name: Set up Docker Buildx
- uses: docker/setup-buildx-action@0d103c3126aa41d772a8362f6aa67afac040f80c # v3.1.0
-
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
diff --git a/.github/workflows/test-pypi-release.yml b/.github/workflows/test-pypi-release.yml
index 01848734..78c79253 100644
--- a/.github/workflows/test-pypi-release.yml
+++ b/.github/workflows/test-pypi-release.yml
@@ -66,4 +66,4 @@ jobs:
user: __token__
repository_url: https://test.pypi.org/legacy/
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
- packages_dir: sdk/python
+ packages_dir: sdk/python/dist
diff --git a/.gitignore b/.gitignore
index c65d5648..8ebdfd23 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,4 +4,5 @@ test.go
.DS_Store
yala.py
yala.yml
-speech.mp3
\ No newline at end of file
+speech.mp3
+.venv
\ No newline at end of file
diff --git a/sdk/python/src/openlit/__init__.py b/sdk/python/src/openlit/__init__.py
index 56ab1176..bff6cd52 100644
--- a/sdk/python/src/openlit/__init__.py
+++ b/sdk/python/src/openlit/__init__.py
@@ -3,8 +3,9 @@ The __init__.py module for the openLIT package.
This module sets up the openLIT configuration and instrumentation for various
large language models (LLMs).
"""
-from typing import Optional, Dict, Any
+from typing import Dict
import logging
+from importlib.util import find_spec
# Import internal modules for setting up tracing and fetching pricing info.
from openlit.otel.tracing import setup_tracing
@@ -91,6 +92,30 @@ class OpenlitConfig:
cls.trace_content = trace_content
cls.disable_metrics = disable_metrics
+def instrument_if_available(instrumentor_name, instrumentor_instance, config,
+ disabled_instrumentors, module_name_map):
+ """Instruments the specified instrumentor if its library is available."""
+ if instrumentor_name in disabled_instrumentors:
+ return
+
+ module_name = module_name_map.get(instrumentor_name)
+
+ if not module_name or find_spec(module_name) is not None:
+ try:
+ instrumentor_instance.instrument(
+ environment=config.environment,
+ application_name=config.application_name,
+ tracer=config.tracer,
+ pricing_info=config.pricing_info,
+ trace_content=config.trace_content,
+ metrics_dict=config.metrics_dict,
+ disable_metrics=config.disable_metrics
+ )
+
+ # pylint: disable=broad-exception-caught
+ except Exception as e:
+ logger.error("Failed to instrument %s: %s", instrumentor_name, e)
+
def init(environment="default", application_name="default", tracer=None, otlp_endpoint=None,
otlp_headers=None, disable_batch=False, trace_content=True, disabled_instrumentors=None,
meter=None, disable_metrics=False):
@@ -115,12 +140,19 @@ def init(environment="default", application_name="default", tracer=None, otlp_en
disabled_instrumentors = disabled_instrumentors if disabled_instrumentors else []
# Check for invalid instrumentor names
- valid_instruments = {
- "openai", "anthropic", "langchain",
- "cohere", "mistral", "chroma",
- "pinecone", "transformers"
+
+ module_name_map = {
+ "openai": "openai",
+ "anthropic": "anthropic",
+ "cohere": "cohere",
+ "mistral": "mistralai",
+ "langchain": "langchain",
+ "chroma": "chromadb",
+ "pinecone": "pincone",
+ "transformers": "transformers"
}
- invalid_instrumentors = set(disabled_instrumentors) - valid_instruments
+
+ invalid_instrumentors = [name for name in disabled_instrumentors if name not in module_name_map]
for invalid_name in invalid_instrumentors:
logger.warning("Invalid instrumentor name detected and ignored: '%s'", invalid_name)
@@ -168,16 +200,8 @@ def init(environment="default", application_name="default", tracer=None, otlp_en
# Initialize and instrument only the enabled instrumentors
for name, instrumentor in instrumentor_instances.items():
- if name not in disabled_instrumentors:
- instrumentor.instrument(
- environment=config.environment,
- application_name=config.application_name,
- tracer=config.tracer,
- pricing_info=config.pricing_info,
- trace_content=config.trace_content,
- metrics_dict=config.metrics_dict,
- disable_metrics=config.disable_metrics
- )
+ instrument_if_available(name, instrumentor, config,
+ disabled_instrumentors, module_name_map)
# pylint: disable=broad-exception-caught
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/cohere/__init__.py b/sdk/python/src/openlit/instrumentation/cohere/__init__.py
index a9a5600a..65ae8da6 100644
--- a/sdk/python/src/openlit/instrumentation/cohere/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/cohere/__init__.py
@@ -7,7 +7,7 @@ from wrapt import wrap_function_wrapper
from openlit.instrumentation.cohere.cohere import chat, chat_stream, embed
-_instruments = ("cohere >= 5.0.0",)
+_instruments = ("cohere >= 5.3.2",)
class CohereInstrumentor(BaseInstrumentor):
"""An instrumentor for Cohere's client library."""
diff --git a/sdk/python/src/openlit/instrumentation/cohere/cohere.py b/sdk/python/src/openlit/instrumentation/cohere/cohere.py
index 1326ea5c..52690adc 100644
--- a/sdk/python/src/openlit/instrumentation/cohere/cohere.py
+++ b/sdk/python/src/openlit/instrumentation/cohere/cohere.py
@@ -173,8 +173,8 @@ def chat(gen_ai_endpoint, version, environment, application_name, tracer,
# Calculate cost of the operation
cost = get_chat_model_cost(kwargs.get("model", "command"),
pricing_info,
- response.meta["billed_units"]["input_tokens"],
- response.meta["billed_units"]["output_tokens"])
+ response.meta.billed_units.input_tokens,
+ response.meta.billed_units.output_tokens)
# Set Span attributes
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
@@ -207,12 +207,12 @@ def chat(gen_ai_endpoint, version, environment, application_name, tracer,
span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
response.response_id)
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
- response.meta["billed_units"]["input_tokens"])
+ response.meta.billed_units.input_tokens)
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
- response.meta["billed_units"]["output_tokens"])
+ response.meta.billed_units.output_tokens)
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
- response.meta["billed_units"]["input_tokens"] +
- response.meta["billed_units"]["output_tokens"])
+ response.meta.billed_units.input_tokens +
+ response.meta.billed_units.output_tokens)
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
cost)
if trace_content:
@@ -241,12 +241,12 @@ def chat(gen_ai_endpoint, version, environment, application_name, tracer,
metrics["genai_requests"].add(1, attributes)
metrics["genai_total_tokens"].add(
- response.meta["billed_units"]["input_tokens"] +
- response.meta["billed_units"]["output_tokens"], attributes)
+ response.meta.billed_units.input_tokens +
+ response.meta.billed_units.output_tokens, attributes)
metrics["genai_completion_tokens"].add(
- response.meta["billed_units"]["output_tokens"], attributes)
+ response.meta.billed_units.output_tokens, attributes)
metrics["genai_prompt_tokens"].add(
- response.meta["billed_units"]["input_tokens"], attributes)
+ response.meta.billed_units.input_tokens, attributes)
metrics["genai_cost"].record(cost, attributes)
# Return original response
@@ -307,8 +307,8 @@ def chat_stream(gen_ai_endpoint, version, environment, application_name,
if event.event_type == "stream-end":
llmresponse = event.response.text
response_id = event.response.response_id
- prompt_tokens = event.response.meta["billed_units"]["input_tokens"]
- completion_tokens = event.response.meta["billed_units"]["output_tokens"]
+ prompt_tokens = event.response.meta.billed_units.input_tokens
+ completion_tokens = event.response.meta.billed_units.output_tokens
finish_reason = event.finish_reason
yield event
diff --git a/sdk/python/src/openlit/instrumentation/langchain/__init__.py b/sdk/python/src/openlit/instrumentation/langchain/__init__.py
index 780b8b8e..362e8e76 100644
--- a/sdk/python/src/openlit/instrumentation/langchain/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/langchain/__init__.py
@@ -7,8 +7,7 @@ from wrapt import wrap_function_wrapper
from openlit.instrumentation.langchain.langchain import general_wrap, hub
-_instruments = ("langchain >= 0.1.1", "langchain-openai >= 0.1.1",
- "langchain-core > 0.1.1", "langchain-community >= 0.0.31")
+_instruments = ("langchain >= 0.1.1",)
WRAPPED_METHODS = [
{