mirror of
https://github.com/logancyang/obsidian-copilot.git
synced 2026-07-22 07:50:24 +00:00
Ignore system prompt for copilot commands (#241)
This commit is contained in:
parent
5dbedc9d58
commit
0618fbaadc
3 changed files with 39 additions and 11 deletions
|
|
@ -143,7 +143,7 @@ class AIState {
|
|||
}
|
||||
|
||||
private initChatPrompt(): void {
|
||||
this.chatPrompt = ChatPromptTemplate.fromPromptMessages([
|
||||
this.chatPrompt = ChatPromptTemplate.fromMessages([
|
||||
SystemMessagePromptTemplate.fromTemplate(
|
||||
this.langChainParams.systemMessage
|
||||
),
|
||||
|
|
@ -531,7 +531,6 @@ class AIState {
|
|||
}
|
||||
|
||||
this.langChainParams.chainType = ChainType.LLM_CHAIN;
|
||||
console.log('Set chain:', ChainType.LLM_CHAIN);
|
||||
break;
|
||||
}
|
||||
case ChainType.RETRIEVAL_QA_CHAIN: {
|
||||
|
|
@ -666,8 +665,11 @@ class AIState {
|
|||
abortController: AbortController,
|
||||
updateCurrentAiMessage: (message: string) => void,
|
||||
addMessage: (message: ChatMessage) => void,
|
||||
debug = false,
|
||||
options: { debug?: boolean, ignoreSystemMessage?: boolean } = {},
|
||||
) {
|
||||
const { debug = false, ignoreSystemMessage = false } = options;
|
||||
|
||||
// Check if chat model is initialized
|
||||
if (!this.validateChatModel(AIState.chatModel)) {
|
||||
const errorMsg = 'Chat model is not initialized properly, check your API key in Copilot setting and make sure you have API access.';
|
||||
new Notice(errorMsg);
|
||||
|
|
@ -691,6 +693,24 @@ class AIState {
|
|||
chainType,
|
||||
} = this.langChainParams;
|
||||
|
||||
const systemPrompt = ignoreSystemMessage ? '' : systemMessage;
|
||||
// Whether to ignore system prompt (for commands)
|
||||
if (ignoreSystemMessage) {
|
||||
const effectivePrompt = ignoreSystemMessage
|
||||
? ChatPromptTemplate.fromMessages([
|
||||
new MessagesPlaceholder("history"),
|
||||
HumanMessagePromptTemplate.fromTemplate("{input}"),
|
||||
])
|
||||
: this.chatPrompt;
|
||||
|
||||
this.setChain(chainType, {
|
||||
...this.langChainParams.options,
|
||||
prompt: effectivePrompt,
|
||||
});
|
||||
} else {
|
||||
this.setChain(chainType, this.langChainParams.options);
|
||||
}
|
||||
|
||||
let fullAIResponse = '';
|
||||
const chain = AIState.chain as any;
|
||||
try {
|
||||
|
|
@ -704,7 +724,7 @@ class AIState {
|
|||
+ `chain type: ${chainType}\n`
|
||||
+ `temperature: ${temperature}\n`
|
||||
+ `maxTokens: ${maxTokens}\n`
|
||||
+ `system message: ${systemMessage}\n`
|
||||
+ `system prompt: ${systemPrompt}\n`
|
||||
+ `chat context turns: ${chatContextTurns}\n`,
|
||||
);
|
||||
console.log('chain:', chain);
|
||||
|
|
@ -733,7 +753,7 @@ class AIState {
|
|||
+ `chain type: ${chainType}\n`
|
||||
+ `temperature: ${temperature}\n`
|
||||
+ `maxTokens: ${maxTokens}\n`
|
||||
+ `system message: ${systemMessage}\n`
|
||||
+ `system prompt: ${systemPrompt}\n`
|
||||
+ `chat context turns: ${chatContextTurns}\n`,
|
||||
);
|
||||
console.log('chain:', chain);
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ import React, {
|
|||
interface CreateEffectOptions {
|
||||
custom_temperature?: number;
|
||||
isVisible?: boolean;
|
||||
ignoreSystemMessage?: boolean;
|
||||
}
|
||||
|
||||
interface ChatProps {
|
||||
|
|
@ -93,7 +94,7 @@ const Chat: React.FC<ChatProps> = ({
|
|||
addMessage,
|
||||
setCurrentAiMessage,
|
||||
setAbortController,
|
||||
debug,
|
||||
{ debug },
|
||||
);
|
||||
};
|
||||
|
||||
|
|
@ -196,14 +197,18 @@ const Chat: React.FC<ChatProps> = ({
|
|||
};
|
||||
}, []);
|
||||
|
||||
// Create an effect for each event type (command)
|
||||
// Create an effect for each event type (Copilot command on selected text)
|
||||
const createEffect = (
|
||||
eventType: string,
|
||||
promptFn: (selectedText: string, eventSubtype?: string) => string,
|
||||
options: CreateEffectOptions = {},
|
||||
) => {
|
||||
return () => {
|
||||
const { custom_temperature, isVisible = false } = options;
|
||||
const {
|
||||
custom_temperature,
|
||||
isVisible = false,
|
||||
ignoreSystemMessage = true, // Ignore system message by default for commands
|
||||
} = options;
|
||||
const handleSelection = async (selectedText: string, eventSubtype?: string) => {
|
||||
// Create a user message with the selected text
|
||||
const promptMessage: ChatMessage = {
|
||||
|
|
@ -229,7 +234,10 @@ const Chat: React.FC<ChatProps> = ({
|
|||
addMessage,
|
||||
setCurrentAiMessage,
|
||||
setAbortController,
|
||||
debug,
|
||||
{
|
||||
debug,
|
||||
ignoreSystemMessage,
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ export const getAIResponse = async (
|
|||
addMessage: (message: ChatMessage) => void,
|
||||
updateCurrentAiMessage: (message: string) => void,
|
||||
updateShouldAbort: (abortController: AbortController | null) => void,
|
||||
debug = false,
|
||||
options: { debug?: boolean, ignoreSystemMessage?: boolean } = {},
|
||||
) => {
|
||||
const abortController = new AbortController();
|
||||
updateShouldAbort(abortController);
|
||||
|
|
@ -24,7 +24,7 @@ export const getAIResponse = async (
|
|||
abortController,
|
||||
updateCurrentAiMessage,
|
||||
addMessage,
|
||||
debug,
|
||||
options,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Model request failed:', error);
|
||||
|
|
|
|||
Loading…
Reference in a new issue