|
1 | 1 | --- |
2 | | -title: Coding with an AI Assistant |
3 | | -description: Write code using Continue and Granite |
| 2 | +title: Using Open-WebUI for a local RAG |
| 3 | +description: Learn how to build a simple local RAG |
4 | 4 | logo: images/ibm-blue-background.png |
5 | 5 | --- |
6 | 6 |
|
7 | | -## Setup |
| 7 | +## Retrieval-Augmented Generation overview |
| 8 | +The LLMs we're using for these labs have been trained on billions of parameters, but they haven't been trained on everything, and the smaller models have less general knowledge to work with. |
| 9 | +For example, even the latest models are trained with aged data, and they couldn't know about current events or the unique data your use-case might need. |
8 | 10 |
|
9 | | -First, get comfortable with Continue. For example, if you prefer that your local assistant have its own slice of the window, you can drag it to the right sidebar. |
| 11 | +RAG allows the user to supplement the LLM's data with up-to-date information from external sources, like databases and documents. |
10 | 12 |
|
11 | | - |
| 13 | +In this lab we're going to use one of the smallest IBM Granite models and show that it's answer is not complete. Then we'll add a small RAG document and allow it render a much better answer |
| 14 | +utilizing both it's internal data combined with the RAG data you give it. |
12 | 15 |
|
13 | | -You can also take a look at the [Continue documentation](https://docs.continue.dev/chat/how-to-use-it), or at least have it open in case you want to refer to it. |
| 16 | +## Configuration and Sanity Check |
14 | 17 |
|
15 | | -Now that our local AI co-pilot is up and running, let’s put it to work. The following examples will focus on `python`, but the same approach applies to other languages like `go`, `javascript`, or `rust`. |
| 18 | +Open up [Open-WebUI](http://localhost:8080/), and you should see something like the following: |
| 19 | + |
16 | 20 |
|
17 | | -A key part of learning to use this technology effectively is exploring the boundaries of what it can and can’t do. |
| 21 | +If you see this that means Open-WebUI is installed correctly, and we can continue configuration, if not, please find a workshop TA or |
| 22 | +raise your hand we'll be there to help you ASAP. |
18 | 23 |
|
19 | | -As you work through this lab, keep in mind: this assistant is here to support your workflow — not to do the work for you! |
20 | | - |
21 | | -!!! tip |
22 | | - If you lose the Continue pane in VSCode, you can re-enable it in VSCode by clicking at the top of the screen under "View --> Appearance --> Secondary Side Bar" and then the Continue window will be visible again. |
23 | | - |
24 | | -## Writing a `main.py` |
25 | | - |
26 | | -Clear the Continue window using `cmd+l` so we can start with a clean slate and create a new file called `main.py` in a new directory. |
27 | | - |
28 | | - |
29 | | - |
30 | | -With your `main.py` open, use the `cmd+i` to open up the `Generate Code` command palette. You should see some information about what file and line will be edited. Give it the following prompt: |
| 24 | +Next as a sanity check, run the following command to confirm you have the [granite3.3:2b](https://ollama.com/library/granite3.3:2b) |
| 25 | +model downloaded in `ollama`. This may take a bit, but we should have a way to copy it directly on your laptop. |
31 | 26 |
|
| 27 | +```bash |
| 28 | +ollama pull granite3.3:2b |
32 | 29 | ``` |
33 | | -Write the code for conway's game of life using pygame |
34 | | -``` |
35 | | - |
36 | | -!!! note |
37 | | - [What is Conway's Game of Life?](https://en.wikipedia.org/wiki/Conway's_Game_of_Life) |
38 | | - |
39 | | -After a few moments, the model should start writing code in the file, it might look something like: |
40 | | - |
41 | 30 |
|
42 | | -## AI-Generated Code |
| 31 | +If you didn't know, the supported languages with `granite3.3:2b` now include: |
43 | 32 |
|
44 | | -You can try to run it... *but would it work?* Do you see any potential errors in this code? If the code you generated worked, then consider yourself lucky! You can see below that this instance of generated code doesn't provide any output. |
| 33 | +- English, German, Spanish, French, Japanese, Portuguese, Arabic, Czech, Italian, Korean, Dutch, Chinese (Simplified) |
45 | 34 |
|
46 | | - |
| 35 | +And the Capabilities also include: |
47 | 36 |
|
48 | | -This is an important lesson for using _any_ AI co-pilot code assistants. While they can provide a lot of helpful code towards what you need, it often won't get you across the finish line. |
| 37 | +- Thinking |
| 38 | +- Summarization |
| 39 | +- Text classification |
| 40 | +- Text extraction |
| 41 | +- Question-answering |
| 42 | +- Retrieval Augmented Generation (RAG) |
| 43 | +- Code related tasks |
| 44 | +- Function-calling tasks |
| 45 | +- Multilingual dialog use cases |
| 46 | +- Fill-in-the-middle |
| 47 | +- Long-context tasks including long document/meeting summarization, long document QA, etc. |
49 | 48 |
|
50 | | -## Cleaning up the AI-Generated Code |
51 | 49 |
|
52 | | -At this point, you can practice debugging or refactoring code with the AI co-pilot. Maybe it's a missing indent or the functions could be better organized for your understanding. |
| 50 | +Next click on the down arrow at the top and select the "granite3.3:2b" if it's not already selected. |
53 | 51 |
|
54 | | -!!! note |
55 | | - You can try using the built-in autocomplete and code assistant functions to generate any missing code. |
56 | | - In the example generated code, a "main" entry point to the script is missing. In this case, using `cmd+I` again and trying the prompt: "write a main function for my game that plays ten rounds of Conway's |
57 | | - game of life using the `board()` function." might help. What happens? |
| 52 | + |
58 | 53 |
|
59 | | -It's hard to read the generated code in the example case, making it difficult to understand the logic. To clean it up, I'll define a `main` function so the entry point exists. There's also a `tkinter` section in the generated code, I decided to put the main game loop there: |
60 | | - |
61 | | -```python |
62 | | -if __name__ == '__main__': |
63 | | - root = tkinter.Tk() |
64 | | - game_of_life(tkinter.Canvas(root)) |
65 | | - root.mainloop() |
| 54 | +Click on the "New Chat" icon to clear the context. Then, ask the model for: |
| 55 | +```bash |
| 56 | +List all the past and current CEOs of the IBM corporation in order of their term as CEO |
66 | 57 | ``` |
| 58 | +For example: |
| 59 | + |
67 | 60 |
|
68 | | -In this generated code, there are also missing imports: |
| 61 | +At first glance, the list looks pretty good. But if you know your IBM CEOs, you'll notice that it misses a few of them, and sometimes adds new names that weren't ever IBM CEOs! |
| 62 | +(Note: the larger granite3.3:8b does a much better job on the IBM CEOs, you can try it later) |
| 63 | +But we can provide the small LLM with a RAG document that supplements the model's missing informaiton with a correct list, so it will generate a better answer. |
69 | 64 |
|
70 | | -```python |
71 | | -import tkinter |
72 | | -import time |
73 | | -``` |
| 65 | +Click on the "New Chat" icon to clear the context. Then download a small text file with the correct list of IBM CEOs to your Downloads folder: |
74 | 66 |
|
75 | | -It looks like the code is improving: |
| 67 | +[IBM.txt](../resources/IBM.txt) |
76 | 68 |
|
77 | | - |
| 69 | +Right click on the IBM.txt URL and select "Save Link As" and save it as IBM in your Downloads folder. |
78 | 70 |
|
79 | | -## Explaining the Code |
| 71 | +In your Open-WebUI browser, click on the "+" under the "send a message" prompt and then select "Upload files" |
80 | 72 |
|
81 | | -To debug further, use Granite-Code to explain what the different functions do. Simply highlight one of them, use `cmd+L` to add it to the context window of your assistant and write a prompt similar to: |
| 73 | +Select the IBM.txt file that you just downloaded in your Downloads folder and press Open. |
82 | 74 |
|
83 | | -```text |
84 | | -what does this function do? |
85 | | -``` |
86 | | - |
87 | | - |
88 | | - |
89 | | -Asking for an explanation of portions of code can be very helpful with understanding logic that isn't clear right away. The model might even catch or highlight problems in the code if your prompt encourages it to. |
90 | | - |
91 | | -## Creating Tests |
92 | | - |
93 | | -One of the most effective ways to streamline your workflow as a developer is by writing tests for your code. Tests act as a safety net, helping you catch unintended changes. Tests can be time-consuming to write, and Granite Code can help generate them for you. |
| 75 | + |
94 | 76 |
|
95 | | -Assuming you still have a function you wanted explained above in the context-window for your local assistant, you can use the prompt: |
96 | | - |
97 | | -```text |
98 | | -write a pytest test for this function |
| 77 | +Now ask it our question about the CEOs of IBM: |
| 78 | +```bash |
| 79 | +List all the past and current CEOs of the IBM corporation in order of their term as CEO |
99 | 80 | ``` |
| 81 | +The answer should now be correct. (For example, always before it forgets John Akers) |
100 | 82 |
|
101 | | -The model generated a great framework for a test here: |
102 | | - |
103 | | - |
104 | | -Notice that the test only spans what is provided in the context, so it isn't integrated into my project yet. But, the code provides a good start. I'll need to create a new test file and integrate `pytest` into my project to use it. |
| 83 | + |
105 | 84 |
|
106 | | -## Adding Comments |
| 85 | +We can also find and download information to pdf from Wikipedia: |
| 86 | +For example: [History of IBM](https://en.wikipedia.org/wiki/History_of_IBM) |
107 | 87 |
|
108 | | -Continue also provides the ability to automatically add comments to code. Try it out! |
| 88 | +On the right of the Wikipedia page, click on "Tools" and click on "Download as PDF" |
109 | 89 |
|
110 | | - |
| 90 | +Then use this History_of_IBM.pdf as a RAG by clicking on the + and select "History_of_IBM.pdf" as a file from your Downloads folder. |
111 | 91 |
|
112 | | -## Conclusion |
113 | | - |
114 | | -This lab was all about using our local, open-source AI co-pilot to write complex code in Python. By combining Continue and Granite-Code, we were able to generate code, explain functions, write tests, and add comments to our code! |
| 92 | +Next, use the Open-WebUI to ask more questions about IBM, or have it summarize the document itself. For example: |
| 93 | +```bash |
| 94 | +Write a short 300 word summary of the History_of_IBM.pdf |
| 95 | +``` |
| 96 | + |
115 | 97 |
|
116 | | -<script data-goatcounter="https://tracker.asgharlabs.io/count" |
117 | | - async src="//tracker.asgharlabs.io/count.js"></script> |
| 98 | +Congratulations, you've completed the Open-WebUI RAG example. |
0 commit comments