Skip to content

Commit 29cc2b0

Browse files
committed
Initial logic for standardised prompt building
Signed-off-by: worksofliam <[email protected]>
1 parent 3e9c3b9 commit 29cc2b0

File tree

5 files changed

+100
-93
lines changed

5 files changed

+100
-93
lines changed

src/aiProviders/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as vscode from "vscode";
33
import { JobManager } from "../config";
44
import Schemas, { AllSQLTypes, SQLType } from "../database/schemas";
55
import Statement from "../database/statement";
6-
import { DB2_SYSTEM_PROMPT } from "./continue/prompts";
6+
import { DB2_SYSTEM_PROMPT } from "./prompts";
77

88
export function canTalkToDb() {
99
return JobManager.getSelection() !== undefined;

src/aiProviders/continue/continueContextProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
canTalkToDb,
1616
generateTableDefinition
1717
} from "../context";
18-
import { DB2_SELF_PROMPT, DB2_SYSTEM_PROMPT } from "./prompts";
18+
import { DB2_SELF_PROMPT, DB2_SYSTEM_PROMPT } from "../prompts";
1919
import Configuration from "../../configuration";
2020

2121
export let isContinueActive = false;

src/aiProviders/copilot/index.ts

Lines changed: 62 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
refsToMarkdown,
1111
} from "../context";
1212
import { JobManager } from "../../config";
13-
import { DB2_SYSTEM_PROMPT } from "../continue/prompts";
14-
import Configuration from "../../configuration";
13+
import { DB2_SYSTEM_PROMPT } from "../prompts";
14+
import { buildPrompt, Db2ContextItems } from "../prompt";
1515

1616
const CHAT_ID = `vscode-db2i.chat`;
1717

@@ -58,107 +58,85 @@ export function activateChat(context: vscode.ExtensionContext) {
5858
token: vscode.CancellationToken
5959
): Promise<IDB2ChatResult> => {
6060
const copilotFamily = request.model.family;
61-
let messages: vscode.LanguageModelChatMessage[] = [];
6261

6362
if (canTalkToDb()) {
64-
// 1. SCHEMA Definiton (semantic)
65-
let usingSchema = getCurrentSchema();
66-
67-
const useSchemaDef: boolean = Configuration.get<boolean>(`ai.useSchemaDefinition`);
68-
if (useSchemaDef) {
69-
const schemaSemantic = await buildSchemaDefinition(usingSchema);
70-
if (schemaSemantic) {
71-
messages.push(
72-
vscode.LanguageModelChatMessage.Assistant(
73-
JSON.stringify(schemaSemantic)
74-
)
75-
);
76-
}
77-
}
78-
7963
switch (request.command) {
80-
case `activity`:
81-
stream.progress(`Grabbing Information about IBM i system`);
82-
const data = await getSystemStatus();
83-
console.log(
84-
`summarize the following data in a readable paragraph: ${data}`
85-
);
86-
messages = [
87-
vscode.LanguageModelChatMessage.User(
88-
`You are a an IBM i savant speciallizing in database features in Db2 for i. Please provide a summary of the current IBM i system state based on the developer requirement.`
89-
),
90-
vscode.LanguageModelChatMessage.User(
91-
`Here is the current IBM i state: ${data}`
92-
),
93-
vscode.LanguageModelChatMessage.User(request.prompt),
94-
];
95-
96-
await copilotRequest(copilotFamily, messages, {}, token, stream);
97-
98-
return { metadata: { command: "activity" } };
99-
100-
case `set-schema`:
101-
stream.progress(`Setting Current Schema for SQL Job`);
102-
const newSchema = request.prompt.split(" ")[0];
103-
if (newSchema) {
104-
const curJob = JobManager.getSelection();
105-
if (curJob) {
106-
const result = await curJob.job.setCurrentSchema(newSchema);
107-
if (result) {
108-
stream.progress(`Set Current Schema: ${newSchema}✅`);
109-
usingSchema = newSchema;
110-
}
111-
}
112-
return;
113-
}
64+
// case `activity`: //TODO: remove
65+
// stream.progress(`Grabbing Information about IBM i system`);
66+
// const data = await getSystemStatus();
67+
// console.log(
68+
// `summarize the following data in a readable paragraph: ${data}`
69+
// );
70+
// messages = [
71+
// vscode.LanguageModelChatMessage.User(
72+
// `You are a an IBM i savant speciallizing in database features in Db2 for i. Please provide a summary of the current IBM i system state based on the developer requirement.`
73+
// ),
74+
// vscode.LanguageModelChatMessage.User(
75+
// `Here is the current IBM i state: ${data}`
76+
// ),
77+
// vscode.LanguageModelChatMessage.User(request.prompt),
78+
// ];
79+
80+
// await copilotRequest(copilotFamily, messages, {}, token, stream);
81+
82+
// return { metadata: { command: "activity" } };
83+
84+
// case `set-schema`:
85+
// stream.progress(`Setting Current Schema for SQL Job`);
86+
// const newSchema = request.prompt.split(" ")[0];
87+
// if (newSchema) {
88+
// const curJob = JobManager.getSelection();
89+
// if (curJob) {
90+
// const result = await curJob.job.setCurrentSchema(newSchema);
91+
// if (result) {
92+
// stream.progress(`Set Current Schema: ${newSchema}✅`);
93+
// usingSchema = newSchema;
94+
// }
95+
// }
96+
// return;
97+
// }
11498

11599
default:
116-
stream.progress(
117-
`Getting information from ${Statement.prettyName(usingSchema)}...`
118-
);
119-
120-
// 2. TABLE References
121-
let refs = await generateTableDefinition(
122-
usingSchema,
123-
request.prompt.split(` `)
124-
);
100+
stream.progress(`Building response...`);
125101

126102
// get history
103+
let history: Db2ContextItems[]|undefined;
127104
if (context.history.length > 0) {
128-
const historyMessages = context.history.map((h) => {
105+
history = context.history.map((h) => {
129106
if ("prompt" in h) {
130-
return vscode.LanguageModelChatMessage.Assistant(h.prompt);
107+
return {
108+
name: `reply`,
109+
description: `reply from Copilot`,
110+
content: h.prompt,
111+
type: `assistant`,
112+
};
131113
} else {
132114
const responseContent = h.response
133115
.filter((r) => r.value instanceof vscode.MarkdownString)
134116
.map((r) => (r.value as vscode.MarkdownString).value)
135117
.join("\n\n");
136-
return vscode.LanguageModelChatMessage.Assistant(
137-
responseContent
138-
);
118+
return {
119+
name: `message`,
120+
description: `message from user`,
121+
content: responseContent,
122+
type: `assistant`,
123+
};
139124
}
140125
});
141-
messages.push(...historyMessages);
142126
}
143127

144-
// add table refs to messages
145-
if (Object.keys(refs).length > 0) {
146-
for (const tableRef of refs) {
147-
messages.push(
148-
vscode.LanguageModelChatMessage.Assistant(tableRef.content)
149-
);
150-
}
151-
}
128+
const contextItems = await buildPrompt(request.prompt, {
129+
history,
130+
progress: stream.progress
131+
});
152132

153-
// 3. DB2 Guidelines
154-
messages.push(
155-
vscode.LanguageModelChatMessage.Assistant(DB2_SYSTEM_PROMPT)
156-
);
157-
158-
stream.progress(`Building response...`);
159-
160-
// 4. user prompt
161-
messages.push(vscode.LanguageModelChatMessage.User(request.prompt));
133+
const messages = contextItems.map(c => {
134+
if (c.type === `user`) {
135+
return vscode.LanguageModelChatMessage.User(c.content);
136+
} else {
137+
return vscode.LanguageModelChatMessage.Assistant(c.content);
138+
}
139+
});
162140

163141
await copilotRequest(
164142
request.model.family,

src/aiProviders/prompt.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,43 @@ import { JobManager } from "../config";
22
import Configuration from "../configuration";
33
import { JobInfo } from "../connection/manager";
44
import { buildSchemaDefinition, canTalkToDb, generateTableDefinition } from "./context";
5+
import { DB2_SYSTEM_PROMPT } from "./prompts";
56

6-
interface Db2ContextItems {
7+
export interface PromptOptions {
8+
history?: Db2ContextItems[];
9+
progress?: (text: string) => void;
10+
}
11+
12+
export interface Db2ContextItems {
713
name: string;
814
description: string;
915
content: string;
1016
type: "user"|"assistant"|"system";
1117
specific?: "copilot"|"continue";
1218
}
1319

14-
export async function buildPrompt(input: string, history?: Db2ContextItems[]): Promise<Db2ContextItems[]> {
20+
export async function buildPrompt(input: string, options: PromptOptions = {}): Promise<Db2ContextItems[]> {
1521
const currentJob: JobInfo = JobManager.getSelection();
1622
let contextItems: Db2ContextItems[] = [];
1723

24+
const progress = (message: string) => {
25+
if (options.progress) {
26+
options.progress(message);
27+
}
28+
};
29+
1830
if (currentJob) {
1931
const currentSchema = currentJob?.job.options.libraries[0] || "QGPL";
20-
const schema = this.getDefaultSchema();
2132

2233
const useSchemaDef: boolean = Configuration.get<boolean>(`ai.useSchemaDefinition`);
2334

2435
if (useSchemaDef) {
25-
const schemaSemantic = await buildSchemaDefinition(schema);
36+
progress(`Building schema definition for ${currentSchema}...`);
37+
const schemaSemantic = await buildSchemaDefinition(currentSchema);
2638
if (schemaSemantic) {
2739
contextItems.push({
2840
name: `SCHEMA Definition`,
29-
description: `${schema} definition`,
41+
description: `${currentSchema} definition`,
3042
content: JSON.stringify(schemaSemantic),
3143
type: "user"
3244
});
@@ -35,13 +47,14 @@ export async function buildPrompt(input: string, history?: Db2ContextItems[]): P
3547

3648
// TODO: self?
3749

50+
progress(`Building table definition for ${currentSchema}...`);
3851
const refs = await generateTableDefinition(
3952
currentSchema,
4053
input.split(` `)
4154
);
4255

43-
if (history) {
44-
contextItems.push(...history);
56+
if (options.history) {
57+
contextItems.push(...options.history);
4558
}
4659

4760
for (const table of refs) {
@@ -52,6 +65,22 @@ export async function buildPrompt(input: string, history?: Db2ContextItems[]): P
5265
type: `assistant`
5366
});
5467
}
68+
69+
if (!options.history) {
70+
contextItems.push({
71+
name: `system prompt`,
72+
content: DB2_SYSTEM_PROMPT,
73+
description: `system prompt`,
74+
type: `system`
75+
});
76+
}
77+
78+
contextItems.push({
79+
name: `user prompt`,
80+
content: input,
81+
description: `user prompt`,
82+
type: `user`
83+
});
5584
}
5685

5786
return contextItems;
File renamed without changes.

0 commit comments

Comments
 (0)