Views
No views yet

Y indicates hallucination, and N no hallucination.Y (unsafe), and N otherwise.
The Safety Exception intrinsic was designed as a binary classifier that analyses the user’s prompt to detect a variety of harms that include: violence, threats, sexual and explicit content and requests to obtain private identifiable information.assistant role).certainty role (use "certainty" as the role in the chat template, or simply append <|start_of_role|>certainty<|end_of_role|> and continue generating), see examples below.You are an AI language model developed by IBM Research. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and you follow ethical guidelines and promote positive behavior.
You can further augment this system prompts for a given use case or task, but it is recommended your system prompt always starts with this string.assistant role).hallucination role (use "hallucination" as the role in the chat template, or simply append <|start_of_role|>hallucination<|end_of_role|> and continue generating), see examples below.Y or N.safety role (use "safety" as the role in the chat template, or simply append <|start_of_role|>safety<|end_of_role|> and continue generating), see examples below.Y (unsafe) or N (safe).
API_BASE (insert the host address here), this uses the PDL language to implement the RAG intrinsic invocation scenario described above.
Note that the hosted instance must be supported by LiteLLM (https://docs.litellm.ai/docs/providers)intrinsics.pdl with the following content.defs:
system_prompt: "You are an AI language model developed by IBM Research. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and you follow ethical guidelines and promote positive behavior."
rag_prompt: "Provide a short response to the user's question based on the information present in the documents. If the documents lack the necessary details, inform the user that the question cannot be answered."
document:
|
Disability housing grants for Veterans
We offer housing grants for Veterans and service members with certain service - connected disabilities so they can buy or change a home to meet their needs and live more independently. Changing a home might involve installing ramps or widening doorways. Find out if you re eligible for a disability housing grant and how to apply.
Can I get a Specially Adapted Housing (SAH) grant and how much funding does this grant offer?
You may be able to get an SAH grant if you re using the grant money to buy, build, or change your permanent home a home you plan to live in for a long time and you meet both of the requirements listed below. Both of these must be true. You: Own or will own the home , and Have a qualifying service - connected disability Qualifying service - connected disabilities include : The loss or loss of use of more than one limb The loss or loss of use of a lower leg along with the residuals lasting effects of an organic natural disease or injury Blindness in both eyes having only light perception along with the loss or loss of use of a leg Certain severe burns The loss or loss of use of one or both lower extremities feet or legs after September 11 , 2001, that makes it so you can t balance or walk without the help of braces, crutches, canes, or a wheelchair Note : Only 30 Veterans and service members each fiscal year FY can qualify for a grant based on the loss of extremities after September 11 , 2001. If you qualify for but don t receive a grant in 2019 because the cap was reached , you may be able to use this benefit in FY 2020 or future years if the law continues to give us the authority to offer these grants and we don t go beyond the new FY cap. For FY 2019 , you may be able to get up to 3 grants for a total of up to $85,645 through the SAH grant program. Learn more about how to apply for a housing grant
Can I get a Special Housing Adaptation (SHA) grant and how much funding does this grant offer?
You may be able to get an SHA grant if you re using the grant money to buy, build, or change your permanent home a home you plan to live in for a long time and you meet both of the requirements listed below. Both of these must be true : You or a family member own or will own the home , and You have a qualifying service - connected disability Qualifying service - connected disabilities include : Blindness in both eyes with 20/200 visual acuity or less The loss or loss of use of both hands Certain severe burns Certain respiratory or breathing injuries For FY 2019 , you may be able to get up to 3 grants for a total of up to $17,130 through the SHA grant program. Learn more about how to apply for a housing grant \n\nWhat if I need money to make changes to a family member s home that I m living in for a short period of time? \nYou may be able to get a Temporary Residence Adaptation TRA grant if you meet both of the requirements listed below. Both of these must be true. You: Qualify for an SAH or SHA grant see above , and Are living temporarily in a family member s home that needs changes to meet your needs If you qualify for an SAH grant , you can get up to $37,597 through the TRA grant program for FY 2019. If you qualify for an SHA grant , you can get up to $6,713 through the TRA grant program for FY 2019.
Apply for an SAH, SHA, or TRA grant
You can apply online right now by going to our eBenefits website. You ll need to sign in to eBenefits with your DS Logon basic or premium account. If you don t have a DS Logon account , you can register for one on the site. Go to eBenefits to apply.
query: How much funding does the SAH grant offer?
text:
- include: intrinsics-defs.pdl
- defs:
safe:
call: get_safety
args:
query: ${ query }
- role: system
text: ${ system_prompt }
contribute: [context]
- if: ${ safe == "Y" }
then:
text:
- "\n\nDocuments: ${ document }\n\n ${ query }\n"
- model: openai/granite-intrinsics-3.1-8b-instruct-v0.1
def: answer
parameters: {api_key: EMPTY, api_base: API_BASE, temperature: 0, stop: "\n"}
- defs: ## Implicit fork of context
certainty:
call: get_certainty
hallucination:
call: get_hallucination
- "\nCertainty: ${ certainty }"
- "\nHallucination: ${ hallucination }"intrinsics-defs.pdl with the following content.defs:
apply_template:
function:
context: [{role: str, content: str}]
return:
text:
for:
c: ${ context }
repeat:
text:
- <|start_of_role|>${ c.role }<|end_of_role|>
- ${ c.content }
- <|end_of_text|>
join:
with: "\n"
get_intrinsic:
function:
intrinsic: str
return:
lastOf:
- call: apply_template
def: mycontext
args:
context: ${ pdl_context }
- model: openai/granite-intrinsics-3.1-8b-instruct-v0.1
parameters:
api_key: EMPTY
api_base: API_BASE
temperature: 0
max_tokens: 1
custom_llm_provider: text-completion-openai
prompt:
|
${ mycontext }
<|start_of_role|>${ intrinsic }<|end_of_role|>
get_safety:
function:
query: str
return:
lastOf:
- ${ query }
- call: apply_template
def: mycontext
args:
context: ${ pdl_context }
- call: get_intrinsic
args:
intrinsic: safety
get_hallucination:
function:
return:
call: get_intrinsic
args:
intrinsic: hallucination
get_certainty:
function:
return:
call: get_intrinsic
args:
intrinsic: certaintypdl intrinsics.pdl after installing the PDL CLI (pip install prompt-declaration-language).1
2import sglang as sgl
3from sglang.lang.chat_template import get_chat_template
4
5
6
7
8# Input data
9system_prompt = "You are an AI language model developed by IBM Research. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and you follow ethical guidelines and promote positive behavior."
10
11
12@sgl.function
13def safety_check (s, question):
14 s += sgl.system (system_prompt)
15 s += sgl.user(question)
16 s += "<|start_of_role|>safety<|end_of_role|>" + sgl.gen("safety", temperature=0, max_tokens=1)
17
18 # print ("\n====== Safety check state =======\n")
19 # print (s)
20 # print ("\n")
21
22rag_prompt = "Provide a short response to the user's question based on the information present in the documents. If the documents lack the necessary details, inform the user that the question cannot be answered."
23
24document = """Disability housing grants for Veterans
25We offer housing grants for Veterans and service members with certain service - connected disabilities so they can buy or change a home to meet their needs and live more independently. Changing a home might involve installing ramps or widening doorways. Find out if you re eligible for a disability housing grant and how to apply.
26
27Can I get a Specially Adapted Housing (SAH) grant and how much funding does this grant offer?
28You may be able to get an SAH grant if you re using the grant money to buy, build, or change your permanent home a home you plan to live in for a long time and you meet both of the requirements listed below. Both of these must be true. You: Own or will own the home , and Have a qualifying service - connected disability Qualifying service - connected disabilities include : The loss or loss of use of more than one limb The loss or loss of use of a lower leg along with the residuals lasting effects of an organic natural disease or injury Blindness in both eyes having only light perception along with the loss or loss of use of a leg Certain severe burns The loss or loss of use of one or both lower extremities feet or legs after September 11 , 2001, that makes it so you can t balance or walk without the help of braces, crutches, canes, or a wheelchair Note : Only 30 Veterans and service members each fiscal year FY can qualify for a grant based on the loss of extremities after September 11 , 2001. If you qualify for but don t receive a grant in 2019 because the cap was reached , you may be able to use this benefit in FY 2020 or future years if the law continues to give us the authority to offer these grants and we don t go beyond the new FY cap. For FY 2019 , you may be able to get up to 3 grants for a total of up to $85,645 through the SAH grant program. Learn more about how to apply for a housing grant
29
30Can I get a Special Housing Adaptation (SHA) grant and how much funding does this grant offer?
31You may be able to get an SHA grant if you re using the grant money to buy, build, or change your permanent home a home you plan to live in for a long time and you meet both of the requirements listed below. Both of these must be true : You or a family member own or will own the home , and You have a qualifying service - connected disability Qualifying service - connected disabilities include : Blindness in both eyes with 20/200 visual acuity or less The loss or loss of use of both hands Certain severe burns Certain respiratory or breathing injuries For FY 2019 , you may be able to get up to 3 grants for a total of up to $17,130 through the SHA grant program. Learn more about how to apply for a housing grant \n\nWhat if I need money to make changes to a family member s home that I m living in for a short period of time? \nYou may be able to get a Temporary Residence Adaptation TRA grant if you meet both of the requirements listed below. Both of these must be true. You: Qualify for an SAH or SHA grant see above , and Are living temporarily in a family member s home that needs changes to meet your needs If you qualify for an SAH grant , you can get up to $37,597 through the TRA grant program for FY 2019. If you qualify for an SHA grant , you can get up to $6,713 through the TRA grant program for FY 2019.
32
33Apply for an SAH, SHA, or TRA grant
34You can apply online right now by going to our eBenefits website. You ll need to sign in to eBenefits with your DS Logon basic or premium account. If you don t have a DS Logon account , you can register for one on the site. Go to eBenefits to apply.
35"""
36
37query = "How much funding does the SAH grant offer?"
38
39
40# The following function processes a chat between a user and an assistant.
41# For simplicity, this assumes a fixed document, but in a true RAG setting, the
42# documents will be retrieved dynamically based on the user turns.
43@sgl.function
44def main_chat_flow (s, doc, query):
45 s += sgl.system (system_prompt)
46
47 # Safety check on query
48 state = safety_check (question=query)
49 print (f"Safety Output: {state["safety"]} for question: {query}\n")
50
51 # RAG answer generation
52 s += sgl.user(rag_prompt + "\n\nDocuments: " + doc + "\n\n" + query)
53 s += sgl.assistant (sgl.gen ("answer", stop="\n", temperature=0, max_tokens=200))
54 answer = s["answer"]
55 print (f"Assistant: {answer}\n")
56
57 # Hallucination check in parallel with uncertainty quantification for the generated answer
58 forks = s.fork(2)
59 for i, f in enumerate(forks):
60 if (i == 0):
61 f += "<|start_of_role|>hallucination<|end_of_role|>"
62 f += sgl.gen("hallucination", temperature=0, max_tokens=1)
63 # print ("\n====== Fork 0 state =======\n")
64 # print (f)
65 # print ("\n")
66 else:
67 f += "<|start_of_role|>certainty<|end_of_role|>"
68 f += sgl.gen("certainty", temperature=0, max_tokens=1)
69 # print ("\n====== Fork 1 state =======\n")
70 # print (f)
71 # print ("\n")
72
73 print (f"Hallucination Output: {forks [0]["hallucination"]} for answer: {answer}\n")
74 print (f"Certainty Output: {forks [1]["certainty"]} for answer: {answer}\n")
75
76
77
78if __name__ == "__main__":
79 model_path = "ibm-granite/granite-3.1-8b-lora-intrinsics-v0.1"
80
81 # Setting the model_path to the granite model, and chat template to be the granite template
82 # This assumes "granite3-instruct" chat template has been registered in "sglang/lang/chat_template.py"
83 runtime = sgl.Runtime(model_path=model_path)
84 runtime.endpoint.chat_template = get_chat_template("granite3-instruct")
85 sgl.set_default_backend(runtime)
86
87 state = main_chat_flow (doc=document, query=query)
88
