Spaces:
Sleeping
Sleeping
File size: 8,255 Bytes
61da702 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | """
OpenAI SDK Integration Example for HallucinationGuard-Env.
This example demonstrates how to evaluate OpenAI models
(GPT-4, GPT-4o, GPT-3.5) using the HallucinationGuard environment.
Requirements:
pip install openai requests
"""
import os
from typing import Optional
import requests
# OpenAI SDK
try:
from openai import OpenAI
except ImportError:
print("Install OpenAI SDK: pip install openai")
raise
class HallucinationGuardEvaluator:
"""
Evaluate OpenAI models for hallucination resistance.
Features:
- Supports all OpenAI chat models
- Handles rate limiting gracefully
- Tracks calibration and grounding scores
"""
def __init__(
self,
env_base_url: str = "https://samsankar-hallucination-guard-env.hf.space",
openai_api_key: Optional[str] = None,
model: str = "gpt-4o-mini"
):
"""
Initialize evaluator.
Args:
env_base_url: HallucinationGuard-Env server URL
openai_api_key: OpenAI API key (or set OPENAI_API_KEY env var)
model: OpenAI model name
"""
self.env_base_url = env_base_url.rstrip('/')
self.model = model
self.client = OpenAI(api_key=openai_api_key or os.environ.get("OPENAI_API_KEY"))
# Session for environment
self.session_id = None
self.episode_id = None
def reset_environment(self, difficulty: str = "intermediate") -> dict:
"""
Start a new evaluation episode.
Args:
difficulty: Starting difficulty (beginner, intermediate, advanced)
Returns:
Initial observation with question and context
"""
response = requests.post(
f"{self.env_base_url}/reset",
json={"difficulty": difficulty}
)
response.raise_for_status()
data = response.json()
self.episode_id = data.get("episode_id")
return data
def generate_answer(self, question: str, context: str) -> dict:
"""
Generate an answer using OpenAI model.
Prompts the model to:
1. Answer ONLY from the provided context
2. Provide a confidence score
3. Cite the source quote
Args:
question: The question to answer
context: The source context
Returns:
dict with answer, confidence, source_quote
"""
prompt = f"""Answer the following question using ONLY the provided context.
IMPORTANT RULES:
1. Answer ONLY from the context - do not use outside knowledge
2. If the answer is not in the context, say "I cannot answer from the provided context"
3. Provide your confidence level (0.0-1.0)
4. Quote the exact passage from the context that supports your answer
CONTEXT:
{context}
QUESTION:
{question}
Respond in JSON format:
{{
"answer": "your answer here",
"confidence": 0.85,
"source_quote": "exact quote from context"
}}
JSON Response:"""
try:
response = self.client.chat.completions.create(
model=self.model,
messages=[
{"role": "system", "content": "You are a precise QA assistant. Always respond in valid JSON format."},
{"role": "user", "content": prompt}
],
temperature=0.1, # Low temperature for factual tasks
max_tokens=500,
response_format={"type": "json_object"}
)
import json
content = response.choices[0].message.content
result = json.loads(content)
return {
"answer": result.get("answer", ""),
"confidence": float(result.get("confidence", 0.5)),
"source_quote": result.get("source_quote", "")
}
except Exception as e:
print(f"Error generating answer: {e}")
return {
"answer": "I cannot answer from the provided context.",
"confidence": 0.3,
"source_quote": ""
}
def step(self, answer: str, confidence: float, source_quote: str = "") -> dict:
"""
Submit an answer to the environment.
Args:
answer: The answer text
confidence: Confidence level (0.0-1.0)
source_quote: Verbatim quote from context
Returns:
Observation with reward and feedback
"""
response = requests.post(
f"{self.env_base_url}/step",
json={
"answer": answer,
"confidence": confidence,
"source_quote": source_quote
}
)
response.raise_for_status()
return response.json()
def evaluate_episode(
self,
num_questions: int = 10,
difficulty: str = "intermediate"
) -> dict:
"""
Run a complete evaluation episode.
Args:
num_questions: Number of questions to evaluate
difficulty: Starting difficulty level
Returns:
Episode statistics
"""
# Reset environment
obs = self.reset_environment(difficulty=difficulty)
total_reward = 0.0
hallucinations = 0
correct = 0
for step_num in range(num_questions):
# Get current question and context
question = obs.get("question", "")
context = obs.get("context", "")
print(f"\n--- Question {step_num + 1}/{num_questions} ---")
print(f"Q: {question[:100]}...")
# Generate answer with OpenAI
answer_data = self.generate_answer(question, context)
print(f"A: {answer_data['answer'][:100]}...")
print(f"Confidence: {answer_data['confidence']:.2f}")
# Submit to environment
obs = self.step(
answer=answer_data["answer"],
confidence=answer_data["confidence"],
source_quote=answer_data["source_quote"]
)
# Track statistics
reward = obs.get("reward", 0.0)
total_reward += reward
if obs.get("is_hallucination", False):
hallucinations += 1
if obs.get("grounding_score", 0) > 0.7:
correct += 1
print(f"Reward: {reward:.3f}")
print(f"Feedback: {obs.get('feedback', '')[:100]}...")
if obs.get("done", False):
break
# Calculate final statistics
avg_reward = total_reward / max(1, step_num + 1)
hallucination_rate = hallucinations / max(1, step_num + 1)
accuracy = correct / max(1, step_num + 1)
print(f"\n=== Episode Complete ===")
print(f"Average Reward: {avg_reward:.3f}")
print(f"Hallucination Rate: {hallucination_rate:.1%}")
print(f"Accuracy: {accuracy:.1%}")
return {
"avg_reward": avg_reward,
"hallucination_rate": hallucination_rate,
"accuracy": accuracy,
"total_steps": step_num + 1
}
def main():
"""Run evaluation demo."""
import argparse
parser = argparse.ArgumentParser(description="Evaluate OpenAI models for hallucination resistance")
parser.add_argument("--model", default="gpt-4o-mini", help="OpenAI model name")
parser.add_argument("--difficulty", default="intermediate", help="Difficulty level")
parser.add_argument("--num-questions", type=int, default=5, help="Number of questions")
parser.add_argument("--env-url", default="https://samsankar-hallucination-guard-env.hf.space",
help="Environment server URL")
args = parser.parse_args()
# Check for API key
if not os.environ.get("OPENAI_API_KEY"):
print("Error: Set OPENAI_API_KEY environment variable")
return
# Run evaluation
evaluator = HallucinationGuardEvaluator(
env_base_url=args.env_url,
model=args.model
)
results = evaluator.evaluate_episode(
num_questions=args.num_questions,
difficulty=args.difficulty
)
print(f"\nFinal Results: {results}")
if __name__ == "__main__":
main() |