Skip to content
Draft
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
58 changes: 57 additions & 1 deletion packages/agents-openai/src/openaiChatCompletionsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,14 @@ export class OpenAIChatCompletionsModel implements Model {
async *getStreamedResponse(
request: ModelRequest,
): AsyncIterable<ResponseStreamEvent> {
const span = request.tracing ? createGenerationSpan() : undefined;
const span = request.tracing
? createGenerationSpan({
data: {
model: this.#model,
model_config: request.modelSettings,
},
})
: undefined;
try {
if (span) {
span.start();
Expand Down Expand Up @@ -218,6 +225,55 @@ export class OpenAIChatCompletionsModel implements Model {
event.type === 'response_done' &&
response.usage?.total_tokens === 0
) {
response.choices = event.response.output
.flatMap((o): OpenAI.Chat.Completions.ChatCompletion.Choice[] => {
if (o.type === 'function_call') {
Copy link
Member

Choose a reason for hiding this comment

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

The logic here requires updates when the platform adds new types. We'd like to explore more robust way to handle the conversion here.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I was worried about that too. Do you have any suggestions? This is the first time I've actually looked into this API!

Copy link
Author

Choose a reason for hiding this comment

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

@seratch do you have any preferences on how to handle the conversion here?

return [
{
finish_reason: 'tool_calls',
index: 0,
logprobs: null,
message: {
content: null,
refusal: null,
role: 'assistant',
tool_calls: [
{
type: 'function',
function: {
arguments: o.arguments,
name: o.name,
},
id: o.callId,
},
],
},
},
];
} else if (o.type === 'message') {
return o.content
.filter((c) => c.type === 'output_text')
.map((c) => {
return {
finish_reason: 'stop',
index: 0,
logprobs: null,
message: {
content: c.text,
refusal: null,
role: 'assistant',
},
};
});
} else {
return [];
}
})
.map((choice, index) => ({
...choice,
index,
}));

response.usage = {
prompt_tokens: event.response.usage.inputTokens,
completion_tokens: event.response.usage.outputTokens,
Expand Down