From dae9fed492339c19f62bf01c0a3d92bb357059ae Mon Sep 17 00:00:00 2001 From: Peter Salas Date: Mon, 20 Nov 2023 16:33:04 -0800 Subject: [PATCH 1/4] Add openai.finish_reason span attribute for OpenAIChatModel --- packages/ai-jsx/package.json | 2 +- packages/ai-jsx/src/lib/openai.tsx | 4 ++++ packages/docs/docs/changelog.md | 6 +++++- packages/examples/test/core/completion.tsx | 4 ++++ packages/examples/test/lib/openai.tsx | 2 +- 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/ai-jsx/package.json b/packages/ai-jsx/package.json index fe8a8bd42..718bb56da 100644 --- a/packages/ai-jsx/package.json +++ b/packages/ai-jsx/package.json @@ -4,7 +4,7 @@ "repository": "fixie-ai/ai-jsx", "bugs": "https://github.com/fixie-ai/ai-jsx/issues", "homepage": "https://ai-jsx.com", - "version": "0.28.0", + "version": "0.28.1", "volta": { "extends": "../../package.json" }, diff --git a/packages/ai-jsx/src/lib/openai.tsx b/packages/ai-jsx/src/lib/openai.tsx index 4e69624e1..1720e67d8 100644 --- a/packages/ai-jsx/src/lib/openai.tsx +++ b/packages/ai-jsx/src/lib/openai.tsx @@ -567,6 +567,10 @@ export async function* OpenAIChatModel( } while (next.value.choices.length == 0); logger.trace({ deltaMessage: next.value }, 'Got delta message'); + + if (next.value.choices[0].finish_reason !== null) { + logger.setAttribute('openai.finish_reason', next.value.choices[0].finish_reason); + } return next.value.choices[0].delta; } diff --git a/packages/docs/docs/changelog.md b/packages/docs/docs/changelog.md index 5e13ef34b..e09495104 100644 --- a/packages/docs/docs/changelog.md +++ b/packages/docs/docs/changelog.md @@ -1,6 +1,10 @@ # Changelog -## 0.28.0 +## 0.28.1 + +- Add `openai.finish_reason` span attribute for `OpenAIChatModel` + +## [0.28.0](https://github.com/fixie-ai/ai-jsx/commit/73251358a1121c98e9059c57d3c905b6156447c4) - Improved completion/prompt logging to include explicit message text diff --git a/packages/examples/test/core/completion.tsx b/packages/examples/test/core/completion.tsx index 622695f63..2aa0ff0f0 100644 --- a/packages/examples/test/core/completion.tsx +++ b/packages/examples/test/core/completion.tsx @@ -92,6 +92,7 @@ describe('OpenTelemetry', () => { "ai.jsx.result": "opentel response from OpenAI", "ai.jsx.tag": "Stream", "ai.jsx.tree": ""▮"", + "openai.finish_reason": "stop", }, { "ai.jsx.result": "opentel response from OpenAI", @@ -119,6 +120,7 @@ describe('OpenTelemetry', () => { {"hello"} ", + "openai.finish_reason": "stop", }, { "ai.jsx.result": "opentel response from OpenAI", @@ -198,6 +200,7 @@ describe('OpenTelemetry', () => { "ai.jsx.result": "opentel response from OpenAI", "ai.jsx.tag": "Stream", "ai.jsx.tree": ""▮"", + "openai.finish_reason": "stop", }, { "ai.jsx.result": "opentel response from OpenAI", @@ -257,6 +260,7 @@ describe('OpenTelemetry', () => { {"hello"} ", + "openai.finish_reason": "stop", }, { "ai.jsx.result": "opentel response from OpenAI", diff --git a/packages/examples/test/lib/openai.tsx b/packages/examples/test/lib/openai.tsx index cd3b0ff9e..0da693e7d 100644 --- a/packages/examples/test/lib/openai.tsx +++ b/packages/examples/test/lib/openai.tsx @@ -21,7 +21,7 @@ describe('OpenAIChatModel', () => { }), ]); - yield { choices: [{ delta: { role: 'assistant', content: 'Hi!' } }] }; + yield { choices: [{ delta: { role: 'assistant', content: 'Hi!' }, finish_reason: 'stop' }] }; }, }, }, From cbfdd1c39d0ebf0d19596d58880505700a444b48 Mon Sep 17 00:00:00 2001 From: Peter Salas Date: Mon, 20 Nov 2023 16:35:59 -0800 Subject: [PATCH 2/4] Strengthen check --- packages/ai-jsx/src/lib/openai.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ai-jsx/src/lib/openai.tsx b/packages/ai-jsx/src/lib/openai.tsx index 1720e67d8..c4bd72f2d 100644 --- a/packages/ai-jsx/src/lib/openai.tsx +++ b/packages/ai-jsx/src/lib/openai.tsx @@ -568,7 +568,7 @@ export async function* OpenAIChatModel( logger.trace({ deltaMessage: next.value }, 'Got delta message'); - if (next.value.choices[0].finish_reason !== null) { + if (next.value.choices[0].finish_reason) { logger.setAttribute('openai.finish_reason', next.value.choices[0].finish_reason); } return next.value.choices[0].delta; From 7743c8fcf2d13c5005b27f3ccada857897ac8b9c Mon Sep 17 00:00:00 2001 From: Peter Salas Date: Mon, 20 Nov 2023 16:51:20 -0800 Subject: [PATCH 3/4] Lift the setAttribute call to work around OpenTelemetry logger using current span --- packages/ai-jsx/src/lib/openai.tsx | 6 +++++- packages/examples/test/core/completion.tsx | 2 -- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/ai-jsx/src/lib/openai.tsx b/packages/ai-jsx/src/lib/openai.tsx index c4bd72f2d..fae5e2063 100644 --- a/packages/ai-jsx/src/lib/openai.tsx +++ b/packages/ai-jsx/src/lib/openai.tsx @@ -546,6 +546,7 @@ export async function* OpenAIChatModel( throw ex; } + let finishReason: string | undefined = undefined; const iterator = chatResponse[Symbol.asyncIterator](); // We have a single response iterator, but we'll wrap tokens _within_ the structure of or @@ -569,7 +570,7 @@ export async function* OpenAIChatModel( logger.trace({ deltaMessage: next.value }, 'Got delta message'); if (next.value.choices[0].finish_reason) { - logger.setAttribute('openai.finish_reason', next.value.choices[0].finish_reason); + finishReason = next.value.choices[0].finish_reason; } return next.value.choices[0].delta; } @@ -672,6 +673,9 @@ export async function* OpenAIChatModel( } // Render the completion conversation to log it. + if (finishReason) { + logger.setAttribute('openai.finish_reason', finishReason); + } await renderToConversation(outputMessages, render, logger, 'completion', tokenCountForConversationMessage); return AI.AppendOnlyStream; } diff --git a/packages/examples/test/core/completion.tsx b/packages/examples/test/core/completion.tsx index 2aa0ff0f0..530e783e6 100644 --- a/packages/examples/test/core/completion.tsx +++ b/packages/examples/test/core/completion.tsx @@ -92,7 +92,6 @@ describe('OpenTelemetry', () => { "ai.jsx.result": "opentel response from OpenAI", "ai.jsx.tag": "Stream", "ai.jsx.tree": ""▮"", - "openai.finish_reason": "stop", }, { "ai.jsx.result": "opentel response from OpenAI", @@ -200,7 +199,6 @@ describe('OpenTelemetry', () => { "ai.jsx.result": "opentel response from OpenAI", "ai.jsx.tag": "Stream", "ai.jsx.tree": ""▮"", - "openai.finish_reason": "stop", }, { "ai.jsx.result": "opentel response from OpenAI", From 35d67a6858f9b15f0c0d0820fd14c133b339bfd7 Mon Sep 17 00:00:00 2001 From: Peter Salas Date: Mon, 20 Nov 2023 17:00:21 -0800 Subject: [PATCH 4/4] Fix lint --- packages/ai-jsx/src/lib/openai.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/ai-jsx/src/lib/openai.tsx b/packages/ai-jsx/src/lib/openai.tsx index fae5e2063..48871a5ca 100644 --- a/packages/ai-jsx/src/lib/openai.tsx +++ b/packages/ai-jsx/src/lib/openai.tsx @@ -672,10 +672,13 @@ export async function* OpenAIChatModel( } } - // Render the completion conversation to log it. + // TS doesn't realize that the advance closure can set `finishReason`. + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (finishReason) { logger.setAttribute('openai.finish_reason', finishReason); } + + // Render the completion conversation to log it. await renderToConversation(outputMessages, render, logger, 'completion', tokenCountForConversationMessage); return AI.AppendOnlyStream; }