Skip to content

Commit

Permalink
fix tagger & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sabrenner committed Sep 26, 2024
1 parent 455e6e0 commit 1667e6e
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 217 deletions.
2 changes: 1 addition & 1 deletion packages/dd-trace/src/llmobs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
OUTPUT_DOCUMENTS: '_ml_obs.meta.output.documents',
OUTPUT_MESSAGES: '_ml_obs.meta.output.messages',
OUTPUT_VALUE: '_ml_obs.meta.output.value',

EVP_PROXY_AGENT_BASE_PATH: 'evp_proxy/v2',
EVP_PROXY_AGENT_ENDPOINT: 'evp_proxy/v2/api/v2/llmobs',
EVP_SUBDOMAIN_HEADER_NAME: 'X-Datadog-EVP-Subdomain',
Expand Down
73 changes: 48 additions & 25 deletions packages/dd-trace/src/llmobs/tagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ class LLMObsTagger {
}

tagSpanTags (span, tags) {
// new tags will be merged with existing tags
try {
const currentTags = span.context()._tags[TAGS]
if (currentTags) {
Object.assign(tags, currentTags)
Object.assign(tags, JSON.parse(currentTags))
}
span.setTag(TAGS, JSON.stringify(tags))
} catch {
Expand All @@ -110,7 +111,6 @@ class LLMObsTagger {
span.setTag(key, data)
} else {
try {
// this will help showcase unfinished promises being passed in as values
span.setTag(key, JSON.stringify(data))
} catch {
const type = key === INPUT_VALUE ? 'input' : 'output'
Expand All @@ -129,33 +129,49 @@ class LLMObsTagger {
try {
const documents = data.map(document => {
if (typeof document === 'string') {
return document
return { text: document }
}

if (document == null || typeof document !== 'object') {
logger.warn('Documents must be a string, object, or list of objects.')
return undefined
}

const { text, name, id, score } = document

if (text && typeof text !== 'string') {
logger.warn(`Invalid property found in ${document}: text must be a string.`)
if (typeof text !== 'string') {
logger.warn('Document text must be a string.')
return undefined
}

if (name && typeof name !== 'string') {
logger.warn(`Invalid property found in ${document}: name must be a string.`)
return undefined
const documentObj = { text }

if (name) {
if (typeof name !== 'string') {
logger.warn('Document name must be a string.')
return undefined
}
documentObj.name = name
}

if (id && typeof id !== 'string') {
logger.warn(`Invalid property found in ${document}: id must be a string.`)
return undefined
if (id) {
if (typeof id !== 'string') {
logger.warn('Document ID must be a string.')
return undefined
}
documentObj.id = id
}

if (score && typeof score !== 'number') {
logger.warn(`Invalid property found in ${document}: score must be a number.`)
return undefined
if (score) {
if (typeof score !== 'number') {
logger.warn('Document score must be a number.')
return undefined
}
documentObj.score = score
}

return document
}).filter(doc => !!doc) // filter out bad documents?
return documentObj
}).filter(doc => !!doc)

span.setTag(key, JSON.stringify(documents))
} catch {
Expand All @@ -174,28 +190,35 @@ class LLMObsTagger {
try {
const messages = data.map(message => {
if (typeof message === 'string') {
return message
return { content: message }
}

if (message == null || typeof message !== 'object') {
logger.warn('Messages must be a string, object, or list of objects')
return undefined
}

const content = message.content || ''
const role = message.role

if (typeof content !== 'string') {
logger.warn(`Invalid property found in ${message}: content must be a string.`)
logger.warn('Message content must be a string.')
return undefined
}

message.content = content

if (role && typeof role !== 'string') {
logger.warn(`Invalid property found in ${message}: role must be a string.`)
if (!role) {
return { content }
} else if (typeof role !== 'string') {
logger.warn('Message role must be a string.')
return undefined
}

return message
}).filter(msg => !!msg) // filter out bad messages?
return { content, role }
}).filter(msg => !!msg)

span.setTag(key, JSON.stringify(messages))
if (messages.length) {
span.setTag(key, JSON.stringify(messages))
}
} catch {
const type = key === INPUT_MESSAGES ? 'input' : 'output'
logger.warn(`Failed to parse ${type} messages.`)
Expand Down
Loading

0 comments on commit 1667e6e

Please sign in to comment.