Skip to content

Commit 0dc84de

Browse files
authored
Merge pull request #334 from codefori/ai-context/use-generate-sql
Refactor Db2i context providers and enhance SQL prompt guidelines
2 parents 8494008 + 7f1f92d commit 0dc84de

File tree

10 files changed

+620
-155
lines changed

10 files changed

+620
-155
lines changed

media/context.png

28.9 KB
Loading

package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@
101101
}
102102
}
103103
},
104+
{
105+
"id": "vscode-db2i.ai",
106+
"title": "AI integrations",
107+
"properties": {
108+
"vscode-db2i.ai.useSchemaDefinition": {
109+
"type": "boolean",
110+
"description": "Provide Schema definition as additional context in Continue and Copilot",
111+
"default": true
112+
}
113+
}
114+
},
104115
{
105116
"id": "vscode-db2i.resultsets",
106117
"title": "Viewing Data",

src/aiProviders/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Db2 for i Context Provider Developer Guide
2+
3+
This document provides a guide for prompt engineering for Db2 for i. More specifically, how we construct prompts for both Continue and Copilot AI extensions.
4+
5+
## Key Concepts
6+
7+
A key component of the Db2 for i Code Assistant is the ability to provide contextually relevant information about a users Db2i connection and schema. This is done through something called a context provider.
8+
9+
A context Provider does the following:
10+
- Gathers relevant database information such as table information, column information, based on the user's prompt
11+
- Creates additional "context items" or messages with the information gathered
12+
- Builds a prompt with the context items that is then sent to the AI extension (Continue or Copilot)
13+
14+
Example for the following user input:
15+
```
16+
@Db2i how can I find the number of employees per department?
17+
```
18+
19+
The context provider would gather information about the user's connection and schema, and then build a prompt with the following context items:
20+
- current schema: `SAMPLE`
21+
- table reference: `EMPLOYEE` and `DEPARTMENT`
22+
- column reference: `EMPLOYEE.EMPNO`, `EMPLOYEE.LASTNAME`, `DEPARTMENT.DEPTNO`, `DEPARTMENT.DEPTNAME`, etc
23+
24+
## Continue and Copilot Parity
25+
26+
Both Continue and Copilot AI extensions have APIs for building prompts with context items. The APIs are similar but have some differences. It is important that the actual context items are identical in both extensions.
27+
- In Copilot, we register a `ChatParticipant`: `@Db2i` that is responsible for building the prompt with context items
28+
- In Continue, we register a `ContextProvider`: `@Db2i` that is responsible for building the prompt with context items
29+
30+
**Note**: We refer to the `ChatParticipant` and `ContextProvider` as `@Db2i` in this document and we use "Context Provider" to refer to both.
31+
32+
## Prompt formatting
33+
34+
The prompt format is very simple:
35+
36+
```plaintext
37+
## Database Schema Definition
38+
39+
SCHEMA: [schema_name]
40+
TABLES: [table_name_1], [table_name_2], ...
41+
42+
## Referenced Tables
43+
TABLE: [table_name_1]
44+
- Columns:
45+
* [column_name]: [data type] [constraints/notes]
46+
* [column_name]: [data type] [constraints/notes]
47+
- Primary Key: [column(s)]
48+
- Foreign Keys: [if any, specify references]
49+
50+
TABLE: [table_name_2]
51+
- Columns:
52+
* [column_name]: [data type] [constraints/notes]
53+
* [column_name]: [data type] [constraints/notes]
54+
- Primary Key: [column(s)]
55+
- Foreign Keys: [if any, specify references]
56+
57+
-- (Repeat for all relevant tables)
58+
59+
## Database Guidelines
60+
61+
- [Guideline 1]
62+
- [Guideline 2]
63+
- [Guideline 3]
64+
- ...
65+
66+
## Example
67+
68+
input:
69+
"Generate a SQL query to join the 'orders' and 'customers' tables to list all customers with their respective orders, including only orders with a total amount greater than 100."
70+
71+
### Expected Output Format
72+
73+
- Provide the complete SQL query.
74+
- Include any assumptions as inline comments if needed.
75+
- Format the query clearly and consistently.
76+
77+
[Insert the user's specific request here]
78+
79+
```
80+
81+
There are 4 main sections in the prompt:
82+
- **Database Schema Definition**: This section provides the schema information for the user's connection. It includes the current schema and a list of tables in the schema.
83+
- **Referenced Tables**: This section provides detailed information about the tables referenced in the user's query. It includes the columns, primary key, and foreign keys of each table.
84+
- **Database Guidelines**: This section provides general guidelines for writing SQL queries based on the schema context. It includes style conventions, performance optimization tips, validation guidelines, and additional guidelines.
85+
- **Example**: This section provides an example of the user's specific request and the expected output format.
86+
87+
The users input is inserted at the end of the prompt. This format allows a top down view of the database by providing the schema information first, followed by the reference tables, guidelines, and finally the user's specific request.
88+
89+
## Context Provider Implementation
90+
91+
In both Copilot and Continue, the same prompt format is followed. Here is an example of what that looks like in Continue:
92+
93+
![alt text](../../media/context.png)
94+
95+
96+
97+
98+

0 commit comments

Comments
 (0)