-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add vectorsearch entities (#1992)
Co-authored-by: James Sumners <jsumners@newrelic.com>
- Loading branch information
1 parent
6b44a3a
commit ef74b2e
Showing
5 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const LangChainEvent = require('./event') | ||
const crypto = require('crypto') | ||
|
||
/** | ||
* @typedef {object} LangChainVectorSearchResultParams | ||
* @augments LangChainEventParams | ||
* @property {string} pageContent The stringified contents of the pageContent attribute on each returned search result document. | ||
* @property {number} [sequence=0] The index of the document in the search result documents list. | ||
*/ | ||
/** | ||
* @type {LangChainVectorSearchResultParams} | ||
*/ | ||
const defaultParams = { | ||
pageContent: '', | ||
sequence: 0 | ||
} | ||
|
||
class LangChainVectorSearchResult extends LangChainEvent { | ||
search_id = crypto.randomUUID() | ||
|
||
constructor(params) { | ||
params = Object.assign({}, defaultParams, params) | ||
super(params) | ||
|
||
this.page_content = params.pageContent | ||
this.sequence = params.sequence | ||
} | ||
} | ||
|
||
module.exports = LangChainVectorSearchResult |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const LangChainEvent = require('./event') | ||
|
||
/** | ||
* @typedef {object} LangChainVectorSearchParams | ||
* @augments LangChainEventParams | ||
* @property {string} query First parameter of similaritySearch method. | ||
* @property {number} k Second parameter of similaritySearch method. | ||
* @property {object} documents The set of documents returned in a response. | ||
*/ | ||
/** | ||
* @type {LangChainVectorSearchParams} | ||
*/ | ||
const defaultParams = { | ||
documents: [] | ||
} | ||
|
||
class LangChainVectorSearch extends LangChainEvent { | ||
duration; | ||
['response.number_of_documents'] = 0 | ||
|
||
constructor(params) { | ||
params = Object.assign({}, defaultParams, params) | ||
super(params) | ||
const { segment } = params | ||
|
||
this.duration = segment?.getDurationInMillis() | ||
this['request.query'] = params.query | ||
this['request.k'] = params.k | ||
this['response.number_of_documents'] = params.documents?.length | ||
} | ||
} | ||
|
||
module.exports = LangChainVectorSearch |
71 changes: 71 additions & 0 deletions
71
test/unit/llm-events/langchain/vector-search-result.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const tap = require('tap') | ||
const LangChainVectorSearchResult = require('../../../../lib/llm-events/langchain/vector-search-result') | ||
|
||
tap.beforeEach((t) => { | ||
t.context._tx = { | ||
trace: { | ||
custom: { | ||
get() { | ||
return { | ||
'llm.conversation_id': 'test-conversation' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
t.context.agent = { | ||
config: { | ||
applications() { | ||
return ['test-app'] | ||
} | ||
}, | ||
tracer: { | ||
getTransaction() { | ||
return t.context._tx | ||
} | ||
} | ||
} | ||
|
||
t.context.segment = { | ||
id: 'segment-1', | ||
transaction: { | ||
id: 'tx-1', | ||
traceId: 'trace-1' | ||
} | ||
} | ||
|
||
t.context.runId = 'run-1' | ||
t.context.metadata = { foo: 'foo' } | ||
}) | ||
|
||
tap.test('create entity', async (t) => { | ||
const search = new LangChainVectorSearchResult({ | ||
...t.context, | ||
sequence: 1, | ||
pageContent: 'hello world' | ||
}) | ||
t.match(search, { | ||
id: /[a-z0-9-]{36}/, | ||
appName: 'test-app', | ||
conversation_id: 'test-conversation', | ||
request_id: 'run-1', | ||
span_id: 'segment-1', | ||
transaction_id: 'tx-1', | ||
trace_id: 'trace-1', | ||
['metadata.foo']: 'foo', | ||
ingest_source: 'Node', | ||
vendor: 'langchain', | ||
virtual_llm: true, | ||
sequence: 1, | ||
page_content: 'hello world', | ||
search_id: /[a-z0-9-]{36}/ | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const tap = require('tap') | ||
const LangChainVectorSearch = require('../../../../lib/llm-events/langchain/vector-search') | ||
|
||
tap.beforeEach((t) => { | ||
t.context._tx = { | ||
trace: { | ||
custom: { | ||
get() { | ||
return { | ||
'llm.conversation_id': 'test-conversation' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
t.context.agent = { | ||
config: { | ||
applications() { | ||
return ['test-app'] | ||
} | ||
}, | ||
tracer: { | ||
getTransaction() { | ||
return t.context._tx | ||
} | ||
} | ||
} | ||
|
||
t.context.segment = { | ||
id: 'segment-1', | ||
transaction: { | ||
id: 'tx-1', | ||
traceId: 'trace-1' | ||
}, | ||
getDurationInMillis() { | ||
return 42 | ||
} | ||
} | ||
t.context.runId = 'run-1' | ||
}) | ||
|
||
tap.test('create entity', async (t) => { | ||
const search = new LangChainVectorSearch({ | ||
...t.context, | ||
query: 'hello world', | ||
k: 1 | ||
}) | ||
t.match(search, { | ||
'id': /[a-z0-9-]{36}/, | ||
'appName': 'test-app', | ||
'conversation_id': 'test-conversation', | ||
'request_id': 'run-1', | ||
'span_id': 'segment-1', | ||
'transaction_id': 'tx-1', | ||
'trace_id': 'trace-1', | ||
'ingest_source': 'Node', | ||
'vendor': 'langchain', | ||
'virtual_llm': true, | ||
'request.query': 'hello world', | ||
'request.k': 1, | ||
'duration': 42, | ||
'response.number_of_documents': 0 | ||
}) | ||
}) |