Skip to content

Commit

Permalink
Add openai.finish_reason span attribute for OpenAIChatModel (#507)
Browse files Browse the repository at this point in the history
This adds an attribute (`openai.finish_reason`) that `OpenAIChatModel`
uses to indicate how the stream ended.
  • Loading branch information
petersalas authored Nov 21, 2023
1 parent 7325135 commit 1b14e53
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
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) {
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

4 comments on commit 1b14e53

@vercel
Copy link

@vercel vercel bot commented on 1b14e53 Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-voice – ./packages/voice

ai-jsx-voice-git-main-fixie-ai.vercel.app
ai-jsx-voice-fixie-ai.vercel.app
voice.fixie.ai
ai-jsx-voice.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 1b14e53 Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-docs – ./packages/docs

ai-jsx-docs.vercel.app
ai-jsx-docs-fixie-ai.vercel.app
ai-jsx-docs-git-main-fixie-ai.vercel.app
docs.ai-jsx.com

@vercel
Copy link

@vercel vercel bot commented on 1b14e53 Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-tutorial-nextjs – ./packages/tutorial-nextjs

ai-jsx-tutorial-nextjs-git-main-fixie-ai.vercel.app
ai-jsx-tutorial-nextjs.vercel.app
ai-jsx-tutorial-nextjs-fixie-ai.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 1b14e53 Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-nextjs-demo – ./packages/nextjs-demo

ai-jsx-nextjs-demo.vercel.app
ai-jsx-nextjs-demo-git-main-fixie-ai.vercel.app
ai-jsx-nextjs-demo-fixie-ai.vercel.app

Please sign in to comment.