Skip to content

Commit 8dc7f41

Browse files
umaannamalaihmstepanek
authored andcommitted
Add LLM attribute to LangChain (#1057)
* Add HanaDB and KDBAI instrumentation. * Add LLM attr to transactions. * Add testing to vectorstores. * [Mega-Linter] Apply linters fixes --------- Co-authored-by: umaannamalai <[email protected]>
1 parent 26beba0 commit 8dc7f41

File tree

6 files changed

+28
-1
lines changed

6 files changed

+28
-1
lines changed

newrelic/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,6 +2423,11 @@ def _process_module_builtin_defaults():
24232423
"instrument_langchain_vectorstore_similarity_search",
24242424
)
24252425

2426+
_process_module_definition(
2427+
"langchain_community.vectorstores.thirdai_neuraldb",
2428+
"newrelic.hooks.mlmodel_langchain",
2429+
"instrument_langchain_vectorstore_similarity_search",
2430+
)
24262431
_process_module_definition(
24272432
"langchain_community.vectorstores.tigris",
24282433
"newrelic.hooks.mlmodel_langchain",

newrelic/hooks/mlmodel_langchain.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"langchain_community.vectorstores.mongodb_atlas": "MongoDBAtlasVectorSearch",
6666
"langchain_community.vectorstores.myscale": "MyScale",
6767
"langchain_community.vectorstores.neo4j_vector": "Neo4jVector",
68+
"langchain_community.vectorstores.thirdai_neuraldb": "NeuralDBVectorStore",
6869
"langchain_community.vectorstores.nucliadb": "NucliaDB",
6970
"langchain_community.vectorstores.opensearch_vector_search": "OpenSearchVectorSearch",
7071
"langchain_community.vectorstores.pgembedding": "PGEmbedding",
@@ -128,6 +129,7 @@ async def wrap_asimilarity_search(wrapped, instance, args, kwargs):
128129
return await wrapped(*args, **kwargs)
129130

130131
transaction.add_ml_model_info("Langchain", LANGCHAIN_VERSION)
132+
transaction._add_agent_attribute("llm", True)
131133

132134
function_name = callable_name(wrapped)
133135

@@ -209,6 +211,8 @@ def wrap_similarity_search(wrapped, instance, args, kwargs):
209211
return wrapped(*args, **kwargs)
210212

211213
transaction.add_ml_model_info("Langchain", LANGCHAIN_VERSION)
214+
transaction._add_agent_attribute("llm", True)
215+
212216
function_name = callable_name(wrapped)
213217

214218
# LLMVectorSearch and Error data
@@ -290,6 +294,8 @@ def wrap_tool_sync_run(wrapped, instance, args, kwargs):
290294

291295
# Framework metric also used for entity tagging in the UI
292296
transaction.add_ml_model_info("Langchain", LANGCHAIN_VERSION)
297+
transaction._add_agent_attribute("llm", True)
298+
293299
tool_id = str(uuid.uuid4())
294300

295301
run_args = bind_args(wrapped, args, kwargs)
@@ -401,6 +407,7 @@ async def wrap_tool_async_run(wrapped, instance, args, kwargs):
401407
return await wrapped(*args, **kwargs)
402408
# Framework metric also used for entity tagging in the UI
403409
transaction.add_ml_model_info("Langchain", LANGCHAIN_VERSION)
410+
transaction._add_agent_attribute("llm", True)
404411

405412
run_args = bind_args(wrapped, args, kwargs)
406413

@@ -543,6 +550,8 @@ async def wrap_chain_async_run(wrapped, instance, args, kwargs):
543550

544551
# Framework metric also used for entity tagging in the UI
545552
transaction.add_ml_model_info("Langchain", LANGCHAIN_VERSION)
553+
transaction._add_agent_attribute("llm", True)
554+
546555
run_args = bind_args(wrapped, args, kwargs)
547556
span_id = None
548557
trace_id = None
@@ -588,6 +597,8 @@ def wrap_chain_sync_run(wrapped, instance, args, kwargs):
588597

589598
# Framework metric also used for entity tagging in the UI
590599
transaction.add_ml_model_info("Langchain", LANGCHAIN_VERSION)
600+
transaction._add_agent_attribute("llm", True)
601+
591602
run_args = bind_args(wrapped, args, kwargs)
592603
span_id = None
593604
trace_id = None

tests/mlmodel_langchain/test_agent.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from langchain.prompts import ChatPromptTemplate
1919
from langchain.tools import tool
2020
from langchain_core.prompts import MessagesPlaceholder
21-
from testing_support.fixtures import reset_core_stats_engine
21+
from testing_support.fixtures import reset_core_stats_engine, validate_attributes
2222
from testing_support.validators.validate_transaction_metrics import (
2323
validate_transaction_metrics,
2424
)
@@ -70,6 +70,7 @@ def prompt():
7070
],
7171
background_task=True,
7272
)
73+
@validate_attributes("agent", ["llm"])
7374
@background_task()
7475
def test_sync_agent(chat_openai_client, tools, prompt):
7576
agent = create_openai_functions_agent(chat_openai_client, tools, prompt)
@@ -89,6 +90,7 @@ def test_sync_agent(chat_openai_client, tools, prompt):
8990
],
9091
background_task=True,
9192
)
93+
@validate_attributes("agent", ["llm"])
9294
@background_task()
9395
def test_async_agent(loop, chat_openai_client, tools, prompt):
9496
agent = create_openai_functions_agent(chat_openai_client, tools, prompt)

tests/mlmodel_langchain/test_chain.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from mock import patch
2727
from testing_support.fixtures import (
2828
reset_core_stats_engine,
29+
validate_attributes,
2930
validate_custom_event_count,
3031
validate_transaction_error_event_count,
3132
)
@@ -1136,6 +1137,7 @@ def test_langchain_chain(
11361137
],
11371138
background_task=True,
11381139
)
1140+
@validate_attributes("agent", ["llm"])
11391141
@background_task()
11401142
def _test():
11411143
set_trace_info()
@@ -1597,6 +1599,7 @@ def test_async_langchain_chain(
15971599
],
15981600
background_task=True,
15991601
)
1602+
@validate_attributes("agent", ["llm"])
16001603
@background_task()
16011604
def _test():
16021605
set_trace_info()

tests/mlmodel_langchain/test_tool.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from testing_support.fixtures import ( # override_application_settings,
2424
override_application_settings,
2525
reset_core_stats_engine,
26+
validate_attributes,
2627
validate_custom_event_count,
2728
validate_transaction_error_event_count,
2829
)
@@ -95,6 +96,7 @@ def _multi_arg_tool(first_num: int, second_num: int):
9596
],
9697
background_task=True,
9798
)
99+
@validate_attributes("agent", ["llm"])
98100
@background_task()
99101
def test_langchain_single_arg_tool(set_trace_info, single_arg_tool):
100102
set_trace_info()
@@ -113,6 +115,7 @@ def test_langchain_single_arg_tool(set_trace_info, single_arg_tool):
113115
],
114116
background_task=True,
115117
)
118+
@validate_attributes("agent", ["llm"])
116119
@background_task()
117120
def test_langchain_single_arg_tool_async(set_trace_info, single_arg_tool, loop):
118121
set_trace_info()

tests/mlmodel_langchain/test_vectorstore.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from langchain_community.vectorstores.faiss import FAISS
2020
from testing_support.fixtures import (
2121
reset_core_stats_engine,
22+
validate_attributes,
2223
validate_custom_event_count,
2324
)
2425
from testing_support.validators.validate_custom_events import validate_custom_events
@@ -121,6 +122,7 @@ def test_vectorstore_modules_instrumented():
121122
],
122123
background_task=True,
123124
)
125+
@validate_attributes("agent", ["llm"])
124126
@background_task()
125127
def test_pdf_pagesplitter_vectorstore_in_txn(set_trace_info, embedding_openai_client):
126128
set_trace_info()
@@ -159,6 +161,7 @@ def test_pdf_pagesplitter_vectorstore_outside_txn(set_trace_info, embedding_open
159161
],
160162
background_task=True,
161163
)
164+
@validate_attributes("agent", ["llm"])
162165
@background_task()
163166
def test_async_pdf_pagesplitter_vectorstore_in_txn(loop, set_trace_info, embedding_openai_client):
164167
async def _test():

0 commit comments

Comments
 (0)