Skip to content

Commit

Permalink
feat: Add vectorsearch entities (#1992)
Browse files Browse the repository at this point in the history
Co-authored-by: James Sumners <jsumners@newrelic.com>
  • Loading branch information
svetlanabrennan and jsumners-nr authored Feb 6, 2024
1 parent 6b44a3a commit ef74b2e
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/llm-events/langchain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
module.exports = {
LangChainEvent: require('./event'),
LangChainCompletionMessage: require('./chat-completion-message'),
LangChainCompletionSummary: require('./chat-completion-summary')
LangChainCompletionSummary: require('./chat-completion-summary'),
LangChainVectorSearch: require('./vector-search'),
LangChainVectorSearchResult: require('./vector-search-result')
}
37 changes: 37 additions & 0 deletions lib/llm-events/langchain/vector-search-result.js
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
40 changes: 40 additions & 0 deletions lib/llm-events/langchain/vector-search.js
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 test/unit/llm-events/langchain/vector-search-result.test.js
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}/
})
})
72 changes: 72 additions & 0 deletions test/unit/llm-events/langchain/vector-search.test.js
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
})
})

0 comments on commit ef74b2e

Please sign in to comment.