Spaces:
Sleeping
Sleeping
File size: 5,868 Bytes
fd1a435 | 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 | # run_validation_agent.py
import asyncio
from openai import AsyncOpenAI
from schemas import ValidationResult
# ------------------------
# GPT validation via OpenAI SDK (Direct)
# ------------------------
async def validate_gpt_key(api_key: str, model: str = "gpt-4o") -> ValidationResult:
"""
Validates a GPT API key using OpenAI SDK directly.
"""
try:
# Create OpenAI client
client = AsyncOpenAI(api_key=api_key)
# Simple completion test
response = await client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are an API key validator."},
{"role": "user", "content": "ping"}
],
max_tokens=10,
temperature=0
)
# Check if we got a valid response
if response.choices and response.choices[0].message.content:
return ValidationResult(is_valid=True)
return ValidationResult(
is_valid=False,
error="No valid response from OpenAI API"
)
except Exception as e:
error_msg = str(e)
# Provide more specific error messages
if "invalid_api_key" in error_msg.lower():
error_msg = "Invalid API key"
elif "rate_limit" in error_msg.lower():
error_msg = "Rate limit exceeded"
elif "insufficient_quota" in error_msg.lower():
error_msg = "Insufficient quota/credits"
return ValidationResult(
is_valid=False,
error=f"GPT validation failed: {error_msg}"
)
# ------------------------
# Gemini (Google Generative AI) validation
# ------------------------
async def validate_gemini_key(api_key: str, model: str = "gemini-2.0-flash-exp") -> ValidationResult:
"""
Validates a Gemini API key.
"""
try:
import google.generativeai as genai
# Configure with API key
genai.configure(api_key=api_key)
# Try to generate content
model_instance = genai.GenerativeModel(model)
response = await model_instance.generate_content_async("ping")
if response and response.text:
return ValidationResult(is_valid=True)
return ValidationResult(
is_valid=False,
error="No valid response from Gemini API"
)
except Exception as e:
error_msg = str(e)
if "API_KEY_INVALID" in error_msg or "invalid" in error_msg.lower():
error_msg = "Invalid API key"
return ValidationResult(
is_valid=False,
error=f"Gemini validation failed: {error_msg}"
)
# ------------------------
# Grok (xAI) validation
# ------------------------
async def validate_grok_key(api_key: str, model: str = "grok-beta") -> ValidationResult:
"""
Validates a Grok API key using OpenAI-compatible endpoint.
"""
try:
# Grok uses OpenAI-compatible API
client = AsyncOpenAI(
api_key=api_key,
base_url="https://api.x.ai/v1"
)
response = await client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are an API key validator."},
{"role": "user", "content": "ping"}
],
max_tokens=10,
temperature=0
)
if response.choices and response.choices[0].message.content:
return ValidationResult(is_valid=True)
return ValidationResult(
is_valid=False,
error="No valid response from Grok API"
)
except Exception as e:
error_msg = str(e)
if "invalid_api_key" in error_msg.lower():
error_msg = "Invalid API key"
return ValidationResult(
is_valid=False,
error=f"Grok validation failed: {error_msg}"
)
# ------------------------
# Main runner
# ------------------------
async def run_validation_agent(model: str, api_key: str) -> ValidationResult:
"""
Validates API keys for GPT, Gemini, or Grok.
Routes to appropriate validator based on model name.
"""
model_lower = model.lower()
try:
if "gpt" in model_lower or "o1" in model_lower:
return await validate_gpt_key(api_key, model)
elif "gemini" in model_lower:
return await validate_gemini_key(api_key, model)
elif "grok" in model_lower:
return await validate_grok_key(api_key, model)
else:
return ValidationResult(
is_valid=False,
error=f"Unsupported model: {model}"
)
except Exception as e:
return ValidationResult(
is_valid=False,
error=f"Validation error: {str(e)}"
)
# ------------------------
# Optional test runner
# ------------------------
if __name__ == "__main__":
import os
from dotenv import load_dotenv
load_dotenv()
async def main():
tests = [
("gpt-4o", "OPENAI_API_KEY"),
("gemini-2.0-flash-exp", "GEMINI_API_KEY"),
("grok-beta", "GROK_API_KEY")
]
for model, key_env in tests:
key = os.getenv(key_env, "")
if not key:
print(f"⚠️ {model.upper()}: No API key found in env ({key_env})\n")
continue
print(f"Testing {model.upper()} key...")
result = await run_validation_agent(model, key)
if result.is_valid:
print(f"✅ Valid\n")
else:
print(f"❌ Invalid | {result.error}\n")
asyncio.run(main()) |