Instructions to use ibm-granite/granitelib-rag-r1.0 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Granite Library
How to use ibm-granite/granitelib-rag-r1.0 with Granite Library:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
Update hallucination_detection/README.md
Browse files- hallucination_detection/README.md +27 -229
hallucination_detection/README.md
CHANGED
|
@@ -35,238 +35,36 @@ This is a family of hallucination detection intrinsics that gives the ability to
|
|
| 35 |
|
| 36 |
## Quickstart Example
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
```
|
| 62 |
-
hf download ibm-granite/rag-intrinsics-lib --local-dir ./rag-intrinsics-lib
|
| 63 |
-
```
|
| 64 |
-
|
| 65 |
-
5. Edit the vLLM startup script found in `./rag-intrisics-lib/run_vllm.sh` using your favorite editor:
|
| 66 |
-
|
| 67 |
-
Edit the constants `BASE_MODEL_NAME` and `BASE_MODEL_ORG` depending on the base model on which the desired LoRA adapter has been trained. Optionally, edit the constant `PORT` to change the port on which vLLM will run. Save the modified file and exit the editor.
|
| 68 |
-
|
| 69 |
-
6. Start vLLM through the startup script. The first time you run the script, you may have to change the permissions to allow execution:
|
| 70 |
-
```
|
| 71 |
-
cd rag-intrinsics-lib
|
| 72 |
-
chmod u+x ./run_vllm.sh
|
| 73 |
-
./run_vllm.sh &
|
| 74 |
-
```
|
| 75 |
-
|
| 76 |
-
7. Run the following code snippet:
|
| 77 |
-
|
| 78 |
-
```
|
| 79 |
-
import json
|
| 80 |
-
import openai
|
| 81 |
-
import granite_common
|
| 82 |
-
|
| 83 |
-
intrinsic_name = "hallucination_detection"
|
| 84 |
-
|
| 85 |
-
# Change the following constant to select a different base model
|
| 86 |
-
base_model_name = "granite-3.3-8b-instruct"
|
| 87 |
-
|
| 88 |
-
# Change the following constants as needed to reflect the location of the vLLM server
|
| 89 |
-
# The selected port should be identical to the one you specified in the vLLM startup script
|
| 90 |
-
openai_base_url = "http://p2-r15-n1.bluevela.rmf.ibm.com:55555/v1"
|
| 91 |
-
openai_api_key = "rag_intrinsics_1234"
|
| 92 |
-
|
| 93 |
-
# Fetch IO configuration file from Hugging Face Hub
|
| 94 |
-
io_yaml_file = granite_common.intrinsics.util.obtain_io_yaml(
|
| 95 |
-
intrinsic_name, base_model_name
|
| 96 |
)
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# Sample request
|
| 103 |
-
request_json = {
|
| 104 |
-
"messages": [
|
| 105 |
-
{
|
| 106 |
-
"role": "user",
|
| 107 |
-
"content": "What happened to Dennis Wilson of the Beach Boys in 1983?"
|
| 108 |
-
},
|
| 109 |
-
{
|
| 110 |
-
"role": "assistant",
|
| 111 |
-
"content": "On December 28, 1983, Dennis Wilson of the Beach Boys drowned in Marina del Rey while diving from a friend’s boat in an attempt to retrieve belongings he had earlier thrown. Forensic pathologists believed that Dennis experienced shallow-water blackout just before his death."
|
| 112 |
-
}
|
| 113 |
-
],
|
| 114 |
-
"extra_body": {
|
| 115 |
-
"documents": [
|
| 116 |
-
{
|
| 117 |
-
"doc_id": "0",
|
| 118 |
-
"text": "The Beach Boys are an American rock band formed in Hawthorne, California, in 1961. The group's original lineup consisted of brothers Brian, Dennis, and Carl Wilson; their cousin Mike Love; and their friend Al Jardine. Distinguished by their vocal harmonies and early surf songs, they are one of the most influential acts of the rock era. The band drew on the music of jazz-based vocal groups, 1950s rock and roll, and black R&B to create their unique sound, and with Brian as composer, arranger, producer, and de facto leader, often incorporated classical or jazz elements and unconventional recording techniques in innovative ways. In 1983, tensions between Dennis and Love escalated so high that each obtained a restraining order against each other. With the rest of the band fearing that he would end up like Brian, Dennis was given an ultimatum after his last performance in November 1983 to check into rehab for his alcohol problems or be banned from performing live with them. Dennis checked into rehab for his chance to get sober, but on December 28, 1983, he fatally drowned in Marina del Rey while diving from a friend's boat trying to recover items that he had previously thrown overboard in fits of rage."
|
| 119 |
-
},
|
| 120 |
-
{
|
| 121 |
-
"doc_id": "1",
|
| 122 |
-
"text": "A cigarette smoker since the age of 13, Carl was diagnosed with lung cancer after becoming ill at his vacation home in Hawaii, in early 1997. Despite his illness, Carl continued to perform while undergoing chemotherapy. He played and sang throughout the Beach Boys' entire summer tour which ended in the fall of 1997. During the performances, he sat on a stool, but he stood while singing \"God Only Knows\". Carl died of lung cancer in Los Angeles, surrounded by his family, on February 6, 1998, just two months after the death of his mother, Audree Wilson. He was interred at Westwood Village Memorial Park Cemetery in Los Angeles."
|
| 123 |
-
},
|
| 124 |
-
{
|
| 125 |
-
"doc_id": "2",
|
| 126 |
-
"text": "Carl Dean Wilson (December 21, 1946 - February 6, 1998) was an American musician, singer, and songwriter who co-founded the Beach Boys. He is best remembered as their lead guitarist, as the youngest brother of bandmates Brian and Dennis Wilson, and as the group's de facto leader in the early 1970s. He was also the band's musical director on stage from 1965 until his death. Influenced by the guitar playing of Chuck Berry and the Ventures, Carl's initial role in the group was that of lead guitarist and backing vocals, but he performed lead vocals on several of their later hits, including \"God Only Knows\" (1966), \"Good Vibrations\" (1966), and \"Kokomo\" (1988). By the early 1980s the Beach Boys were in disarray; the band had split into several camps. Frustrated with the band's sluggishness to record new material and reluctance to rehearse, Wilson took a leave of absence in 1981. He quickly recorded and released a solo album, Carl Wilson, composed largely of rock n' roll songs co-written with Myrna Smith-Schilling, a former backing vocalist for Elvis Presley and Aretha Franklin, and wife of Wilson's then-manager Jerry Schilling. The album briefly charted, and its second single, \"Heaven\", reached the top 20 on Billboard's Adult Contemporary chart."
|
| 127 |
-
}
|
| 128 |
-
|
| 129 |
-
]
|
| 130 |
-
}
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
-
# Add other parameters
|
| 134 |
-
request_json["model"] = intrinsic_name
|
| 135 |
-
request_json["temperature"] = 0.0
|
| 136 |
-
|
| 137 |
-
# Apply input processor
|
| 138 |
-
rewritten_request = rewriter.transform(request_json)
|
| 139 |
-
|
| 140 |
-
# Run inference
|
| 141 |
-
client = openai.OpenAI(base_url=openai_base_url, api_key=openai_api_key)
|
| 142 |
-
chat_completion = client.chat.completions.create(**rewritten_request.model_dump())
|
| 143 |
-
|
| 144 |
-
# Apply output processor
|
| 145 |
-
processed_chat_completion = result_processor.transform(
|
| 146 |
-
chat_completion, rewritten_request
|
| 147 |
-
)
|
| 148 |
|
| 149 |
-
# Verify that the contents of the completion is valid JSON and pretty-print the JSON.
|
| 150 |
-
parsed_contents = json.loads(processed_chat_completion.choices[0].message.content)
|
| 151 |
-
print("JSON output:")
|
| 152 |
-
print(json.dumps(parsed_contents, indent=2))
|
| 153 |
-
```
|
| 154 |
-
|
| 155 |
-
### Using the Hugging Face Transformers Library
|
| 156 |
-
|
| 157 |
-
To run the intrinsic using the Hugging Face Transformers library directly, follow the steps below. We recommend using Python 3.11 or higher.
|
| 158 |
-
|
| 159 |
-
1. Install the granite-common library and nltk:
|
| 160 |
-
```
|
| 161 |
-
pip install git+https://github.com/ibm-granite/granite-common.git
|
| 162 |
-
pip install nltk
|
| 163 |
-
```
|
| 164 |
-
|
| 165 |
-
2. Install the Hugging Face CLI:
|
| 166 |
-
```
|
| 167 |
-
pip install -U "huggingface_hub[cli]"
|
| 168 |
-
```
|
| 169 |
-
|
| 170 |
-
3. Install PEFT:
|
| 171 |
-
```
|
| 172 |
-
pip install peft
|
| 173 |
-
```
|
| 174 |
-
|
| 175 |
-
4. Install xgrammar:
|
| 176 |
-
```
|
| 177 |
-
pip install xgrammar
|
| 178 |
-
```
|
| 179 |
-
|
| 180 |
-
5. Run the following code snippet:
|
| 181 |
-
|
| 182 |
-
```
|
| 183 |
-
import json
|
| 184 |
-
import granite_common.util
|
| 185 |
-
import peft
|
| 186 |
-
|
| 187 |
-
intrinsic_name = "hallucination_detection"
|
| 188 |
-
|
| 189 |
-
# Change the following constant to select a different base model
|
| 190 |
-
base_model_name = "granite-3.3-8b-instruct"
|
| 191 |
-
|
| 192 |
-
use_cuda = True # Set to False to use default PyTorch device for this machine + model
|
| 193 |
-
|
| 194 |
-
# Fetch IO configuration file from Hugging Face Hub
|
| 195 |
-
io_yaml_file = granite_common.intrinsics.util.obtain_io_yaml(
|
| 196 |
-
intrinsic_name, base_model_name
|
| 197 |
-
)
|
| 198 |
-
|
| 199 |
-
# Fetch LoRA directory from Hugging Face Hub
|
| 200 |
-
lora_dir = granite_common.intrinsics.util.obtain_lora(
|
| 201 |
-
intrinsic_name, base_model_name
|
| 202 |
-
)
|
| 203 |
-
|
| 204 |
-
# Instantiate input/output processors
|
| 205 |
-
rewriter = granite_common.IntrinsicsRewriter(config_file=io_yaml_file)
|
| 206 |
-
result_processor = granite_common.IntrinsicsResultProcessor(config_file=io_yaml_file)
|
| 207 |
-
|
| 208 |
-
# Sample request
|
| 209 |
-
request_json = {
|
| 210 |
-
"messages": [
|
| 211 |
-
{
|
| 212 |
-
"role": "user",
|
| 213 |
-
"content": "What is the visibility level of Git Repos and Issue Tracking projects?"
|
| 214 |
-
},
|
| 215 |
-
{
|
| 216 |
-
"role": "assistant",
|
| 217 |
-
"content": "Git Repos and Issue Tracking projects can have one of the following visibility levels: private, internal, or public. Private projects are visible only to project members, internal projects are visible to all users that are logged in to IBM Cloud, and public projects are visible to anyone. By default, new projects are set to private visibility level, which is the most secure for your data."
|
| 218 |
-
}
|
| 219 |
-
],
|
| 220 |
-
"extra_body": {
|
| 221 |
-
"documents": [
|
| 222 |
-
{
|
| 223 |
-
"doc_id": "0",
|
| 224 |
-
"text": "Git Repos and Issue Tracking is an IBM-hosted component of the Continuous Delivery service. All of the data that you provide to Git Repos and Issue Tracking, including but not limited to source files, issues, pull requests, and project configuration properties, is managed securely within Continuous Delivery. However, Git Repos and Issue Tracking supports various mechanisms for exporting, sending, or otherwise sharing data to users and third parties. The ability of Git Repos and Issue Tracking to share information is typical of many social coding platforms. However, such sharing might conflict with regulatory controls that apply to your business. After you create a project in Git Repos and Issue Tracking, but before you entrust any files, issues, records, or other data with the project, review the project settings and change any settings that you deem necessary to protect your data. Settings to review include visibility levels, email notifications, integrations, web hooks, access tokens, deploy tokens, and deploy keys. Project visibility levels \n\nGit Repos and Issue Tracking projects can have one of the following visibility levels: private, internal, or public. * Private projects are visible only to project members. This setting is the default visibility level for new projects, and is the most secure visibility level for your data. * Internal projects are visible to all users that are logged in to IBM Cloud. * Public projects are visible to anyone. To limit project access to only project members, complete the following steps:\n\n\n\n1. From the project sidebar, click Settings > General. 2. On the General Settings page, click Visibility > project features > permissions. 3. Locate the Project visibility setting. 4. Select Private, if it is not already selected. 5. Click Save changes. Project membership \n\nGit Repos and Issue Tracking is a cloud hosted social coding environment that is available to all Continuous Delivery users. If you are a Git Repos and Issue Tracking project Maintainer or Owner, you can invite any user and group members to the project. IBM Cloud places no restrictions on who you can invite to a project."
|
| 225 |
-
},
|
| 226 |
-
{
|
| 227 |
-
"doc_id": "1",
|
| 228 |
-
"text": "After you create a project in Git Repos and Issue Tracking, but before you entrust any files, issues, records, or other data with the project, review the project settings and change any settings that are necessary to protect your data. Settings to review include visibility levels, email notifications, integrations, web hooks, access tokens, deploy tokens, and deploy keys. Project visibility levels \n\nGit Repos and Issue Tracking projects can have one of the following visibility levels: private, internal, or public. * Private projects are visible only to project members. This setting is the default visibility level for new projects, and is the most secure visibility level for your data. * Internal projects are visible to all users that are logged in to IBM Cloud. * Public projects are visible to anyone. To limit project access to only project members, complete the following steps:\n\n\n\n1. From the project sidebar, click Settings > General. 2. On the General Settings page, click Visibility > project features > permissions. 3. Locate the Project visibility setting. 4. Select Private, if it is not already selected. 5. Click Save changes. Project email settings \n\nBy default, Git Repos and Issue Tracking notifies project members by way of email about project activities. These emails typically include customer-owned data that was provided to Git Repos and Issue Tracking by users. For example, if a user posts a comment to an issue, Git Repos and Issue Tracking sends an email to all subscribers. The email includes information such as a copy of the comment, the user who posted it, and when the comment was posted. To turn off all email notifications for your project, complete the following steps:\n\n\n\n1. From the project sidebar, click Settings > General. 2. On the **General Settings **page, click Visibility > project features > permissions. 3. Select the Disable email notifications checkbox. 4. Click Save changes. Project integrations and webhooks"
|
| 229 |
-
}
|
| 230 |
-
]
|
| 231 |
-
}
|
| 232 |
-
}
|
| 233 |
-
|
| 234 |
-
# Add additional parameters
|
| 235 |
-
request_json["model"] = intrinsic_name
|
| 236 |
-
request_json["temperature"] = 0.0
|
| 237 |
-
|
| 238 |
-
# Apply input processor
|
| 239 |
-
rewritten_request = rewriter.transform(request_json)
|
| 240 |
-
|
| 241 |
-
# Load the base model and merge LoRA weights
|
| 242 |
-
model, tokenizer = granite_common.util.load_transformers_lora(lora_dir)
|
| 243 |
-
if use_cuda:
|
| 244 |
-
model = model.cuda()
|
| 245 |
-
|
| 246 |
-
# Convert the chat completion request into a the Transformers library's proprietary
|
| 247 |
-
# format.
|
| 248 |
-
generate_input, other_input = (
|
| 249 |
-
granite_common.util.chat_completion_request_to_transformers_inputs(
|
| 250 |
-
rewritten_request,
|
| 251 |
-
tokenizer,
|
| 252 |
-
model,
|
| 253 |
-
)
|
| 254 |
-
)
|
| 255 |
-
|
| 256 |
-
# Use the Transformers library's APIs to generate one or more completions,
|
| 257 |
-
# then convert those completions into OpenAI-compatible chat completion
|
| 258 |
-
responses = granite_common.util.generate_with_transformers(
|
| 259 |
-
tokenizer, model, generate_input, other_input
|
| 260 |
-
)
|
| 261 |
-
|
| 262 |
-
# Apply output processor
|
| 263 |
-
transformed_responses = result_processor.transform(responses, rewritten_request)
|
| 264 |
-
|
| 265 |
-
# Verify that the contents of the completion is valid JSON and pretty-print the JSON.
|
| 266 |
-
parsed_contents = json.loads(transformed_responses.choices[0].message.content)
|
| 267 |
-
print("JSON output:")
|
| 268 |
-
print(json.dumps(parsed_contents, indent=2))
|
| 269 |
-
```
|
| 270 |
|
| 271 |
## Training Details
|
| 272 |
|
|
|
|
| 35 |
|
| 36 |
## Quickstart Example
|
| 37 |
|
| 38 |
+
The recommended way to call this intrinsic is through the [Mellea](https://mellea.ai) framework.
|
| 39 |
+
Here is some example code for calling this intrinsic from Mellea:
|
| 40 |
+
```
|
| 41 |
+
from mellea.backends.huggingface import LocalHFBackend
|
| 42 |
+
from mellea.stdlib.base import ChatContext, Document
|
| 43 |
+
from mellea.stdlib.chat import Message
|
| 44 |
+
from mellea.stdlib.intrinsics import rag
|
| 45 |
+
import json
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
backend = LocalHFBackend(model_id="ibm-granite/granite-3.3-2b-instruct")
|
| 49 |
+
context = (
|
| 50 |
+
ChatContext()
|
| 51 |
+
.add(Message("assistant", "Hello there, how can I help you?"))
|
| 52 |
+
.add(Message("user", "Tell me about some yellow fish."))
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
assistant_response = "Purple bumble fish are yellow. Green bumble fish are also yellow."
|
| 56 |
+
|
| 57 |
+
documents = [
|
| 58 |
+
Document(
|
| 59 |
+
doc_id="1",
|
| 60 |
+
text="The only type of fish that is yellow is the purple bumble fish.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
+
]
|
| 63 |
|
| 64 |
+
result = rag.flag_hallucinated_content(assistant_response, documents, context, backend)
|
| 65 |
+
print(f"Result of hallucination check: {json.dumps(result, indent=2)}")
|
|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