Skip to content

Commit

Permalink
Merge remote-tracking branch 'workshop-llm/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
isc-lperezra committed Dec 16, 2024
2 parents a625746 + f791589 commit 0920d82
Show file tree
Hide file tree
Showing 17 changed files with 1,421 additions and 76 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
.local
.npm
.cache
.env
shared
data/model
data/model
__pycache__
13 changes: 13 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"objectscript.conn": {
"username": "superuser",
"password": "SYS",
"active": true,
"ns": "LLMRAG",
"host": "localhost",
"port": 52774,
"docker-compose": {
"service": "iris"
}
}
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 InterSystems Developer Community
Copyright (c) 2024 InterSystems Developer Community

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
110 changes: 84 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# workshop-llm
Workshop to create a RAG application to use LLM models. This workshop is developed in Python using Jupyter Notebook connected using the official libraries to IRIS.
Workshop to create a RAG application using LLM models.

You can find more in-depth information in https://learning.intersystems.com.
This workshop is developed in Python 🐍 (Jupyter Notebook) and InterSystems IRIS.

The main purpose is to show you the main steps to create a RAG application using an LLM and a vector database.

You can find more in-depth information in https://learning.intersystems.com.

# What do you need to install?
* [Git](https://git-scm.com/downloads)
Expand All @@ -13,42 +16,97 @@ You can find more in-depth information in https://learning.intersystems.com.
# Setup
Build the image we will use during the workshop:

```console
$ git clone https://github.com/intersystems-ib/workshop-llm
$ cd workshop-llm
$ docker-compose build
Clone the repository:
```bash
git clone https://github.com/intersystems-ib/workshop-llm
cd workshop-llm
```

Build the image:
```bash
docker compose build
```

Run the containers:
```bash
docker compose up -d
```

# Configuration
After running the containers, you should be able to access to:
* InterSystems IRIS [Management Portal](http://localhost:52774/csp/sys/UtilHome.csp). You can login using `superuser` / `SYS`
* [Jupyter Notebook](http://localhost:8888)

# Explore RAG applications using Jupyter

## Medicine Leaflet examples

You have some medicine leaflets (in spanish) in [./data](./data).

This example is about creating a RAG Q&A application that can answer questions about those medicine leaflets.

Open [Jupyter Notebook](http://localhost:8888), there you can find:
* [QA-PDF-LLM.ipynb](./jupyter/QA-PDF-LLM.ipynb) - RAG example using [MistralAI](https://mistral.ai) LLM
* [QA-PDF-Local.ipynb](./jupyter/QA-PDF-Local.ipynb) - RAG example using a local LLM

![alt text](/images/jupyter.png)

You can test the project step by step or execute it at one time, feel free.

The main purpose of this example is to identify the main steps to create a RAG application using MISTRAL as LLM and IRIS as vector database to save and search the specific context.
## Hoolefoods data model text to SQL

## **ATTENTION** Docker configuration
This example is about a company called Holefoods that sells food with some hole on it :)

There is a known issue related to the permission request from Docker Desktop to access to the folders of the project, this permission has to be granted before to launch **docker-compose up -d**. To allow the file sharing in Docker Desktop you have to open settings option, select **Resources** and **File Sharing**, from that screen you have to include the path to the project, you can see here an example:
![alt text](/images/fileSharing.png)
Using the sales data model of the company, the goal is to create an assistant that can translate natural language questions into valid SQL that answer the question.

If you don't share this folder previously PostgreSQL database won't be initialized and the project will fail.
In [Jupyter Notebook](http://localhost:8888), you will find:
* [QA-SQL-LLM.ipynb](./jupyter/QA-SQL-LLM.ipynb) - text to SQL example using OpenAI LLM.

## Test Production
* Run the containers that we will use in the workshop:
# Create other applications

There are some other examples you can try to build and modify in your local environment.

First of all, create a new environment and install some requirements:

```bash
# create a local venv environment
python3 -m venv .venv

# activate venv
source .venv/bin/activate

# install dependencies
pip3 install -r requirements.txt
```

Create an `.env` file for storing API keys for OpenAI / MistralAI. They will be used in the applications.

```
OPENAI_API_KEY="your-api"
MISTRAL_API_KEY="your-api"
```
docker-compose build

docker-compose up -d
## Text to SQL service API
You can find a sample Text to SQL based on [QA-SQL-LLM.ipynb](./jupyter/QA-SQL-LLM.ipynb) [here](python/holefoods_text2sql/main.py).

You can run it like this:

```bash
cd python/holefoods_text2sql
fastapi dev main.py
```
Automatically an IRIS instance, will be deployed, a Jupyter Notebook is deployed under (http://localhost:8888) too.

## IRIS database
Then open http://127.0.0.1:8000/docs to explore the API and try it out using the web client.

* Open the [Management Portal](http://localhost:52774/csp/sys/UtilHome.csp).
* Login using the default `superuser`/ `SYS` account.
* Open System Explorer --> SQL
* Select NAMESPACE USER and Schema `Test`
## Streamlit Assistant
There is also a great example of a langchain / streamlit chatbot assitant in https://alejandro-ao.com/how-to-use-streaming-in-langchain-and-streamlit/

# Testing with Jupyter Notebook
You can play with it here as well:

This project is devolped in Python using Jupyter Notebook, you can access to it from [here](http://localhost:8888) and open LLMTests.ipnyb file.
![alt text](/images/jupyter.png)
```bash
cd python/assitant
streamlit run chatbot.py
```

Then open http://localhost:8501 and have a look at it.

You can test the project step by step or execute it at one time, feel free.
Are you able to add the logic to reproduce the Medicine Leaflet example in the assitant ?
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ services:

# iris
iris:
init: true
container_name: iris
build:
context: .
Expand All @@ -14,7 +13,6 @@ services:
- ./iris/shared:/shared
environment:
- ISC_DATA_DIRECTORY=/shared/durable
command: --check-caps false --ISCAgent false

# jupyter notebook
jupyter:
Expand Down
17 changes: 6 additions & 11 deletions iris/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@ USER irisowner
# copy files to image
WORKDIR /opt/irisapp
COPY --chown=irisowner:irisowner src src
COPY --chown=irisowner:irisowner iris/iris.script iris.script
COPY --chown=irisowner:irisowner iris/sql sql

COPY --chown=$ISC_PACKAGE_MGRUSER:$ISC_PACKAGE_IRISGROUP /iris/irissession.sh /
RUN chmod +x /irissession.sh

SHELL ["/irissession.sh"]

RUN \
zn "%SYS" \
set sc = $SYSTEM.OBJ.LoadDir("/opt/irisapp/src/Install", "ck", , 1) \
if sc do ##class(LLMRAG.Installer).Install() \
zn "LLMRAG" \
DO $SYSTEM.OBJ.LoadDir("/opt/irisapp/src/LLMRAG", "ck", , 1)
# run iris.script
RUN iris start IRIS \
&& iris session IRIS < /opt/irisapp/iris.script \
&& iris stop IRIS quietly
21 changes: 21 additions & 0 deletions iris/iris.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

// unexpire default passwords
set $namespace = "%SYS"
do ##class(Security.Users).UnExpireUserPasswords("*")

// create namespace
do $SYSTEM.OBJ.Load("/opt/irisapp/src/LLMRAG/Installer.cls", "ck")
do ##class(LLMRAG.Installer).Install()

// import source code & schemas
set $namespace="LLMRAG"
do $SYSTEM.SQL.Schema.ImportDDL("/opt/irisapp/sql/dump.sql",,"IRIS")

// install zpm
s r=##class(%Net.HttpRequest).%New(),r.Server="pm.community.intersystems.com",r.SSLConfiguration="ISC.FeatureTracker.SSL.Config" d r.Get("/packages/zpm/latest/installer"),$system.OBJ.LoadStream(r.HttpResponse.Data,"c")

// install some sample data (Holefoods, etc.)
zpm "install samples-bi"

// load some modifications on Holefoods (e.g. ForeignKeys, etc.)
do $SYSTEM.OBJ.LoadDir("/opt/irisapp/src/HoleFoods/", "ck", ,1)
20 changes: 0 additions & 20 deletions iris/irissession.sh

This file was deleted.

10 changes: 3 additions & 7 deletions jupyter/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
FROM jupyter/minimal-notebook

USER root

# install IRIS DBAPI driver
COPY /jupyter/intersystems_irispython-3.2.0-py3-none-any.whl /
RUN chmod 777 /intersystems_irispython-3.2.0-py3-none-any.whl

COPY /jupyter/requirements.txt /
RUN chmod 777 /requirements.txt

RUN pip install /*.whl

# install python requirements
COPY /jupyter/requirements.txt /
RUN pip install -r /requirements.txt
Loading

0 comments on commit 0920d82

Please sign in to comment.