Spaces:
Sleeping
Onyx.py
Browse filesfrom flask import Flask, request, jsonify
import requests
import os
import time
import uuid
app = Flask(__name__)
# --- CONFIGURATION ---
# We look for the secret key named "ONYX_SECRET" in the Space Settings.
# set it in env
ONYX_URL = "https://cloud.onyx.app/api/chat/send-chat-message"
API_KEY = os.environ.get("ONYX_SECRET")
@app .route('/')
def home():
if not API_KEY:
return "Server is running, but waiting for Hiren to set the ONYX_SECRET in Settings."
return "Server is Online and Connected to Onyx!"
# This makes it compatible with OpenAI apps
@app .route('/v1/chat/completions', methods=['POST'])
def chat_proxy():
# 1. Get the message from the user
data = request.json
# If no message sent, error out
if not data or 'messages' not in data:
return jsonify({"error": "No messages provided"}), 400
# Get the last message the user typed
user_input = data['messages'][-1]['content']
# --- BELOW IS HIREN'S EXACT CODE LOGIC ---
# We just replaced "Hi" with user_input
payload = {
"message": user_input,
"llm_override": {
"model_provider": "OpenAI",
"model_version": "gpt-5.2",
"temperature": data.get('temperature', 0.7) # Use user's temperature or default
},
"allowed_tool_ids": [123],
"forced_tool_id": 123,
"file_descriptors": [],
"internal_search_filters": {
"source_type": ["ingestion_api"],
"document_set": ["<string>"],
"time_cutoff": "2023-11-07T05:31:56Z",
"tags": [{"tag_key": "<string>", "tag_value": "<string>"}]
},
"deep_research": False,
"origin": "unset",
"parent_message_id": -1,
"chat_session_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"chat_session_info": {
"persona_id": 0,
"description": "<string>",
"project_id": 123
},
"stream": False,
"include_citations": True
}
headers = {
"Authorization": API_KEY, # This uses the key Hiren will set
"Content-Type": "application/json"
}
try:
# Send to Onyx
response = requests.post(ONYX_URL, json=payload, headers=headers)
# Check if Onyx worked
if response.status_code == 200:
# We assume Onyx returns the text directly in the body
bot_text = response.text
# Format it so it looks like OpenAI (Required for "OpenAI Compatible")
return jsonify({
"id": f"chatcmpl-{uuid.uuid4()}",
"object": "chat.completion",
"created": int(time.time()),
"model": "gpt-5.2",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": bot_text
},
"finish_reason": "stop"
}]
})
else:
return jsonify({"error": f"Onyx Error: {response.text}"}), response.status_code
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)
- RADISON.py +0 -0
|
File without changes
|