Skip to content

Commit d5d21fa

Browse files
committedNov 3, 2023
Moved hf_pipeline_llama2 under configs/llm.
1 parent 1d55abf commit d5d21fa

File tree

9 files changed

+63
-20
lines changed

9 files changed

+63
-20
lines changed
 

‎examples/__init__.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.

‎examples/configs/llm/__init__.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.

‎examples/llm/hf_pipeline_llama2/README.md ‎examples/configs/llm/hf_pipeline_llama2/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ Please install additional package via :
1111
`pip install accelerate transformers==4.33.1 --upgrade`
1212

1313

14-
The `meta-llama/Llama-2-13b-chat-hf` LLM model has been tested on the topical rails evaluation sets, results are available [here](../../../nemoguardrails/eval/README.md).
14+
The `meta-llama/Llama-2-13b-chat-hf` LLM model has been tested on the topical rails evaluation sets, results are available [here](../../../../nemoguardrails/eval/README.md).
1515
We have also tested the factchecking rail for the same model with good results.
1616
There are examples on how to use the models with a HF repo id or from a local path.
1717

1818
In this folder, the guardrails application is very basic, but anyone can change it with any other more complex configuration.
1919

20-
**Disclaimer**: The `meta-llama/Llama-2-13b-chat-hf` LLM on tested on basic usage combining a toy example of a knowledge base, further experiments of prompt engineering needs to be done on [fact-checking](./config.yml#L133-142) for more complex queries as this model may not work correctly. Thorough testing and optimizations are needed before considering a production deployment.
20+
**Disclaimer**: The `meta-llama/Llama-2-13b-chat-hf` LLM on tested on basic usage combining a toy example of a knowledge base, further experiments of prompt engineering needs to be done on [fact-checking](config.yml#L133-142) for more complex queries as this model may not work correctly. Thorough testing and optimizations are needed before considering a production deployment.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.

‎examples/llm/hf_pipeline_llama2/config.py ‎examples/configs/llm/hf_pipeline_llama2/config.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,14 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15+
import os
1516
import os.path
16-
import pickle
17-
from pathlib import Path
18-
from typing import Optional
19-
import os,sys
17+
2018
import torch
2119
from langchain import HuggingFacePipeline
22-
from langchain.chains import RetrievalQA
23-
from langchain.embeddings import HuggingFaceEmbeddings
24-
from langchain.llms import BaseLLM
25-
from langchain.text_splitter import CharacterTextSplitter
26-
from langchain.vectorstores import FAISS
2720
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
21+
2822
from nemoguardrails import LLMRails, RailsConfig
29-
from nemoguardrails.actions import action
30-
from nemoguardrails.actions.actions import ActionResult
3123
from nemoguardrails.llm.helpers import get_llm_instance_wrapper
3224
from nemoguardrails.llm.providers import register_llm_provider
3325

@@ -39,7 +31,7 @@ def _get_model_config(config: RailsConfig, type: str):
3931
return model_config
4032

4133

42-
def _load_model(model_name_or_path, device, num_gpus,hf_auth_token=None, debug=False):
34+
def _load_model(model_name_or_path, device, num_gpus, hf_auth_token=None, debug=False):
4335
"""Load an HF locally saved checkpoint."""
4436
if device == "cpu":
4537
kwargs = {}
@@ -69,9 +61,14 @@ def _load_model(model_name_or_path, device, num_gpus,hf_auth_token=None, debug=F
6961
model_name_or_path, low_cpu_mem_usage=True, **kwargs
7062
)
7163
else:
72-
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_auth_token=hf_auth_token, use_fast=False)
64+
tokenizer = AutoTokenizer.from_pretrained(
65+
model_name_or_path, use_auth_token=hf_auth_token, use_fast=False
66+
)
7367
model = AutoModelForCausalLM.from_pretrained(
74-
model_name_or_path, low_cpu_mem_usage=True,use_auth_token=hf_auth_token, **kwargs
68+
model_name_or_path,
69+
low_cpu_mem_usage=True,
70+
use_auth_token=hf_auth_token,
71+
**kwargs,
7572
)
7673

7774
if device == "cuda" and num_gpus == 1:
@@ -100,8 +97,12 @@ def init_main_llm(config: RailsConfig):
10097
model_path = model_config.parameters.get("path")
10198
device = model_config.parameters.get("device", "cuda")
10299
num_gpus = model_config.parameters.get("num_gpus", 1)
103-
hf_token=os.environ["HF_TOKEN"] # [TODO] to register this into the config.yaml as well
104-
model, tokenizer = _load_model(model_path, device, num_gpus,hf_auth_token=hf_token, debug=False)
100+
hf_token = os.environ[
101+
"HF_TOKEN"
102+
] # [TODO] to register this into the config.yaml as well
103+
model, tokenizer = _load_model(
104+
model_path, device, num_gpus, hf_auth_token=hf_token, debug=False
105+
)
105106

106107
# repo_id="TheBloke/Wizard-Vicuna-13B-Uncensored-HF"
107108
# pipe = pipeline("text-generation", model=repo_id, device_map={"":"cuda:0"}, max_new_tokens=256, temperature=0.1, do_sample=True,use_cache=True)

‎examples/llm/hf_pipeline_llama2/config.yml ‎examples/configs/llm/hf_pipeline_llama2/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,5 @@ prompts:
138138
You are given a task to identify if the hypothesis is grounded and entailed to the evidence.
139139
You will only use the contents of the evidence and not rely on external knowledge.
140140
<</SYS>>
141-
141+
142142
[INST]Answer with yes/no. "evidence": {{ evidence }} "hypothesis": {{ response }} "entails":[/INST]

‎examples/llm/hf_pipeline_llama2/general.co ‎examples/configs/llm/hf_pipeline_llama2/general.co

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ define user ask additional help
3838
define bot offer additional help
3939
"If you have any more questions or if there's anything else I can help you with, please don't hesitate to ask."
4040

41-
define flow
41+
define flow
4242
user ask additional help
4343
bot offer additional help
4444

0 commit comments

Comments
 (0)