Skip to content

v5 chore: fully remove deprecated function call types from the tool runner #1431

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

Closed
Closed
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
50 changes: 19 additions & 31 deletions src/lib/AbstractChatCompletionRunner.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { type CompletionUsage } from '../resources/completions';
import {
type ChatCompletion,
type ChatCompletionMessage,
type ChatCompletionMessageParam,
type ChatCompletionCreateParams,
type ChatCompletionTool,
import type { CompletionUsage } from '../resources/completions';
import type {
ChatCompletion,
ChatCompletionMessage,
ChatCompletionMessageParam,
ChatCompletionCreateParams,
ChatCompletionTool,
ChatCompletionMessageToolCall,
} from '../resources/chat/completions';
import { OpenAIError } from '../error';
import {
@@ -13,14 +14,14 @@ import {
type BaseFunctionsArgs,
RunnableToolFunction,
} from './RunnableFunction';
import { ChatCompletionToolRunnerParams } from './ChatCompletionRunner';
import { ChatCompletionStreamingToolRunnerParams } from './ChatCompletionStreamingRunner';
import { isAssistantMessage, isFunctionMessage, isToolMessage } from './chatCompletionUtils';
import type { ChatCompletionToolRunnerParams } from './ChatCompletionRunner';
import type { ChatCompletionStreamingToolRunnerParams } from './ChatCompletionStreamingRunner';
import { isAssistantMessage, isToolMessage } from './chatCompletionUtils';
import { BaseEvents, EventStream } from './EventStream';
import { ParsedChatCompletion } from '../resources/beta/chat/completions';
import type { ParsedChatCompletion } from '../resources/beta/chat/completions';
import OpenAI from '../index';
import { isAutoParsableTool, parseChatCompletion } from '../lib/parser';
import { RequestOptions } from '../internal/request-options';
import type { RequestOptions } from '../internal/request-options';
Copy link
Collaborator

Choose a reason for hiding this comment

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

aside: can / should we update eslint to automatically add these type annotations?


const DEFAULT_MAX_CHAT_COMPLETIONS = 10;
export interface RunnerOptions extends RequestOptions {
@@ -57,11 +58,9 @@ export class AbstractChatCompletionRunner<

if (emit) {
this._emit('message', message);
if ((isFunctionMessage(message) || isToolMessage(message)) && message.content) {
if (isToolMessage(message) && message.content) {
// Note, this assumes that {role: 'tool', content: …} is always the result of a call of tool of type=function.
this._emit('functionCallResult', message.content as string);
} else if (isAssistantMessage(message) && message.function_call) {
this._emit('functionCall', message.function_call);
} else if (isAssistantMessage(message) && message.tool_calls) {
for (const tool_call of message.tool_calls) {
if (tool_call.type === 'function') {
@@ -101,17 +100,12 @@ export class AbstractChatCompletionRunner<
while (i-- > 0) {
const message = this.messages[i];
if (isAssistantMessage(message)) {
const { function_call, ...rest } = message;

// TODO: support audio here
const ret: Omit<ChatCompletionMessage, 'audio'> = {
...rest,
...message,
content: (message as ChatCompletionMessage).content ?? null,
refusal: (message as ChatCompletionMessage).refusal ?? null,
};
if (function_call) {
ret.function_call = function_call;
}
return ret;
}
}
@@ -127,12 +121,9 @@ export class AbstractChatCompletionRunner<
return this.#getFinalMessage();
}

#getFinalFunctionCall(): ChatCompletionMessage.FunctionCall | undefined {
#getFinalFunctionCall(): ChatCompletionMessageToolCall.Function | undefined {
for (let i = this.messages.length - 1; i >= 0; i--) {
const message = this.messages[i];
if (isAssistantMessage(message) && message?.function_call) {
return message.function_call;
}
if (isAssistantMessage(message) && message?.tool_calls?.length) {
return message.tool_calls.at(-1)?.function;
}
@@ -145,17 +136,14 @@ export class AbstractChatCompletionRunner<
* @returns a promise that resolves with the content of the final FunctionCall, or rejects
* if an error occurred or the stream ended prematurely without producing a ChatCompletionMessage.
*/
async finalFunctionCall(): Promise<ChatCompletionMessage.FunctionCall | undefined> {
async finalFunctionCall(): Promise<ChatCompletionMessageToolCall.Function | undefined> {
await this.done();
return this.#getFinalFunctionCall();
}

#getFinalFunctionCallResult(): string | undefined {
for (let i = this.messages.length - 1; i >= 0; i--) {
const message = this.messages[i];
if (isFunctionMessage(message) && message.content != null) {
return message.content;
}
if (
isToolMessage(message) &&
message.content != null &&
@@ -402,13 +390,13 @@ export class AbstractChatCompletionRunner<
}

export interface AbstractChatCompletionRunnerEvents extends BaseEvents {
functionCall: (functionCall: ChatCompletionMessage.FunctionCall) => void;
functionCall: (functionCall: ChatCompletionMessageToolCall.Function) => void;
message: (message: ChatCompletionMessageParam) => void;
chatCompletion: (completion: ChatCompletion) => void;
finalContent: (contentSnapshot: string) => void;
finalMessage: (message: ChatCompletionMessageParam) => void;
finalChatCompletion: (completion: ChatCompletion) => void;
finalFunctionCall: (functionCall: ChatCompletionMessage.FunctionCall) => void;
finalFunctionCall: (functionCall: ChatCompletionMessageToolCall.Function) => void;
functionCallResult: (content: string) => void;
finalFunctionCallResult: (content: string) => void;
totalUsage: (usage: CompletionUsage) => void;
7 changes: 0 additions & 7 deletions src/lib/chatCompletionUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
type ChatCompletionAssistantMessageParam,
type ChatCompletionFunctionMessageParam,
type ChatCompletionMessageParam,
type ChatCompletionToolMessageParam,
} from '../resources';
@@ -11,12 +10,6 @@ export const isAssistantMessage = (
return message?.role === 'assistant';
};

export const isFunctionMessage = (
message: ChatCompletionMessageParam | null | undefined,
): message is ChatCompletionFunctionMessageParam => {
return message?.role === 'function';
};

export const isToolMessage = (
message: ChatCompletionMessageParam | null | undefined,
): message is ChatCompletionToolMessageParam => {
8 changes: 4 additions & 4 deletions tests/lib/ChatCompletionRunFunctions.test.ts
Original file line number Diff line number Diff line change
@@ -130,12 +130,12 @@ class RunnerListener {
readonly contents: string[] = [];
readonly messages: ChatCompletionMessageParam[] = [];
readonly chatCompletions: OpenAI.Chat.ChatCompletion[] = [];
readonly functionCalls: OpenAI.Chat.ChatCompletionMessage.FunctionCall[] = [];
readonly functionCalls: OpenAI.Chat.ChatCompletionMessageToolCall.Function[] = [];
readonly functionCallResults: string[] = [];
finalContent: string | null = null;
finalMessage: ChatCompletionMessageParam | undefined;
finalChatCompletion: OpenAI.Chat.ChatCompletion | undefined;
finalFunctionCall: OpenAI.Chat.ChatCompletionMessage.FunctionCall | undefined;
finalFunctionCall: OpenAI.Chat.ChatCompletionMessageToolCall.Function | undefined;
finalFunctionCallResult: string | undefined;
totalUsage: OpenAI.CompletionUsage | undefined;
error: OpenAIError | undefined;
@@ -247,13 +247,13 @@ class StreamingRunnerListener {
readonly eventContents: [string, string][] = [];
readonly eventMessages: ChatCompletionMessageParam[] = [];
readonly eventChatCompletions: OpenAI.Chat.ChatCompletion[] = [];
readonly eventFunctionCalls: OpenAI.Chat.ChatCompletionMessage.FunctionCall[] = [];
readonly eventFunctionCalls: OpenAI.Chat.ChatCompletionMessageToolCall.Function[] = [];
readonly eventFunctionCallResults: string[] = [];

finalContent: string | null = null;
finalMessage: ChatCompletionMessageParam | undefined;
finalChatCompletion: OpenAI.Chat.ChatCompletion | undefined;
finalFunctionCall: OpenAI.Chat.ChatCompletionMessage.FunctionCall | undefined;
finalFunctionCall: OpenAI.Chat.ChatCompletionMessageToolCall.Function | undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm maybe we should also rename these to tool calls?

cc @kwhinnery-openai do you have any thoughts here?

finalFunctionCallResult: string | undefined;
error: OpenAIError | undefined;
gotConnect = false;
2 changes: 1 addition & 1 deletion tests/lib/azure.test.ts
Original file line number Diff line number Diff line change
@@ -134,7 +134,7 @@ describe('instantiate azure client', () => {

const spy = jest.spyOn(client, 'request');

await expect(client.get('/foo', { signal: controller.signal })).rejects.toThrowError(APIUserAbortError);
await expect(client.get('/foo', { signal: controller.signal })).rejects.toThrow(APIUserAbortError);
Copy link
Collaborator

Choose a reason for hiding this comment

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

question: why change this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was also flagged as deprecated and azure is custom code. We should update codegen with this change too.

expect(spy).toHaveBeenCalledTimes(1);
});