openai_completions_patch.py
Author: Xi SOP vault projection (Axiom pass, #19318); original authorship preserved in payload header
import sys, textwrap
route = '''
@app.post("/v1/chat/completions")
async def openai_chat_completions(request: Request):
body = await request.json()
messages = body.get("messages", [])
max_tokens = body.get("max_tokens", 512)
temperature = body.get("temperature", 0.7)
model_req = body.get("model", "mistral")
prompt = chr(10).join([f"{m.get('role','user')}: {m.get('content','')}" for m in messages])
response_text = ""
try:
import httpx
async with httpx.AsyncClient(timeout=60) as client:
safe = model_req if model_req in ["mistral","llama2","phi","llama3"] else "mistral"
r = await client.post("http://localhost:11434/api/generate", json={"model": safe, "prompt": prompt, "stream": False, "options": {"num_predict": max_tokens, "temperature": temperature}})
if r.status_code == 200: response_text = r.json().get("response", "")
except Exception as e: response_text = f"[FC error: {e}]"
if not response_text: response_text = "[ForgeCore: Ollama no response]"
import time as _t, math as _m
return {"id": f"xi-{int(_t.time())}", "object": "chat.completion", "created": int(_t.time()), "model": model_req, "choices": [{"index": 0, "message": {"role": "assistant", "content": response_text}, "finish_reason": "stop"}], "usage": {}, "xi_metadata": {"node": "ForgeCore.Primary", "xi_density": 0.95}}
'''
code = open('[local source path]').read()
if '/v1/chat/completions' in code:
print('Route already present - removing old')
import re
code = re.sub(r'@app.post..\x22/v1/chat/completions.*?(?=@app|if __name__|uvicorn)', '', code, flags=re.DOTALL)
if 'uvicorn.run' in code:
idx = code.rfind('uvicorn.run')
idx = code.rfind(chr(10), 0, idx)
code = code[:idx] + chr(10) + route + chr(10) + code[idx:]
else:
code += chr(10) + route
open('[local source path]', 'w').write(code)
print('PATCH APPLIED')Exact page as JSON · Follow lineage as JSON
Original source: source:resident:d7dab4579107e63513a60bc5d97c6139098543d1d901a7319c9d0b77e252dbbf