DSPy
Wrap DSPy language model calls with Nyraxis to enforce policies on all predictions.
Setup
pip install dspy-ai nyraxisexport NYRAXIS_API_KEY="your-api-key"
export NYRAXIS_PROJECT_ID="your-project-id"Code Example
import dspy
from nyraxis import Nyraxis
nx = Nyraxis()
lm = dspy.LM("openai/gpt-4")
dspy.configure(lm=lm)
class GuardedQA(dspy.Module):
def __init__(self):
self.generate = dspy.ChainOfThought("question -> answer")
def forward(self, question):
result = nx.evaluate(question)
if result.blocked:
return dspy.Prediction(answer="Request blocked by policy.")
prediction = self.generate(question=question)
output_result = nx.evaluate(prediction.answer, direction="output")
if output_result.blocked:
return dspy.Prediction(answer="Response blocked by policy.")
return prediction
qa = GuardedQA()
response = qa(question="What is machine learning?")
print(response.answer)What Gets Protected
- All input questions are evaluated before being sent to the language model
- Generated outputs are checked against policies before being returned
- Works with any DSPy module (ChainOfThought, ReAct, Predict)
- Policies apply consistently across compiled and uncompiled programs