Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit99d0364

Browse files
authored
Merge pull requestex3ndr#36 from sahandevs/add-support-for-notebook
add support for notebooks
2 parents6135eb0 +539db98 commit99d0364

File tree

5 files changed

+109
-4
lines changed

5 files changed

+109
-4
lines changed

‎package.json‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@
3737
{
3838
"title":"Llama coder",
3939
"properties": {
40+
"notebook.includeMarkup": {
41+
"type":"boolean",
42+
"default":true,
43+
"description":"Include markup cell types in prompt"
44+
},
45+
"notebook.includeCellOutputs": {
46+
"type":"boolean",
47+
"default":false,
48+
"description":"Include Cell previous output results in the prompt"
49+
},
50+
"notebook.cellOutputLimit": {
51+
"type":"number",
52+
"default":256,
53+
"description":"truncate cell output result if exceeds this limit"
54+
},
4055
"inference.endpoint": {
4156
"type":"string",
4257
"default":"",
@@ -122,7 +137,7 @@
122137
"pretest":"yarn run compile && yarn run lint",
123138
"lint":"eslint src --ext ts",
124139
"test":"jest",
125-
"package":"vsce package"
140+
"package":"npx @vscode/vsce package"
126141
},
127142
"devDependencies": {
128143
"@types/jest":"^29.5.10",

‎src/config.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ class Config {
4545
};
4646
}
4747

48+
// Notebook
49+
getnotebook(){
50+
letconfig=vscode.workspace.getConfiguration('notebook');
51+
52+
letincludeMarkup=config.get('includeMarkup')asboolean;
53+
letincludeCellOutputs=config.get('includeCellOutputs')asboolean;
54+
letcellOutputLimit=config.get('cellOutputLimit')asnumber;
55+
return{
56+
includeMarkup,
57+
includeCellOutputs,
58+
cellOutputLimit,
59+
};
60+
}
61+
4862
get #config(){
4963
returnvscode.workspace.getConfiguration('inference');
5064
};

‎src/prompts/filter.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
importtypevscodefrom'vscode';
22

33
exportfunctionisSupported(doc:vscode.TextDocument){
4-
returndoc.uri.scheme==='file';
4+
returndoc.uri.scheme==='file'||doc.uri.scheme==='vscode-notebook-cell';
55
}
66

77
exportfunctionisNotNeeded(doc:vscode.TextDocument,position:vscode.Position,context:vscode.InlineCompletionContext):boolean{

‎src/prompts/preparePrompt.ts‎

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import vscode from 'vscode';
22
import{detectLanguage}from'./processors/detectLanguage';
33
import{fileHeaders}from'./processors/fileHeaders';
44
import{languages}from'./processors/languages';
5+
import{config}from'../config';
6+
7+
vardecoder=newTextDecoder("utf8");
8+
9+
functiongetNotebookDocument(document:vscode.TextDocument):vscode.NotebookDocument|undefined{
10+
returnvscode.workspace.notebookDocuments
11+
.find(x=>x.uri.path===document.uri.path);
12+
}
513

614
exportasyncfunctionpreparePrompt(document:vscode.TextDocument,position:vscode.Position,context:vscode.InlineCompletionContext){
715

@@ -11,6 +19,75 @@ export async function preparePrompt(document: vscode.TextDocument, position: vsc
1119
letprefix=text.slice(0,offset);
1220
letsuffix:string=text.slice(offset);
1321

22+
letnotebookConfig=config.notebook;
23+
24+
// If this is a notebook, add the surrounding cells to the prefix and suffix
25+
letnotebookDocument=getNotebookDocument(document);
26+
letlanguage=detectLanguage(document.uri.fsPath,document.languageId);
27+
letcommentStart:string|undefined=undefined;
28+
if(language){
29+
commentStart=languages[language].comment?.start;
30+
}
31+
32+
if(notebookDocument){
33+
letbeforeCurrentCell=true;
34+
35+
letprefixCells="";
36+
letsuffixCells="";
37+
38+
notebookDocument.getCells().forEach((cell)=>{
39+
letout="";
40+
41+
if(cell.document.uri.fragment===document.uri.fragment){
42+
beforeCurrentCell=false;// switch to suffix mode
43+
return;
44+
}
45+
46+
// add the markdown cell output to the prompt as a comment
47+
if(cell.kind===vscode.NotebookCellKind.Markup&&commentStart){
48+
if(notebookConfig.includeMarkup){
49+
for(constlineofcell.document.getText().split('\n')){
50+
out+=`\n${commentStart}${line}`;
51+
}
52+
}
53+
}else{
54+
out+=cell.document.getText();
55+
}
56+
57+
// if there is any outputs add them to the prompt as a comment
58+
constaddCellOutputs=notebookConfig.includeCellOutputs
59+
&&beforeCurrentCell
60+
&&cell.kind===vscode.NotebookCellKind.Code
61+
&&commentStart;
62+
if(addCellOutputs){
63+
letcellOutputs=cell.outputs
64+
.map(x=>x.items
65+
.filter(x=>x.mime==='text/plain')
66+
.map(x=>decoder.decode(x.data))
67+
.map(x=>x.slice(0,notebookConfig.cellOutputLimit).split('\n')))
68+
.flat(3);
69+
70+
if(cellOutputs.length>0){
71+
out+=`\n${commentStart}Output:`;
72+
for(constlineofcellOutputs){
73+
out+=`\n${commentStart}${line}`;
74+
}
75+
}
76+
}
77+
78+
// update the prefix/suffix
79+
if(beforeCurrentCell){
80+
prefixCells+=out;
81+
}else{
82+
suffixCells+=out;
83+
}
84+
85+
});
86+
87+
prefix=prefixCells+prefix;
88+
suffix=suffix+suffixCells;
89+
}
90+
1491
// Trim suffix
1592
// If suffix is too small it is safe to assume that it could be ignored which would allow us to use
1693
// more powerful completition instead of in middle one
@@ -22,7 +99,6 @@ export async function preparePrompt(document: vscode.TextDocument, position: vsc
2299
// NOTE: Most networks don't have a concept of filenames and expected language, but we expect that some files in training set has something in title that
23100
// would indicate filename and language
24101
// NOTE: If we can't detect language, we could ignore this since the number of languages that need detection is limited
25-
letlanguage=detectLanguage(document.uri.fsPath,document.languageId);
26102
if(language){
27103
prefix=fileHeaders(prefix,document.uri.fsPath,languages[language]);
28104
}

‎src/prompts/processors/languages.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export const languages: { [key in Language]: LanguageDescriptor } = {
111111
},
112112
python:{
113113
name:'Python',
114-
extensions:['.py'],
114+
extensions:['.py','ipynb'],
115115
comment:{start:'#'}
116116
},
117117
c:{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp