Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add openai.finish_reason span attribute for OpenAIChatModel #507

Merged
merged 4 commits into from
Nov 21, 2023
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
2 changes: 1 addition & 1 deletion packages/ai-jsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
11 changes: 11 additions & 0 deletions packages/ai-jsx/src/lib/openai.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <AssistantMessage> or <FunctionCall>
Expand All @@ -567,6 +568,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) {
petersalas marked this conversation as resolved.
Show resolved Hide resolved
finishReason = next.value.choices[0].finish_reason;
}
return next.value.choices[0].delta;
}

Expand Down Expand Up @@ -667,6 +672,12 @@ export async function* OpenAIChatModel(
}
}

// 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;
Expand Down
6 changes: 5 additions & 1 deletion packages/docs/docs/changelog.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 2 additions & 0 deletions packages/examples/test/core/completion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe('OpenTelemetry', () => {
{"hello"}
</UserMessage>
</OpenAIChatModel>",
"openai.finish_reason": "stop",
},
{
"ai.jsx.result": "opentel response from OpenAI",
Expand Down Expand Up @@ -257,6 +258,7 @@ describe('OpenTelemetry', () => {
{"hello"}
</UserMessage>
</OpenAIChatModel>",
"openai.finish_reason": "stop",
},
{
"ai.jsx.result": "opentel response from OpenAI",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/test/lib/openai.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('OpenAIChatModel', () => {
}),
]);

yield { choices: [{ delta: { role: 'assistant', content: 'Hi!' } }] };
yield { choices: [{ delta: { role: 'assistant', content: 'Hi!' }, finish_reason: 'stop' }] };
},
},
},
Expand Down