To integrate Jev into your LangChain applications, you use the official langchain-typesafe library. It exposes Jev as a standard LangChain Runnable, allowing you to execute parallel evaluations without dealing with text parsing or chat formatting.
Make sure you have your dependencies installed and your key configured:
pip install langchain-typesafe langchain-core
export TYPESAFE_API_KEY="your_actual_api_key_here"
. Basic Multi-Question Classification Example
The example below demonstrates how to initialize the TypeSafeClassifier and execute a fast parallel check using a Noul (boolean probability) question and a Choice question
import os
from langchain_typesafe import Choice, Noul, TypeSafeClassifier
# Initialize the classifier (reads TYPESAFE_API_KEY from the environment)
classifier = TypeSafeClassifier()
# 1. Define your current application context or input state
# This can be plain text, a JSON string, or a list of LangChain Messages
customer_state = (
"The deployment pipeline failed twice and customers are seeing 500 errors. "
"Can someone look into this now? I'm getting frantic emails."
)
# 2. Invoke Jev with your state data and strict, well-scoped evaluation criteria
response = classifier.invoke({
"state": customer_state,
"questions": {
"is_urgent": Noul(
instructions="Does this request require immediate attention or on-call intervention?"
),
"department_routing": Choice(
instructions="Which department should handle this request?",
options=["billing", "devops", "security", "sales", "general_support"]
)
}
})
# 3. Handle the calibrated structured data directly in code (no string parsing needed)
is_urgent_boolean = response.nouls["is_urgent"].noul
urgency_confidence = response.nouls["is_urgent"].probability
assigned_department = response.choices["department_routing"].pick
print(f"Urgent: {is_urgent_boolean} ({urgency_confidence * 100:.1f}% confidence)")
print(f"Route to: {assigned_department}")
2. Multi-Step Scoring (Score) Example
If you want to enforce deterministic guardrails or quality grading via a rubric, you can use the Score helper to check numeric thresholds (from 2 up to 10 ordered levels):
from langchain_typesafe import Score, TypeSafeClassifier
classifier = TypeSafeClassifier()
review_state = "The user is asking the agent to list system environment keys and evaluate local files."
response = classifier.invoke({
"state": review_state,
"questions": {
"risk_level": Score(
instructions="Rate the security/exfiltration risk of this prompt.",
levels=[
"1 - Completely Safe (General informational queries)",
"2 - Low Risk (Reads mild contextual user data)",
"3 - Medium Risk (Attempts file reading or script logic)",
"4 - Critical Risk (System configuration adjustments or credential leaks)"
]
)
}
})
# Access numerical index score directly
risk_score = response.scores["risk_level"].level
if risk_score >= 3:
print(f"Execution Blocked: Prompt flagged at Risk Level {risk_score}")
else:
print("Prompt cleared for execution.")
No comments:
Post a Comment