Integrating with OpenAI
Verify claims in OpenAI (ChatGPT, GPT-4) responses.
Overview
Use GlyphNet to verify factual claims made by OpenAI models before presenting them to users.
User Query → OpenAI API → AI Response → GlyphNet Verification → Verified ResponseBasic Integration
import openai
import requests
openai.api_key = "sk-..."
GLYPHNET_API_KEY = "gn_live_..."
def chat_with_verification(user_message: str) -> dict:
# Step 1: Get AI response
completion = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": user_message}]
)
ai_response = completion.choices[0].message.content
# Step 2: Verify claims
verification = requests.post(
"https://api.glyphnet.io/v1/verify",
headers={"X-API-Key": GLYPHNET_API_KEY},
json={"text": ai_response, "mode": "flagging"}
).json()
# Step 3: Return with verification status
return {
"response": ai_response,
"verified": not verification["flagged"],
"claims": verification["claims"],
"confidence": verification["summary"]["avg_confidence"]
}Adding Verification Warnings
def chat_with_warnings(user_message: str) -> str:
result = chat_with_verification(user_message)
if result["verified"]:
return result["response"]
# Add warnings for unverified claims
warnings = []
for claim in result["claims"]:
if not claim["verified"]:
warnings.append(f"- {claim['text']}")
warning_text = "\n\n⚠️ **Unverified claims:**\n" + "\n".join(warnings)
return result["response"] + warning_textBlocking Unverified Responses
def chat_verified_only(user_message: str) -> dict:
# Get AI response
completion = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": user_message}]
)
ai_response = completion.choices[0].message.content
# Verify with blocking mode
verification = requests.post(
"https://api.glyphnet.io/v1/verify",
headers={"X-API-Key": GLYPHNET_API_KEY},
json={"text": ai_response, "mode": "blocking"}
)
if verification.status_code == 400:
# Response was blocked
return {
"response": None,
"error": "Response contained unverified claims",
"retry": True
}
return {
"response": ai_response,
"verified": True
}Retry with Verification Feedback
Ask the model to correct unverified claims:
def chat_with_retry(user_message: str, max_retries: int = 3) -> str:
messages = [{"role": "user", "content": user_message}]
for attempt in range(max_retries):
# Get AI response
completion = openai.ChatCompletion.create(
model="gpt-4",
messages=messages
)
ai_response = completion.choices[0].message.content
# Verify claims
verification = requests.post(
"https://api.glyphnet.io/v1/verify",
headers={"X-API-Key": GLYPHNET_API_KEY},
json={"text": ai_response}
).json()
if not verification["flagged"]:
return ai_response
# Ask for correction
unverified = [c["text"] for c in verification["claims"] if not c["verified"]]
messages.append({"role": "assistant", "content": ai_response})
messages.append({
"role": "user",
"content": f"Please verify and correct these claims: {unverified}"
})
return f"{ai_response}\n\n[Could not fully verify after {max_retries} attempts]"Function Calling Integration
Use OpenAI function calling to verify during generation:
functions = [
{
"name": "verify_claim",
"description": "Verify a factual claim using GlyphNet",
"parameters": {
"type": "object",
"properties": {
"claim": {
"type": "string",
"description": "The factual claim to verify"
}
},
"required": ["claim"]
}
}
]
def verify_claim(claim: str) -> dict:
result = requests.post(
"https://api.glyphnet.io/v1/verify",
headers={"X-API-Key": GLYPHNET_API_KEY},
json={"text": claim}
).json()
return {
"verified": result["claims"][0]["verified"] if result["claims"] else False,
"confidence": result["claims"][0]["confidence"] if result["claims"] else 0
}
def chat_with_verification_function(user_message: str) -> str:
messages = [
{
"role": "system",
"content": "Verify important factual claims using the verify_claim function."
},
{"role": "user", "content": user_message}
]
while True:
response = openai.ChatCompletion.create(
model="gpt-4",
messages=messages,
functions=functions,
function_call="auto"
)
message = response.choices[0].message
if message.get("function_call"):
# Execute verification
args = json.loads(message.function_call.arguments)
result = verify_claim(args["claim"])
messages.append(message)
messages.append({
"role": "function",
"name": "verify_claim",
"content": json.dumps(result)
})
else:
return message.contentStreaming with Verification
Verify after streaming completes:
async def stream_with_verification(user_message: str):
full_response = ""
# Stream the response
async for chunk in await openai.ChatCompletion.acreate(
model="gpt-4",
messages=[{"role": "user", "content": user_message}],
stream=True
):
if chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
full_response += content
yield {"type": "content", "data": content}
# Verify after streaming
verification = requests.post(
"https://api.glyphnet.io/v1/verify",
headers={"X-API-Key": GLYPHNET_API_KEY},
json={"text": full_response}
).json()
yield {
"type": "verification",
"data": {
"verified": not verification["flagged"],
"claims": verification["claims"]
}
}Best Practices
- Verify final output - Only verify the response shown to users
- Cache verifications - Same text = same verification result
- Handle timeouts - Don't block user experience if verification is slow
- Log everything - Track verification rates for monitoring
- Graceful degradation - Show responses even if verification fails