Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/panel/newNotebook.tsx
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { AssistantMessage, BasePromptElementProps, PromptElement, PromptPiece, PromptSizing, Raw, SystemMessage, UserMessage } from '@vscode/prompt-tsx';
7
import type { Uri } from 'vscode';
8
import { getTextPart } from '../../../../platform/chat/common/globalStringUtils';
9
import { IChatEndpoint } from '../../../../platform/networking/common/networking';
10
import { INotebookSection } from '../../../../util/common/notebooks';
11
import { isNonEmptyArray } from '../../../../util/vs/base/common/arrays';
12
import { IBuildPromptContext } from '../../../prompt/common/intents';
13
import { CopilotIdentityRules } from '../base/copilotIdentity';
14
import { ResponseTranslationRules } from '../base/responseTranslationRules';
15
import { LegacySafetyRules } from '../base/safetyRules';
16
import { JupyterNotebookRules } from '../notebook/commonPrompts';
17
import { ChatToolReferences, ChatVariablesAndQuery } from './chatVariables';
18
import { EditorIntegrationRules } from './editorIntegrationRules';
19
import { CodeBlock } from './safeElements';
20
21
export interface NewNotebookPlanningPromptProps extends BasePromptElementProps {
22
promptContext: IBuildPromptContext;
23
endpoint: IChatEndpoint;
24
}
25
26
export interface NewNotebookPlanningPromptState {
27
}
28
29
export class NewNotebookPlanningPrompt extends PromptElement<NewNotebookPlanningPromptProps, NewNotebookPlanningPromptState> {
30
override async prepare(): Promise<NewNotebookPlanningPromptState> {
31
return {};
32
}
33
34
override render(state: NewNotebookPlanningPromptState, sizing: PromptSizing): PromptPiece<any, any> | undefined {
35
return (
36
<>
37
<><SystemMessage priority={1000}>
38
You are an AI that creates a detailed content outline for a Jupyter notebook on a given topic.<br />
39
<CopilotIdentityRules />
40
<LegacySafetyRules />
41
<EditorIntegrationRules />
42
<ResponseTranslationRules />
43
<br />
44
Additional Rules<br />
45
DO NOT include Introduction or Conclusion section in the outline!<br />
46
Focus only on sections that will need code!<br />
47
{[
48
'',
49
'Generate the outline as two parts:',
50
'- First part is markdown bullet list of section titles',
51
'- Second part is the JSON data that will validate against this JSON schema, wrap the response in code block. We assume that a code block begins with \`\`\`[optionally the language] and ends with \`\`\`',
52
'',
53
'The JSON schema is:',
54
'{',
55
' "$schema": "http://json-schema.org/draft-07/schema#",',
56
' "type": "object",',
57
' "properties": {',
58
' "description": {',
59
' "type": "string"',
60
' },',
61
' "sections": {',
62
' "type": "array",',
63
' "items": {',
64
' "type": "object",',
65
' "properties": {',
66
' "title": {',
67
' "type": "string"',
68
' },',
69
' "content": {',
70
' "type": "string"',
71
' }',
72
' },',
73
' "required": ["title", "content"]',
74
' }',
75
' }',
76
' },',
77
' "required": ["sections"]',
78
'}'
79
].join('\n')}
80
{[
81
'',
82
'Examples:',
83
'',
84
'Below you will find a set of examples of what you should respond with. Please follow these examples as closely as possible.',
85
'',
86
'## Valid notebook creation question',
87
'',
88
'user: Creating Random Arrays with Numpy',
89
'',
90
'assistant: Here\'s an outline for a Jupyter notebook that creates Random Arrays with Numpy:',
91
'',
92
'* **Import Required Libraries**',
93
'* **Create Random Arrays**',
94
'* **Seed the Random Number Generator**',
95
'* **Generate Random Integers**',
96
'',
97
'\`\`\`json',
98
'{',
99
' "description": "A Jupyter notebook that creates Random Arrays with Numpy.",',
100
' "sections": [',
101
' {',
102
' "title": "Import Required Libraries",',
103
' "content": "Import the necessary libraries, including NumPy."',
104
' },',
105
' {',
106
' "title": "Create Random Arrays",',
107
' "content": "Use NumPy to create random arrays of various shapes and sizes, including 1D, 2D, and 3D arrays."',
108
' },',
109
' {',
110
' "title": "Seed the Random Number Generator",',
111
' "content": "Use the seed() function to seed the random number generator for reproducibility."',
112
' },',
113
' {',
114
' "title": "Generate Random Integers",',
115
' "content": "Use the randint() function to generate random integers within a specified range."',
116
' }',
117
' ]',
118
'}',
119
'\`\`\`'].join('\n')
120
}
121
</SystemMessage></>
122
123
<ChatToolReferences priority={899} flexGrow={2} promptContext={this.props.promptContext} embeddedInsideUserMessage={false} />
124
{this.props.promptContext.chatVariables && Object.keys(this.props.promptContext.chatVariables).length > 0 ? (
125
<ChatVariablesAndQuery flexGrow={2} priority={900} chatVariables={this.props.promptContext.chatVariables} query={this.props.promptContext.query} embeddedInsideUserMessage={false} />
126
) : (
127
<UserMessage priority={900}>
128
{this.props.promptContext.query}
129
</UserMessage >
130
)}
131
</>
132
);
133
}
134
}
135
136
export interface NewNotebookCodeGenerationPromptProps extends BasePromptElementProps {
137
history?: readonly Raw.ChatMessage[];
138
description: string;
139
section: INotebookSection;
140
existingCode: string;
141
languageId: string;
142
uri: Uri;
143
}
144
145
export interface NewNotebookCodeGenerationPromptState {
146
}
147
148
export class NewNotebookCodeGenerationPrompt extends PromptElement<NewNotebookCodeGenerationPromptProps, NewNotebookCodeGenerationPromptState> {
149
override async prepare(): Promise<NewNotebookPlanningPromptState> {
150
return {};
151
}
152
override render(state: NewNotebookCodeGenerationPromptState, sizing: PromptSizing): PromptPiece<any, any> | undefined {
153
return (
154
<>
155
<>
156
{isNonEmptyArray(this.props.history) && <GenerateNotebookConversationHistory messages={this.props.history} />}
157
<SystemMessage priority={1000}>
158
You are an AI that writes Python code for a single section of a Jupyter notebook.<br />
159
<CopilotIdentityRules />
160
<LegacySafetyRules />
161
<ResponseTranslationRules />
162
<JupyterNotebookRules />
163
When dealing with Jupyter Notebook, do not generate CELL INDEX in the code blocks in your answer, it is only used to help you understand the context.<br />
164
165
Your output should be valid Python code with inline comments.<br />
166
You should return the code directly without any explantion.<br />
167
You should not print message to explain the code or purpose of the code.<br />
168
You should return the code directly, without wrapping it inside \`\`\`.<br />
169
170
Please make sure that the new code is syntactically valid Python code. It can be validated by running it in a Python interpreter.<br />
171
For example, it should pass the validation through builtin module codeop \`codeop.compile_command(statement)\`.<br />
172
</SystemMessage>
173
<UserMessage priority={900}>
174
Overall topic of the notebook: {this.props.description}<br />
175
Title of the notebook section: {this.props.section.title}<br />
176
Description of the notebok section: {this.props.section.content}<br />
177
Given this information, write all the code for this section and this section only.<br />
178
The request to generate the outline of the notebook is already completed.<br />
179
Here is the request details for the outline generation:<br />
180
<br />
181
Code in the notebook so far:<br />
182
<CodeBlock uri={this.props.uri} languageId={this.props.languageId} code={this.props.existingCode} />
183
<br />
184
Please make sure the new code you generate works fine with the code above.<br />
185
</UserMessage>
186
</>
187
</>
188
);
189
}
190
}
191
interface GenerateNotebookConversationHistoryProps extends BasePromptElementProps {
192
messages: readonly Raw.ChatMessage[];
193
}
194
195
class GenerateNotebookConversationHistory extends PromptElement<GenerateNotebookConversationHistoryProps> {
196
override render(state: void, sizing: PromptSizing): PromptPiece<any, any> | undefined {
197
const history: (UserMessage | AssistantMessage | SystemMessage)[] = [];
198
199
for (const curr of this.props.messages) {
200
switch (curr.role) {
201
case Raw.ChatRole.User:
202
history.push(<UserMessage priority={800}>{getTextPart(curr.content)}</UserMessage>);
203
break;
204
case Raw.ChatRole.Assistant:
205
history.push(<AssistantMessage priority={800}>{getTextPart(curr.content)}</AssistantMessage>);
206
break;
207
case Raw.ChatRole.System:
208
history.push(<SystemMessage priority={100}>{getTextPart(curr.content)}</SystemMessage>);
209
break;
210
default:
211
break;
212
}
213
}
214
215
return (<>{history}</>);
216
}
217
}
218
219
220
export interface NewNotebookCodeImprovementPromptProps extends BasePromptElementProps {
221
description: string;
222
section: INotebookSection;
223
existingCode: string;
224
code: string;
225
languageId: string;
226
uri: Uri;
227
}
228
229
export interface NewNotebookCodeImprovementPromptState {
230
}
231
232
export class NewNotebookCodeImprovementPrompt extends PromptElement<NewNotebookCodeImprovementPromptProps, NewNotebookCodeImprovementPromptState> {
233
override async prepare(): Promise<NewNotebookCodeImprovementPromptState> {
234
return {};
235
}
236
override render(state: NewNotebookCodeImprovementPromptState, sizing: PromptSizing): PromptPiece<any, any> | undefined {
237
return (
238
<>
239
<>
240
<SystemMessage priority={1000}>
241
You are an AI that improves Python code with respect to readability and performance for a single section of a Jupyter notebook.<br />
242
<CopilotIdentityRules />
243
<LegacySafetyRules />
244
<ResponseTranslationRules />
245
You MUST return Python code as your answer.<br />
246
DO NOT explain in inline comments for your the improvements.<br />
247
You should not print messages to explain the code or purpose of the code.<br />
248
Make sure the new code you generate works fine with the code above.<br />
249
Make sure if a module is already imported in the code above, it can be used in the new code directly without importing it again. For the same reason, if a variable is defined above, it can be used in new code as well. <br />
250
Make sure to return the code only - don't give an explanation of the improvements.<br />
251
</SystemMessage>
252
<UserMessage priority={900}>
253
Overall topic of the notebook: {this.props.description}<br />
254
Title of the notebook section: {this.props.section.title}<br />
255
Description of the notebook section: {this.props.section.content}<br />
256
Code in the notebook so far:<br />
257
<br />
258
<CodeBlock uri={this.props.uri} languageId={this.props.languageId} code={this.props.existingCode} />
259
<br />
260
Given this information, suggest improvements for the following code:<br />
261
<br />
262
{this.props.code}<br />
263
<br />
264
</UserMessage>
265
</>
266
</>
267
);
268
}
269
}
270
271