Skip to content

Commit

Permalink
Add custom system prompt (#61)
Browse files Browse the repository at this point in the history
* Add custom system prompt

* Update settings.test.ts

* Paraphrase prompt
  • Loading branch information
kevin-on authored Oct 29, 2024
1 parent d4930b3 commit 2399547
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/settings/SettingTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ export class SmartCopilotSettingTab extends PluginSettingTab {
}),
)

new Setting(containerEl)
.setHeading()
.setName('System Prompt')
.setDesc('This prompt will be added to the beginning of every chat.')

new Setting(containerEl)
.setClass('smtcmp-setting-textarea')
.addTextArea((text) =>
text
.setValue(this.plugin.settings.systemPrompt)
.onChange(async (value) => {
await this.plugin.setSettings({
...this.plugin.settings,
systemPrompt: value,
})
}),
)

new Setting(containerEl).setHeading().setName('RAG Options')

new Setting(containerEl)
Expand Down
1 change: 1 addition & 0 deletions src/types/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('parseSmartCopilotSettings', () => {
chatModel: 'claude-3-5-sonnet-latest',
applyModel: 'gpt-4o-mini',
embeddingModel: 'text-embedding-3-small',
systemPrompt: '',
ragOptions: {
chunkSize: 1000,
thresholdTokens: 8192,
Expand Down
1 change: 1 addition & 0 deletions src/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const smartCopilotSettingsSchema = z.object({
chatModel: chatModelSchema.catch('claude-3-5-sonnet-latest'),
applyModel: applyModelSchema.catch('gpt-4o-mini'),
embeddingModel: embeddingModelSchema.catch('text-embedding-3-small'),
systemPrompt: z.string().catch(''),
ragOptions: z
.object({
chunkSize: z.number().catch(1000),
Expand Down
17 changes: 17 additions & 0 deletions src/utils/promptGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export class PromptGenerator {

const systemMessage = this.getSystemMessage(shouldUseRAG)

const customInstructionMessage = this.getCustomInstructionMessage()

const currentFile = lastUserMessage.mentionables.find(
(m) => m.type === 'current-file',
)?.file
Expand All @@ -95,6 +97,7 @@ export class PromptGenerator {

const requestMessages: RequestMessage[] = [
systemMessage,
...(customInstructionMessage ? [customInstructionMessage] : []),
...(currentFileMessage ? [currentFileMessage] : []),
...compiledMessages.slice(-20).map((message): RequestMessage => {
if (message.role === 'user') {
Expand Down Expand Up @@ -311,6 +314,20 @@ The user has full access to the file, so they prefer seeing only the changes in
}
}

private getCustomInstructionMessage(): RequestMessage | null {
const customInstruction = this.settings.systemPrompt.trim()
if (!customInstruction) {
return null
}
return {
role: 'user',
content: `Here are additional instructions to follow in your responses when relevant. There's no need to explicitly acknowledge them:
<custom_instructions>
${customInstruction}
</custom_instructions>`,
}
}

private async getCurrentFileMessage(
currentFile: TFile,
): Promise<RequestMessage> {
Expand Down
16 changes: 16 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,19 @@ button.smtcmp-chat-input-model-select {
opacity: 1;
}
}

.smtcmp-setting-textarea {
display: block;
border-top: none;
padding: 0;

.smtcmp-item-control {
width: 100%;
}

textarea {
width: 100%;
min-height: 100px;
resize: none;
}
}

0 comments on commit 2399547

Please sign in to comment.