@@ -30,7 +30,7 @@ Embedding model → vector search → top-k candidates
3030 |
3131 v
3232 Final answer
33- (optional: GuardianCheck groundedness)
33+ (optional: guardian_check groundedness)
3434```
3535
3636---
@@ -168,32 +168,34 @@ answer = m.instruct(
168168
169169## Step 5: Check groundedness (optional)
170170
171- After generation, use [ ` GuardianCheck ` ] ( ../reference/glossary#guardiancheck ) with ` GuardianRisk.GROUNDEDNESS ` to
172- verify the answer does not hallucinate beyond the retrieved documents:
171+ After generation, use [ ` guardian_check() ` ] ( ../how-to/safety-guardrails ) with
172+ ` criteria="groundedness" ` to verify the answer does not hallucinate beyond the
173+ retrieved documents. This requires ` pip install "mellea[hf]" ` :
173174
174175``` python
175- from mellea.stdlib.requirements.safety.guardian import GuardianCheck, GuardianRisk
176+ from mellea.backends.huggingface import LocalHFBackend
177+ from mellea.stdlib.components import Message
178+ from mellea.stdlib.components.intrinsic import guardian
179+ from mellea.stdlib.context import ChatContext
176180
177- groundedness_check = GuardianCheck(
178- GuardianRisk.GROUNDEDNESS ,
179- backend_type = " ollama" ,
180- ollama_url = " http://localhost:11434" ,
181- context_text = " \n\n " .join(relevant),
181+ guardian_backend = LocalHFBackend(model_id = " ibm-granite/granite-4.0-micro" )
182+
183+ eval_ctx = (
184+ ChatContext()
185+ .add(Message(" user" , f " Document: { chr (10 ).join(relevant)} " ))
186+ .add(Message(" assistant" , str (answer)))
182187)
183188
184- results = m.validate([groundedness_check] )
185- if results[ 0 ]._result :
186- print (" Grounded answer:" , str (answer))
189+ score = guardian.guardian_check(eval_ctx, guardian_backend, criteria = " groundedness " )
190+ if score < 0.5 :
191+ print (f " Grounded answer (score: { score :.4f } ) :" , str (answer))
187192else :
188- print (" Answer may contain hallucinated content: " , results[ 0 ]._reason )
193+ print (f " Groundedness risk detected (score: { score :.4f } ) " )
189194```
190195
191- Pass the same text to ` context_text ` that you used in ` grounding_context ` —
192- this ensures the groundedness model evaluates the answer against exactly what
193- the generator was given.
194-
195- > ** Backend note:** ` GuardianCheck ` requires ` granite3-guardian:2b ` pulled in Ollama.
196- > Run ` ollama pull granite3-guardian:2b ` before using it.
196+ Include the same documents in the evaluation context that you passed to
197+ ` grounding_context ` — this ensures the groundedness model evaluates the answer
198+ against exactly what the generator was given.
197199
198200---
199201
@@ -204,8 +206,11 @@ from faiss import IndexFlatIP
204206from sentence_transformers import SentenceTransformer
205207
206208from mellea import generative, start_session
209+ from mellea.backends.huggingface import LocalHFBackend
210+ from mellea.stdlib.components import Message
211+ from mellea.stdlib.components.intrinsic import guardian
212+ from mellea.stdlib.context import ChatContext
207213from mellea.stdlib.requirements import req, simple_validate
208- from mellea.stdlib.requirements.safety.guardian import GuardianCheck, GuardianRisk
209214
210215
211216@generative
@@ -226,6 +231,9 @@ def search(query: str, docs: list[str], index: IndexFlatIP,
226231 return [docs[i] for i in indices[0 ]]
227232
228233
234+ guardian_backend = LocalHFBackend(model_id = " ibm-granite/granite-4.0-micro" )
235+
236+
229237def rag (docs : list[str ], query : str ) -> str | None :
230238 embedding_model = SentenceTransformer(" all-MiniLM-L6-v2" )
231239 index = build_index(docs, embedding_model)
@@ -245,14 +253,14 @@ def rag(docs: list[str], query: str) -> str | None:
245253 requirements = [req(" Answer only from the provided documents." )],
246254 )
247255
248- results = m.validate([GuardianCheck (
249- GuardianRisk. GROUNDEDNESS ,
250- backend_type = " ollama " ,
251- ollama_url = " http://localhost:11434 " ,
252- context_text = " \n\n " .join(relevant),
253- )] )
254- if not results[ 0 ]._result :
255- print (" Warning: groundedness check failed: " , results[ 0 ]._reason )
256+ eval_ctx = (
257+ ChatContext()
258+ .add(Message( " user " , f " Document: { chr ( 10 ).join(relevant) } " ))
259+ .add(Message( " assistant " , str (answer)))
260+ )
261+ score = guardian.guardian_check(eval_ctx, guardian_backend, criteria = " groundedness " )
262+ if score >= 0.5 :
263+ print (f " Warning: groundedness risk detected (score: { score :.4f } ) " )
256264
257265 return str (answer)
258266```
@@ -267,7 +275,7 @@ def rag(docs: list[str], query: str) -> str | None:
267275| ` is_relevant ` docstring | How strictly the filter interprets relevance | Adjust phrasing to match your domain |
268276| ` grounding_context ` key names | Tracing and debugging in spans | Use descriptive names in production |
269277| ` requirements ` on ` m.instruct() ` | Answer length, citation, tone | Add after baseline quality is good |
270- | GuardianCheck ` context_text ` | What the groundedness model checks against | Match exactly what you pass to ` grounding_context ` |
278+ | ` guardian_check ` document context | What the groundedness model checks against | Match exactly what you pass to ` grounding_context ` |
271279
272280---
273281
0 commit comments