Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ali/s3en 2391 python sdk #199

Merged
merged 8 commits into from
Jun 12, 2024
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
42 changes: 32 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ langtrace.init(custom_remote_exporter=<your_exporter>, batch=<True or False>)

### Configure Langtrace

| Parameter | Type | Default Value | Description |
| -------------------------- | ----------------------------------- | ----------------------------- | ------------------------------------------------------------------------------ |
| `api_key` | `str` | `LANGTRACE_API_KEY` or `None` | The API key for authentication. |
| `batch` | `bool` | `True` | Whether to batch spans before sending them. |
| `write_spans_to_console` | `bool` | `False` | Whether to write spans to the console. |
| `custom_remote_exporter` | `Optional[Exporter]` | `None` | Custom remote exporter. If `None`, a default `LangTraceExporter` will be used. |
| `api_host` | `Optional[str]` | `https://langtrace.ai/` | The API host for the remote exporter. |
| `disable_instrumentations` | `Optional[DisableInstrumentations]` | `None` | You can pass an object to disable instrumentation for specific vendors ex: `{'only': ['openai']}` or `{'all_except': ['openai']}`
| Parameter | Type | Default Value | Description |
| -------------------------- | ----------------------------------- | ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `api_key` | `str` | `LANGTRACE_API_KEY` or `None` | The API key for authentication. |
| `batch` | `bool` | `True` | Whether to batch spans before sending them. |
| `write_spans_to_console` | `bool` | `False` | Whether to write spans to the console. |
| `custom_remote_exporter` | `Optional[Exporter]` | `None` | Custom remote exporter. If `None`, a default `LangTraceExporter` will be used. |
| `api_host` | `Optional[str]` | `https://langtrace.ai/` | The API host for the remote exporter. |
| `disable_instrumentations` | `Optional[DisableInstrumentations]` | `None` | You can pass an object to disable instrumentation for specific vendors ex: `{'only': ['openai']}` or `{'all_except': ['openai']}` |

### Additional Customization

Expand All @@ -165,7 +165,30 @@ def example():
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.
- `inject_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.

```python
from langtrace_python_sdk import inject_additional_attributes



def do_llm_stuff(name=""):
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Say this is a test three times"}],
stream=False,
)
return response


def main():
response = inject_additional_attributes(lambda: do_llm_stuff(name="llm"), {'user.id': 'userId'})

# if the function do not take arguments then this syntax will work
response = inject_additional_attributes(do_llm_stuff, {'user.id': 'userId'})
```

- `with_additional_attributes` - is behaving the same as `inject_additional_attributes` but as a decorator, this will be deprecated soon.

```python
from langtrace_python_sdk import with_langtrace_root_span, with_additional_attributes
Expand Down Expand Up @@ -237,7 +260,6 @@ We welcome contributions to this project. To get started, fork this repository a

If you want to run any of the examples go to `run_example.py` file, you will find `ENABLED_EXAMPLES`. choose the example you want to run and just toggle the flag to `True` and run the file using `python src/run_example.py`


---

## Security
Expand Down
2 changes: 0 additions & 2 deletions src/examples/langchain_example/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
langtrace.init()


# @with_additional_attributes({"user.id": "1234", "user.feedback.rating": 1})
def api_call_1():
llm = ChatOpenAI()
prompt = ChatPromptTemplate.from_messages(
Expand All @@ -33,7 +32,6 @@ def api_call_1():
print(res)


# @with_additional_attributes({"user.id": "37373", "user.feedback.rating": 1})
def api_call_2():
llm = ChatOpenAI()
prompt = ChatPromptTemplate.from_messages(
Expand Down
1 change: 0 additions & 1 deletion src/examples/openai_example/chat_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
client = OpenAI()


@with_additional_attributes({"user.id": "1234", "user.feedback.rating": 1})
def api():
response = client.chat.completions.create(
model="gpt-4",
Expand Down
13 changes: 10 additions & 3 deletions src/examples/pinecone_example/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from langtrace_python_sdk import with_langtrace_root_span
from langtrace_python_sdk import (
get_prompt_from_registry,
with_langtrace_root_span,
with_additional_attributes,
inject_additional_attributes,
)


class PineconeRunner:
@with_langtrace_root_span("Pinecone")
def run(self):
from .basic import basic as basic_app
from .basic import basic as do_llm_stuff

basic_app()
response = inject_additional_attributes(do_llm_stuff, {"user.id": 1234})
print(response)
return response
15 changes: 5 additions & 10 deletions src/examples/pinecone_example/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from pinecone import Pinecone, ServerlessSpec

from langtrace_python_sdk import (
get_prompt_from_registry,
langtrace,
with_langtrace_root_span,
with_additional_attributes,
)
from langtrace_python_sdk.utils.with_root_span import SendUserFeedback

_ = load_dotenv(find_dotenv())

langtrace.init()

client = OpenAI()
Expand All @@ -32,10 +32,8 @@ def create_index():
)


@with_additional_attributes({"db.embedding_model": "text-embedding-ada-002"})
@with_langtrace_root_span("Pinecone Basic")
def basic(span_id=None, trace_id=None):

def basic():
result = client.embeddings.create(
model="text-embedding-ada-002",
input="Some random text string goes here",
Expand All @@ -53,10 +51,7 @@ def basic(span_id=None, trace_id=None):
resp = index.query(
vector=embedding, top_k=1, include_values=False, namespace="test-namespace"
)
SendUserFeedback().evaluate(
{"spanId": span_id, "traceId": trace_id, "userScore": 1, "userId": "123"}
)
# SendUserFeedback().evaluate(
# {"spanId": span_id, "traceId": trace_id, "userScore": 1, "userId": "123"}
# )
return [res, resp]


# create_index()
6 changes: 5 additions & 1 deletion src/langtrace_python_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
)

from langtrace_python_sdk.utils.prompt_registry import get_prompt_from_registry
from langtrace_python_sdk.utils.with_root_span import SendUserFeedback
from langtrace_python_sdk.utils.with_root_span import (
SendUserFeedback,
inject_additional_attributes,
)

__all__ = [
"langtrace",
"with_langtrace_root_span",
"with_additional_attributes",
"inject_additional_attributes",
"get_prompt_from_registry",
"SendUserFeedback",
]
21 changes: 18 additions & 3 deletions src/langtrace_python_sdk/utils/with_root_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

import asyncio
import os
from deprecated import deprecated
from functools import wraps
from typing import Optional

import requests
from opentelemetry import baggage, context, trace
from opentelemetry.trace import SpanKind

from langtrace_python_sdk.constants.exporter.langtrace_exporter import (
LANGTRACE_REMOTE_URL,
)
from langtrace_python_sdk.constants.instrumentation.common import (
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY,
)
Expand Down Expand Up @@ -87,7 +85,14 @@ async def async_wrapper(*args, **kwargs):
return decorator


@deprecated(reason="Use inject_additional_attributes instead")
def with_additional_attributes(attributes={}):
print(
Fore.YELLOW
+ "with_additional_attributes is deprecated, use inject_additional_attributes instead"
+ Fore.RESET
)

def decorator(func):
@wraps(func)
def sync_wrapper(*args, **kwargs):
Expand All @@ -113,6 +118,16 @@ async def async_wrapper(*args, **kwargs):
return decorator


def inject_additional_attributes(fn, attributes=None):
if attributes:
new_ctx = baggage.set_baggage(
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, attributes
)
context.attach(new_ctx)

return fn()


class SendUserFeedback:
_langtrace_host: str
_langtrace_api_key: str
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.1.15"
__version__ = "2.1.16"
Loading