Python SDK for LangTrace
Project description
What is Langtrace?
Langtrace stands as a developer-centric, open-source solution, fully compatible with OpenTelemetry. It enables developers to effortlessly trace, monitor, and debug their LLM applications, offering robust support for automatic instrumentation.
Supported LLM Modules
Langtrace supports a comprehensive range of LLMs, VectorDBs, and frameworks, ensuring wide coverage for your development needs:
LLMs
- OpenAI
- Anthropic
- Azure OpenAI
VectorDBs
- Pinecone
- Chromadb
Frameworks
- LangChain
- LlamaIndex
- Haystack
We are actively working to extend our support to additional libraries!
Getting Started
To begin utilizing Langtrace, follow these straightforward steps:
- Install the package using
pip install langtrace-python-sdk
. - Incorporate Langtrace into your project with
from langtrace_python_sdk import langtrace
.- This import should precede any other LLM module imports (such as OpenAI, LlamaIndex, etc.) to ensure proper functionality.
- Initialize Langtrace by adding
langtrace.init(write_to_langtrace_cloud=false)
to your code. - Congratulations, you've completed the basic setup! You will now begin to see traces from your LLM modules logged directly to the console.
Exporting Traces to Langtrace
To configure trace exporting, you have two options:
You'll need a Langtrace api_key
, which can be acquired by logging into your Langtrace account.
- Direct Initialization: Utilize
langtrace.init(api_key=<YOUR_API_KEY>)
. - Environment Variables: Set
LANGTRACE_API_KEY
, then addlangtrace.init()
at the beginning of your file.
Additional Customization
@with_langtrace_root_span
- this decorator is designed to organize and relate different spans, in a hierarchical manner. When you're performing multiple operations that you want to monitor together as a unit, this function helps by establishing a "parent" (LangtraceRootSpan
or whatever is passed toname
) span. Then, any calls to the LLM APIs made within the given function (fn) will be considered "children" of this parent span. This setup is especially useful for tracking the performance or behavior of a group of operations collectively, rather than individually.
from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
@with_langtrace_root_span()
def example():
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Say this is a test three times"}],
stream=False,
)
return response
with_additional_attributes
- this function is designed to enhance the traces by adding custom attributes to the current context. These custom attributes provide extra details about the operations being performed, making it easier to analyze and understand their behavior.
from langtrace_python_sdk.utils.with_root_span import (
with_langtrace_root_span,
with_additional_attributes,
)
@with_additional_attributes({"user.id": "1234", "user.feedback.rating": 1})
def api_call1():
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Say this is a test three times"}],
stream=False,
)
return response
@with_additional_attributes({"user.id": "5678", "user.feedback.rating": -1})
def api_call2():
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Say this is a test three times"}],
stream=False,
)
return response
@with_langtrace_root_span()
def chat_completion():
api_call1()
api_call2()
Langtrace Cloud
Currently under development 🚧
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file langtrace_python_sdk-1.2.9.tar.gz
.
File metadata
- Download URL: langtrace_python_sdk-1.2.9.tar.gz
- Upload date:
- Size: 40.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f741ed6ed5f9326e8328cd80c0545fd253f156ae56afb371a4660180803246af |
|
MD5 | b7dc1c27b135c9e596fa1cc935149467 |
|
BLAKE2b-256 | c1d6f58d5bf271b0dd4701d47b4b17041a9bc5e9129514abef119cd8be7fbb48 |
File details
Details for the file langtrace_python_sdk-1.2.9-py3-none-any.whl
.
File metadata
- Download URL: langtrace_python_sdk-1.2.9-py3-none-any.whl
- Upload date:
- Size: 60.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe6794e20f0dcb40378db204377a9cae21cd6e99eb01dc3920feec84a11ff8c2 |
|
MD5 | 48b126f14f6d714a969b08ae781fa00d |
|
BLAKE2b-256 | 8396008c028fe3b89a92df3a1e4fb3c589057848d345ed4463bcbe10da059c9f |