Skip to content

Commit 84e552b

Browse files
[8.19] [AI Assistant] Fix some OpenAI models not accepting temperature for Inference service (#218887) (#219962)
# Backport This will backport the following commits from `main` to `8.19`: - [[AI Assistant] Fix some OpenAI models not accepting temperature for Inference service (#218887)](#218887) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Quynh Nguyen (Quinn)","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-05-01T16:23:27Z","message":"[AI Assistant] Fix some OpenAI models not accepting temperature for Inference service (#218887)\n\n## Summary\n\nThis PR fixes #213652 by adding\nlogic to exclude the `temperature` param from the body request for those\nmodels.\n\n\n**Observability AI Assistant**\n\n**Before**\n\n<img width=\"955\" alt=\"Screenshot 2025-04-22 at 16 05 28\"\nsrc=\"https://github.com/user-attachments/assets/496e41ac-926e-41c2-814a-af12ddc17e95\"\n/>\n\n**After**\n<img width=\"955\" alt=\"Screenshot 2025-04-22 at 16 03 29\"\nsrc=\"https://github.com/user-attachments/assets/1d3ab290-2101-40a8-8f6b-59064e772291\"\n/>\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [ ] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n---------\n\nCo-authored-by: Elastic Machine <[email protected]>","sha":"7aabcc6f5ecaeca63e1df4b6cd0940414f6f37bb","branchLabelMapping":{"^v9.1.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:enhancement",":ml","backport:version","Team:AI Infra","v9.1.0","v8.19.0"],"title":"[Inference] Fix some OpenAI models not accepting temperature for Inference service","number":218887,"url":"https://github.com/elastic/kibana/pull/218887","mergeCommit":{"message":"[AI Assistant] Fix some OpenAI models not accepting temperature for Inference service (#218887)\n\n## Summary\n\nThis PR fixes #213652 by adding\nlogic to exclude the `temperature` param from the body request for those\nmodels.\n\n\n**Observability AI Assistant**\n\n**Before**\n\n<img width=\"955\" alt=\"Screenshot 2025-04-22 at 16 05 28\"\nsrc=\"https://github.com/user-attachments/assets/496e41ac-926e-41c2-814a-af12ddc17e95\"\n/>\n\n**After**\n<img width=\"955\" alt=\"Screenshot 2025-04-22 at 16 03 29\"\nsrc=\"https://github.com/user-attachments/assets/1d3ab290-2101-40a8-8f6b-59064e772291\"\n/>\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [ ] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n---------\n\nCo-authored-by: Elastic Machine <[email protected]>","sha":"7aabcc6f5ecaeca63e1df4b6cd0940414f6f37bb"}},"sourceBranch":"main","suggestedTargetBranches":["8.19"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/218887","number":218887,"mergeCommit":{"message":"[AI Assistant] Fix some OpenAI models not accepting temperature for Inference service (#218887)\n\n## Summary\n\nThis PR fixes #213652 by adding\nlogic to exclude the `temperature` param from the body request for those\nmodels.\n\n\n**Observability AI Assistant**\n\n**Before**\n\n<img width=\"955\" alt=\"Screenshot 2025-04-22 at 16 05 28\"\nsrc=\"https://github.com/user-attachments/assets/496e41ac-926e-41c2-814a-af12ddc17e95\"\n/>\n\n**After**\n<img width=\"955\" alt=\"Screenshot 2025-04-22 at 16 03 29\"\nsrc=\"https://github.com/user-attachments/assets/1d3ab290-2101-40a8-8f6b-59064e772291\"\n/>\n\n\n### Checklist\n\nCheck the PR satisfies following conditions. \n\nReviewers should verify this PR satisfies this list as well.\n\n- [ ] Any text added follows [EUI's writing\nguidelines](https://elastic.github.io/eui/#/guidelines/writing), uses\nsentence case text and includes [i18n\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\n- [ ]\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\nwas added for features that require explanation or tutorials\n- [ ] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n- [ ] If a plugin configuration key changed, check if it needs to be\nallowlisted in the cloud and added to the [docker\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\n- [ ] This was checked for breaking HTTP API changes, and any breaking\nchanges have been approved by the breaking-change committee. The\n`release_note:breaking` label should be applied in these situations.\n- [ ] [Flaky Test\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was\nused on any tests changed\n- [ ] The PR description includes the appropriate Release Notes section,\nand the correct `release_note:*` label is applied per the\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\n\n### Identify risks\n\nDoes this PR introduce any risks? For example, consider risks like hard\nto test bugs, performance regression, potential of data loss.\n\nDescribe the risk, its severity, and mitigation for each identified\nrisk. Invite stakeholders and evaluate how to proceed before merging.\n\n- [ ] [See some risk\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\n- [ ] ...\n\n---------\n\nCo-authored-by: Elastic Machine <[email protected]>","sha":"7aabcc6f5ecaeca63e1df4b6cd0940414f6f37bb"}},{"branch":"8.19","label":"v8.19.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Elastic Machine <[email protected]>
1 parent 33d5c8c commit 84e552b

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
lines changed

x-pack/platform/plugins/shared/inference/server/chat_complete/adapters/inference/create_openai_request.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { OpenAIRequest } from '../openai/types';
1010
import { messagesToOpenAI, toolChoiceToOpenAI, toolsToOpenAI } from '../openai';
1111
import type { CreateOpenAIRequestOptions } from './types';
1212
import { applyProviderTransforms } from './providers';
13+
import { getTemperatureIfValid } from '../../utils/get_temperature';
1314

1415
export const createRequest = (options: CreateOpenAIRequestOptions): OpenAIRequest => {
1516
const {
@@ -31,13 +32,13 @@ export const createRequest = (options: CreateOpenAIRequestOptions): OpenAIReques
3132
tools,
3233
});
3334
request = {
34-
temperature,
35+
...getTemperatureIfValid(temperature, { connector: options.connector, modelName }),
3536
model: modelName,
3637
messages: messagesToOpenAI({ system: wrapped.system, messages: wrapped.messages }),
3738
};
3839
} else {
3940
request = {
40-
temperature,
41+
...getTemperatureIfValid(temperature, { connector: options.connector, modelName }),
4142
model: modelName,
4243
messages: messagesToOpenAI({ system, messages }),
4344
tool_choice: toolChoiceToOpenAI(toolChoice),

x-pack/platform/plugins/shared/inference/server/chat_complete/adapters/openai/openai_adapter.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { OpenAIRequest } from './types';
1717
import { messagesToOpenAI, toolsToOpenAI, toolChoiceToOpenAI } from './to_openai';
1818
import { processOpenAIStream } from './process_openai_stream';
1919
import { emitTokenCountEstimateIfMissing } from './emit_token_count_if_missing';
20+
import { getTemperatureIfValid } from '../../utils/get_temperature';
2021

2122
export const openAIAdapter: InferenceConnectorAdapter = {
2223
chatComplete: ({
@@ -27,17 +28,19 @@ export const openAIAdapter: InferenceConnectorAdapter = {
2728
tools,
2829
temperature = 0,
2930
functionCalling = 'auto',
30-
modelName,
31+
modelName: modelName,
3132
logger,
3233
abortSignal,
3334
metadata,
3435
}) => {
36+
const connector = executor.getConnector();
3537
const useSimulatedFunctionCalling =
3638
functionCalling === 'auto'
3739
? !isNativeFunctionCallingSupported(executor.getConnector())
3840
: functionCalling === 'simulated';
3941

4042
let request: OpenAIRequest;
43+
4144
if (useSimulatedFunctionCalling) {
4245
const wrapped = wrapWithSimulatedFunctionCalling({
4346
system,
@@ -47,14 +50,14 @@ export const openAIAdapter: InferenceConnectorAdapter = {
4750
});
4851
request = {
4952
stream: true,
50-
temperature,
53+
...getTemperatureIfValid(temperature, { connector, modelName }),
5154
model: modelName,
5255
messages: messagesToOpenAI({ system: wrapped.system, messages: wrapped.messages }),
5356
};
5457
} else {
5558
request = {
5659
stream: true,
57-
temperature,
60+
...getTemperatureIfValid(temperature, { connector, modelName }),
5861
model: modelName,
5962
messages: messagesToOpenAI({ system, messages }),
6063
tool_choice: toolChoiceToOpenAI(toolChoice),
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
import { getTemperatureIfValid } from './get_temperature';
8+
import { InferenceConnector, InferenceConnectorType } from '@kbn/inference-common';
9+
10+
const OPENAI_CONNECTOR = { type: InferenceConnectorType.OpenAI } as InferenceConnector;
11+
const GEMINI_CONNECTOR = { type: InferenceConnectorType.Gemini } as InferenceConnector;
12+
describe('getTemperatureIfValid', () => {
13+
it('returns an empty object if temperature is undefined', () => {
14+
expect(
15+
getTemperatureIfValid(undefined, { connector: OPENAI_CONNECTOR, modelName: 'gpt-3.5-turbo' })
16+
).toEqual({});
17+
});
18+
19+
it('returns an object with temperature if OpenAI model accepts', () => {
20+
expect(
21+
getTemperatureIfValid(0.7, { connector: OPENAI_CONNECTOR, modelName: 'gpt-3.5-turbo' })
22+
).toEqual({
23+
temperature: 0.7,
24+
});
25+
expect(
26+
getTemperatureIfValid(0.7, {
27+
connector: OPENAI_CONNECTOR,
28+
modelName: 'gpt-fake-o1',
29+
})
30+
).toEqual({
31+
temperature: 0.7,
32+
});
33+
});
34+
35+
it('returns an object with temperature if not OpenAI connector', () => {
36+
expect(
37+
getTemperatureIfValid(0.7, {
38+
connector: GEMINI_CONNECTOR,
39+
modelName: 'gemma',
40+
})
41+
).toEqual({
42+
temperature: 0.7,
43+
});
44+
});
45+
46+
it("returns an empty object for OpenAI models that don't support temperature", () => {
47+
['o1', 'o1-pro', 'o3', 'o1-mini', 'o3-mini'].forEach((model) => {
48+
expect(getTemperatureIfValid(0.7, { connector: OPENAI_CONNECTOR, modelName: model })).toEqual(
49+
{}
50+
);
51+
});
52+
});
53+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
import { InferenceConnector, InferenceConnectorType } from '@kbn/inference-common';
8+
9+
const OPENAI_MODELS_WITHOUT_TEMPERATURE = ['o1', 'o3'];
10+
11+
export const getTemperatureIfValid = (
12+
temperature?: number,
13+
{ connector, modelName }: { connector?: InferenceConnector; modelName?: string } = {}
14+
) => {
15+
if (temperature === undefined) return {};
16+
17+
const model =
18+
modelName ?? connector?.config?.providerConfig?.model_id ?? connector?.config?.defaultModel;
19+
20+
if (
21+
(connector?.type === InferenceConnectorType.OpenAI ||
22+
connector?.type === InferenceConnectorType.Inference) &&
23+
model
24+
) {
25+
const normalizedModelName = model.toLowerCase();
26+
const shouldExcludeTemperature = OPENAI_MODELS_WITHOUT_TEMPERATURE.some((m) =>
27+
normalizedModelName.startsWith(m)
28+
);
29+
return shouldExcludeTemperature ? {} : { temperature };
30+
}
31+
return { temperature };
32+
};

0 commit comments

Comments
 (0)