Hiren122 commited on
Commit
f184aa9
·
verified ·
1 Parent(s): 3d084de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -25
app.py CHANGED
@@ -1,39 +1,93 @@
1
- from flask import Flask, request, jsonify, Response
2
  import requests
3
- import os
4
  import time
5
- import uuid
6
- import json
7
 
8
  app = Flask(__name__)
9
 
 
 
10
  ONYX_URL = "https://cloud.onyx.app/api/chat/send-chat-message"
11
- API_KEY = os.environ.get("ONYX_SECRET")
12
 
13
- @app.route('/')
14
- def home():
15
- if not API_KEY:
16
- return "Server is running, but waiting for Hiren to set the ONYX_SECRET in Settings."
17
- return "Server is Online and Connected to Onyx!"
18
-
19
- # ===================== MODELS LIST =====================
20
- @app.route('/v1/models', methods=['GET'])
21
- def list_models():
22
- return jsonify({
23
- "object": "list",
24
- "data": [
25
- {"id": "gpt-5.2", "object": "model", "owned_by": "openai"},
26
- {"id": "claude-opus-4.5", "object": "model", "owned_by": "anthropic"},
27
- {"id": "claude-sonnet-4.5", "object": "model", "owned_by": "anthropic"},
28
- {"id": "gemini-3-pro-preview", "object": "model", "owned_by": "google"},
29
- {"id": "gemini-3-flash-preview", "object": "model", "owned_by": "google"}
30
- ]
31
- })
32
 
33
- # ===================== CHAT =====================
34
  @app.route('/v1/chat/completions', methods=['POST'])
35
  def chat_proxy():
36
  data = request.json
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  if not data or 'messages' not in data:
38
  return jsonify({"error": "No messages provided"}), 400
39
 
 
1
+ import json
2
  import requests
 
3
  import time
4
+ import os
5
+ from flask import Flask, request, Response, stream_with_context
6
 
7
  app = Flask(__name__)
8
 
9
+ # CONFIGURATION: Set this in your HF Space Secrets
10
+ ONYX_API_KEY = os.getenv("ONYX_SECRET", "your_onyx_token_here")
11
  ONYX_URL = "https://cloud.onyx.app/api/chat/send-chat-message"
 
12
 
13
+ def transform_to_openai_chunk(content, model_name, finish_reason=None):
14
+ """Encapsulates content into OpenAI's SSE format."""
15
+ chunk = {
16
+ "id": f"chatcmpl-{int(time.time())}",
17
+ "object": "chat.completion.chunk",
18
+ "created": int(time.time()),
19
+ "model": model_name,
20
+ "choices": [{
21
+ "index": 0,
22
+ "delta": {"content": content} if content else {},
23
+ "finish_reason": finish_reason
24
+ }]
25
+ }
26
+ return f"data: {json.dumps(chunk)}\n\n"
 
 
 
 
 
27
 
 
28
  @app.route('/v1/chat/completions', methods=['POST'])
29
  def chat_proxy():
30
  data = request.json
31
+
32
+ # DYNAMIC PARSING:
33
+ # Logic: "Provider Name / Model Name" -> provider="Provider Name", version="Model Name"
34
+ raw_model = data.get("model", "OpenAI / gpt-4o")
35
+ if "/" in raw_model:
36
+ provider, version = [part.strip() for part in raw_model.split("/", 1)]
37
+ else:
38
+ # Fallback if the user doesn't use a slash
39
+ provider, version = "OpenAI", raw_model.strip()
40
+
41
+ messages = data.get("messages", [])
42
+ user_content = messages[-1].get("content", "") if messages else ""
43
+
44
+ onyx_payload = {
45
+ "message": user_content,
46
+ "llm_override": {
47
+ "model_provider": provider,
48
+ "model_version": version,
49
+ "temperature": data.get("temperature", 0.7)
50
+ },
51
+ "stream": True, # Force stream mode logic
52
+ "include_citations": True,
53
+ "deep_research": False,
54
+ "parent_message_id": -1,
55
+ "chat_session_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a"
56
+ }
57
+
58
+ headers = {
59
+ "Authorization": f"Bearer {ONYX_API_KEY}",
60
+ "Content-Type": "application/json"
61
+ }
62
+
63
+ def generate():
64
+ try:
65
+ with requests.post(ONYX_URL, json=onyx_payload, headers=headers, stream=True) as r:
66
+ for line in r.iter_lines():
67
+ if not line:
68
+ continue
69
+
70
+ packet = json.loads(line.decode('utf-8'))
71
+
72
+ # Onyx streams use 'message_delta' for actual text chunks
73
+ if packet.get("type") == "message_delta":
74
+ yield transform_to_openai_chunk(packet.get("delta", ""), raw_model)
75
+
76
+ # Finalize the stream for the OpenAI SDK
77
+ yield transform_to_openai_chunk(None, raw_model, finish_reason="stop")
78
+ yield "data: [DONE]\n\n"
79
+ except Exception as e:
80
+ yield f"data: {json.dumps({'error': str(e)})}\n\n"
81
+
82
+ return Response(stream_with_context(generate()), mimetype='text/event-stream')
83
+
84
+ @app.route('/')
85
+ def index():
86
+ return "Onyx OpenAI Proxy is Online. Point your SDK to /v1", 200
87
+
88
+ if __name__ == '__main__':
89
+ # HF.co listens on 7860
90
+ app.run(host='0.0.0.0', port=7860)
91
  if not data or 'messages' not in data:
92
  return jsonify({"error": "No messages provided"}), 400
93