Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,7 @@ src/pdl/_version.py
*~

# Mac
.DS_Store
.DS_Store

# Model files
*.gguf
14 changes: 5 additions & 9 deletions examples/intrinsics/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
To run the example in this folder, you need to first create the model on ollama by following the initial steps in this
notebook: https://github.com/ibm-granite-community/granite-snack-cookbook/blob/main/recipes/Intrinsics/Granite_RAG_LoRA.ipynb

Execute all the initial cells in the notebook up to the section entitled: `Choose your Embeddings Model`.
Note that it works for Python 3.12 as well.
This example is an adaptaion of this notebook: https://github.com/ibm-granite-community/granite-snack-cookbook/blob/main/recipes/Intrinsics/Granite_RAG_LoRA.ipynb which will build a new model. In particular, `granite3.0-rag` will be constructed from `granite3.0:8b` and a LoRA adapter which will enahance the base model with RAG capabiltiies. The steps to build the model are embedded into the PDL program.

To run, use `pdl examples/intrinsics/demo-hallucination.pdl`. The output should be

Expand All @@ -11,11 +7,11 @@ Did Faith Hill take a break from recording after releasing her second album, It

The answer is: Yes, Faith Hill took a three-year break from recording after releasing her second album, It Matters to Me.
I am not hallucinating, promise!
The citation is: After the release of It Matters to Me,
The citation is: After the release of It Matters to Me,
Hill took a three-year break from recording to give herself a rest from four years of touring
and to begin a family with McGraw. During her break, she joined forces with her husband
for their first duet, "It's Your Love". The song stayed at number one for six weeks,
and won awards from both the Academy of Country Music and the Country Music Association. Hill has remarked that sometimes when they perform the song together,
and to begin a family with McGraw. During her break, she joined forces with her husband
for their first duet, "It's Your Love". The song stayed at number one for six weeks,
and won awards from both the Academy of Country Music and the Country Music Association. Hill has remarked that sometimes when they perform the song together,
"it [doesn't] feel like anybody else was really watching."
```

Expand Down
2 changes: 1 addition & 1 deletion examples/intrinsics/demo-hallucination.json

Large diffs are not rendered by default.

72 changes: 69 additions & 3 deletions examples/intrinsics/demo-hallucination.pdl
Original file line number Diff line number Diff line change
@@ -1,11 +1,77 @@
# Granite.runtime Intrinsics Demo with PDL

# Provide document(s) to Granite3-rag model with hallucination_tag
# Granite3-rag model: Base model granite3-dense:8b setup with Granite RAG LoRA (Low-Rank Adaption) on ollama
# Provide document(s) to granite3-rag model with hallucination_tag
# granite3-rag model: Base model granite3-dense:8b setup with Granite RAG LoRA (Low-Rank Adaption) on Ollama

# The model responds to a query providing a hallucination score
# The model responds to a query and provides a hallucination score

description: Responds to a query and provides a hallucination score
text:
# Install dependencies
- contribute: []
lang: command
args:
- "/bin/sh"
- "-c"
- "rm -rf llama.cpp || true && git clone https://github.com/ggml-org/llama.cpp.git llama.cpp"
- contribute: []
lang: command
args:
- "/bin/sh"
- "-c"
- |
pip install "git+https://github.com/ibm-granite-community/utils.git" \
"huggingface_hub" \
"langchain_community" \
"langchain_ollama" \
"ollama" \
-r llama.cpp/requirements.txt
# Download base model and LoRA adapter and build granite3-rag model
- contribute: []
lang: python
code: |
import os
import subprocess
import huggingface_hub
import ollama

granite3_model = 'granite3-dense:8b'
granite3_rag_model = 'granite3-rag:8b'

# Ollama pull base model
granite3_base_model = 'granite3-dense:8b'
ollama.pull(granite3_base_model)

# HG: download LoRA adapter
lora_folder = huggingface_hub.snapshot_download(repo_id="ibm-granite/granite-rag-3.0-8b-lora")
base_folder = huggingface_hub.snapshot_download(repo_id="ibm-granite/granite-3.0-8b-instruct", allow_patterns="*.json")
lora_gguf = "granite-rag-3.0-8b-lora-fp16.gguf"
subprocess.run(f"python3 llama.cpp/convert_lora_to_gguf.py --outtype f16 --outfile {lora_gguf} --base {base_folder} -- {lora_folder}",
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# Write Modelfile for granite-rag:8b for Ollama
model_file = "Modelfile"
with open(model_file, "w") as modelfile:
modelfile.write(f"""\
FROM {granite3_base_model}
ADAPTER {lora_gguf}
""")

# Ideally we should use ollama.create() but there is no option to pass a modelfile
subprocess.run(f"ollama create {granite3_rag_model} -f Modelfile",
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# Rm model file
os.remove(model_file)

result = f"Model {granite3_rag_model} available"
# Remove dependencies
- contribute: []
lang: command
args:
- "/bin/sh"
- "-c"
- "rm -rf llama.cpp"
- role: system
contribute: [context]
text:
Expand Down
2 changes: 1 addition & 1 deletion pdl-live-react/demos/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ EOF
pdl --trace "$T"/demo5.json "$TOP"/examples/talk/6-code-json.pdl
pdl --trace "$T"/demo6.json "$UI"/demos/error.pdl || true
pdl --trace "$T"/demo7.json "$TOP"/examples/talk/4-function.pdl
# TODO demo8...
pdl --trace "$T"/demo8.json "$TOP"/examples/intrinsics/demo-hallucination.pdl
pdl --stream none --trace "$T"/demo9.json "$TOP"/examples/gsm8k/gsm8.pdl
2 changes: 1 addition & 1 deletion pdl-live-react/src/demos/demo8.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

What is the color of the sky?
{"color": "Blue"}
{"color": "Blue"}
13 changes: 13 additions & 0 deletions tests/results/examples/intrinsics/demo-hallucination.0.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Did Faith Hill take a break from recording after releasing her second album, It Matters to Me?

The answer is: Yes, Faith Hill took a three-year break from recording after releasing her second album, It Matters to Me.
I am not hallucinating, promise!
The citation is: After discovering that Hill was
pregnant with their first child, the couple married on October 6, 1996. The
couple have three daughters together: Gracie Katherine (born 1997), Maggie Elizabeth (born 1998)
and Audrey Caroline (born 2001). Since their marriage, Hill and McGraw have endeavored
never to be apart for more than three consecutive days. After the release of It Matters to Me,
Hill took a three-year break from recording to give herself a rest from four years of touring
and to begin a family with McGraw. During her break, she joined forces with her husband
for their first duet, "It's Your Love".

19 changes: 19 additions & 0 deletions tests/results/examples/intrinsics/demo-hallucination.1.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Running command git clone --filter=blob:none --quiet https://github.com/ibm-granite-community/utils.git /private/var/folders/jv/ydx4xrxs7jjdzn1qqb50pbs80000gn/T/pip-req-build-pgzu86xo

[notice] A new release of pip is available: 23.2.1 -> 25.0.1
[notice] To update, run: pip install --upgrade pip

Fetching 4 files: 100%|███████████████████████████████████████████████████| 4/4 [00:00<00:00, 30283.78it/s]
Fetching 8 files: 100%|██████████████████████████████████████████████████| 8/8 [00:00<00:00, 112222.18it/s]
Did Faith Hill take a break from recording after releasing her second album, It Matters to Me?

The answer is: Yes, Faith Hill took a three-year break from recording after releasing her second album, It Matters to Me.
I am not hallucinating, promise!
The citation is: After discovering that Hill was
pregnant with their first child, the couple married on October 6, 1996. The
couple have three daughters together: Gracie Katherine (born 1997), Maggie Elizabeth (born 1998)
and Audrey Caroline (born 2001). Since their marriage, Hill and McGraw have endeavored
never to be apart for more than three consecutive days. After the release of It Matters to Me,
Hill took a three-year break from recording to give herself a rest from four years of touring
and to begin a family with McGraw. During her break, she joined forces with her husband
for their first duet, "It's Your Love".
2 changes: 2 additions & 0 deletions tests/test_examples_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
pathlib.Path("examples")
/ "intrinsics"
/ "demo-hallucination.pdl", # TODO check why
pathlib.Path("examples"),
pathlib.Path("examples")
/ "hello"
/ "hello-function-empty-context.pdl", # TODO CREATE RESULTS FILE
Expand Down Expand Up @@ -236,6 +237,7 @@ def test_valid_programs(capsys: CaptureFixture[str], monkeypatch: MonkeyPatch) -
actual_parse_error: set[str] = set()
actual_runtime_error: set[str] = set()
wrong_results = {}

for pdl_file_name in pathlib.Path(".").glob("**/*.pdl"):
scope: ScopeType = PdlDict({})
if str(pdl_file_name) in TO_SKIP:
Expand Down
Loading