File size: 2,405 Bytes
4118a69
 
cc4e60f
4118a69
81d7fdb
 
00f6edb
81d7fdb
cc4e60f
4118a69
81d7fdb
 
4118a69
846a8ec
 
fb2d081
 
4118a69
fb2d081
4118a69
 
fb2d081
 
1a41bcb
88164a4
1a41bcb
00f6edb
 
 
4118a69
 
 
543154d
846a8ec
 
00f6edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94844cd
00f6edb
798eb25
fac749f
 
1a41bcb
 
4118a69
1a41bcb
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
from fastapi import FastAPI, Depends # Depends might be used by root endpoint
from fastapi.middleware.cors import CORSMiddleware

# Local module imports
from auth import get_api_key # Potentially for root endpoint
from credentials_manager import CredentialManager
from express_key_manager import ExpressKeyManager
from vertex_ai_init import init_vertex_ai

# Routers
from routes import models_api
from routes import chat_api

app = FastAPI(title="OpenAI to Gemini Adapter")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

credential_manager = CredentialManager()
app.state.credential_manager = credential_manager # Store manager on app state

express_key_manager = ExpressKeyManager()
app.state.express_key_manager = express_key_manager # Store express key manager on app state

# Include API routers
app.include_router(models_api.router) 
app.include_router(chat_api.router)

@app.on_event("startup")
async def startup_event():
    # Check SA credentials availability
    sa_credentials_available = await init_vertex_ai(credential_manager)
    sa_count = credential_manager.get_total_credentials() if sa_credentials_available else 0
    
    # Check Express API keys availability
    express_keys_count = express_key_manager.get_total_keys()
    
    # Print detailed status
    print(f"INFO: SA credentials loaded: {sa_count}")
    print(f"INFO: Express API keys loaded: {express_keys_count}")
    print(f"INFO: Total authentication methods available: {(1 if sa_count > 0 else 0) + (1 if express_keys_count > 0 else 0)}")
    
    # Determine overall status
    if sa_count > 0 or express_keys_count > 0:
        print("INFO: Vertex AI authentication initialization completed successfully. At least one authentication method is available.")
        if sa_count == 0:
            print("INFO: No SA credentials found, but Express API keys are available for authentication.")
        elif express_keys_count == 0:
            print("INFO: No Express API keys found, but SA credentials are available for authentication.")
    else:
        print("ERROR: Failed to initialize any authentication method. Both SA credentials and Express API keys are missing. API will fail.")

@app.get("/")
async def root():
    return {
        "status": "ok",
        "message": "OpenAI to Gemini Adapter is running."
    }