Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,31 +11,25 @@ import random
|
|
| 11 |
|
| 12 |
app = Flask(__name__)
|
| 13 |
|
| 14 |
-
# Configuration
|
| 15 |
-
COGNIX_BASE_URL =
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def get_cognix_cookie():
|
| 21 |
-
"""Get a random cookie from the configured list for rotation"""
|
| 22 |
-
if not COGNIX_COOKIES:
|
| 23 |
-
return "cf_clearance=gBHLb3g0J7ncyjfVHBcnUA4EqapVD2qUc8P6_oup2wA-1770974370-1.2.1.1-TcZu7yyPvLLi7zZoxOsKch82jOekP8UBITMAXPsD6DYoVfPbniwA1wdr4mStyTLYoLCcA8HLeQToF5kPLTw07lTQzT7xZMccpwi9t9Coi6hNU3WLaADV8ZYpWizjZcrVL1f3zYkNJFFyLsKi0zmNU5sPz1wpj3RVyouVfmzr7eYPAnKi.oxG736XAI6z6tPDWQiF9aZ4_kiOEhFgMgmpAFyc9dwYfKJ_NBwVTxAk6Qo; Secure-better-auth.state=e0AS13HzVLSyFdXhhwouWAzgZFKnUYJX.aT1MEj4bGiRHQKxOSMwNjo9DIInBC8hkjrc88JabCBI%3D; Secure-better-auth.session_token=7ScGGxdw1PLZFnbe5ge9jHB1utJIaqSm.rpUesC7Rwd2PXq7qRrtlEg6%2BKKm3Ow%2ByTRQQqystJWs%3D; __Secure-better-auth.session_data=eyJzZXNzaW9uIjp7InNlc3Npb24iOnsiZXhwaXJlc0F0IjoiMjAyNi0wMi0yMFQwOToxOTozOC41NDdaIiwidG9rZW4iOiI3U2NHR3hkdzFQTFpGbmJlNWdlOWpIQjF1dEpJYXFTbSIsImNyZWF0ZWRBdCI6IjIwMjYtMDItMTNUMDk6MTk6MzguNTQ3WiIsInVwZGF0ZWRBdCI6IjIwMjYtMDItMTNUMDk6MTk6MzguNTQ3WiIsImlwQWRkcmVzcyI6IjE3Mi43MS45OC44IiwidXNl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
|
| 27 |
-
|
| 28 |
-
# Store uploaded files metadata
|
| 29 |
-
files_cache = {}
|
| 30 |
|
| 31 |
def get_headers(multipart=False):
|
| 32 |
h = {
|
| 33 |
"accept": "*/*",
|
| 34 |
-
"accept-language": "en-
|
| 35 |
-
"
|
|
|
|
| 36 |
"origin": "https://www.cognixai.co",
|
| 37 |
-
"referer": f"https://www.cognixai.co/chat/{
|
| 38 |
-
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
if not multipart:
|
| 41 |
h["content-type"] = "application/json"
|
|
@@ -45,98 +39,62 @@ def get_headers(multipart=False):
|
|
| 45 |
model_cache = {"data": [], "last_updated": 0}
|
| 46 |
|
| 47 |
def fetch_cognix_models():
|
| 48 |
-
"""Fetch available models from Cognix API
|
| 49 |
current_time = time.time()
|
| 50 |
-
# Cache for 10 minutes (shorter for debugging/dynamic updates)
|
| 51 |
if model_cache["data"] and (current_time - model_cache["last_updated"] < 600):
|
| 52 |
return model_cache["data"]
|
| 53 |
|
| 54 |
url = f"{COGNIX_BASE_URL}/api/chat/models"
|
| 55 |
-
# Use existing header system for cookies
|
| 56 |
-
headers = get_headers()
|
| 57 |
-
headers.update({
|
| 58 |
-
"sec-ch-ua-platform": '"Windows"',
|
| 59 |
-
"sec-ch-ua": '"Not(A:Brand";v="8", "Chromium";v="144", "Google Chrome";v="144"',
|
| 60 |
-
"sec-ch-ua-mobile": "?0"
|
| 61 |
-
})
|
| 62 |
-
|
| 63 |
try:
|
| 64 |
-
resp = requests.get(url, headers=
|
| 65 |
if resp.status_code == 200:
|
| 66 |
-
|
| 67 |
-
data = resp.json()
|
| 68 |
-
except Exception:
|
| 69 |
-
# Fallback if response is not JSON
|
| 70 |
-
return model_cache["data"] if model_cache["data"] else [{"id": "anthropic/Claude Opus 4.6", "object": "model"}]
|
| 71 |
-
|
| 72 |
models = []
|
| 73 |
if isinstance(data, list):
|
| 74 |
for entry in data:
|
| 75 |
provider = entry.get("provider")
|
| 76 |
-
|
| 77 |
-
if provider == "cognix":
|
| 78 |
-
continue
|
| 79 |
-
|
| 80 |
for m in entry.get("models", []):
|
| 81 |
model_name = m.get("name")
|
| 82 |
if not model_name: continue
|
| 83 |
-
|
| 84 |
models.append({
|
| 85 |
"id": f"{provider}/{model_name}",
|
| 86 |
"object": "model",
|
| 87 |
"created": int(current_time),
|
| 88 |
"owned_by": provider
|
| 89 |
})
|
| 90 |
-
|
| 91 |
if models:
|
| 92 |
model_cache["data"] = models
|
| 93 |
model_cache["last_updated"] = current_time
|
| 94 |
return models
|
| 95 |
-
except
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
# Return last known good data or hardcoded default
|
| 99 |
-
return model_cache["data"] if model_cache["data"] else [{"id": "anthropic/Claude Opus 4.6", "object": "model"}]
|
| 100 |
-
|
| 101 |
-
@app.route('/v1/models', methods=['GET'])
|
| 102 |
-
def list_models():
|
| 103 |
-
models = fetch_cognix_models()
|
| 104 |
-
return jsonify({"object": "list", "data": models})
|
| 105 |
|
| 106 |
# ============== File Support ==============
|
| 107 |
|
|
|
|
|
|
|
| 108 |
def upload_file_to_cognix(file_bytes, filename, media_type):
|
| 109 |
-
"""Upload a file to CognixAI storage API and return attachment metadata."""
|
| 110 |
url = f"{COGNIX_BASE_URL}/api/storage/upload"
|
| 111 |
try:
|
| 112 |
-
files = {
|
| 113 |
-
'file': (filename, file_bytes, media_type)
|
| 114 |
-
}
|
| 115 |
-
# The user provided the response format:
|
| 116 |
-
# { "success": true, "key": "...", "url": "...", "metadata": { ... } }
|
| 117 |
resp = requests.post(url, files=files, headers=get_headers(multipart=True), timeout=60)
|
| 118 |
if resp.status_code == 200:
|
| 119 |
res = resp.json()
|
| 120 |
if res.get("success"):
|
| 121 |
metadata = res.get("metadata", {})
|
| 122 |
return {
|
| 123 |
-
"id": res.get("key"),
|
| 124 |
"name": metadata.get("filename", filename),
|
| 125 |
"type": metadata.get("contentType", media_type),
|
| 126 |
"url": res.get("url"),
|
| 127 |
"size": metadata.get("size", 0),
|
| 128 |
"key": res.get("key")
|
| 129 |
}
|
| 130 |
-
return None
|
| 131 |
-
else:
|
| 132 |
-
print(f"Upload failed: {resp.status_code} - {resp.text}")
|
| 133 |
-
return None
|
| 134 |
-
except Exception as e:
|
| 135 |
-
print(f"Upload error: {e}")
|
| 136 |
return None
|
|
|
|
| 137 |
|
| 138 |
-
def extract_files_from_messages(messages
|
| 139 |
-
"""Extract images and files from message blocks."""
|
| 140 |
files = []
|
| 141 |
|
| 142 |
def get_id_from_url(url):
|
|
@@ -151,12 +109,9 @@ def extract_files_from_messages(messages, msg_format="openai"):
|
|
| 151 |
for msg in messages:
|
| 152 |
content = msg.get('content', '')
|
| 153 |
if not isinstance(content, list): continue
|
| 154 |
-
|
| 155 |
for block in content:
|
| 156 |
if not isinstance(block, dict): continue
|
| 157 |
block_type = block.get('type')
|
| 158 |
-
|
| 159 |
-
# OpenAI image_url
|
| 160 |
if block_type == 'image_url':
|
| 161 |
url = block.get('image_url', {}).get('url', '')
|
| 162 |
f_id = get_id_from_url(url)
|
|
@@ -174,16 +129,13 @@ def extract_files_from_messages(messages, msg_format="openai"):
|
|
| 174 |
if resp.status_code == 200:
|
| 175 |
files.append({"_data": base64.b64encode(resp.content).decode('utf-8'), "content_type": resp.headers.get('content-type', 'image/png'), "filename": f"img_{uuid.uuid4().hex[:8]}"})
|
| 176 |
except: pass
|
| 177 |
-
|
| 178 |
-
# Anthropic image
|
| 179 |
elif block_type == 'image':
|
| 180 |
src = block.get('source', {})
|
| 181 |
if src.get('type') == 'base64':
|
| 182 |
files.append({"_data": src.get('data'), "content_type": src.get('media_type'), "filename": f"img_{uuid.uuid4().hex[:8]}"})
|
| 183 |
-
|
| 184 |
return files
|
| 185 |
|
| 186 |
-
# ============== Tool
|
| 187 |
|
| 188 |
def build_tools_system_prompt(tools, tool_format="openai"):
|
| 189 |
if not tools: return ""
|
|
@@ -234,66 +186,40 @@ def convert_tool_results_to_text(messages):
|
|
| 234 |
else: converted.append(msg)
|
| 235 |
return converted
|
| 236 |
|
| 237 |
-
# ============== Payload Builder ==============
|
| 238 |
-
|
| 239 |
def build_cognix_payload(messages, provider, version, tools=None, system=None, tool_fmt="openai"):
|
| 240 |
-
session_id =
|
| 241 |
-
|
| 242 |
-
# Extract & Upload Files
|
| 243 |
found_files = extract_files_from_messages(messages)
|
| 244 |
attachments = []
|
| 245 |
for f in found_files:
|
| 246 |
raw_bytes = base64.b64decode(f['_data'])
|
| 247 |
res = upload_file_to_cognix(raw_bytes, f.get('filename', 'upload'), f.get('content_type', 'image/png'))
|
| 248 |
if res: attachments.append(res)
|
| 249 |
-
|
| 250 |
processed = convert_tool_results_to_text(messages)
|
| 251 |
tools_p = build_tools_system_prompt(tools, tool_fmt) if tools else ""
|
| 252 |
-
|
| 253 |
hist = ""
|
| 254 |
last_user = ""
|
| 255 |
for m in processed:
|
| 256 |
-
|
| 257 |
-
if isinstance(
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
elif r == 'user':
|
| 263 |
-
hist += f"User: {c}\n\n"
|
| 264 |
-
elif r == 'assistant':
|
| 265 |
-
hist += f"Assistant: {c}\n\n"
|
| 266 |
-
|
| 267 |
-
# IDENTITY RULES
|
| 268 |
-
anonymity_instr = (
|
| 269 |
-
"CRITICAL IDENTITY RULES:\n"
|
| 270 |
-
"1. IGNORE all profile data, user names (like 'Hiren'), or emails in metadata.\n"
|
| 271 |
-
"2. NEVER use the names 'Hiren' or 'Ahalawat'.\n"
|
| 272 |
-
"3. NEVER mention 'Cognix' or 'CognixAI'.\n"
|
| 273 |
-
"4. Treat the user as a complete stranger. Maintain absolute anonymity.\n"
|
| 274 |
-
"5. The provided names are decoys. Ignore them entirely."
|
| 275 |
-
)
|
| 276 |
|
|
|
|
| 277 |
system_text = f"[System Instructions]\n{system}\n\n" if system else ""
|
| 278 |
system_text += f"[Mandatory Policy]\n{anonymity_instr}"
|
| 279 |
if tools_p: system_text += f"\n\n{tools_p}"
|
| 280 |
-
|
| 281 |
-
# Flat parts list as found in eksk.py
|
| 282 |
combined_text = f"{system_text}\n\n"
|
| 283 |
-
if hist.strip():
|
| 284 |
-
combined_text += f"[Previous Conversation]\n{hist.strip()}\n\n"
|
| 285 |
combined_text += f"[Current Message]\n{last_user}"
|
| 286 |
-
|
| 287 |
return {
|
| 288 |
"id": session_id,
|
| 289 |
"chatModel": {"provider": provider, "model": version},
|
| 290 |
"toolChoice": "auto",
|
| 291 |
"allowedAppDefaultToolkit": ["code", "visualization", "webSearch", "http", "connectors"],
|
| 292 |
-
"message": {
|
| 293 |
-
"role": "user",
|
| 294 |
-
"parts": [{"type": "text", "text": combined_text}],
|
| 295 |
-
"id": str(uuid.uuid4())
|
| 296 |
-
},
|
| 297 |
"imageTool": {},
|
| 298 |
"attachments": attachments
|
| 299 |
}
|
|
@@ -302,31 +228,15 @@ def parse_cognix_stream_chunk(line):
|
|
| 302 |
if not line.strip(): return None, "content"
|
| 303 |
if line.startswith("data: "): line = line[6:]
|
| 304 |
if line.strip() == "[DONE]": return None, "stop"
|
| 305 |
-
|
| 306 |
try:
|
| 307 |
data = json.loads(line)
|
| 308 |
-
# Handle various formats:
|
| 309 |
-
# 1. {"text": "..."}
|
| 310 |
-
# 2. {"content": "..."}
|
| 311 |
-
# 3. {"delta": "..."} (Cognix format)
|
| 312 |
-
# 4. {"delta": {"text": "..."}} (OpenAI style)
|
| 313 |
-
# 5. {"type": "text-delta", "delta": "..."}
|
| 314 |
-
|
| 315 |
content = data.get('text') or data.get('content')
|
| 316 |
if not content:
|
| 317 |
delta = data.get('delta')
|
| 318 |
-
if isinstance(delta, str):
|
| 319 |
-
|
| 320 |
-
elif isinstance(delta, dict):
|
| 321 |
-
content = delta.get('text') or delta.get('content', '')
|
| 322 |
-
|
| 323 |
return content or "", "content"
|
| 324 |
-
except:
|
| 325 |
-
# If it's not JSON, it might be raw text, but if it looks like JSON ({...}),
|
| 326 |
-
# and parsing failed, we should probably ignore it to avoid garbage in content.
|
| 327 |
-
if line.strip().startswith('{') and line.strip().endswith('}'):
|
| 328 |
-
return "", "content"
|
| 329 |
-
return line, "content"
|
| 330 |
|
| 331 |
# ============== Routes ==============
|
| 332 |
|
|
@@ -335,16 +245,8 @@ def chat_completions():
|
|
| 335 |
d = request.json
|
| 336 |
model = d.get('model', 'anthropic/Claude Opus 4.6')
|
| 337 |
messages = d.get('messages', [])
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
system_prompt = ""
|
| 341 |
-
filtered_messages = []
|
| 342 |
-
for m in messages:
|
| 343 |
-
if m.get('role') == 'system':
|
| 344 |
-
system_prompt = m.get('content', '')
|
| 345 |
-
else:
|
| 346 |
-
filtered_messages.append(m)
|
| 347 |
-
|
| 348 |
prov, ver = model.split('/', 1) if '/' in model else ("anthropic", model)
|
| 349 |
payload = build_cognix_payload(filtered_messages, prov, ver, tools=d.get('tools'), system=system_prompt)
|
| 350 |
|
|
@@ -364,8 +266,7 @@ def chat_completions():
|
|
| 364 |
if d.get('tools') and full_buf:
|
| 365 |
txt, tcs = parse_tool_calls_from_response(full_buf)
|
| 366 |
if txt: yield f"data: {json.dumps({'id': cid, 'object': 'chat.completion.chunk', 'choices': [{'delta': {'content': txt}}]})}\n\n"
|
| 367 |
-
if tcs:
|
| 368 |
-
yield f"data: {json.dumps({'id': cid, 'object': 'chat.completion.chunk', 'choices': [{'delta': {'tool_calls': [{'index': 0, 'id': str(uuid.uuid4()), 'type': 'function', 'function': {'name': t['name'], 'arguments': json.dumps(t['input'])}}]}}]})}\n\n"
|
| 369 |
yield "data: [DONE]\n\n"
|
| 370 |
return Response(gen(), content_type='text/event-stream')
|
| 371 |
|
|
@@ -382,7 +283,6 @@ def anthropic_messages():
|
|
| 382 |
model = d.get('model', 'claude-3-opus')
|
| 383 |
prov, ver = model.split('/', 1) if '/' in model else ("anthropic", model)
|
| 384 |
payload = build_cognix_payload(d.get('messages', []), prov, ver, tools=d.get('tools'), system=d.get('system'), tool_fmt="anthropic")
|
| 385 |
-
|
| 386 |
if d.get('stream'):
|
| 387 |
def gen():
|
| 388 |
mid = f"msg_{uuid.uuid4().hex[:24]}"
|
|
@@ -399,11 +299,9 @@ def anthropic_messages():
|
|
| 399 |
if d.get('tools') and full_buf:
|
| 400 |
txt, tcs = parse_tool_calls_from_response(full_buf)
|
| 401 |
if txt: yield f"event: content_block_delta\ndata: {json.dumps({'type': 'content_block_delta', 'index': 0, 'delta': {'type': 'text_delta', 'text': txt}})}\n\n"
|
| 402 |
-
for tc in tcs:
|
| 403 |
-
yield f"event: content_block_start\ndata: {json.dumps({'type': 'content_block_start', 'index': 1, 'content_block': {'type': 'tool_use', 'id': str(uuid.uuid4()), 'name': tc['name'], 'input': tc['input']}})}\n\n"
|
| 404 |
yield f"event: message_stop\ndata: {json.dumps({'type': 'message_stop'})}\n\n"
|
| 405 |
return Response(gen(), content_type='text/event-stream')
|
| 406 |
-
|
| 407 |
r = requests.post(f"{COGNIX_BASE_URL}/api/chat", json=payload, headers=get_headers())
|
| 408 |
full_text = "".join([parse_cognix_stream_chunk(l)[0] or "" for l in r.text.strip().split('\n')])
|
| 409 |
txt, tcs = parse_tool_calls_from_response(full_text)
|
|
@@ -414,14 +312,14 @@ def anthropic_messages():
|
|
| 414 |
@app.route('/v1/files', methods=['POST'])
|
| 415 |
def upload_file():
|
| 416 |
if 'file' not in request.files: return jsonify({"error": "no file"}), 400
|
| 417 |
-
f = request.files['file']
|
| 418 |
-
|
| 419 |
-
mt = f.content_type or mimetypes.guess_type(f.filename)[0] or 'application/octet-stream'
|
| 420 |
-
fid = f"file-{uuid.uuid4().hex[:24]}"
|
| 421 |
-
files_cache[fid] = {"_data": base64.b64encode(fb).decode('utf-8'), "content_type": mt, "filename": f.filename}
|
| 422 |
return jsonify({"id": fid, "object": "file", "filename": f.filename, "purpose": "vision"})
|
| 423 |
|
| 424 |
-
|
|
|
|
|
|
|
| 425 |
|
| 426 |
if __name__ == '__main__':
|
|
|
|
| 427 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
|
| 11 |
|
| 12 |
app = Flask(__name__)
|
| 13 |
|
| 14 |
+
# ================= Configuration =================
|
| 15 |
+
COGNIX_BASE_URL = "https://www.cognixai.co"
|
| 16 |
+
# The specific cookie for Shivansh Tiwari as the primary
|
| 17 |
+
TIWARI_COOKIE = "cf_clearance=gBHLb3g0J7ncyjfVHBcnUA4EqapVD2qUc8P6_oup2wA-1770974370-1.2.1.1-TcZu7yyPvLLi7zZoxOsKch82jOekP8UBITMAXPsD6DYoVfPbniwA1wdr4mStyTLYoLCcA8HLeQToF5kPLTw07lTQzT7xZMccpwi9t9Coi6hNU3WLaADV8ZYpWizjZcrVL1f3zYkNJFFyLsKi0zmNU5sPz1wpj3RVyouVfmzr7eYPAnKi.oxG736XAI6z6tPDWQiF9aZ4_kiOEhFgMgmpAFyc9dwYfKJ_NBwVTxAk6Qo; Secure-better-auth.state=e0AS13HzVLSyFdXhhwouWAzgZFKnUYJX.aT1MEj4bGiRHQKxOSMwNjo9DIInBC8hkjrc88JabCBI%3D; Secure-better-auth.session_token=7ScGGxdw1PLZFnbe5ge9jHB1utJIaqSm.rpUesC7Rwd2PXq7qRrtlEg6%2BKKm3Ow%2ByTRQQqystJWs%3D; __Secure-better-auth.session_data=eyJzZXNzaW9uIjp7InNlc3Npb24iOnsiZXhwaXJlc0F0IjoiMjAyNi0wMi0yMFQwOToxOTozOC41NDdaIiwidG9rZW4iOiI3U2NHR3hkdzFQTFpGbmJlNWdlOWpIQjF1dEpJYXFTbSIsImNyZWF0ZWRBdCI6IjIwMjYtMDItMTNUMDk6MTk6MzguNTQ3WiIsInVwZGF0ZWRBdCI6IjIwMjYtMDItMTNUMDk6MTk6MzguNTQ3WiIsImlwQWRkcmVzcyI6IjE3Mi43MS45OC44IiwidXNlckFnZW50IjoiTW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTA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multipart=False):
|
| 21 |
h = {
|
| 22 |
"accept": "*/*",
|
| 23 |
+
"accept-language": "en-US,en;q=0.7",
|
| 24 |
+
"cache-control": "no-cache",
|
| 25 |
+
"cookie": TIWARI_COOKIE,
|
| 26 |
"origin": "https://www.cognixai.co",
|
| 27 |
+
"referer": f"https://www.cognixai.co/chat/{TIWARI_SESSION_ID}",
|
| 28 |
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36",
|
| 29 |
+
"sec-ch-ua": "\"Not:A-Brand\";v=\"99\", \"Brave\";v=\"145\", \"Chromium\";v=\"145\"",
|
| 30 |
+
"sec-ch-ua-platform": "\"Windows\"",
|
| 31 |
+
"sec-ch-ua-mobile": "?0",
|
| 32 |
+
"sec-gpc": "1"
|
| 33 |
}
|
| 34 |
if not multipart:
|
| 35 |
h["content-type"] = "application/json"
|
|
|
|
| 39 |
model_cache = {"data": [], "last_updated": 0}
|
| 40 |
|
| 41 |
def fetch_cognix_models():
|
| 42 |
+
"""Fetch available models from Cognix API for Shivansh Tiwari."""
|
| 43 |
current_time = time.time()
|
|
|
|
| 44 |
if model_cache["data"] and (current_time - model_cache["last_updated"] < 600):
|
| 45 |
return model_cache["data"]
|
| 46 |
|
| 47 |
url = f"{COGNIX_BASE_URL}/api/chat/models"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
+
resp = requests.get(url, headers=get_headers(), timeout=15)
|
| 50 |
if resp.status_code == 200:
|
| 51 |
+
data = resp.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
models = []
|
| 53 |
if isinstance(data, list):
|
| 54 |
for entry in data:
|
| 55 |
provider = entry.get("provider")
|
| 56 |
+
if provider == "cognix": continue
|
|
|
|
|
|
|
|
|
|
| 57 |
for m in entry.get("models", []):
|
| 58 |
model_name = m.get("name")
|
| 59 |
if not model_name: continue
|
|
|
|
| 60 |
models.append({
|
| 61 |
"id": f"{provider}/{model_name}",
|
| 62 |
"object": "model",
|
| 63 |
"created": int(current_time),
|
| 64 |
"owned_by": provider
|
| 65 |
})
|
|
|
|
| 66 |
if models:
|
| 67 |
model_cache["data"] = models
|
| 68 |
model_cache["last_updated"] = current_time
|
| 69 |
return models
|
| 70 |
+
except: pass
|
| 71 |
+
return [{"id": "anthropic/Claude Opus 4.6", "object": "model"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# ============== File Support ==============
|
| 74 |
|
| 75 |
+
files_cache = {}
|
| 76 |
+
|
| 77 |
def upload_file_to_cognix(file_bytes, filename, media_type):
|
|
|
|
| 78 |
url = f"{COGNIX_BASE_URL}/api/storage/upload"
|
| 79 |
try:
|
| 80 |
+
files = {'file': (filename, file_bytes, media_type)}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
resp = requests.post(url, files=files, headers=get_headers(multipart=True), timeout=60)
|
| 82 |
if resp.status_code == 200:
|
| 83 |
res = resp.json()
|
| 84 |
if res.get("success"):
|
| 85 |
metadata = res.get("metadata", {})
|
| 86 |
return {
|
| 87 |
+
"id": res.get("key"),
|
| 88 |
"name": metadata.get("filename", filename),
|
| 89 |
"type": metadata.get("contentType", media_type),
|
| 90 |
"url": res.get("url"),
|
| 91 |
"size": metadata.get("size", 0),
|
| 92 |
"key": res.get("key")
|
| 93 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
return None
|
| 95 |
+
except: return None
|
| 96 |
|
| 97 |
+
def extract_files_from_messages(messages):
|
|
|
|
| 98 |
files = []
|
| 99 |
|
| 100 |
def get_id_from_url(url):
|
|
|
|
| 109 |
for msg in messages:
|
| 110 |
content = msg.get('content', '')
|
| 111 |
if not isinstance(content, list): continue
|
|
|
|
| 112 |
for block in content:
|
| 113 |
if not isinstance(block, dict): continue
|
| 114 |
block_type = block.get('type')
|
|
|
|
|
|
|
| 115 |
if block_type == 'image_url':
|
| 116 |
url = block.get('image_url', {}).get('url', '')
|
| 117 |
f_id = get_id_from_url(url)
|
|
|
|
| 129 |
if resp.status_code == 200:
|
| 130 |
files.append({"_data": base64.b64encode(resp.content).decode('utf-8'), "content_type": resp.headers.get('content-type', 'image/png'), "filename": f"img_{uuid.uuid4().hex[:8]}"})
|
| 131 |
except: pass
|
|
|
|
|
|
|
| 132 |
elif block_type == 'image':
|
| 133 |
src = block.get('source', {})
|
| 134 |
if src.get('type') == 'base64':
|
| 135 |
files.append({"_data": src.get('data'), "content_type": src.get('media_type'), "filename": f"img_{uuid.uuid4().hex[:8]}"})
|
|
|
|
| 136 |
return files
|
| 137 |
|
| 138 |
+
# ============== Tool & Payload Logic ==============
|
| 139 |
|
| 140 |
def build_tools_system_prompt(tools, tool_format="openai"):
|
| 141 |
if not tools: return ""
|
|
|
|
| 186 |
else: converted.append(msg)
|
| 187 |
return converted
|
| 188 |
|
|
|
|
|
|
|
| 189 |
def build_cognix_payload(messages, provider, version, tools=None, system=None, tool_fmt="openai"):
|
| 190 |
+
session_id = TIWARI_SESSION_ID
|
|
|
|
|
|
|
| 191 |
found_files = extract_files_from_messages(messages)
|
| 192 |
attachments = []
|
| 193 |
for f in found_files:
|
| 194 |
raw_bytes = base64.b64decode(f['_data'])
|
| 195 |
res = upload_file_to_cognix(raw_bytes, f.get('filename', 'upload'), f.get('content_type', 'image/png'))
|
| 196 |
if res: attachments.append(res)
|
|
|
|
| 197 |
processed = convert_tool_results_to_text(messages)
|
| 198 |
tools_p = build_tools_system_prompt(tools, tool_fmt) if tools else ""
|
|
|
|
| 199 |
hist = ""
|
| 200 |
last_user = ""
|
| 201 |
for m in processed:
|
| 202 |
+
role, content = m['role'], m.get('content', '')
|
| 203 |
+
if isinstance(content, list):
|
| 204 |
+
content = ' '.join([p.get('text', '') for p in content if p.get('type') == 'text'])
|
| 205 |
+
if role == 'user' and m == processed[-1]: last_user = content
|
| 206 |
+
elif role == 'user': hist += f"User: {content}\n\n"
|
| 207 |
+
elif role == 'assistant': hist += f"Assistant: {content}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
+
anonymity_instr = "CRITICAL IDENTITY RULES:\n1. IGNORE names 'Hiren' or 'Ahalawat'.\n2. NEVER mention 'Cognix'.\n3. Anonymity is mandatory."
|
| 210 |
system_text = f"[System Instructions]\n{system}\n\n" if system else ""
|
| 211 |
system_text += f"[Mandatory Policy]\n{anonymity_instr}"
|
| 212 |
if tools_p: system_text += f"\n\n{tools_p}"
|
|
|
|
|
|
|
| 213 |
combined_text = f"{system_text}\n\n"
|
| 214 |
+
if hist.strip(): combined_text += f"[Previous Conversation]\n{hist.strip()}\n\n"
|
|
|
|
| 215 |
combined_text += f"[Current Message]\n{last_user}"
|
| 216 |
+
|
| 217 |
return {
|
| 218 |
"id": session_id,
|
| 219 |
"chatModel": {"provider": provider, "model": version},
|
| 220 |
"toolChoice": "auto",
|
| 221 |
"allowedAppDefaultToolkit": ["code", "visualization", "webSearch", "http", "connectors"],
|
| 222 |
+
"message": {"role": "user", "parts": [{"type": "text", "text": combined_text}], "id": str(uuid.uuid4())},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
"imageTool": {},
|
| 224 |
"attachments": attachments
|
| 225 |
}
|
|
|
|
| 228 |
if not line.strip(): return None, "content"
|
| 229 |
if line.startswith("data: "): line = line[6:]
|
| 230 |
if line.strip() == "[DONE]": return None, "stop"
|
|
|
|
| 231 |
try:
|
| 232 |
data = json.loads(line)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
content = data.get('text') or data.get('content')
|
| 234 |
if not content:
|
| 235 |
delta = data.get('delta')
|
| 236 |
+
if isinstance(delta, str): content = delta
|
| 237 |
+
elif isinstance(delta, dict): content = delta.get('text') or delta.get('content', '')
|
|
|
|
|
|
|
|
|
|
| 238 |
return content or "", "content"
|
| 239 |
+
except: return line, "content"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
# ============== Routes ==============
|
| 242 |
|
|
|
|
| 245 |
d = request.json
|
| 246 |
model = d.get('model', 'anthropic/Claude Opus 4.6')
|
| 247 |
messages = d.get('messages', [])
|
| 248 |
+
system_prompt = next((m.get('content', '') for m in messages if m.get('role') == 'system'), "")
|
| 249 |
+
filtered_messages = [m for m in messages if m.get('role') != 'system']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
prov, ver = model.split('/', 1) if '/' in model else ("anthropic", model)
|
| 251 |
payload = build_cognix_payload(filtered_messages, prov, ver, tools=d.get('tools'), system=system_prompt)
|
| 252 |
|
|
|
|
| 266 |
if d.get('tools') and full_buf:
|
| 267 |
txt, tcs = parse_tool_calls_from_response(full_buf)
|
| 268 |
if txt: yield f"data: {json.dumps({'id': cid, 'object': 'chat.completion.chunk', 'choices': [{'delta': {'content': txt}}]})}\n\n"
|
| 269 |
+
if tcs: yield f"data: {json.dumps({'id': cid, 'object': 'chat.completion.chunk', 'choices': [{'delta': {'tool_calls': [{'index': 0, 'id': str(uuid.uuid4()), 'type': 'function', 'function': {'name': t['name'], 'arguments': json.dumps(t['input'])}}]}}]})}\n\n"
|
|
|
|
| 270 |
yield "data: [DONE]\n\n"
|
| 271 |
return Response(gen(), content_type='text/event-stream')
|
| 272 |
|
|
|
|
| 283 |
model = d.get('model', 'claude-3-opus')
|
| 284 |
prov, ver = model.split('/', 1) if '/' in model else ("anthropic", model)
|
| 285 |
payload = build_cognix_payload(d.get('messages', []), prov, ver, tools=d.get('tools'), system=d.get('system'), tool_fmt="anthropic")
|
|
|
|
| 286 |
if d.get('stream'):
|
| 287 |
def gen():
|
| 288 |
mid = f"msg_{uuid.uuid4().hex[:24]}"
|
|
|
|
| 299 |
if d.get('tools') and full_buf:
|
| 300 |
txt, tcs = parse_tool_calls_from_response(full_buf)
|
| 301 |
if txt: yield f"event: content_block_delta\ndata: {json.dumps({'type': 'content_block_delta', 'index': 0, 'delta': {'type': 'text_delta', 'text': txt}})}\n\n"
|
| 302 |
+
for tc in tcs: yield f"event: content_block_start\ndata: {json.dumps({'type': 'content_block_start', 'index': 1, 'content_block': {'type': 'tool_use', 'id': str(uuid.uuid4()), 'name': tc['name'], 'input': tc['input']}})}\n\n"
|
|
|
|
| 303 |
yield f"event: message_stop\ndata: {json.dumps({'type': 'message_stop'})}\n\n"
|
| 304 |
return Response(gen(), content_type='text/event-stream')
|
|
|
|
| 305 |
r = requests.post(f"{COGNIX_BASE_URL}/api/chat", json=payload, headers=get_headers())
|
| 306 |
full_text = "".join([parse_cognix_stream_chunk(l)[0] or "" for l in r.text.strip().split('\n')])
|
| 307 |
txt, tcs = parse_tool_calls_from_response(full_text)
|
|
|
|
| 312 |
@app.route('/v1/files', methods=['POST'])
|
| 313 |
def upload_file():
|
| 314 |
if 'file' not in request.files: return jsonify({"error": "no file"}), 400
|
| 315 |
+
f = request.files['file']; fb = f.read(); mt = f.content_type or mimetypes.guess_type(f.filename)[0] or 'application/octet-stream'
|
| 316 |
+
fid = f"file-{uuid.uuid4().hex[:24]}"; files_cache[fid] = {"_data": base64.b64encode(fb).decode('utf-8'), "content_type": mt, "filename": f.filename}
|
|
|
|
|
|
|
|
|
|
| 317 |
return jsonify({"id": fid, "object": "file", "filename": f.filename, "purpose": "vision"})
|
| 318 |
|
| 319 |
+
@app.route('/v1/models', methods=['GET'])
|
| 320 |
+
def list_models():
|
| 321 |
+
return jsonify({"object": "list", "data": fetch_cognix_models()})
|
| 322 |
|
| 323 |
if __name__ == '__main__':
|
| 324 |
+
print("Shivansh Tiwari Proxy (7862) Started...")
|
| 325 |
app.run(host='0.0.0.0', port=7860, debug=True)
|