sentry-python icon indicating copy to clipboard operation
sentry-python copied to clipboard

LLM Monitoring not working for Async OpenAI requests

Open AltafHussain4748 opened this issue 1 year ago • 8 comments

Problem Statement

I just experimented LLM monitoring and could not make it work with AsyncOpenAI.

Below is my code

My sentry init is

sentry_sdk.init(
    dsn=os.environ.get("SENTRY_DSN"),
    integrations=[sentry_logging],
    environment=os.environ.get("ENVIRONMENT", "prod"),
    send_default_pii=True,
)

Function i am using:

@ai_track("Tracking Name")
@async_retry(retries=4)
async def func():
    client = AsyncOpenAI()
    with sentry_sdk.start_transaction(op="ai-inference", name="Structured Data Prompt"):
        response = await client.chat.completions.create(
            model=model,
            messages=messages,
            functions=functions,
            temperature=0.0,
            timeout=120,
        )

Versions

sentry-sdk==2.13.0
openai==1.37.1

Solution Brainstorm

No response

Product Area

Insights

AltafHussain4748 avatar Aug 21 '24 19:08 AltafHussain4748

Assigning to @getsentry/support for routing ⏲️

getsantry[bot] avatar Aug 21 '24 19:08 getsantry[bot]

Routing to @getsentry/product-owners-insights for triage ⏲️

getsantry[bot] avatar Aug 21 '24 23:08 getsantry[bot]

@AltafHussain4748 thank you for the bug report, I just added to our backlog of work to triage next week. We'll update soon.

bcoe avatar Aug 22 '24 21:08 bcoe

Hey @AltafHussain4748 !

Your code looks good, I think the only thing your are missing that you need to enable tracing. (Setting tracing_sample_rate=1.0 in your init() call.)

You can check if data is sent to Sentry by setting debug=True to your init() call and then you see something like this message in your console:

Sending envelope [envelope with 1 items (transaction)] project:5461230 host:o447951.ingest.sentry.io

(The LLM data is in the transaction envelope)

Hope this help!

antonpirker avatar Sep 04 '24 11:09 antonpirker

Hey, sorry I didn't notice this issue before and created a new one, but it doesn't work because of this: https://github.com/getsentry/sentry-python/issues/3496

vetyy avatar Sep 04 '24 12:09 vetyy

Cool @vetyy , thanks for linking!

antonpirker avatar Sep 04 '24 12:09 antonpirker

@AltafHussain4748

This is the configuration I am using

    sentry_sdk.init(
        dsn=dsn,
        release=release,
        environment=environment,
        send_default_pii=True,
        enable_tracing=True,
        integrations=[
            OpenAIIntegration(
                include_prompts=False,  # Exclude prompts from being sent to Sentry, despite send_default_pii=True
                tiktoken_encoding_name="cl100k_base",
            )
        ],
    )

Don't forget to specify tiktoken_encoding_name otherwise it will calculate 0 tokens.

    def count_tokens(self, s):
        # type: (OpenAIIntegration, str) -> int
        if self.tiktoken_encoding is not None:
            return len(self.tiktoken_encoding.encode_ordinary(s))
        return 0

and also make sure you have pip install tiktoken installed.

vetyy avatar Sep 04 '24 13:09 vetyy

Hey @AltafHussain4748 !

Your code looks good, I think the only thing your are missing that you need to enable tracing. (Setting tracing_sample_rate=1.0 in your init() call.)

You can check if data is sent to Sentry by setting debug=True to your init() call and then you see something like this message in your console:

Sending envelope [envelope with 1 items (transaction)] project:5461230 host:o447951.ingest.sentry.io

(The LLM data is in the transaction envelope)

Hope this help!

Thanks for your reply even with this parameter i was not able to make it work

AltafHussain4748 avatar Sep 06 '24 10:09 AltafHussain4748

Hey @AltafHussain4748 ! With version 2.17.0 of the SDK we released a fix for async openai (and anthropic) requests. Could you try the newest version of the SDK and see if this solved your problem?

antonpirker avatar Nov 27 '24 15:11 antonpirker

Since there's been no activity I will close this -- it should be fixed in SDK >= 2.17. If not, please open a new issue.

sentrivana avatar Dec 11 '24 09:12 sentrivana