Spaces:
Configuration error
Configuration error
File size: 6,571 Bytes
317cceb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
import json
import time
import uuid
from datetime import datetime
import httpx_sse
from fastapi import FastAPI, Header
from fastapi.responses import StreamingResponse
from httpx import AsyncClient
from .env import (
TRAE_APP_ID,
TRAE_DEVICE_BRAND,
TRAE_DEVICE_CPU,
TRAE_DEVICE_ID,
TRAE_DEVICE_TYPE,
TRAE_IDE_TOKEN,
TRAE_IDE_VERSION,
TRAE_IDE_VERSION_CODE,
TRAE_IDE_VERSION_TYPE,
TRAE_MACHINE_ID,
TRAE_OS_VERSION,
)
from .types import (
ChatCompletionChunk,
ChatCompletionChunkChoice,
ChatCompletionRequest,
Model,
)
app = FastAPI(
title="Trae2OpenAI Proxy",
description="A api proxy to make trae's builtin models openai compatible",
version="0.1.0",
)
@app.get("/v1/models")
async def list_models(ide_token: str = Header(TRAE_IDE_TOKEN, alias="Authorization")) -> list[Model]:
ide_token = ide_token.removeprefix("Bearer ")
async with AsyncClient() as client:
response = await client.get(
"https://trae-api-sg.mchost.guru/api/ide/v1/model_list",
params={"type": "llm_raw_chat"},
headers={
"x-app-id": TRAE_APP_ID,
"x-device-brand": TRAE_DEVICE_BRAND,
"x-device-cpu": TRAE_DEVICE_CPU,
"x-device-id": TRAE_DEVICE_ID,
"x-device-type": TRAE_DEVICE_TYPE,
"x-ide-token": ide_token,
"x-ide-version": TRAE_IDE_VERSION,
"x-ide-version-code": TRAE_IDE_VERSION_CODE,
"x-ide-version-type": TRAE_IDE_VERSION_TYPE,
"x-machine-id": TRAE_MACHINE_ID,
"x-os-version": TRAE_OS_VERSION,
},
)
return [Model(created=0, id=model["name"]) for model in response.json()["model_configs"]]
@app.post("/v1/chat/completions")
async def create_chat_completions(
request: ChatCompletionRequest, ide_token: str = Header(TRAE_IDE_TOKEN, alias="Authorization")
) -> StreamingResponse:
ide_token = ide_token.removeprefix("Bearer ")
current_turn = sum(1 for msg in request.messages if msg.role == "user")
last_assistant_message = next(filter(lambda msg: msg.role == "assistant", reversed(request.messages)), None)
async def stream_response():
async with AsyncClient() as client:
async with httpx_sse.aconnect_sse(
client,
"POST",
"https://trae-api-sg.mchost.guru/api/ide/v1/chat",
headers={
"x-app-id": TRAE_APP_ID,
"x-device-brand": TRAE_DEVICE_BRAND,
"x-device-cpu": TRAE_DEVICE_CPU,
"x-device-id": TRAE_DEVICE_ID,
"x-device-type": TRAE_DEVICE_TYPE,
"x-ide-token": ide_token,
"x-ide-version": TRAE_IDE_VERSION,
"x-ide-version-code": TRAE_IDE_VERSION_CODE,
"x-ide-version-type": TRAE_IDE_VERSION_TYPE,
"x-machine-id": TRAE_MACHINE_ID,
"x-os-version": TRAE_OS_VERSION,
},
json={
"chat_history": [msg.model_dump() for msg in request.messages[:-1]],
"context_resolvers": [],
"conversation_id": str(uuid.uuid4()),
"current_turn": current_turn,
"generate_suggested_questions": False,
"intent_name": "general_qa_intent",
"is_preset": True,
"last_llm_response_info": (
{"turn": current_turn - 1, "is_error": False, "response": last_assistant_message.content}
if last_assistant_message
else {}
),
"model_name": request.model,
"multi_media": [],
"provider": "",
"session_id": str(uuid.uuid4()),
"user_input": request.messages[-1].content,
"valid_turns": list(range(current_turn)),
"variables": json.dumps(
{"locale": "zh-cn", "current_time": datetime.now().strftime("%Y%m%d %H:%M:%S %A")}
),
},
) as response:
chunk = ChatCompletionChunk(
choices=[],
created=int(time.time()),
id="",
model=request.model,
)
async for sse in response.aiter_sse():
sse_data = sse.json()
if sse.event == "metadata":
chunk.id = str(sse_data["prompt_completion_id"])
elif sse.event == "output":
content = sse_data["response"]
reasoning_content = sse_data["reasoning_content"]
chunk.choices = [
ChatCompletionChunkChoice(
delta={"role": "assistant", "content": content, "reasoning_content": reasoning_content}
)
]
yield f"data: {chunk.model_dump_json()}\n\n"
elif sse.event == "token_usage":
chunk.choices = []
chunk.usage = {
"completion_tokens": sse_data["completion_tokens"],
"prompt_tokens": sse_data["prompt_tokens"],
"total_tokens": sse_data["total_tokens"],
}
yield f"data: {chunk.model_dump_json()}\n\n"
elif sse.event == "done":
chunk.choices = [ChatCompletionChunkChoice(delta={}, finish_reason="stop")]
yield f"data: {chunk.model_dump_json()}\n\ndata: [DONE]\n\n"
elif sse.event == "error":
chunk.choices = [
ChatCompletionChunkChoice(
delta={"role": "assistant", "content": sse.data}, finish_reason="error"
)
]
yield f"data: {chunk.model_dump_json()}\n\ndata: [DONE]\n\n"
return StreamingResponse(stream_response(), media_type="text/event-stream")
|