Skip to content

Commit fe9c9fd

Browse files
authored
Add refusal field to assistant conversations (elastic#243423)
1 parent edc702b commit fe9c9fd

28 files changed

Lines changed: 110 additions & 3 deletions

File tree

oas_docs/output/kibana.serverless.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83925,6 +83925,9 @@ components:
8392583925
reader:
8392683926
$ref: '#/components/schemas/Security_AI_Assistant_API_Reader'
8392783927
description: Message content.
83928+
refusal:
83929+
description: Refusal reason returned by the model when content is filtered.
83930+
type: string
8392883931
role:
8392983932
$ref: '#/components/schemas/Security_AI_Assistant_API_MessageRole'
8393083933
description: Message role.

oas_docs/output/kibana.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94443,6 +94443,9 @@ components:
9444394443
reader:
9444494444
$ref: '#/components/schemas/Security_AI_Assistant_API_Reader'
9444594445
description: Message content.
94446+
refusal:
94447+
description: Refusal reason returned by the model when content is filtered.
94448+
type: string
9444694449
role:
9444794450
$ref: '#/components/schemas/Security_AI_Assistant_API_MessageRole'
9444894451
description: Message role.

x-pack/platform/packages/shared/ai-infra/inference-common/src/chat_complete/api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ export interface ChatCompleteResponse<
197197
* The text content of the LLM response.
198198
*/
199199
content: string;
200+
/**
201+
* Optional refusal reason returned by the model when content is filtered.
202+
*/
203+
refusal?: string;
200204
/**
201205
* The eventual tool calls performed by the LLM.
202206
*/

x-pack/platform/packages/shared/ai-infra/inference-common/src/chat_complete/events.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export type ChatCompletionMessageEvent<TToolOptions extends ToolOptions = ToolOp
3232
* The text content of the LLM response.
3333
*/
3434
content: string;
35+
/**
36+
* Optional refusal reason returned by the model when content is filtered.
37+
*/
38+
refusal?: string;
3539
/**
3640
* Optional deanonymized input messages metadata
3741
*/
@@ -84,6 +88,10 @@ export type ChatCompletionChunkEvent = InferenceTaskEventBase<
8488
* The content chunk
8589
*/
8690
content: string;
91+
/**
92+
* Optional refusal reason chunk.
93+
*/
94+
refusal?: string;
8795
/**
8896
* The tool call chunks
8997
*/

x-pack/platform/packages/shared/ai-infra/inference-common/src/chat_complete/messages.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ export type AssistantMessage<TToolCalls extends ToolCall[] | undefined = ToolCal
5757
* Can be null if the LLM called a tool.
5858
*/
5959
content: string | null;
60+
/**
61+
* Optional refusal reason returned by the model when content is filtered.
62+
*/
63+
refusal?: string | null;
6064
// make sure `toolCalls` inherits the optionality from `TToolCalls`
6165
} & (TToolCalls extends ToolCall[]
6266
? {

x-pack/platform/packages/shared/ai-infra/inference-langchain/src/chat_model/from_inference/chunks.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ export const completionChunkToLangchain = (chunk: ChatCompletionChunkEvent): AIM
2626
};
2727
});
2828

29+
const additionalKwargs = chunk.refusal ? { refusal: chunk.refusal } : {};
30+
2931
return new AIMessageChunk({
3032
content: chunk.content,
3133
tool_call_chunks: toolCallChunks,
32-
additional_kwargs: {},
34+
additional_kwargs: additionalKwargs,
3335
response_metadata: {},
3436
});
3537
};

x-pack/platform/packages/shared/ai-infra/inference-langchain/src/chat_model/from_inference/messages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import type { ChatCompleteResponse } from '@kbn/inference-common';
99
import { AIMessage } from '@langchain/core/messages';
1010

1111
export const responseToLangchainMessage = (response: ChatCompleteResponse): AIMessage => {
12+
const additionalKwargs = response.refusal ? { refusal: response.refusal } : undefined;
1213
return new AIMessage({
1314
content: response.content,
15+
...(additionalKwargs ? { additional_kwargs: additionalKwargs } : {}),
1416
tool_calls: response.toolCalls.map((toolCall) => {
1517
return {
1618
id: toolCall.toolCallId,

x-pack/platform/packages/shared/kbn-elastic-assistant-common/docs/openapi/ess/elastic_assistant_api_2023_10_31.bundled.schema.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,6 +2925,9 @@ components:
29252925
reader:
29262926
$ref: '#/components/schemas/Reader'
29272927
description: Message content.
2928+
refusal:
2929+
description: Refusal reason returned by the model when content is filtered.
2930+
type: string
29282931
role:
29292932
$ref: '#/components/schemas/MessageRole'
29302933
description: Message role.

x-pack/platform/packages/shared/kbn-elastic-assistant-common/docs/openapi/serverless/elastic_assistant_api_2023_10_31.bundled.schema.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,6 +2925,9 @@ components:
29252925
reader:
29262926
$ref: '#/components/schemas/Reader'
29272927
description: Message content.
2928+
refusal:
2929+
description: Refusal reason returned by the model when content is filtered.
2930+
type: string
29282931
role:
29292932
$ref: '#/components/schemas/MessageRole'
29302933
description: Message role.

x-pack/platform/packages/shared/kbn-elastic-assistant-common/impl/schemas/conversations/common_attributes.gen.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@ export const Message = z.object({
421421
* Message content.
422422
*/
423423
content: z.string(),
424+
/**
425+
* Refusal reason returned by the model when content is filtered.
426+
*/
427+
refusal: z.string().optional(),
424428
/**
425429
* Message content.
426430
*/

0 commit comments

Comments
 (0)