Skip to content
Draft
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
2 changes: 1 addition & 1 deletion newrelic/hooks/mlmodel_langgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def wrap_AsyncBackgroundExecutor_submit(wrapped, instance, args, kwargs):
return wrapped(*args, **kwargs)

context = ContextOf(trace=trace, strict=True)
func = coroutine_wrapper(wrapped, context)
func = coroutine_wrapper(func, context)
return wrapped(func, *args, **kwargs)


Expand Down
198 changes: 197 additions & 1 deletion tests/mlmodel_langchain/_mock_external_openai_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,121 @@
},
],
],
"user: What is the capital of France? Answer in one word.": [
{
"content-type": "text/event-stream; charset=utf-8",
"openai-organization": "user-rk8wq9voijy9sejrncvgi0iw",
"openai-processing-ms": "134",
"openai-project": "proj_0Wv6taeZjWf793P67JMswYY3",
"openai-version": "2020-10-01",
"x-ratelimit-limit-requests": "10000",
"x-ratelimit-limit-tokens": "50000000",
"x-ratelimit-remaining-requests": "9999",
"x-ratelimit-remaining-tokens": "49999985",
"x-ratelimit-reset-requests": "6ms",
"x-ratelimit-reset-tokens": "0s",
"x-request-id": "req_22204b237d22427fbfd99c665d8a9964",
},
200,
[
{
"id": "",
"choices": [],
"created": 0,
"model": "",
"object": "",
"service_tier": None,
"system_fingerprint": None,
"usage": None,
"prompt_filter_results": [
{
"prompt_index": 0,
"content_filter_results": {
"hate": {"filtered": False, "severity": "safe"},
"self_harm": {"filtered": False, "severity": "safe"},
"sexual": {"filtered": False, "severity": "safe"},
"violence": {"filtered": False, "severity": "safe"},
},
}
],
},
{
"id": "chatcmpl-DelITaJCJy951hwON0psdz2H9dF7i",
"choices": [
{
"delta": {
"content": "",
"function_call": None,
"refusal": None,
"role": "assistant",
"tool_calls": None,
},
"finish_reason": None,
"index": 0,
"logprobs": None,
"content_filter_results": {},
}
],
"created": 1780079082,
"model": "gpt-3.5-turbo-0125",
"object": "chat.completion.chunk",
"service_tier": "default",
"system_fingerprint": "fp_a7294185dc",
"usage": None,
"obfuscation": "LK4Bn",
},
{
"id": "chatcmpl-DelITaJCJy951hwON0psdz2H9dF7i",
"choices": [
{
"delta": {
"content": "Paris",
"function_call": None,
"refusal": None,
"role": None,
"tool_calls": None,
},
"finish_reason": None,
"index": 0,
"logprobs": None,
"content_filter_results": {},
}
],
"created": 1780079082,
"model": "gpt-3.5-turbo-0125",
"object": "chat.completion.chunk",
"service_tier": "default",
"system_fingerprint": "fp_a7294185dc",
"usage": None,
"obfuscation": "QP",
},
{
"id": "chatcmpl-DelITaJCJy951hwON0psdz2H9dF7i",
"choices": [
{
"delta": {
"content": None,
"function_call": None,
"refusal": None,
"role": None,
"tool_calls": None,
},
"finish_reason": "stop",
"index": 0,
"logprobs": None,
"content_filter_results": {},
}
],
"created": 1780079082,
"model": "gpt-3.5-turbo-0125",
"object": "chat.completion.chunk",
"service_tier": "default",
"system_fingerprint": "fp_a7294185dc",
"usage": None,
"obfuscation": "s",
},
],
],
}
RESPONSES_V1 = {
'system: You are a text manipulation algorithm. | user: Use a tool to add an exclamation to the word "Hello"': [
Expand Down Expand Up @@ -286,7 +401,7 @@
"x-ratelimit-remaining-tokens": "49999970",
"x-ratelimit-reset-requests": "6ms",
"x-ratelimit-reset-tokens": "0s",
"x-request-id": "req_e9add199e2c543f1b0f1dc5318690171",
"x-request-id": "req_619548c272db4f1ab380b83de9fdedef",
},
200,
{
Expand Down Expand Up @@ -754,6 +869,87 @@
"system_fingerprint": None,
},
],
"user: What is the capital of France? Answer in one word.": [
{
"content-type": "application/json",
"openai-organization": "user-rk8wq9voijy9sejrncvgi0iw",
"openai-processing-ms": "238",
"openai-project": "proj_0Wv6taeZjWf793P67JMswYY3",
"openai-version": "2020-10-01",
"x-ratelimit-limit-requests": "10000",
"x-ratelimit-limit-tokens": "50000000",
"x-ratelimit-remaining-requests": "9999",
"x-ratelimit-remaining-tokens": "49999985",
"x-ratelimit-reset-requests": "6ms",
"x-ratelimit-reset-tokens": "0s",
"x-request-id": "req_22204b237d22427fbfd99c665d8a9964",
},
200,
{
"id": "chatcmpl-DelITaJCJy951hwON0psdz2H9dF7i",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": None,
"message": {
"content": "Paris",
"refusal": None,
"role": "assistant",
"annotations": [],
"audio": None,
"function_call": None,
"tool_calls": None,
},
"content_filter_results": {
"hate": {"filtered": False, "severity": "safe"},
"self_harm": {"filtered": False, "severity": "safe"},
"sexual": {"filtered": False, "severity": "safe"},
"violence": {"filtered": False, "severity": "safe"},
},
}
],
"created": 1780077445,
"model": "gpt-3.5-turbo-0125",
"object": "chat.completion",
"service_tier": "default",
"system_fingerprint": "fp_a7294185dc",
"usage": {
"completion_tokens": 2,
"prompt_tokens": 19,
"total_tokens": 21,
"completion_tokens_details": {
"accepted_prediction_tokens": 0,
"audio_tokens": 0,
"reasoning_tokens": 0,
"rejected_prediction_tokens": 0,
},
"prompt_tokens_details": {"audio_tokens": 0, "cached_tokens": 0},
"latency_checkpoint": {
"engine_tbt_ms": 11,
"engine_ttft_ms": 56,
"engine_ttlt_ms": 78,
"pre_inference_ms": 108,
"service_tbt_ms": 16,
"service_ttft_ms": 196,
"service_ttlt_ms": 219,
"total_duration_ms": 120,
"user_visible_ttft_ms": 88,
},
},
"prompt_filter_results": [
{
"prompt_index": 0,
"content_filter_results": {
"hate": {"filtered": False, "severity": "safe"},
"self_harm": {"filtered": False, "severity": "safe"},
"sexual": {"filtered": False, "severity": "safe"},
"violence": {"filtered": False, "severity": "safe"},
},
}
],
},
],
}


Expand Down
83 changes: 83 additions & 0 deletions tests/mlmodel_langchain/test_pregel_executor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import asyncio
from threading import Event

from langgraph.pregel._executor import AsyncBackgroundExecutor, BackgroundExecutor

from newrelic.api.background_task import background_task
from newrelic.api.time_trace import current_trace


@background_task()
def test_background_executor_submit_propagates_context():
trace = current_trace()
test_ran = Event()

def task():
assert current_trace() is trace
test_ran.set()

with BackgroundExecutor(config={}) as submit:
future = submit(task)
future.result()

assert test_ran.is_set()


@background_task()
def test_async_background_executor_submit_propagates_context(loop):
trace = current_trace()
test_ran = Event()

async def task():
assert current_trace() is trace
test_ran.set()

async def _test():
async with AsyncBackgroundExecutor(config={}) as submit:
future = submit(task)
await asyncio.wrap_future(future)

loop.run_until_complete(_test())

assert test_ran.is_set()


def test_background_executor_submit_pass_through_outside_transaction():
test_ran = Event()

def task():
test_ran.set()

with BackgroundExecutor(config={}) as submit:
submit(task).result()

assert test_ran.is_set()


def test_async_background_executor_submit_pass_through_outside_transaction(loop):
test_ran = Event()

async def task():
test_ran.set()

async def _test():
async with AsyncBackgroundExecutor(config={}) as submit:
await asyncio.wrap_future(submit(task))

loop.run_until_complete(_test())

assert test_ran.is_set()
Loading
Loading