Skip to content

Commit 7040b84

Browse files
pgayvalletppisljar
andauthored
[Agent Builder] built-in agents can clear default system instructions (elastic#244692)
## Summary Fix elastic/search-team#11964 - Add a new `replace_default_instructions` option to agent configuration, only usable by built-in agents. - When set to true, we will remove (most of) our default system prompt and use the provided custom instructions as a replacement instead of an extension - Also do some cleanup in the agent prompts by extracting some things to utils --------- Co-authored-by: Peter Pisljar <peter.pisljar@gmail.com>
1 parent fc2ecd1 commit 7040b84

14 files changed

Lines changed: 477 additions & 346 deletions

File tree

x-pack/platform/packages/shared/onechat/onechat-common/agents/definition.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ export interface AgentConfiguration {
7575
* For custom per-step instructions, use the `research` and `answer` configuration fields instead.
7676
*/
7777
instructions?: string;
78+
/**
79+
* If set to true, the custom instructions will be used as a replacement for the system prompt instead of extending it.
80+
*
81+
* This will impact both the research and answer prompts. For custom per-step instructions, use the `research` and `answer` configuration fields instead.
82+
*/
83+
replace_default_instructions?: boolean;
84+
7885
/**
7986
* List of tools exposed to the agent
8087
*/
@@ -96,13 +103,21 @@ export interface AgentResearchStepConfiguration {
96103
* Custom instruction for the agent's research step.
97104
*/
98105
instructions?: string;
106+
/**
107+
* If set to true, the custom instructions will be used as a replacement for the system prompt instead of extending it.
108+
*/
109+
replace_default_instructions?: boolean;
99110
}
100111

101112
export interface AgentAnswerStepConfiguration {
102113
/**
103114
* Custom instruction for the agent's answer step.
104115
*/
105116
instructions?: string;
117+
/**
118+
* If set to true, the custom instructions will be used as a replacement for the system prompt instead of extending it.
119+
*/
120+
replace_default_instructions?: boolean;
106121
}
107122

108123
/**

x-pack/platform/plugins/shared/onechat/server/services/agents/modes/default/answer_agent_structured.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import type { ResolvedAgentCapabilities } from '@kbn/onechat-common';
1111
import type { AgentEventEmitter } from '@kbn/onechat-server';
1212
import { createReasoningEvent } from '@kbn/onechat-genai-utils/langchain';
1313
import type { Logger } from '@kbn/logging';
14-
import { errorAction } from './actions';
14+
import type { ProcessedAttachmentType } from '../utils/prepare_conversation';
1515
import type { ResolvedConfiguration } from '../types';
1616
import { convertError, isRecoverableError } from '../utils/errors';
17+
import { errorAction } from './actions';
1718
import { getStructuredAnswerPrompt } from './prompts';
1819
import { getRandomAnsweringMessage } from './i18n';
1920
import { tags } from './constants';
@@ -38,13 +39,15 @@ export const createAnswerAgentStructured = ({
3839
capabilities,
3940
events,
4041
outputSchema,
42+
attachmentTypes,
4143
}: {
4244
chatModel: InferenceChatModel;
4345
configuration: ResolvedConfiguration;
4446
capabilities: ResolvedAgentCapabilities;
4547
events: AgentEventEmitter;
4648
outputSchema?: Record<string, unknown>;
4749
logger: Logger;
50+
attachmentTypes: ProcessedAttachmentType[];
4851
}) => {
4952
return async (state: StateType) => {
5053
if (state.answerActions.length === 0 && state.errorCount === 0) {
@@ -82,6 +85,7 @@ export const createAnswerAgentStructured = ({
8285
initialMessages: state.initialMessages,
8386
actions: state.mainActions,
8487
answerActions: state.answerActions,
88+
attachmentTypes,
8589
});
8690

8791
const response = await structuredModel.invoke(prompt);

x-pack/platform/plugins/shared/onechat/server/services/agents/modes/default/graph.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type { AgentEventEmitter } from '@kbn/onechat-server';
1818
import { createReasoningEvent, createToolCallMessage } from '@kbn/onechat-genai-utils/langchain';
1919
import type { ResolvedConfiguration } from '../types';
2020
import { convertError, isRecoverableError } from '../utils/errors';
21-
import { getActPrompt, getAnswerPrompt } from './prompts';
21+
import { getResearchAgentPrompt, getAnswerAgentPrompt } from './prompts';
2222
import { getRandomAnsweringMessage, getRandomThinkingMessage } from './i18n';
2323
import { steps, tags } from './constants';
2424
import type { StateType } from './state';
@@ -76,8 +76,9 @@ export const createAgentGraph = ({
7676
}
7777
try {
7878
const response = await researcherModel.invoke(
79-
getActPrompt({
79+
getResearchAgentPrompt({
8080
customInstructions: configuration.research.instructions,
81+
clearSystemMessage: configuration.research.replace_default_instructions,
8182
capabilities,
8283
initialMessages: state.initialMessages,
8384
actions: state.mainActions,
@@ -168,12 +169,14 @@ export const createAgentGraph = ({
168169
}
169170
try {
170171
const response = await answeringModel.invoke(
171-
getAnswerPrompt({
172+
getAnswerAgentPrompt({
172173
customInstructions: configuration.answer.instructions,
174+
clearSystemMessage: configuration.answer.replace_default_instructions,
173175
capabilities,
174176
initialMessages: state.initialMessages,
175177
actions: state.mainActions,
176178
answerActions: state.answerActions,
179+
attachmentTypes: processedConversation.attachmentTypes,
177180
})
178181
);
179182

@@ -202,6 +205,7 @@ export const createAgentGraph = ({
202205
capabilities,
203206
events,
204207
outputSchema,
208+
attachmentTypes: processedConversation.attachmentTypes,
205209
logger,
206210
});
207211

0 commit comments

Comments
 (0)