File size: 3,838 Bytes
c8e4e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6bf7d9
c8e4e29
b6bf7d9
 
 
 
 
c8e4e29
 
b6bf7d9
 
 
c8e4e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6bf7d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8e4e29
 
 
 
 
 
 
3bd9141
c8e4e29
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from fastapi import FastAPI, Body, Request, Depends, HTTPException
from fastapi.responses import HTMLResponse, ORJSONResponse, FileResponse
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from starlette.status import HTTP_401_UNAUTHORIZED
from fastapi.responses import StreamingResponse
import collections
if not hasattr(collections, "MutableSet"):
    collections.MutableSet = collections.abc.MutableSet
if not hasattr(collections, "MutableMapping"):
    collections.MutableMapping = collections.abc.MutableMapping
import httpx
import logging
import os

# ロギング設定を追加
logging.basicConfig(
    level=os.getenv('LOG_LEVEL', 'INFO'),
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# HTTPXのログも有効化する場合は以下も追加
logging.getLogger("httpx").setLevel(logging.DEBUG)

app = FastAPI()

from starlette.middleware.cors import CORSMiddleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://novelai.net", "https://hisaruki.ddns.net"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]
)

security = HTTPBasic()

def authenticate(credentials: HTTPBasicCredentials = Depends(security)):
    correct_username = "administrator"
    correct_password = "X2fK9pL7mR3qZ8vY"
    if not (credentials.username == correct_username and credentials.password == correct_password):
        raise HTTPException(
            status_code=HTTP_401_UNAUTHORIZED,
            detail="認証に失敗しました",
            headers={"WWW-Authenticate": "Basic"},
        )
    return credentials.username
@app.get("/authtest", dependencies=[Depends(authenticate)])
async def eval(request: Request, body: dict = Body):
    return HTMLResponse(content="ok.", media_type="text/plain")


@app.post("/unify/chat/completions")
async def unify_chat_completions(request: Request, body: dict = Body(...)):
    headers = {
        "Authorization": request.headers["authorization"],
        "Content-Type": "application/json"
    }
    endpoint = "https://api.unify.ai/v0/chat/completions"
    logger.debug(f"リクエストボディ: {body}")
    logger.debug(f"リクエストヘッダー: {headers}")

    # streamがFalseの場合は通常のレスポンスを返す
    if not body.get("stream", True):
        async with httpx.AsyncClient(timeout=600) as client:
            logger.debug(f"非ストリーミングモードでリクエスト送信: {endpoint}")
            response = await client.post(endpoint, json=body, headers=headers)
            logger.debug(f"ステータスコード: {response.status_code}")
            logger.debug(f"レスポンスヘッダー: {response.headers}")
            
            if response.status_code != 200:
                logger.error(f"エラーレスポンス: {response.text}")
                raise HTTPException(status_code=response.status_code, detail=response.text)
            
            response_json = response.json()
            logger.debug(f"レスポンスボディ: {response_json}")
            return ORJSONResponse(response_json)

    # streamがTrueの場合は既存のストリーミング処理
    async def stream_response():
        async with httpx.AsyncClient(timeout=600) as client:
            async with client.stream("POST", endpoint, json=body, headers=headers) as response:
                if response.status_code != 200:
                    raise HTTPException(status_code=response.status_code, detail=response.text)
                async for chunk in response.aiter_bytes():
                    yield chunk
    return StreamingResponse(stream_response(), media_type="text/event-stream")