Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code completion for jupyter lab/notebook using StarCoder #378

Closed
wants to merge 78 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
e8a0f4f
add bigcode readme.md
Wzixiao Aug 17, 2023
7132c25
fix readmd image bug
Wzixiao Aug 17, 2023
4e7763f
Improve method in readme
Wzixiao Aug 17, 2023
25251cd
Improve method in readme
Wzixiao Aug 17, 2023
d6e5038
add merge plan
Wzixiao Aug 17, 2023
f85b05c
Continue to improve readme.md
Wzixiao Aug 17, 2023
d9e630c
add changed code color to readme
Wzixiao Aug 18, 2023
10165e9
add save data to laocl plan
Wzixiao Aug 18, 2023
b97fc85
move md Images to local ./mdImages
Wzixiao Aug 18, 2023
7691d2b
fix logic bug in readme
Wzixiao Aug 18, 2023
78730d3
fix logic bug in readme
Wzixiao Aug 19, 2023
9d25bf6
refactor readme
Wzixiao Aug 23, 2023
c0899c4
Complete the preliminary structure
Wzixiao Aug 23, 2023
db8980b
Add dependencies on readme
Wzixiao Aug 23, 2023
7c2d49e
A simple version that completes the continuation
Wzixiao Aug 23, 2023
d7ca78f
Merge pull request #3 from Wzixiao/request
Wzixiao Aug 23, 2023
de3f65e
perfect readme
Wzixiao Aug 23, 2023
b3f5c88
Merge pull request #4 from Wzixiao/request
Wzixiao Aug 23, 2023
ae8087b
perfect readme
Wzixiao Aug 23, 2023
2e7b074
Merge pull request #5 from Wzixiao/request
Wzixiao Aug 23, 2023
0e43347
Added functionality to animate on request
Wzixiao Aug 23, 2023
1502810
add animation to bigcode-zh.md
Wzixiao Aug 23, 2023
84da159
Merge pull request #6 from Wzixiao/animation
Wzixiao Aug 23, 2023
3c5a6f5
fix request loading animation bug
chenJuZ Aug 23, 2023
22aa6a1
remove irrelevant code
Wzixiao Aug 24, 2023
2414664
Merge pull request #7 from Wzixiao/fix-bug
Wzixiao Aug 24, 2023
9d9412d
format code
Wzixiao Aug 24, 2023
2c6b302
Merge pull request #8 from Wzixiao/fix-bug
Wzixiao Aug 24, 2023
eca890a
fix get cell error
Wzixiao Aug 24, 2023
7c7fe46
Merge pull request #9 from Wzixiao/fix-bug
Wzixiao Aug 24, 2023
f6654ae
Fixed issue where no code would appear after pressing enter
Wzixiao Aug 24, 2023
3341761
Merge pull request #10 from Wzixiao/fix-bug
Wzixiao Aug 24, 2023
61ba755
Fixed issue where no code would appear after pressing enter
Wzixiao Aug 24, 2023
18b1841
Merge pull request #11 from Wzixiao/fix-bug
Wzixiao Aug 24, 2023
01cc1fa
Change use other shortcut keys to determine the way to enter the method
Wzixiao Aug 24, 2023
6959e00
Merge pull request #12 from Wzixiao/shortcut-keys
Wzixiao Aug 24, 2023
b682dbc
Fix bug when formatting prompt
Wzixiao Aug 24, 2023
a7fbdab
Merge pull request #13 from Wzixiao/fix-bug
Wzixiao Aug 24, 2023
c3eed45
Fixed that when the code is displayed and the current cell is empty, …
Wzixiao Aug 24, 2023
00a8c1a
Merge pull request #14 from Wzixiao/fix-bug
Wzixiao Aug 24, 2023
7e9a22f
When reloading the animation, the original animation dom should be re…
Wzixiao Aug 24, 2023
6107997
Merge pull request #15 from Wzixiao/fix-bug
Wzixiao Aug 24, 2023
938047d
add debug log
Wzixiao Aug 25, 2023
5e11485
add hot key on request
Wzixiao Aug 25, 2023
aa1e3d3
Merge pull request #16 from Wzixiao/debug-log
Wzixiao Aug 25, 2023
916510b
Merge pull request #17 from Wzixiao/hotkey
Wzixiao Aug 25, 2023
80021b0
Improve the hotkey context, including log, readme, etc.
Wzixiao Aug 25, 2023
ec6919f
Merge pull request #18 from Wzixiao/hotkey
Wzixiao Aug 25, 2023
cbcee9f
add examples
Wzixiao Aug 25, 2023
c4ce981
[200~Fixed a hint error~
Wzixiao Aug 25, 2023
ddbe16f
Merge pull request #19 from Wzixiao/examples
Wzixiao Aug 25, 2023
95942cf
Replace 'continue writing' with 'code complement'
Wzixiao Aug 27, 2023
0440066
Replace 'Add a part function docstring
Wzixiao Aug 27, 2023
77e46e3
add docstring
Wzixiao Aug 28, 2023
247b187
Merge pull request #20 from Wzixiao/file_name
Wzixiao Aug 28, 2023
771c52a
update bigcode icon
Wzixiao Aug 28, 2023
390fad5
Merge pull request #21 from Wzixiao/icon
Wzixiao Aug 28, 2023
2e8c611
add 'Enable code completion' function
Wzixiao Sep 3, 2023
9e007a5
Merge pull request #22 from Wzixiao/select-monitor
Wzixiao Sep 3, 2023
d17070f
Improve some docstring
Wzixiao Sep 3, 2023
758d14b
Merge pull request #23 from Wzixiao/docstring
Wzixiao Sep 3, 2023
1db503c
add token limit
Wzixiao Sep 3, 2023
106cbd3
Merge pull request #24 from Wzixiao/token-limit
Wzixiao Sep 3, 2023
fa1bc6e
Fix the bug of adding new cells
Wzixiao Sep 3, 2023
b156446
remove garbage output
Wzixiao Sep 3, 2023
21ea03d
Merge pull request #25 from Wzixiao/fix-bug
Wzixiao Sep 3, 2023
a99ebc3
Merge pull request #2 from Wzixiao/code-continue-writing
Wzixiao Sep 3, 2023
b2c17f9
Merge remote-tracking branch 'upstream/main'
Wzixiao Sep 3, 2023
054fc10
Improve readme for code completion
Wzixiao Sep 3, 2023
701d798
Add code completion examples to the readme.md
Wzixiao Sep 3, 2023
4b46ea5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 3, 2023
63f719e
change css asking price style
Wzixiao Sep 3, 2023
d38323e
merge remote branch
Wzixiao Sep 3, 2023
90342de
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 3, 2023
1bcc70c
change css asking price style
Wzixiao Sep 3, 2023
951a94a
Merge branch 'main' of github.com:Wzixiao/jupyter-ai-bigcode-code-com…
Wzixiao Sep 3, 2023
1f2ad83
Delete incorrectly uploaded files
Wzixiao Sep 9, 2023
d290932
Merge branch 'main' of https://github.com/jupyterlab/jupyter-ai
Wzixiao Sep 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,5 @@ dev.sh
.jupyter_ystore.db

.yarn

.venv
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ that include variable values.

![Sample with code interpolation and markdown output](./docs/source/_static/sample-markdown.png)


## Code completion

In a cell, press {Short cut configured in the bigcode sidebar} to trigger

![Sample of pressing a shortcut key in code completion](./docs/source/_static/code-completion-setting-key-down.jpg)

Press "enter" to accpet the proposition.

![Sample of pressing enter after a successful request in code completion](./docs/source/_static/code-completion-setting-completion.jpg)

## JupyterLab extension

The Jupyter AI extension for JupyterLab offers a native UI that enables multiple users
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/_static/code-completion-setting.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/_static/debug-levels.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions docs/source/users/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -743,3 +743,43 @@ The `--region-name` parameter is set to the [AWS region code](https://docs.aws.a
The `--request-schema` parameter is the JSON object the endpoint expects as input, with the prompt being substituted into any value that matches the string literal `"<prompt>"`. For example, the request schema `{"text_inputs":"<prompt>"}` will submit a JSON object with the prompt stored under the `text_inputs` key.

The `--response-path` option is a [JSONPath](https://goessner.net/articles/JsonPath/index.html) string that retrieves the language model's output from the endpoint's JSON response. For example, if your endpoint returns an object with the schema `{"generated_texts":["<output>"]}`, its response path is `generated_texts.[0]`.


## Code completion

### Usage

We first need to configure the huggingface token and short cut

![](../_static/code-completion-setting.jpg)

In a cell, press {The short cut configured in the image above} to trigger

![](../_static/code-completion-setting-key-down.jpg)

Press "enter" to accpet the proposition.

![](../_static/code-completion-setting-completion.jpg)


### Configure

Bigcode service url:

- The endpoint URL of the Bigcode service. This is the base URL that the code completion system will connect to when requesting code completions.

Huggingface Access Token:

- Your personal access token for Huggingface's service. This token authenticates and authorizes your requests to the Huggingface API.

Short cut for completion:

- A shortcut or key combination that, when pressed, triggers the code completion feature.

Max prompt tokens:

- The maximum number of tokens that can be sent in the prompt to the code completion API.

Max response tokens:

- Set 'Max Response Tokens' judiciously; a higher value can slow response times, and since we don't support streaming, you'll await the entire response.
83 changes: 83 additions & 0 deletions examples/code-completion/sklearn_digits.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"id": "dcbb2940-4d47-45ac-923a-f80eed1d26f5",
"metadata": {},
"outputs": [],
"source": [
"# load Digits Dataset from sklearn\n"
]
},
{
"cell_type": "markdown",
"id": "f3a60c45-244b-49f2-8527-519c5a2557a6",
"metadata": {},
"source": [
"Print to show there are 1797 images (8 by 8 images for a dimensionality of 64)"
]
},
{
"cell_type": "markdown",
"id": "0c159867-a499-4181-9713-d2558f1a258d",
"metadata": {},
"source": [
"Print to show there are 1797 labels (integers from 0–9)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e216707c-1ea7-446a-ac69-cd013e76b5f2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "fe688a3e-29cb-464d-9eef-7e17ce4b9dee",
"metadata": {},
"source": [
"Showing the Images and the Labels (Digits Dataset)"
]
},
{
"cell_type": "markdown",
"id": "f2f5505f-32b1-4e22-8878-39f20e71335d",
"metadata": {},
"source": [
"This section is really just to show what the images and labels look like. It usually helps to visualize your data to see what you are working with."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4328fb40-b40c-4fde-8ef4-6a5b38c5dd10",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
4 changes: 3 additions & 1 deletion packages/jupyter-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
"@jupyterlab/ui-components": "^4",
"@mui/icons-material": "^5.11.0",
"@mui/material": "^5.11.0",
"mobx": "^6.10.0",
"mobx-react": "^9.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-markdown": "^8.0.6",
Expand Down Expand Up @@ -103,7 +105,7 @@
"stylelint-config-standard": "~24.0.0",
"stylelint-prettier": "^2.0.0",
"ts-jest": "^29",
"typescript": "~4.3.0",
"typescript": "5.1.6",
"y-protocols": "^1.0.5"
},
"sideEffects": [
Expand Down
188 changes: 188 additions & 0 deletions packages/jupyter-ai/src/bigcode/bigcode-code-completion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { JupyterFrontEnd } from '@jupyterlab/application';
import { EditorView } from '@codemirror/view';
import { getNotebookContentUntilCursor } from '../utils/cell-context';
import {
sendToBigCode,
processCompletionResult,
constructContinuationPrompt
} from '../utils/bigcode-request';
import {
insertAndHighlightCode,
removeTextStatus,
moveCursorToEnd,
replaceText
} from '../utils/cell-modification';
import {
addLoadingAnimation,
updateAnimation,
removeLoadingAnimation
} from '../utils/animation';
import { ICell } from '../types/cell';

import CodeCompletionContextStore from '../contexts/code-completion-context-store';

/**
* An object to track the state of the request and the visibility of the result.
* @property {boolean} loading - Indicates if a request is currently being made.
* @property {boolean} viewResult - Indicates if the result of a request is currently being displayed.
*/
const requestState = {
loading: false,
viewResult: false
};

/**
* Checks if the provided context is empty or not.
* @param {ICell[]} context - An array of cells representing the context.
* @returns {boolean} Returns true if the context is empty or undefined, otherwise false.
*/
const isContextEmpty = (context: ICell[]): boolean => {
if (!context || context.length === 0) {
return true; // If the context is empty or undefined, we assume that the user did not write code
}

const combinedContent = context.reduce((acc, cell) => acc + cell.content, '');
return combinedContent.trim() === '';
};

/**
* Handles the success response of the code completion request.
* @param {JupyterFrontEnd} app - The JupyterFrontEnd application instance.
* @param {EditorView} view - The editor view instance.
* @param {{ generated_text: string }[]} result - The result array containing generated text.
*/
const requestSuccess = (
app: JupyterFrontEnd,
view: EditorView,
result: { generated_text: string }[]
) => {
const resultCode = processCompletionResult(result);
requestState.viewResult = true;

if (resultCode === '') {
requestState.viewResult = false;
CodeCompletionContextStore.setCodeOnRequest('');
} else {
insertAndHighlightCode(
app,
CodeCompletionContextStore.codeOnRequest,
resultCode
);
}

updateAnimation(view, 'success');
requestState.loading = false;
};

/**
* Handles the failure of the code completion request.
* @param {EditorView} view - The editor view instance.
*/
const requestFailed = (view: EditorView) => {
CodeCompletionContextStore.setCodeOnRequest('');

updateAnimation(view, 'failed');
requestState.viewResult = false;
requestState.loading = false;
};

/**
* Initiates code completion based on the provided context.
* @param {JupyterFrontEnd} app - The JupyterFrontEnd application instance.
* @param {EditorView} view - The editor view instance.
* @returns {boolean} Indicates whether the function completed successfully.
*/
export const codeCompletion = (
app: JupyterFrontEnd,
view: EditorView
): boolean => {
const context = getNotebookContentUntilCursor(app);

if (!context || isContextEmpty(context)) {
updateAnimation(view, 'failed');
console.error('continueWriting() => context is null');
return false;
}

if (requestState.loading || requestState.viewResult) {
console.error('continueWriting() => request is running');
return true;
}

console.debug('continueWriting() => context: ', context);

requestState.loading = true;
removeLoadingAnimation(view);
addLoadingAnimation(view);
CodeCompletionContextStore.setCodeOnRequest(
context[context.length - 1].content
);
const prompt = constructContinuationPrompt(
context,
CodeCompletionContextStore.maxPromptToken
);

console.debug('codeCompletion() => prompt: ', prompt);

sendToBigCode(prompt, CodeCompletionContextStore.maxResponseToken)
.then(result => {
console.debug('continueWriting() => state is success, result: ', result);
requestSuccess(app, view, result);
})
.catch(async err => {
console.error(await err);
requestFailed(view);
});

return true;
};

/**
* Removes custom colors from the text in the editor.
* @param {EditorView} view - The editor view instance.
* @returns {boolean} Indicates whether the function completed successfully.
*/
export const removeColor = (view: EditorView): boolean => {
if (
CodeCompletionContextStore.codeOnRequest === '' &&
!requestState.viewResult
) {
console.debug(
'removeColor() => No request is running or no continuation code is showing'
);
return false;
}

requestState.viewResult = false;
removeTextStatus(view);
CodeCompletionContextStore.setCodeOnRequest('');
moveCursorToEnd(view);
console.debug('removeColor() => remove code customize color');
return true;
};

/**
* Handles any keypress events when a request is loading or a result is being displayed.
* @param {EditorView} view - The editor view instance.
* @returns {boolean} Indicates whether the function completed successfully.
*/
export const handleAnyKeyPress = (view: EditorView): boolean => {
if (requestState.loading) {
console.debug('handleAnyKeyPress() => request is loading');
return true;
}

if (
CodeCompletionContextStore.codeOnRequest !== '' ||
requestState.viewResult
) {
console.debug(
'handleAnyKeyPress() => code for the user to cancel the display'
);
removeTextStatus(view);
replaceText(view, CodeCompletionContextStore.codeOnRequest);
CodeCompletionContextStore.setCodeOnRequest('');
requestState.viewResult = false;
}
return false;
};
Loading
Loading