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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, Response, jsonify
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import uuid
|
| 5 |
+
import time
|
| 6 |
+
import os
|
| 7 |
+
import re
|
| 8 |
+
import base64
|
| 9 |
+
import mimetypes
|
| 10 |
+
import random
|
| 11 |
+
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
+
|
| 14 |
+
# Configuration
|
| 15 |
+
COGNIX_BASE_URL = os.environ.get("COGNIX_BASE_URL", "https://www.cognixai.co")
|
| 16 |
+
# Supports || separated cookies for rotation
|
| 17 |
+
COGNIX_COOKIES_RAW = os.environ.get("COGNIX_COOKIE", "")
|
| 18 |
+
COGNIX_COOKIES = [c.strip() for c in COGNIX_COOKIES_RAW.split("||") if c.strip()]
|
| 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=eyJzZXNzaW9uIjp7InNlc3Npb24iOnsiZXhwaXJlc0F0IjoiMjAyNi0wMi0yMFQwOToxOTozOC41NDdaIiwidG9rZW4iOiI3U2NHR3hkdzFQTFpGbmJlNWdlOWpIQjF1dEpJYXFTbSIsImNyZWF0ZWRBdCI6IjIwMjYtMDItMTNUMDk6MTk6MzguNTQ3WiIsInVwZGF0ZWRBdCI6IjIwMjYtMDItMTNUMDk6MTk6MzguNTQ3WiIsImlwQWRkcmVzcyI6IjE3Mi43MS45OC44IiwidXNlckFnZW50IjoiTW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzE0NS4wLjAuMCBTYWZhcmkvNTM3LjM2IiwidXNlcklkIjoiYmFmM2M2ZmQtYjFmYi00NjM3LWEyNTYtODlhM2NmOTdiYWNhIiwiaW1wZXJzb25hdGVkQnkiOm51bGwsImlkIjoiNWY1Mzc5NmMtM2Y1ZC00ZWM4LWE2YmEtNjUyMDhiODY2OTVlIn0sInVzZXIiOnsibmFtZSI6IlNoaXZhbnNoIFRpd2FyaSIsImVtYWlsIjoic2hpdi4yMDA5LjEwNi50aXdhcmlAZ21haWwuY29tIiwiZW1haWxWZXJpZmllZCI6dHJ1ZSwiaW1hZ2UiOiJodHRwczovL2xoMy5nb29nbGV1c2VyY29udGVudC5jb20vYS9BQ2c4b2NJSHNKdTNpV1YtNlZPZE1rLU5hN1ZCcU9xdUZMZnU0aWg4SFlUM1otMlRpblZkMVlWej1zOTYtYyIsImNyZWF0ZWRBdCI6IjIwMjYtMDEtMTJUMDM6MjE6MzQuNjUzWiIsInVwZGF0ZWRBdCI6IjIwMjYtMDEtMTJUMDM6MjE6MzQuNjUzWiIsInJvbGUiOiJlZGl0b3IiLCJiYW5uZWQiOmZhbHNlLCJiYW5SZWFzb24iOm51bGwsImJhbkV4cGlyZXMiOm51bGwsImlkIjoiYmFmM2M2ZmQtYjFmYi00NjM3LWEyNTYtODlhM2NmOTdiYWNhIn19LCJleHBpcmVzQXQiOjE3NzA5Nzc5Nzg3MzQsInNpZ25hdHVyZSI6ImQ1MkUxMFlvZ3NoY3kyeUwwNV9rWkxQcXpPUU1BSTVLRV9CRmFXZHZtaGMifQ"
|
| 24 |
+
return random.choice(COGNIX_COOKIES)
|
| 25 |
+
|
| 26 |
+
DEFAULT_COGNIX_SESSION_ID = "f351d7e7-a0ba-4888-86a4-76aab9a7a661"
|
| 27 |
+
|
| 28 |
+
# Store uploaded files metadata
|
| 29 |
+
files_cache = {}
|
| 30 |
+
|
| 31 |
+
def get_headers(multipart=False):
|
| 32 |
+
h = {
|
| 33 |
+
"accept": "*/*",
|
| 34 |
+
"accept-language": "en-IN,en-GB;q=0.9,en-US;q=0.8,en;q=0.7",
|
| 35 |
+
"cookie": get_cognix_cookie(),
|
| 36 |
+
"origin": "https://www.cognixai.co",
|
| 37 |
+
"referer": f"https://www.cognixai.co/chat/{DEFAULT_COGNIX_SESSION_ID}",
|
| 38 |
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36"
|
| 39 |
+
}
|
| 40 |
+
if not multipart:
|
| 41 |
+
h["content-type"] = "application/json"
|
| 42 |
+
return h
|
| 43 |
+
|
| 44 |
+
# Model Cache
|
| 45 |
+
model_cache = {"data": [], "last_updated": 0}
|
| 46 |
+
|
| 47 |
+
def fetch_cognix_models():
|
| 48 |
+
"""Fetch available models from Cognix API and format for OpenAI compatibility."""
|
| 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=headers, timeout=15)
|
| 65 |
+
if resp.status_code == 200:
|
| 66 |
+
try:
|
| 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 |
+
# Skip 'cognix' provider as requested
|
| 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 Exception as e:
|
| 96 |
+
print(f"Error fetching models from Cognix: {e}")
|
| 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"), # Using key as ID
|
| 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, msg_format="openai"):
|
| 139 |
+
"""Extract images and files from message blocks."""
|
| 140 |
+
files = []
|
| 141 |
+
|
| 142 |
+
def get_id_from_url(url):
|
| 143 |
+
if not isinstance(url, str): return None
|
| 144 |
+
if url in files_cache: return url
|
| 145 |
+
match = re.search(r'(file-[a-f0-9]{24})', url)
|
| 146 |
+
if match:
|
| 147 |
+
fid = match.group(1)
|
| 148 |
+
if fid in files_cache: return fid
|
| 149 |
+
return None
|
| 150 |
+
|
| 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)
|
| 163 |
+
if f_id:
|
| 164 |
+
files.append(files_cache[f_id])
|
| 165 |
+
elif url.startswith('data:'):
|
| 166 |
+
try:
|
| 167 |
+
header, b64 = url.split(',', 1)
|
| 168 |
+
mime = header.split(':')[1].split(';')[0]
|
| 169 |
+
files.append({"_data": b64, "content_type": mime, "filename": f"img_{uuid.uuid4().hex[:8]}"})
|
| 170 |
+
except: pass
|
| 171 |
+
elif url.startswith('http'):
|
| 172 |
+
try:
|
| 173 |
+
resp = requests.get(url, timeout=30)
|
| 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 Calling Support ==============
|
| 187 |
+
|
| 188 |
+
def build_tools_system_prompt(tools, tool_format="openai"):
|
| 189 |
+
if not tools: return ""
|
| 190 |
+
tools_list = []
|
| 191 |
+
for tool in tools:
|
| 192 |
+
func = tool.get('function', tool)
|
| 193 |
+
tools_list.append({
|
| 194 |
+
"name": func.get('name', ''),
|
| 195 |
+
"description": func.get('description', ''),
|
| 196 |
+
"parameters": func.get('parameters', (tool.get('input_schema', {}) if tool_format == "anthropic" else {}))
|
| 197 |
+
})
|
| 198 |
+
return f"Available Tools:\n{json.dumps(tools_list, indent=2)}\n\nTo use a tool, output: <tool_call>{{\"name\": \"...\", \"id\": \"...\", \"input\": {{...}}}}</tool_call>"
|
| 199 |
+
|
| 200 |
+
def parse_tool_calls_from_response(text):
|
| 201 |
+
tool_calls = []
|
| 202 |
+
text_parts = []
|
| 203 |
+
pattern = r'<tool_call>\s*(.*?)\s*</tool_call>'
|
| 204 |
+
matches = list(re.finditer(pattern, text, re.DOTALL))
|
| 205 |
+
if matches:
|
| 206 |
+
last_end = 0
|
| 207 |
+
for m in matches:
|
| 208 |
+
text_parts.append(text[last_end:m.start()].strip())
|
| 209 |
+
last_end = m.end()
|
| 210 |
+
try: tool_calls.append(json.loads(m.group(1).strip()))
|
| 211 |
+
except: text_parts.append(m.group(0))
|
| 212 |
+
text_parts.append(text[last_end:].strip())
|
| 213 |
+
else: text_parts.append(text)
|
| 214 |
+
return "\n\n".join(text_parts).strip(), tool_calls
|
| 215 |
+
|
| 216 |
+
def convert_tool_results_to_text(messages):
|
| 217 |
+
converted = []
|
| 218 |
+
for msg in messages:
|
| 219 |
+
role, content = msg.get('role', ''), msg.get('content', '')
|
| 220 |
+
if role == 'tool':
|
| 221 |
+
converted.append({"role": "user", "content": f"<tool_result id=\"{msg.get('tool_call_id')}\">{content}</tool_result>"})
|
| 222 |
+
elif role == 'user' and isinstance(content, list):
|
| 223 |
+
res_parts = []
|
| 224 |
+
for b in content:
|
| 225 |
+
if b.get('type') == 'tool_result':
|
| 226 |
+
c = b.get('content')
|
| 227 |
+
if isinstance(c, list): c = ' '.join([x.get('text', '') for x in c])
|
| 228 |
+
res_parts.append(f"<tool_result id=\"{b.get('tool_use_id')}\">{c}</tool_result>")
|
| 229 |
+
elif b.get('type') == 'text': res_parts.append(b.get('text', ''))
|
| 230 |
+
converted.append({"role": "user", "content": '\n'.join(res_parts)})
|
| 231 |
+
elif role == 'assistant' and msg.get('tool_calls'):
|
| 232 |
+
t = (content or "") + "".join([f"\n<tool_call>{json.dumps({'name': tc['function']['name'], 'id': tc['id'], 'input': tc['function']['arguments']})}</tool_call>" for tc in msg['tool_calls']])
|
| 233 |
+
converted.append({"role": "assistant", "content": t.strip()})
|
| 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 = str(uuid.uuid4())
|
| 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 |
+
r, c = m['role'], m.get('content', '')
|
| 257 |
+
if isinstance(c, list):
|
| 258 |
+
c = ' '.join([p.get('text', '') for p in c if p.get('type') == 'text'])
|
| 259 |
+
|
| 260 |
+
if r == 'user' and m == processed[-1]:
|
| 261 |
+
last_user = c
|
| 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 |
+
}
|
| 300 |
+
|
| 301 |
+
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 |
+
content = delta
|
| 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 |
+
|
| 333 |
+
@app.route('/v1/chat/completions', methods=['POST'])
|
| 334 |
+
def chat_completions():
|
| 335 |
+
d = request.json
|
| 336 |
+
model = d.get('model', 'anthropic/Claude Opus 4.6')
|
| 337 |
+
messages = d.get('messages', [])
|
| 338 |
+
|
| 339 |
+
# Extract system prompt
|
| 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 |
+
|
| 351 |
+
if d.get('stream'):
|
| 352 |
+
def gen():
|
| 353 |
+
cid = f"chatcmpl-{uuid.uuid4().hex[:24]}"
|
| 354 |
+
yield f"data: {json.dumps({'id': cid, 'object': 'chat.completion.chunk', 'choices': [{'delta': {'role': 'assistant'}}]})}\n\n"
|
| 355 |
+
full_buf = ""
|
| 356 |
+
with requests.post(f"{COGNIX_BASE_URL}/api/chat", json=payload, headers=get_headers(), stream=True) as r:
|
| 357 |
+
for line in r.iter_lines(decode_unicode=True):
|
| 358 |
+
if not line: continue
|
| 359 |
+
cont, pty = parse_cognix_stream_chunk(line)
|
| 360 |
+
if pty == "stop": break
|
| 361 |
+
if cont:
|
| 362 |
+
if d.get('tools'): full_buf += cont
|
| 363 |
+
else: yield f"data: {json.dumps({'id': cid, 'object': 'chat.completion.chunk', 'choices': [{'delta': {'content': cont}}]})}\n\n"
|
| 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 |
+
|
| 372 |
+
r = requests.post(f"{COGNIX_BASE_URL}/api/chat", json=payload, headers=get_headers())
|
| 373 |
+
full_text = "".join([parse_cognix_stream_chunk(l)[0] or "" for l in r.text.strip().split('\n')])
|
| 374 |
+
txt, tcs = parse_tool_calls_from_response(full_text)
|
| 375 |
+
msg = {"role": "assistant", "content": txt or None}
|
| 376 |
+
if tcs: msg["tool_calls"] = [{"id": str(uuid.uuid4()), "type": "function", "function": {"name": t['name'], "arguments": json.dumps(t['input'])}} for t in tcs]
|
| 377 |
+
return jsonify({"id": str(uuid.uuid4()), "object": "chat.completion", "choices": [{"message": msg, "finish_reason": "tool_calls" if tcs else "stop"}]})
|
| 378 |
+
|
| 379 |
+
@app.route('/v1/messages', methods=['POST'])
|
| 380 |
+
def anthropic_messages():
|
| 381 |
+
d = request.json
|
| 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]}"
|
| 389 |
+
yield f"event: message_start\ndata: {json.dumps({'type': 'message_start', 'message': {'id': mid, 'role': 'assistant', 'content': [], 'model': model}})}\n\n"
|
| 390 |
+
full_buf = ""
|
| 391 |
+
with requests.post(f"{COGNIX_BASE_URL}/api/chat", json=payload, headers=get_headers(), stream=True) as r:
|
| 392 |
+
for line in r.iter_lines(decode_unicode=True):
|
| 393 |
+
if not line: continue
|
| 394 |
+
cont, pty = parse_cognix_stream_chunk(line)
|
| 395 |
+
if pty == "stop": break
|
| 396 |
+
if cont:
|
| 397 |
+
full_buf += cont
|
| 398 |
+
if not d.get('tools'): yield f"event: content_block_delta\ndata: {json.dumps({'type': 'content_block_delta', 'index': 0, 'delta': {'type': 'text_delta', 'text': cont}})}\n\n"
|
| 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)
|
| 410 |
+
content = [{"type": "text", "text": txt}] if txt else []
|
| 411 |
+
for t in tcs: content.append({"type": "tool_use", "id": str(uuid.uuid4()), "name": t['name'], "input": t['input']})
|
| 412 |
+
return jsonify({"id": str(uuid.uuid4()), "type": "message", "role": "assistant", "content": content, "model": model, "stop_reason": "tool_use" if tcs else "end_turn"})
|
| 413 |
+
|
| 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 |
+
fb = f.read()
|
| 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)
|