Skip to content

Commit

Permalink
add embedding tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Nov 17, 2023
1 parent 1097e6a commit 516a94a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
12 changes: 12 additions & 0 deletions lib/instrumentation/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function shouldSkipInstrumentation(config, shim) {
return semver.lt(pkgVersion, MIN_VERSION)
}

// eslint-disable-next-line sonarjs/cognitive-complexity
module.exports = function initialize(agent, openai, moduleName, shim) {
if (shouldSkipInstrumentation(agent.config, shim)) {
shim.logger.debug(
Expand Down Expand Up @@ -224,6 +225,11 @@ module.exports = function initialize(agent, openai, moduleName, shim) {
promise: true,
// eslint-disable-next-line max-params
after(_shim, _fn, _name, err, response, segment) {
if (!response) {
// If we get an error, it is possible that `response = null`.
// In that case, we define it to be an empty object.
response = {}
}
response.headers = segment[openAiHeaders]
response.api_key = segment[openAiApiKey]
const embedding = new LlmEmbedding({
Expand All @@ -234,6 +240,12 @@ module.exports = function initialize(agent, openai, moduleName, shim) {
})

recordEvent('LlmEmbedding', embedding)

if (err) {
const llmError = new OpenAiLlmError({ cause: err, embedding, response })
shim.agent.errors.add(segment.transaction, llmError)
}

// cleanup keys on response before returning to user code
delete response.api_key
delete response.headers
Expand Down
3 changes: 0 additions & 3 deletions lib/llm-events/openai/llm-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ class OpenAiLlmError extends Error {
code: cause.error.code,
param: cause.error.param
}
// this.statusCode = cause.status
// this.code = cause.error.code
// this.param = cause.error.param
this.completion_id = summary?.id
this.embedding_id = embedding?.id
}
Expand Down
16 changes: 16 additions & 0 deletions test/lib/openai-responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,19 @@ responses.set('Invalid role.', {
}
}
})

responses.set('Embedding not allowed.', {
headers: {
'content-type': 'application/json; charset=utf-8',
'x-request-id': '25e22d5a7af9e40b7d5b1828593b52aa'
},
code: 403,
body: {
error: {
message: 'You are not allowed to generate embeddings from this model',
type: 'invalid_request_error',
param: null,
code: null
}
}
})
42 changes: 40 additions & 2 deletions test/versioned/openai/openai.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,53 @@ tap.test('OpenAI instrumentation', (t) => {
code: null,
param: null
},
completion_id: /[\w\d]{32}/,
completion_id: /\w{32}/,
cause: {
message: /'bad-role' is not one of/,
type: 'invalid_request_error'
}
},
customAttributes: {},
agentAttributes: {
spanId: /[\w\d]+/
spanId: /\w+/
}
})

tx.end()
test.end()
})
})

t.test('embedding invalid payload errors should be tracked', (test) => {
const { client, agent } = t.context
helper.runInTransaction(agent, async (tx) => {
try {
await client.embeddings.create({
model: 'gpt-4',
input: 'Embedding not allowed.'
})
} catch {}

t.equal(tx.exceptions.length, 1)
t.match(tx.exceptions[0], {
error: {
http: {
statusCode: 403
},
error: {
code: null,
param: null
},
completion_id: undefined,
embedding_id: /\w{32}/,
cause: {
message: 'You are not allowed to generate embeddings from this model',
type: 'invalid_request_error'
}
},
customAttributes: {},
agentAttributes: {
spanId: /\w+/
}
})

Expand Down

0 comments on commit 516a94a

Please sign in to comment.