Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/datadog-plugin-openai/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const services = require('./services')
const Sampler = require('../../dd-trace/src/sampler')
const { MEASURED } = require('../../../ext/tags')

// String#replaceAll unavailable on Node.js@v14 (dd-trace@<=v3)
const RE_NEWLINE = /\n/g
const RE_TAB = /\t/g

// TODO: In the future we should refactor config.js to make it requirable
let MAX_TEXT_LEN = 128

Expand Down Expand Up @@ -85,11 +89,11 @@ class OpenApiPlugin extends TracingPlugin {
store.prompt = prompt
if (typeof prompt === 'string' || (Array.isArray(prompt) && typeof prompt[0] === 'number')) {
// This is a single prompt, either String or [Number]
tags[`openai.request.prompt`] = normalizeStringOrTokenArray(prompt)
tags[`openai.request.prompt`] = normalizeStringOrTokenArray(prompt, true)
} else if (Array.isArray(prompt)) {
// This is multiple prompts, either [String] or [[Number]]
for (let i = 0; i < prompt.length; i++) {
tags[`openai.request.prompt.${i}`] = normalizeStringOrTokenArray(prompt[i])
tags[`openai.request.prompt.${i}`] = normalizeStringOrTokenArray(prompt[i], true)
}
}
}
Expand Down Expand Up @@ -559,8 +563,8 @@ function truncateText (text) {
if (!text) return

text = text
.replaceAll('\n', '\\n')
.replaceAll('\t', '\\t')
.replace(RE_NEWLINE, '\\n')
.replace(RE_TAB, '\\t')

if (text.length > MAX_TEXT_LEN) {
return text.substring(0, MAX_TEXT_LEN) + '...'
Expand Down Expand Up @@ -690,7 +694,7 @@ function normalizeRequestPayload (methodName, args) {
* "foo" -> "foo"
* [1,2,3] -> "[1, 2, 3]"
*/
function normalizeStringOrTokenArray (input, truncate = true) {
function normalizeStringOrTokenArray (input, truncate) {
const normalized = Array.isArray(input)
? `[${input.join(', ')}]` // "[1, 2, 999]"
: input // "foo"
Expand Down
6 changes: 3 additions & 3 deletions packages/datadog-plugin-openai/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('Plugin', () => {
expect(traces[0][0].meta).to.have.property('openai.api_base', 'https://api.openai.com/v1')
expect(traces[0][0].meta).to.have.property('openai.organization.name', 'kill-9')
expect(traces[0][0].meta).to.have.property('openai.request.model', 'text-davinci-002')
expect(traces[0][0].meta).to.have.property('openai.request.prompt', 'Hello, ')
expect(traces[0][0].meta).to.have.property('openai.request.prompt', 'Hello, \\n\\nFriend\\t\\tHi')
expect(traces[0][0].meta).to.have.property('openai.request.stop', 'time')
expect(traces[0][0].meta).to.have.property('openai.request.suffix', 'foo')
expect(traces[0][0].meta).to.have.property('openai.request.user', 'hunter2')
Expand All @@ -151,7 +151,7 @@ describe('Plugin', () => {

const result = await openai.createCompletion({
model: 'text-davinci-002',
prompt: 'Hello, ',
prompt: 'Hello, \n\nFriend\t\tHi',
suffix: 'foo',
max_tokens: 7,
temperature: 1.01,
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('Plugin', () => {
expect(externalLoggerStub).to.have.been.calledWith({
status: 'info',
message: 'sampled createCompletion',
prompt: 'Hello, ',
prompt: 'Hello, \n\nFriend\t\tHi',
choices: [
{
text: 'FOO BAR BAZ',
Expand Down