Add reasoning_effort for o1 and o3 (#1628)

This commit is contained in:
Peter Goldstein
2025-02-07 00:01:48 -05:00
committed by GitHub
parent e3f1b85846
commit 7265f74c24
2 changed files with 17 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@llamaindex/openai": patch
---
Add reasoning_effort for o1 and o3 models
+12
View File
@@ -140,6 +140,12 @@ export function isFunctionCallingModel(llm: LLM): llm is OpenAI {
return isChatModel && !isOld && !isO1;
}
export function isReasoningModel(model: ChatModel | string): boolean {
const isO1 = model.startsWith("o1");
const isO3 = model.startsWith("o3");
return isO1 || isO3;
}
export function isTemperatureSupported(model: ChatModel | string): boolean {
return !model.startsWith("o3");
}
@@ -152,6 +158,7 @@ export type OpenAIAdditionalChatOptions = Omit<
| "messages"
| "model"
| "temperature"
| "reasoning_effort"
| "top_p"
| "stream"
| "tools"
@@ -166,6 +173,7 @@ export class OpenAI extends ToolCallLLM<OpenAIAdditionalChatOptions> {
// string & {} is a hack to allow any string, but still give autocomplete
| (string & {});
temperature: number;
reasoningEffort?: "low" | "medium" | "high" | undefined;
topP: number;
maxTokens?: number | undefined;
additionalChatOptions?: OpenAIAdditionalChatOptions | undefined;
@@ -197,6 +205,9 @@ export class OpenAI extends ToolCallLLM<OpenAIAdditionalChatOptions> {
super();
this.model = init?.model ?? "gpt-4o";
this.temperature = init?.temperature ?? 0.1;
this.reasoningEffort = isReasoningModel(this.model)
? init?.reasoningEffort
: undefined;
this.topP = init?.topP ?? 1;
this.maxTokens = init?.maxTokens ?? undefined;
@@ -354,6 +365,7 @@ export class OpenAI extends ToolCallLLM<OpenAIAdditionalChatOptions> {
const baseRequestParams = <OpenAILLM.Chat.ChatCompletionCreateParams>{
model: this.model,
temperature: isTemperatureSupported(this.model) ? this.temperature : null,
reasoning_effort: this.reasoningEffort,
max_tokens: this.maxTokens,
tools: tools?.map(OpenAI.toTool),
messages: OpenAI.toOpenAIMessage(messages),