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

Add local mode to Qiskit Code Assistant Guide #2275

Merged
merged 43 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
3e1d989
local mode guide
vabarbosa Nov 9, 2024
2af223b
Update checkPatternsIndex.ts
vabarbosa Nov 9, 2024
8340c7f
Update qiskit-code-assistant-local.mdx
vabarbosa Nov 9, 2024
5325ef9
Update qiskit_bot.yaml
vabarbosa Nov 9, 2024
28c971a
local mode guide
vabarbosa Nov 10, 2024
2687b53
Merge branch 'va-qiskit-code-assistant' of https://github.com/vabarbo…
vabarbosa Nov 10, 2024
e310dfc
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
55dead5
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
209f4a5
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
d63843b
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
8a84f8d
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
8ac731a
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
675e699
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
b047aa8
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
54aa0fe
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
060e3db
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
c4c15d0
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
0bc0e45
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
3119085
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
315e605
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
4b79bac
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
c272d7f
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
71e76cc
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 10, 2024
b32c04d
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
a4c5b15
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
3b88aa9
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
d3a3c04
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
50666ab
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
242ef25
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
e882dd5
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
ceab2e1
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
7605d1c
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
e6828b5
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
a05b284
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
577c746
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
d73cfb1
Apply suggestions from code review
vabarbosa Nov 11, 2024
91661ae
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
acce034
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
ae787f4
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 11, 2024
344cfce
Update docs/guides/qiskit-code-assistant-local.mdx
vabarbosa Nov 12, 2024
96a03ac
additional review feedback
vabarbosa Nov 13, 2024
3cfa669
Update qiskit-code-assistant-local.mdx
vabarbosa Nov 13, 2024
08afe63
revert change to preview
Eric-Arellano Nov 13, 2024
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
4 changes: 4 additions & 0 deletions docs/guides/_toc.json
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@
{
"title": "Use Qiskit Code Assistant in VS Code",
"url": "/guides/qiskit-code-assistant-vscode"
},
{
"title": "Use Qiskit Code Assistant in local mode",
"url": "/guides/qiskit-code-assistant-local"
}
]
}
Expand Down
201 changes: 201 additions & 0 deletions docs/guides/qiskit-code-assistant-local.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
---
title: Use Qiskit Code Assistant in local mode
description: Learn how to deploy and use the Qiskit Code Assistant model locally.
---

{/* cspell:ignore ollama, gguf */}

# Use Qiskit Code Assistant in local mode

Learn how to install, configure, and use the Qiskit Code Assistant model on your local machine.

<Admonition type="note" title="Notes">
- Qiskit Code Assistant is in preview release status and is subject to change.
- If you have feedback or want to contact the developer team, use the [Qiskit Slack Workspace channel](https://qiskit.enterprise.slack.com/archives/C07LYA6PL83) or the related public GitHub repositories.
</Admonition>

## Download the Qiskit Code Assistant model

The Qiskit Code Assistant model is available in <DefinitionTooltip definition="GGUF is a binary format that is designed for fast loading and saving of models, and for ease of reading.">GGUF file format</DefinitionTooltip> and can be downloaded from the Hugging Face Hub in one of two ways.

<details>

<summary>Download from the Hugging Face website</summary>

Follow these steps to download the Qiskit Code Assistant GGUF model from the Hugging Face website:

1. Navigate to the [`granite-8b-qiskit`](https://huggingface.co/qiskit) model page
vabarbosa marked this conversation as resolved.
Show resolved Hide resolved
1. Select the `granite-8b-qiskit` GGUF model version
1. Go to the Files and Versions tab and download the GGUF model

</details>


<details>

<summary>Download using the Hugging Face CLI</summary>

To download the `granite-8b-qiskit` GGUF model using the Hugging Face CLI, follow these steps:

1. Install the [Hugging Face CLI](https://huggingface.co/docs/huggingface_hub/main/en/guides/cli)
1. Log in to your Hugging Face account

```
huggingface-cli login
```

1. Download the `granite-8b-qiskit` GGUF model

```
huggingface-cli download <HF REPO NAME> <GGUF PATH> --local-dir <LOCAL PATH>
```

</details>


## Use the Qiskit Code Assistant model

There are multiple ways to deploy and interact with the downloaded `granite-8b-qiskit` GGUF model. This guide demonstrates using [Ollama](https://ollama.com) as follows: either with the [Ollama application](#using-the-ollama-application), or with the [`llama-cpp-python` package](#using-the-llama-cpp-python-package).
vabarbosa marked this conversation as resolved.
Show resolved Hide resolved

### Using the Ollama application

The Ollama application provides a simple solution to run the GGUF models locally. It is easy to use, with a CLI that makes the whole setup process, model management, and interaction fairly straightforward. It’s ideal for quick experimentation and for users that want fewer technical details to handle.

#### Install Ollama

1. Download the [Ollama application](https://ollama.com/download)
1. Install the downloaded file
1. Launch the installed Ollama application

<Admonition type="info">The application is running successfully when the Ollama icon appears in the desktop menu bar. You can also verify the service is running by going to http://localhost:11434/.</Admonition>

1. Try Ollama in your terminal and start running models. For example:

```
ollama run granite3-dense:8b
```

vabarbosa marked this conversation as resolved.
Show resolved Hide resolved
#### Set up Ollama with the Qiskit Code Assistant GGUF model

vabarbosa marked this conversation as resolved.
Show resolved Hide resolved
1. Create a `Modelfile` entering the following content and be sure to update `<PATH-TO-GGUF-FILE>` to the actual path of your downloaded model.

```
FROM <PATH-TO-GGUF-FILE>
TEMPLATE """{{ if .System }}System:
vabarbosa marked this conversation as resolved.
Show resolved Hide resolved
{{ .System }}

{{ end }}
{{ if .Prompt }}Question:
{{ .Prompt }}

{{ end }}
Answer:
```python
{{ .Response }}
"""

SYSTEM """"""

PARAMETER stop "<fim_prefix>"
PARAMETER stop "<fim_middle>"
PARAMETER stop "<fim_suffix>"
PARAMETER stop "<fim_pad>"
PARAMETER stop "<|endoftext|>"

PARAMETER mirostat 0
PARAMETER mirostat_eta 0.1
PARAMETER mirostat_tau 5.0
PARAMETER num_ctx 10000
PARAMETER repeat_penalty 1.0
PARAMETER temperature 0.8
PARAMETER seed 0
PARAMETER tfs_z 1.0
PARAMETER num_predict 1024
PARAMETER top_k 50
PARAMETER top_p 0.95
PARAMETER min_p 0.05
```

1. Run the following command to create a custom model instance based on the `Modelfile`.

```
ollama create granite-8b-qiskit -f ./path-to-model-file
```

<Admonition type="note">This process may take some time for Ollama to read the model file, initialize the model instance, and configure it according to the specifications provided.</Admonition>


#### Run the Qiskit Code Assistant model in Ollama

After the `granite-8b-qiskit` GGUF model has been set up in Ollama, run the following command to launch the model and interact with it in the terminal (in chat mode).

```
ollama run granite-8b-qiskit
```

Some useful commands:

- `ollama list` - List models on your computer
- `ollama rm granite-8b-qiskit` - Remove/delete the model
- `ollama show granite-8b-qiskit` - Show model information
- `ollama stop granite-8b-qiskit` - Stop a model that is currently running
- `ollama ps` - List which models are currently loaded

### Using the `llama-cpp-python` package

An alternative to the Ollama application is the `llama-cpp-python` package, which is a Python binding for `llama.cpp`. It gives you more control and flexibility to run the GGUF model locally, and is ideal for users who wish to integrate the local model in their workflows and Python applications.

1. Install [`llama-cpp-python`](https://pypi.org/project/llama-cpp-python/)
1. Interact with the model from within your application using `llama_cpp`. For example:

```python
from llama_cpp import Llama

model_path = <PATH-TO-GGUF-FILE>

model = Llama(
model_path,
seed=17,
n_ctx=10000,
n_gpu_layers=37, # to offload in gpu, but put 0 if all in cpu
)

input = 'Generate a quantum circuit with 2 qubits'
raw_pred = model(input)["choices"][0]["text"]
```

You can also add text generation parameters to the model to customize the inference:

```python
generation_kwargs = {
"max_tokens": 512,
"echo": False, # Echo the prompt in the output
"top_k": 1
}

raw_pred = model(input, **generation_kwargs)["choices"][0]["text"]
```

### Use the Qiskit Code Assistant extensions

Use the VS Code extension and JupyterLab extension for the Qiskit Code Assistant to prompt the locally deployed `granite-8b-qiskit` GGUF model. Once you have the Ollama application [set up with the model](#using-the-ollama-application), you can configure the extensions to connect to the local service.


#### Connect with the Qiskit Code Assistant VS Code extension

With the Qiskit Code Assistant VS Code extension, you can interact with the model and perform code completion while writing your code. This can work well for users looking for assistance writing Qiskit code for their Python applications.

1. Install the [Qiskit Code Assistant VS Code extension](/guides/qiskit-code-assistant-vscode).
1. In VS Code, go to the **User Settings** and set the **Qiskit Code Assistant: Url** to the URL of your local Ollama deployment (for example, http://localhost:11434).
1. Reload VS Code by going to **View > Command Palette...** and selecting **Developer: Reload Window**.

The `granite-8b-qiskit` configured in Ollama should appear in the status bar and is then ready to use.

#### Connect with the Qiskit Code Assistant JupyterLab extension

With the Qiskit Code Assistant JupyterLab extension, you can interact with the model and perform code completion directly in your Jupyter Notebook. Users who predominantly work with Jupyter Notebooks can take advantage of this extension to further enhance their experience writing Qiskit code.

1. Install the [Qiskit Code Assistant JupyterLab extension](/guides/qiskit-code-assistant-jupyterlab).
1. In JupyterLab, go to the **Settings Editor** and set the **Qiskit Code Assistant Service API** to the URL of your local Ollama deployment (for example, http://localhost:11434).

The `granite-8b-qiskit` configured in Ollama should appear in the status bar and is then ready to use.
7 changes: 7 additions & 0 deletions qiskit_bot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ notifications:
- "cbjuan"
- "@abbycross"
- "@beckykd"
"docs/guides/qiskit-code-assistant-local":
- "@cbjuan"
- "@vabarbosa"
- "@lucaburatti7"
- "@adarsh-tiwari17"
- "@abbycross"
- "@beckykd"
"docs/guides/pulse":
- "`@nkanazawa1989`"
- "@abbycross"
Expand Down
1 change: 1 addition & 0 deletions scripts/js/commands/checkPatternsIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const ALLOWLIST_MISSING_FROM_INDEX: Set<string> = new Set([
"/guides/qiskit-code-assistant",
"/guides/qiskit-code-assistant-jupyterlab",
"/guides/qiskit-code-assistant-vscode",
"/guides/qiskit-code-assistant-local",
"/guides/addons",
]);

Expand Down