webscout 8.2.8__py3-none-any.whl → 8.2.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- webscout/AIauto.py +32 -14
- webscout/AIbase.py +96 -37
- webscout/AIutel.py +491 -87
- webscout/Bard.py +441 -323
- webscout/Extra/GitToolkit/__init__.py +10 -10
- webscout/Extra/YTToolkit/ytapi/video.py +232 -232
- webscout/Litlogger/README.md +10 -0
- webscout/Litlogger/__init__.py +7 -59
- webscout/Litlogger/formats.py +4 -0
- webscout/Litlogger/handlers.py +103 -0
- webscout/Litlogger/levels.py +13 -0
- webscout/Litlogger/logger.py +92 -0
- webscout/Provider/AISEARCH/Perplexity.py +332 -358
- webscout/Provider/AISEARCH/felo_search.py +9 -35
- webscout/Provider/AISEARCH/genspark_search.py +30 -56
- webscout/Provider/AISEARCH/hika_search.py +4 -16
- webscout/Provider/AISEARCH/iask_search.py +410 -436
- webscout/Provider/AISEARCH/monica_search.py +4 -30
- webscout/Provider/AISEARCH/scira_search.py +6 -32
- webscout/Provider/AISEARCH/webpilotai_search.py +38 -64
- webscout/Provider/Blackboxai.py +153 -35
- webscout/Provider/Deepinfra.py +339 -339
- webscout/Provider/ExaChat.py +358 -358
- webscout/Provider/Gemini.py +169 -169
- webscout/Provider/GithubChat.py +1 -2
- webscout/Provider/Glider.py +3 -3
- webscout/Provider/HeckAI.py +171 -81
- webscout/Provider/OPENAI/BLACKBOXAI.py +766 -735
- webscout/Provider/OPENAI/Cloudflare.py +7 -7
- webscout/Provider/OPENAI/FreeGemini.py +6 -5
- webscout/Provider/OPENAI/NEMOTRON.py +8 -20
- webscout/Provider/OPENAI/Qwen3.py +283 -0
- webscout/Provider/OPENAI/README.md +952 -1253
- webscout/Provider/OPENAI/TwoAI.py +357 -0
- webscout/Provider/OPENAI/__init__.py +5 -1
- webscout/Provider/OPENAI/ai4chat.py +40 -40
- webscout/Provider/OPENAI/api.py +808 -649
- webscout/Provider/OPENAI/c4ai.py +3 -3
- webscout/Provider/OPENAI/chatgpt.py +555 -555
- webscout/Provider/OPENAI/chatgptclone.py +493 -487
- webscout/Provider/OPENAI/chatsandbox.py +4 -3
- webscout/Provider/OPENAI/copilot.py +242 -0
- webscout/Provider/OPENAI/deepinfra.py +5 -2
- webscout/Provider/OPENAI/e2b.py +63 -5
- webscout/Provider/OPENAI/exaai.py +416 -410
- webscout/Provider/OPENAI/exachat.py +444 -443
- webscout/Provider/OPENAI/freeaichat.py +2 -2
- webscout/Provider/OPENAI/glider.py +5 -2
- webscout/Provider/OPENAI/groq.py +5 -2
- webscout/Provider/OPENAI/heckai.py +308 -307
- webscout/Provider/OPENAI/mcpcore.py +8 -2
- webscout/Provider/OPENAI/multichat.py +4 -4
- webscout/Provider/OPENAI/netwrck.py +6 -5
- webscout/Provider/OPENAI/oivscode.py +287 -0
- webscout/Provider/OPENAI/opkfc.py +496 -496
- webscout/Provider/OPENAI/pydantic_imports.py +172 -0
- webscout/Provider/OPENAI/scirachat.py +15 -9
- webscout/Provider/OPENAI/sonus.py +304 -303
- webscout/Provider/OPENAI/standardinput.py +433 -433
- webscout/Provider/OPENAI/textpollinations.py +4 -4
- webscout/Provider/OPENAI/toolbaz.py +413 -413
- webscout/Provider/OPENAI/typefully.py +3 -3
- webscout/Provider/OPENAI/typegpt.py +11 -5
- webscout/Provider/OPENAI/uncovrAI.py +463 -462
- webscout/Provider/OPENAI/utils.py +90 -79
- webscout/Provider/OPENAI/venice.py +431 -425
- webscout/Provider/OPENAI/wisecat.py +387 -381
- webscout/Provider/OPENAI/writecream.py +3 -3
- webscout/Provider/OPENAI/x0gpt.py +365 -378
- webscout/Provider/OPENAI/yep.py +39 -13
- webscout/Provider/TTI/README.md +55 -101
- webscout/Provider/TTI/__init__.py +4 -9
- webscout/Provider/TTI/aiarta.py +365 -0
- webscout/Provider/TTI/artbit.py +0 -0
- webscout/Provider/TTI/base.py +64 -0
- webscout/Provider/TTI/fastflux.py +200 -0
- webscout/Provider/TTI/magicstudio.py +201 -0
- webscout/Provider/TTI/piclumen.py +203 -0
- webscout/Provider/TTI/pixelmuse.py +225 -0
- webscout/Provider/TTI/pollinations.py +221 -0
- webscout/Provider/TTI/utils.py +11 -0
- webscout/Provider/TTS/__init__.py +2 -1
- webscout/Provider/TTS/base.py +159 -159
- webscout/Provider/TTS/openai_fm.py +129 -0
- webscout/Provider/TextPollinationsAI.py +308 -308
- webscout/Provider/TwoAI.py +239 -44
- webscout/Provider/UNFINISHED/Youchat.py +330 -330
- webscout/Provider/UNFINISHED/puterjs.py +635 -0
- webscout/Provider/UNFINISHED/test_lmarena.py +119 -119
- webscout/Provider/Writecream.py +246 -246
- webscout/Provider/__init__.py +2 -0
- webscout/Provider/ai4chat.py +33 -8
- webscout/Provider/koala.py +169 -169
- webscout/Provider/oivscode.py +309 -0
- webscout/Provider/samurai.py +3 -2
- webscout/Provider/typegpt.py +3 -3
- webscout/Provider/uncovr.py +368 -368
- webscout/client.py +70 -0
- webscout/litprinter/__init__.py +58 -58
- webscout/optimizers.py +419 -419
- webscout/scout/README.md +3 -1
- webscout/scout/core/crawler.py +134 -64
- webscout/scout/core/scout.py +148 -109
- webscout/scout/element.py +106 -88
- webscout/swiftcli/Readme.md +323 -323
- webscout/swiftcli/plugins/manager.py +9 -2
- webscout/version.py +1 -1
- webscout/zeroart/__init__.py +134 -134
- webscout/zeroart/effects.py +100 -100
- webscout/zeroart/fonts.py +1238 -1238
- {webscout-8.2.8.dist-info → webscout-8.2.9.dist-info}/METADATA +159 -35
- {webscout-8.2.8.dist-info → webscout-8.2.9.dist-info}/RECORD +116 -161
- {webscout-8.2.8.dist-info → webscout-8.2.9.dist-info}/WHEEL +1 -1
- {webscout-8.2.8.dist-info → webscout-8.2.9.dist-info}/entry_points.txt +1 -0
- webscout/Litlogger/Readme.md +0 -175
- webscout/Litlogger/core/__init__.py +0 -6
- webscout/Litlogger/core/level.py +0 -23
- webscout/Litlogger/core/logger.py +0 -165
- webscout/Litlogger/handlers/__init__.py +0 -12
- webscout/Litlogger/handlers/console.py +0 -33
- webscout/Litlogger/handlers/file.py +0 -143
- webscout/Litlogger/handlers/network.py +0 -173
- webscout/Litlogger/styles/__init__.py +0 -7
- webscout/Litlogger/styles/colors.py +0 -249
- webscout/Litlogger/styles/formats.py +0 -458
- webscout/Litlogger/styles/text.py +0 -87
- webscout/Litlogger/utils/__init__.py +0 -6
- webscout/Litlogger/utils/detectors.py +0 -153
- webscout/Litlogger/utils/formatters.py +0 -200
- webscout/Provider/TTI/AiForce/README.md +0 -159
- webscout/Provider/TTI/AiForce/__init__.py +0 -22
- webscout/Provider/TTI/AiForce/async_aiforce.py +0 -224
- webscout/Provider/TTI/AiForce/sync_aiforce.py +0 -245
- webscout/Provider/TTI/FreeAIPlayground/README.md +0 -99
- webscout/Provider/TTI/FreeAIPlayground/__init__.py +0 -9
- webscout/Provider/TTI/FreeAIPlayground/async_freeaiplayground.py +0 -181
- webscout/Provider/TTI/FreeAIPlayground/sync_freeaiplayground.py +0 -180
- webscout/Provider/TTI/ImgSys/README.md +0 -174
- webscout/Provider/TTI/ImgSys/__init__.py +0 -23
- webscout/Provider/TTI/ImgSys/async_imgsys.py +0 -202
- webscout/Provider/TTI/ImgSys/sync_imgsys.py +0 -195
- webscout/Provider/TTI/MagicStudio/README.md +0 -101
- webscout/Provider/TTI/MagicStudio/__init__.py +0 -2
- webscout/Provider/TTI/MagicStudio/async_magicstudio.py +0 -111
- webscout/Provider/TTI/MagicStudio/sync_magicstudio.py +0 -109
- webscout/Provider/TTI/Nexra/README.md +0 -155
- webscout/Provider/TTI/Nexra/__init__.py +0 -22
- webscout/Provider/TTI/Nexra/async_nexra.py +0 -286
- webscout/Provider/TTI/Nexra/sync_nexra.py +0 -258
- webscout/Provider/TTI/PollinationsAI/README.md +0 -146
- webscout/Provider/TTI/PollinationsAI/__init__.py +0 -23
- webscout/Provider/TTI/PollinationsAI/async_pollinations.py +0 -311
- webscout/Provider/TTI/PollinationsAI/sync_pollinations.py +0 -265
- webscout/Provider/TTI/aiarta/README.md +0 -134
- webscout/Provider/TTI/aiarta/__init__.py +0 -2
- webscout/Provider/TTI/aiarta/async_aiarta.py +0 -482
- webscout/Provider/TTI/aiarta/sync_aiarta.py +0 -440
- webscout/Provider/TTI/artbit/README.md +0 -100
- webscout/Provider/TTI/artbit/__init__.py +0 -22
- webscout/Provider/TTI/artbit/async_artbit.py +0 -155
- webscout/Provider/TTI/artbit/sync_artbit.py +0 -148
- webscout/Provider/TTI/fastflux/README.md +0 -129
- webscout/Provider/TTI/fastflux/__init__.py +0 -22
- webscout/Provider/TTI/fastflux/async_fastflux.py +0 -261
- webscout/Provider/TTI/fastflux/sync_fastflux.py +0 -252
- webscout/Provider/TTI/huggingface/README.md +0 -114
- webscout/Provider/TTI/huggingface/__init__.py +0 -22
- webscout/Provider/TTI/huggingface/async_huggingface.py +0 -199
- webscout/Provider/TTI/huggingface/sync_huggingface.py +0 -195
- webscout/Provider/TTI/piclumen/README.md +0 -161
- webscout/Provider/TTI/piclumen/__init__.py +0 -23
- webscout/Provider/TTI/piclumen/async_piclumen.py +0 -268
- webscout/Provider/TTI/piclumen/sync_piclumen.py +0 -233
- webscout/Provider/TTI/pixelmuse/README.md +0 -79
- webscout/Provider/TTI/pixelmuse/__init__.py +0 -4
- webscout/Provider/TTI/pixelmuse/async_pixelmuse.py +0 -249
- webscout/Provider/TTI/pixelmuse/sync_pixelmuse.py +0 -182
- webscout/Provider/TTI/talkai/README.md +0 -139
- webscout/Provider/TTI/talkai/__init__.py +0 -4
- webscout/Provider/TTI/talkai/async_talkai.py +0 -229
- webscout/Provider/TTI/talkai/sync_talkai.py +0 -207
- webscout/Provider/UNFINISHED/oivscode.py +0 -351
- {webscout-8.2.8.dist-info → webscout-8.2.9.dist-info}/licenses/LICENSE.md +0 -0
- {webscout-8.2.8.dist-info → webscout-8.2.9.dist-info}/top_level.txt +0 -0
webscout/Provider/OPENAI/api.py
CHANGED
|
@@ -1,121 +1,268 @@
|
|
|
1
1
|
"""
|
|
2
|
-
OpenAI-Compatible API Server
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
application designed to work with OpenAI's API.
|
|
8
|
-
|
|
9
|
-
Usage:
|
|
10
|
-
# From command line:
|
|
11
|
-
python -m webscout.Provider.OPENAI.api --port 8080 --api-key "your-key"
|
|
12
|
-
|
|
13
|
-
# From Python code:
|
|
14
|
-
from webscout.Provider.OPENAI.api import start_server
|
|
15
|
-
start_server(port=8080, api_key="your-key")
|
|
2
|
+
Webscout OpenAI-Compatible API Server
|
|
3
|
+
|
|
4
|
+
A FastAPI-based server that provides OpenAI-compatible endpoints for various LLM providers.
|
|
5
|
+
Supports streaming and non-streaming chat completions with comprehensive error handling,
|
|
6
|
+
authentication, and provider management.
|
|
16
7
|
"""
|
|
17
8
|
|
|
18
9
|
from __future__ import annotations
|
|
19
10
|
|
|
20
|
-
import logging
|
|
21
11
|
import json
|
|
22
|
-
import
|
|
23
|
-
import secrets
|
|
12
|
+
import logging
|
|
24
13
|
import os
|
|
25
|
-
import
|
|
14
|
+
import secrets
|
|
15
|
+
import sys
|
|
26
16
|
import time
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
from
|
|
30
|
-
|
|
31
|
-
from fastapi.responses import StreamingResponse, RedirectResponse, HTMLResponse, JSONResponse, FileResponse
|
|
32
|
-
from fastapi.staticfiles import StaticFiles
|
|
17
|
+
import uuid
|
|
18
|
+
import inspect
|
|
19
|
+
from typing import List, Dict, Optional, Union, Any, Generator, Callable
|
|
20
|
+
import types
|
|
33
21
|
|
|
22
|
+
import uvicorn
|
|
23
|
+
from fastapi import FastAPI, Response, Request, Body
|
|
24
|
+
from fastapi.middleware.cors import CORSMiddleware
|
|
25
|
+
from fastapi.responses import StreamingResponse, RedirectResponse, JSONResponse
|
|
26
|
+
from fastapi.openapi.utils import get_openapi
|
|
27
|
+
from fastapi.routing import APIRoute
|
|
34
28
|
from fastapi.exceptions import RequestValidationError
|
|
35
29
|
from fastapi.security import APIKeyHeader
|
|
36
|
-
from starlette.exceptions import HTTPException
|
|
30
|
+
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
37
31
|
from starlette.status import (
|
|
38
|
-
|
|
39
|
-
HTTP_422_UNPROCESSABLE_ENTITY,
|
|
32
|
+
HTTP_422_UNPROCESSABLE_ENTITY,
|
|
40
33
|
HTTP_404_NOT_FOUND,
|
|
41
34
|
HTTP_401_UNAUTHORIZED,
|
|
42
35
|
HTTP_403_FORBIDDEN,
|
|
43
36
|
HTTP_500_INTERNAL_SERVER_ERROR,
|
|
44
37
|
)
|
|
45
|
-
|
|
46
|
-
from
|
|
47
|
-
from typing import
|
|
38
|
+
|
|
39
|
+
from webscout.Provider.OPENAI.pydantic_imports import BaseModel, Field
|
|
40
|
+
from typing import Literal
|
|
48
41
|
|
|
49
42
|
# Import provider classes from the OPENAI directory
|
|
50
43
|
from webscout.Provider.OPENAI import *
|
|
51
44
|
from webscout.Provider.OPENAI.utils import (
|
|
52
|
-
|
|
53
|
-
ChatCompletionMessage, CompletionUsage
|
|
45
|
+
ChatCompletion, Choice, ChatCompletionMessage, CompletionUsage
|
|
54
46
|
)
|
|
55
47
|
|
|
56
|
-
logger = logging.getLogger(__name__)
|
|
57
48
|
|
|
49
|
+
# Configuration constants
|
|
58
50
|
DEFAULT_PORT = 8000
|
|
51
|
+
DEFAULT_HOST = "0.0.0.0"
|
|
52
|
+
API_VERSION = "v1"
|
|
53
|
+
|
|
54
|
+
# Setup logging
|
|
55
|
+
logging.basicConfig(
|
|
56
|
+
level=logging.INFO,
|
|
57
|
+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
|
58
|
+
handlers=[
|
|
59
|
+
logging.StreamHandler(sys.stdout),
|
|
60
|
+
]
|
|
61
|
+
)
|
|
62
|
+
logger = logging.getLogger("webscout.api")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class ServerConfig:
|
|
66
|
+
"""Centralized configuration management for the API server."""
|
|
67
|
+
|
|
68
|
+
def __init__(self):
|
|
69
|
+
self.api_key: Optional[str] = None
|
|
70
|
+
self.provider_map: Dict[str, Any] = {}
|
|
71
|
+
self.default_provider: str = "ChatGPT"
|
|
72
|
+
self.base_url: Optional[str] = None
|
|
73
|
+
self.host: str = DEFAULT_HOST
|
|
74
|
+
self.port: int = DEFAULT_PORT
|
|
75
|
+
self.debug: bool = False
|
|
76
|
+
self.cors_origins: List[str] = ["*"]
|
|
77
|
+
self.max_request_size: int = 10 * 1024 * 1024 # 10MB
|
|
78
|
+
self.request_timeout: int = 300 # 5 minutes
|
|
79
|
+
|
|
80
|
+
def update(self, **kwargs) -> None:
|
|
81
|
+
"""Update configuration with provided values."""
|
|
82
|
+
for key, value in kwargs.items():
|
|
83
|
+
if hasattr(self, key) and value is not None:
|
|
84
|
+
setattr(self, key, value)
|
|
85
|
+
logger.info(f"Config updated: {key} = {value}")
|
|
86
|
+
|
|
87
|
+
def validate(self) -> None:
|
|
88
|
+
"""Validate configuration settings."""
|
|
89
|
+
if self.port < 1 or self.port > 65535:
|
|
90
|
+
raise ValueError(f"Invalid port number: {self.port}")
|
|
91
|
+
|
|
92
|
+
if self.default_provider not in self.provider_map and self.provider_map:
|
|
93
|
+
available_providers = list(set(v.__name__ for v in self.provider_map.values()))
|
|
94
|
+
logger.warning(f"Default provider '{self.default_provider}' not found. Available: {available_providers}")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
# Global configuration instance
|
|
98
|
+
config = ServerConfig()
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
# Define Pydantic models for multimodal content parts, aligning with OpenAI's API
|
|
102
|
+
class TextPart(BaseModel):
|
|
103
|
+
"""Text content part for multimodal messages."""
|
|
104
|
+
type: Literal["text"]
|
|
105
|
+
text: str
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class ImageURL(BaseModel):
|
|
109
|
+
"""Image URL configuration for multimodal messages."""
|
|
110
|
+
url: str # Can be http(s) or data URI
|
|
111
|
+
detail: Optional[Literal["auto", "low", "high"]] = Field(
|
|
112
|
+
"auto",
|
|
113
|
+
description="Specifies the detail level of the image."
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class ImagePart(BaseModel):
|
|
118
|
+
"""Image content part for multimodal messages."""
|
|
119
|
+
type: Literal["image_url"]
|
|
120
|
+
image_url: ImageURL
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
MessageContentParts = Union[TextPart, ImagePart]
|
|
124
|
+
|
|
59
125
|
|
|
60
126
|
class Message(BaseModel):
|
|
127
|
+
"""Chat message model compatible with OpenAI API."""
|
|
61
128
|
role: Literal["system", "user", "assistant", "function", "tool"]
|
|
62
|
-
content: str
|
|
129
|
+
content: Optional[Union[str, List[MessageContentParts]]] = Field(
|
|
130
|
+
None,
|
|
131
|
+
description="The content of the message. Can be a string, a list of content parts (for multimodal), or null."
|
|
132
|
+
)
|
|
63
133
|
name: Optional[str] = None
|
|
134
|
+
# Future: Add tool_calls and tool_call_id for function calling support
|
|
135
|
+
# tool_calls: Optional[List[ToolCall]] = None
|
|
136
|
+
# tool_call_id: Optional[str] = None
|
|
64
137
|
|
|
65
138
|
class ChatCompletionRequest(BaseModel):
|
|
66
|
-
model: str
|
|
67
|
-
messages: List[Message]
|
|
68
|
-
temperature: Optional[float] = None
|
|
69
|
-
top_p: Optional[float] = None
|
|
70
|
-
n: Optional[int] = 1
|
|
71
|
-
stream: Optional[bool] = False
|
|
72
|
-
max_tokens: Optional[int] = None
|
|
73
|
-
presence_penalty: Optional[float] = None
|
|
74
|
-
frequency_penalty: Optional[float] = None
|
|
75
|
-
logit_bias: Optional[Dict[str, float]] = None
|
|
76
|
-
user: Optional[str] = None
|
|
77
|
-
stop: Optional[Union[str, List[str]]] = None
|
|
78
|
-
|
|
139
|
+
model: str = Field(..., description="ID of the model to use. See the model endpoint for the available models.")
|
|
140
|
+
messages: List[Message] = Field(..., description="A list of messages comprising the conversation so far.")
|
|
141
|
+
temperature: Optional[float] = Field(None, description="What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.")
|
|
142
|
+
top_p: Optional[float] = Field(None, description="An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.")
|
|
143
|
+
n: Optional[int] = Field(1, description="How many chat completion choices to generate for each input message.")
|
|
144
|
+
stream: Optional[bool] = Field(False, description="If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.")
|
|
145
|
+
max_tokens: Optional[int] = Field(None, description="The maximum number of tokens to generate in the chat completion.")
|
|
146
|
+
presence_penalty: Optional[float] = Field(None, description="Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.")
|
|
147
|
+
frequency_penalty: Optional[float] = Field(None, description="Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.")
|
|
148
|
+
logit_bias: Optional[Dict[str, float]] = Field(None, description="Modify the likelihood of specified tokens appearing in the completion.")
|
|
149
|
+
user: Optional[str] = Field(None, description="A unique identifier representing your end-user, which can help the API to monitor and detect abuse.")
|
|
150
|
+
stop: Optional[Union[str, List[str]]] = Field(None, description="Up to 4 sequences where the API will stop generating further tokens.")
|
|
151
|
+
|
|
79
152
|
class Config:
|
|
80
153
|
extra = "ignore" # Ignore extra fields that aren't in the model
|
|
154
|
+
schema_extra = {
|
|
155
|
+
"example": {
|
|
156
|
+
"model": "ChatGPT/gpt-4",
|
|
157
|
+
"messages": [
|
|
158
|
+
{"role": "system", "content": "You are a helpful assistant."},
|
|
159
|
+
{"role": "user", "content": "Hello, how are you?"}
|
|
160
|
+
],
|
|
161
|
+
"temperature": 0.7,
|
|
162
|
+
"max_tokens": 150,
|
|
163
|
+
"stream": False
|
|
164
|
+
}
|
|
165
|
+
}
|
|
81
166
|
|
|
82
|
-
class
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
167
|
+
class ModelInfo(BaseModel):
|
|
168
|
+
"""Model information for the models endpoint."""
|
|
169
|
+
id: str
|
|
170
|
+
object: str = "model"
|
|
171
|
+
created: int
|
|
172
|
+
owned_by: str
|
|
88
173
|
|
|
89
|
-
@classmethod
|
|
90
|
-
def from_exception(cls, exception: Exception, status_code: int = HTTP_500_INTERNAL_SERVER_ERROR):
|
|
91
|
-
return cls(format_exception(exception), status_code)
|
|
92
174
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
175
|
+
class ModelListResponse(BaseModel):
|
|
176
|
+
"""Response model for the models list endpoint."""
|
|
177
|
+
object: str = "list"
|
|
178
|
+
data: List[ModelInfo]
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
class ErrorDetail(BaseModel):
|
|
182
|
+
"""Error detail structure compatible with OpenAI API."""
|
|
183
|
+
message: str
|
|
184
|
+
type: str = "server_error"
|
|
185
|
+
param: Optional[str] = None
|
|
186
|
+
code: Optional[str] = None
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class ErrorResponse(BaseModel):
|
|
190
|
+
"""Error response structure compatible with OpenAI API."""
|
|
191
|
+
error: ErrorDetail
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
class APIError(Exception):
|
|
195
|
+
"""Custom exception for API errors."""
|
|
196
|
+
|
|
197
|
+
def __init__(self, message: str, status_code: int = HTTP_500_INTERNAL_SERVER_ERROR,
|
|
198
|
+
error_type: str = "server_error", param: Optional[str] = None,
|
|
199
|
+
code: Optional[str] = None):
|
|
200
|
+
self.message = message
|
|
201
|
+
self.status_code = status_code
|
|
202
|
+
self.error_type = error_type
|
|
203
|
+
self.param = param
|
|
204
|
+
self.code = code
|
|
205
|
+
super().__init__(message)
|
|
206
|
+
|
|
207
|
+
def to_response(self) -> JSONResponse:
|
|
208
|
+
"""Convert to FastAPI JSONResponse."""
|
|
209
|
+
error_detail = ErrorDetail(
|
|
210
|
+
message=self.message,
|
|
211
|
+
type=self.error_type,
|
|
212
|
+
param=self.param,
|
|
213
|
+
code=self.code
|
|
214
|
+
)
|
|
215
|
+
error_response = ErrorResponse(error=error_detail)
|
|
216
|
+
return JSONResponse(
|
|
217
|
+
status_code=self.status_code,
|
|
218
|
+
content=error_response.model_dump(exclude_none=True)
|
|
219
|
+
)
|
|
96
220
|
|
|
97
|
-
def render(self, content) -> bytes:
|
|
98
|
-
return str(content).encode(errors="ignore")
|
|
99
221
|
|
|
100
222
|
class AppConfig:
|
|
223
|
+
"""Legacy configuration class for backward compatibility."""
|
|
101
224
|
api_key: Optional[str] = None
|
|
102
225
|
provider_map = {}
|
|
103
226
|
default_provider = "ChatGPT"
|
|
227
|
+
base_url: Optional[str] = None
|
|
104
228
|
|
|
105
229
|
@classmethod
|
|
106
230
|
def set_config(cls, **data):
|
|
231
|
+
"""Set configuration values."""
|
|
107
232
|
for key, value in data.items():
|
|
108
233
|
setattr(cls, key, value)
|
|
234
|
+
# Sync with new config system
|
|
235
|
+
config.update(**data)
|
|
236
|
+
|
|
237
|
+
# Custom route class to handle dynamic base URLs
|
|
238
|
+
# Note: The /docs 404 issue is likely related to server execution (Werkzeug logs vs. Uvicorn script).
|
|
239
|
+
# This DynamicBaseRoute, when AppConfig.base_url is None, should act as a passthrough and not break /docs.
|
|
240
|
+
# If AppConfig.base_url is set, this route class has limitations in correctly handling prefixed routes
|
|
241
|
+
# without more complex path manipulation or using FastAPI's APIRouter prefixing/mounting features.
|
|
242
|
+
class DynamicBaseRoute(APIRoute):
|
|
243
|
+
def get_route_handler(self) -> Callable:
|
|
244
|
+
original_route_handler = super().get_route_handler()
|
|
245
|
+
async def custom_route_handler(request: Request) -> Response:
|
|
246
|
+
if AppConfig.base_url:
|
|
247
|
+
if not request.url.path.startswith(AppConfig.base_url):
|
|
248
|
+
# This logic might need refinement if base_url is used.
|
|
249
|
+
# For API routes not matching the prefix, a 404 might be appropriate.
|
|
250
|
+
# Docs routes (/docs, /openapi.json) are usually at the root.
|
|
251
|
+
# The current 'pass' allows root docs even if base_url is set for APIs.
|
|
252
|
+
pass
|
|
253
|
+
return await original_route_handler(request)
|
|
254
|
+
return custom_route_handler
|
|
109
255
|
|
|
110
256
|
def create_app():
|
|
111
257
|
app = FastAPI(
|
|
112
258
|
title="Webscout OpenAI API",
|
|
113
259
|
description="OpenAI API compatible interface for various LLM providers",
|
|
114
260
|
version="0.1.0",
|
|
115
|
-
docs_url=
|
|
261
|
+
docs_url="/docs",
|
|
262
|
+
redoc_url="/redoc",
|
|
263
|
+
openapi_url="/openapi.json",
|
|
116
264
|
)
|
|
117
|
-
|
|
118
|
-
# Add CORS middleware to allow cross-origin requests
|
|
265
|
+
app.router.route_class = DynamicBaseRoute
|
|
119
266
|
app.add_middleware(
|
|
120
267
|
CORSMiddleware,
|
|
121
268
|
allow_origins=["*"],
|
|
@@ -123,65 +270,121 @@ def create_app():
|
|
|
123
270
|
allow_methods=["*"],
|
|
124
271
|
allow_headers=["*"],
|
|
125
272
|
)
|
|
126
|
-
|
|
127
273
|
api = Api(app)
|
|
128
274
|
api.register_authorization()
|
|
129
|
-
api.register_json_middleware() # Add custom JSON middleware
|
|
130
275
|
api.register_validation_exception_handler()
|
|
131
276
|
api.register_routes()
|
|
132
|
-
|
|
133
|
-
# Initialize provider map
|
|
134
277
|
initialize_provider_map()
|
|
135
|
-
|
|
278
|
+
|
|
279
|
+
def custom_openapi():
|
|
280
|
+
if app.openapi_schema:
|
|
281
|
+
return app.openapi_schema
|
|
282
|
+
|
|
283
|
+
openapi_schema = get_openapi(
|
|
284
|
+
title=app.title,
|
|
285
|
+
version=app.version,
|
|
286
|
+
description=app.description,
|
|
287
|
+
routes=app.routes,
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
if "components" not in openapi_schema: openapi_schema["components"] = {}
|
|
291
|
+
if "schemas" not in openapi_schema["components"]: openapi_schema["components"]["schemas"] = {}
|
|
292
|
+
|
|
293
|
+
# Use Pydantic's schema generation for accuracy
|
|
294
|
+
# Assuming Pydantic v1 .schema() or v2 .model_json_schema() based on pydantic_imports
|
|
295
|
+
# For broader compatibility, trying .schema() first.
|
|
296
|
+
# If using Pydantic v2 primarily, .model_json_schema() is preferred.
|
|
297
|
+
schema_method_name = "model_json_schema" if hasattr(BaseModel, "model_json_schema") else "schema"
|
|
298
|
+
|
|
299
|
+
# Add/update schemas derived from Pydantic models to ensure they are correctly defined
|
|
300
|
+
pydantic_models_to_register = {
|
|
301
|
+
"TextPart": TextPart,
|
|
302
|
+
"ImageURL": ImageURL,
|
|
303
|
+
"ImagePart": ImagePart,
|
|
304
|
+
"Message": Message,
|
|
305
|
+
"ChatCompletionRequest": ChatCompletionRequest,
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
for name, model_cls in pydantic_models_to_register.items():
|
|
309
|
+
if schema_method_name == "model_json_schema":
|
|
310
|
+
schema_data = model_cls.model_json_schema(ref_template="#/components/schemas/{model}")
|
|
311
|
+
else:
|
|
312
|
+
schema_data = model_cls.schema()
|
|
313
|
+
# Pydantic might add a "title" to the schema, which is often not desired for component schemas
|
|
314
|
+
if "title" in schema_data:
|
|
315
|
+
del schema_data["title"]
|
|
316
|
+
openapi_schema["components"]["schemas"][name] = schema_data
|
|
317
|
+
|
|
318
|
+
app.openapi_schema = openapi_schema
|
|
319
|
+
return app.openapi_schema
|
|
320
|
+
|
|
321
|
+
app.openapi = custom_openapi
|
|
136
322
|
return app
|
|
137
323
|
|
|
138
324
|
def create_app_debug():
|
|
139
|
-
logging.basicConfig(level=logging.DEBUG)
|
|
140
325
|
return create_app()
|
|
141
326
|
|
|
142
|
-
def initialize_provider_map():
|
|
143
|
-
"""Initialize the provider map
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
327
|
+
def initialize_provider_map() -> None:
|
|
328
|
+
"""Initialize the provider map by discovering available providers."""
|
|
329
|
+
logger.info("Initializing provider map...")
|
|
330
|
+
|
|
331
|
+
try:
|
|
332
|
+
from webscout.Provider.OPENAI.base import OpenAICompatibleProvider
|
|
333
|
+
module = sys.modules["webscout.Provider.OPENAI"]
|
|
334
|
+
|
|
335
|
+
provider_count = 0
|
|
336
|
+
model_count = 0
|
|
337
|
+
|
|
338
|
+
for name, obj in inspect.getmembers(module):
|
|
339
|
+
if (
|
|
340
|
+
inspect.isclass(obj)
|
|
341
|
+
and issubclass(obj, OpenAICompatibleProvider)
|
|
342
|
+
and obj.__name__ != "OpenAICompatibleProvider"
|
|
343
|
+
):
|
|
344
|
+
provider_name = obj.__name__
|
|
345
|
+
AppConfig.provider_map[provider_name] = obj
|
|
346
|
+
config.provider_map[provider_name] = obj
|
|
347
|
+
provider_count += 1
|
|
348
|
+
|
|
349
|
+
# Register available models for this provider
|
|
350
|
+
if hasattr(obj, "AVAILABLE_MODELS") and isinstance(
|
|
351
|
+
obj.AVAILABLE_MODELS, (list, tuple, set)
|
|
352
|
+
):
|
|
353
|
+
for model in obj.AVAILABLE_MODELS:
|
|
354
|
+
if model and isinstance(model, str):
|
|
355
|
+
model_key = f"{provider_name}/{model}"
|
|
356
|
+
AppConfig.provider_map[model_key] = obj
|
|
357
|
+
config.provider_map[model_key] = obj
|
|
358
|
+
model_count += 1
|
|
359
|
+
|
|
360
|
+
# Fallback to ChatGPT if no providers found
|
|
361
|
+
if not AppConfig.provider_map:
|
|
362
|
+
logger.warning("No providers found, using ChatGPT fallback")
|
|
363
|
+
try:
|
|
364
|
+
from webscout.Provider.OPENAI.chatgpt import ChatGPT
|
|
365
|
+
fallback_models = ["gpt-4", "gpt-4o", "gpt-4o-mini", "gpt-3.5-turbo"]
|
|
366
|
+
|
|
367
|
+
AppConfig.provider_map["ChatGPT"] = ChatGPT
|
|
368
|
+
config.provider_map["ChatGPT"] = ChatGPT
|
|
369
|
+
|
|
370
|
+
for model in fallback_models:
|
|
371
|
+
model_key = f"ChatGPT/{model}"
|
|
372
|
+
AppConfig.provider_map[model_key] = ChatGPT
|
|
373
|
+
config.provider_map[model_key] = ChatGPT
|
|
374
|
+
|
|
375
|
+
AppConfig.default_provider = "ChatGPT"
|
|
376
|
+
config.default_provider = "ChatGPT"
|
|
377
|
+
provider_count = 1
|
|
378
|
+
model_count = len(fallback_models)
|
|
379
|
+
except ImportError as e:
|
|
380
|
+
logger.error(f"Failed to import ChatGPT fallback: {e}")
|
|
381
|
+
raise APIError("No providers available", HTTP_500_INTERNAL_SERVER_ERROR)
|
|
382
|
+
|
|
383
|
+
logger.info(f"Initialized {provider_count} providers with {model_count} models")
|
|
384
|
+
|
|
385
|
+
except Exception as e:
|
|
386
|
+
logger.error(f"Failed to initialize provider map: {e}")
|
|
387
|
+
raise APIError(f"Provider initialization failed: {e}", HTTP_500_INTERNAL_SERVER_ERROR)
|
|
185
388
|
|
|
186
389
|
class Api:
|
|
187
390
|
def __init__(self, app: FastAPI) -> None:
|
|
@@ -194,522 +397,446 @@ class Api:
|
|
|
194
397
|
if AppConfig.api_key is not None:
|
|
195
398
|
auth_header = await self.get_api_key(request)
|
|
196
399
|
path = request.url.path
|
|
197
|
-
if path.startswith("/v1"):
|
|
400
|
+
if path.startswith("/v1"): # Only protect /v1 routes
|
|
401
|
+
# Also allow access to /docs, /openapi.json etc. if AppConfig.base_url is not set or path is not under it
|
|
402
|
+
# This logic should be fine as it only protects /v1 paths
|
|
198
403
|
if auth_header is None:
|
|
199
404
|
return ErrorResponse.from_message("API key required", HTTP_401_UNAUTHORIZED)
|
|
200
|
-
# Strip "Bearer " prefix if present
|
|
201
405
|
if auth_header.startswith("Bearer "):
|
|
202
406
|
auth_header = auth_header[7:]
|
|
203
|
-
if AppConfig.api_key is None or not secrets.compare_digest(AppConfig.api_key, auth_header):
|
|
407
|
+
if AppConfig.api_key is None or not secrets.compare_digest(AppConfig.api_key, auth_header): # AppConfig.api_key check is redundant after outer if
|
|
204
408
|
return ErrorResponse.from_message("Invalid API key", HTTP_403_FORBIDDEN)
|
|
205
409
|
return await call_next(request)
|
|
206
410
|
|
|
207
|
-
def register_json_middleware(self):
|
|
208
|
-
@self.app.middleware("http")
|
|
209
|
-
async def parse_json_middleware(request: Request, call_next):
|
|
210
|
-
if request.method == "POST" and "/v1/chat/completions" in request.url.path:
|
|
211
|
-
try:
|
|
212
|
-
# Try parsing the JSON body manually first to catch JSON errors early
|
|
213
|
-
body = await request.body()
|
|
214
|
-
if body:
|
|
215
|
-
body_str = body.decode('utf-8', errors='ignore')
|
|
216
|
-
original_body = body_str
|
|
217
|
-
logger.debug(f"Original request body: {body_str}")
|
|
218
|
-
|
|
219
|
-
# PowerShell with curl often has formatting issues with JSON
|
|
220
|
-
try:
|
|
221
|
-
# First try normal JSON parsing
|
|
222
|
-
json.loads(body_str)
|
|
223
|
-
logger.debug("JSON parsed successfully")
|
|
224
|
-
except json.JSONDecodeError as e:
|
|
225
|
-
logger.warning(f"JSON parse error, attempting fixes: {str(e)}")
|
|
226
|
-
|
|
227
|
-
# Series of fixes to try for common PowerShell JSON issues
|
|
228
|
-
try:
|
|
229
|
-
# Fix 1: Try to clean up the JSON string
|
|
230
|
-
# Replace literal backslash+quote with just quote
|
|
231
|
-
body_str = body_str.replace("\\\"", "\"")
|
|
232
|
-
# Add double quotes to unquoted property names and string values
|
|
233
|
-
# This is a common issue with PowerShell's curl
|
|
234
|
-
import re
|
|
235
|
-
|
|
236
|
-
# Try a full JSON correction - replace single quotes with double quotes
|
|
237
|
-
# This is a more aggressive fix that might work in simple cases
|
|
238
|
-
fixed_body = body_str.replace("'", "\"")
|
|
239
|
-
try:
|
|
240
|
-
json.loads(fixed_body)
|
|
241
|
-
body_str = fixed_body
|
|
242
|
-
logger.info("Fixed JSON by replacing single quotes with double quotes")
|
|
243
|
-
except json.JSONDecodeError:
|
|
244
|
-
# If that didn't work, try more sophisticated fixes
|
|
245
|
-
pass
|
|
246
|
-
|
|
247
|
-
# Check for missing quotes around property names
|
|
248
|
-
# Look for patterns like {model: instead of {"model":
|
|
249
|
-
body_str = re.sub(r'\{([^"\s][^:\s]*)(\s*:)', r'{"\1"\2', body_str)
|
|
250
|
-
body_str = re.sub(r',\s*([^"\s][^:\s]*)(\s*:)', r', "\1"\2', body_str)
|
|
251
|
-
|
|
252
|
-
# Try to parse with the fixed body
|
|
253
|
-
json.loads(body_str)
|
|
254
|
-
# If successful, modify the request._body for downstream processing
|
|
255
|
-
logger.info(f"Successfully fixed JSON format\nOriginal: {original_body}\nFixed: {body_str}")
|
|
256
|
-
request._body = body_str.encode('utf-8')
|
|
257
|
-
except Exception as fix_error:
|
|
258
|
-
logger.error(f"Failed to fix JSON: {str(fix_error)}")
|
|
259
|
-
|
|
260
|
-
# Let's return a helpful error message with the proper format example
|
|
261
|
-
example = json.dumps({
|
|
262
|
-
"model": "gpt-4",
|
|
263
|
-
"messages": [{"role": "user", "content": "Hello"}]
|
|
264
|
-
})
|
|
265
|
-
return JSONResponse(
|
|
266
|
-
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
267
|
-
content=jsonable_encoder({
|
|
268
|
-
"detail": [
|
|
269
|
-
{
|
|
270
|
-
"loc": ["body", 0],
|
|
271
|
-
"message": f"Invalid JSON format: {str(e)}. Make sure to use double quotes for both keys and values. Example: {example}",
|
|
272
|
-
"type": "json_invalid"
|
|
273
|
-
}
|
|
274
|
-
]
|
|
275
|
-
}),
|
|
276
|
-
)
|
|
277
|
-
except Exception as e:
|
|
278
|
-
error_detail = str(e)
|
|
279
|
-
logger.error(f"Request processing error: {error_detail}")
|
|
280
|
-
return JSONResponse(
|
|
281
|
-
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
282
|
-
content=jsonable_encoder({
|
|
283
|
-
"detail": [
|
|
284
|
-
{
|
|
285
|
-
"loc": ["body", 0],
|
|
286
|
-
"message": f"Request processing error: {error_detail}",
|
|
287
|
-
"type": "request_invalid"
|
|
288
|
-
}
|
|
289
|
-
]
|
|
290
|
-
}),
|
|
291
|
-
)
|
|
292
|
-
return await call_next(request)
|
|
293
|
-
|
|
294
411
|
def register_validation_exception_handler(self):
|
|
412
|
+
"""Register comprehensive exception handlers."""
|
|
413
|
+
|
|
414
|
+
@self.app.exception_handler(APIError)
|
|
415
|
+
async def api_error_handler(request: Request, exc: APIError):
|
|
416
|
+
"""Handle custom API errors."""
|
|
417
|
+
logger.error(f"API Error: {exc.message} (Status: {exc.status_code})")
|
|
418
|
+
return exc.to_response()
|
|
419
|
+
|
|
295
420
|
@self.app.exception_handler(RequestValidationError)
|
|
296
421
|
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
422
|
+
errors = exc.errors()
|
|
423
|
+
error_messages = []
|
|
424
|
+
body = await request.body()
|
|
425
|
+
is_empty_body = not body or body.strip() in (b"", b"null", b"{}")
|
|
426
|
+
for error in errors:
|
|
427
|
+
loc = error.get("loc", [])
|
|
428
|
+
# Ensure loc_str is user-friendly
|
|
429
|
+
loc_str_parts = []
|
|
430
|
+
for item in loc:
|
|
431
|
+
if item == "body": # Skip "body" part if it's the first element of a longer path
|
|
432
|
+
if len(loc) > 1: continue
|
|
433
|
+
loc_str_parts.append(str(item))
|
|
434
|
+
loc_str = " -> ".join(loc_str_parts)
|
|
435
|
+
|
|
436
|
+
msg = error.get("msg", "Validation error")
|
|
437
|
+
|
|
438
|
+
# Check if this error is for the 'content' field specifically due to multimodal input
|
|
439
|
+
if len(loc) >=3 and loc[0] == 'body' and loc[1] == 'messages' and loc[-1] == 'content':
|
|
440
|
+
# Check if the error type suggests a string was expected but a list (or vice-versa) was given for content
|
|
441
|
+
if "Input should be a valid string" in msg and error.get("input_type") == "list":
|
|
442
|
+
error_messages.append({
|
|
443
|
+
"loc": loc,
|
|
444
|
+
"message": f"Invalid message content: {msg}. Ensure content matches the expected format (string or list of content parts). Path: {loc_str}",
|
|
445
|
+
"type": error.get("type", "validation_error")
|
|
446
|
+
})
|
|
447
|
+
continue # Skip default message formatting for this specific case
|
|
448
|
+
elif "Input should be a valid list" in msg and error.get("input_type") == "string":
|
|
449
|
+
error_messages.append({
|
|
450
|
+
"loc": loc,
|
|
451
|
+
"message": f"Invalid message content: {msg}. Ensure content matches the expected format (string or list of content parts). Path: {loc_str}",
|
|
452
|
+
"type": error.get("type", "validation_error")
|
|
453
|
+
})
|
|
454
|
+
continue
|
|
455
|
+
|
|
456
|
+
if "body" in loc:
|
|
457
|
+
if len(loc) > 1 and loc[1] == "messages":
|
|
458
|
+
error_messages.append({
|
|
459
|
+
"loc": loc,
|
|
460
|
+
"message": "The 'messages' field is required and must be a non-empty array of message objects. " + f"Error: {msg} at {loc_str}",
|
|
461
|
+
"type": error.get("type", "validation_error")
|
|
462
|
+
})
|
|
463
|
+
elif len(loc) > 1 and loc[1] == "model":
|
|
464
|
+
error_messages.append({
|
|
465
|
+
"loc": loc,
|
|
466
|
+
"message": "The 'model' field is required and must be a string. " + f"Error: {msg} at {loc_str}",
|
|
467
|
+
"type": error.get("type", "validation_error")
|
|
468
|
+
})
|
|
469
|
+
else:
|
|
470
|
+
error_messages.append({
|
|
471
|
+
"loc": loc,
|
|
472
|
+
"message": f"{msg} at {loc_str}",
|
|
473
|
+
"type": error.get("type", "validation_error")
|
|
474
|
+
})
|
|
475
|
+
else:
|
|
476
|
+
error_messages.append({
|
|
477
|
+
"loc": loc,
|
|
478
|
+
"message": f"{msg} at {loc_str}",
|
|
479
|
+
"type": error.get("type", "validation_error")
|
|
480
|
+
})
|
|
481
|
+
if request.url.path == "/v1/chat/completions":
|
|
482
|
+
example = ChatCompletionRequest.Config.schema_extra["example"]
|
|
483
|
+
if is_empty_body:
|
|
484
|
+
return JSONResponse(
|
|
485
|
+
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
486
|
+
content={
|
|
487
|
+
"error": {
|
|
488
|
+
"message": "Request body is required and must include 'model' and 'messages'.",
|
|
489
|
+
"type": "invalid_request_error",
|
|
490
|
+
"param": None,
|
|
491
|
+
"code": "body_missing"
|
|
492
|
+
},
|
|
493
|
+
"example": example
|
|
494
|
+
}
|
|
495
|
+
)
|
|
496
|
+
return JSONResponse(
|
|
497
|
+
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
498
|
+
content={"detail": error_messages, "example": example}
|
|
499
|
+
)
|
|
305
500
|
return JSONResponse(
|
|
306
501
|
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
307
|
-
content=
|
|
502
|
+
content={"detail": error_messages}
|
|
308
503
|
)
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
async def http_exception_handler(request: Request, exc: HTTPException):
|
|
504
|
+
@self.app.exception_handler(StarletteHTTPException)
|
|
505
|
+
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
|
|
312
506
|
return JSONResponse(
|
|
313
507
|
status_code=exc.status_code,
|
|
314
|
-
content=
|
|
508
|
+
content={"detail": exc.detail}
|
|
315
509
|
)
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
async def json_decode_error_handler(request: Request, exc: json.JSONDecodeError):
|
|
510
|
+
@self.app.exception_handler(Exception)
|
|
511
|
+
async def general_exception_handler(request: Request, exc: Exception):
|
|
319
512
|
return JSONResponse(
|
|
320
|
-
status_code=
|
|
321
|
-
content=
|
|
322
|
-
"detail": [
|
|
323
|
-
{
|
|
324
|
-
"loc": ["body", 0],
|
|
325
|
-
"message": f"Invalid JSON format: {str(exc)}",
|
|
326
|
-
"type": "json_invalid"
|
|
327
|
-
}
|
|
328
|
-
]
|
|
329
|
-
}),
|
|
513
|
+
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
|
|
514
|
+
content={"detail": f"Internal server error: {str(exc)}"}
|
|
330
515
|
)
|
|
331
516
|
|
|
332
517
|
def register_routes(self):
|
|
333
|
-
@self.app.get("/")
|
|
334
|
-
async def
|
|
518
|
+
@self.app.get("/", include_in_schema=False)
|
|
519
|
+
async def root():
|
|
520
|
+
# Note: If /docs is 404ing, check if server is Uvicorn (expected) or Werkzeug (from logs).
|
|
521
|
+
# Werkzeug logs suggest possible execution of a Flask app or WSGI misconfiguration.
|
|
522
|
+
# This api.py file is intended for Uvicorn.
|
|
335
523
|
return RedirectResponse(url="/docs")
|
|
336
524
|
|
|
337
|
-
@self.app.get("/v1")
|
|
338
|
-
async def read_root_v1(request: Request):
|
|
339
|
-
return RedirectResponse(url="/docs")
|
|
340
|
-
|
|
341
|
-
@self.app.get("/docs", include_in_schema=False)
|
|
342
|
-
async def custom_swagger_ui(request: Request):
|
|
343
|
-
from fastapi.openapi.docs import get_swagger_ui_html
|
|
344
|
-
return get_swagger_ui_html(
|
|
345
|
-
openapi_url=self.app.openapi_url,
|
|
346
|
-
title=f"{self.app.title} - Swagger UI"
|
|
347
|
-
)
|
|
348
|
-
|
|
349
|
-
@self.app.get("/v1//models", include_in_schema=False) # Handle double slash case
|
|
350
|
-
async def list_models_double_slash():
|
|
351
|
-
"""Redirect double slash models endpoint to the correct one"""
|
|
352
|
-
return RedirectResponse(url="/v1/models")
|
|
353
|
-
|
|
354
|
-
@self.app.get("/v1/models")
|
|
525
|
+
@self.app.get("/v1/models", response_model=ModelListResponse)
|
|
355
526
|
async def list_models():
|
|
356
|
-
|
|
357
|
-
from webscout.Provider.OPENAI.utils import ModelData, ModelList
|
|
358
|
-
models_data = []
|
|
359
|
-
|
|
360
|
-
# Get current timestamp
|
|
361
|
-
created_time = int(time.time())
|
|
362
|
-
|
|
527
|
+
models = []
|
|
363
528
|
for model_name, provider_class in AppConfig.provider_map.items():
|
|
364
|
-
if
|
|
365
|
-
#
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
"created": created_time,
|
|
374
|
-
"allow_create_engine": False,
|
|
375
|
-
"allow_sampling": True,
|
|
376
|
-
"allow_logprobs": True,
|
|
377
|
-
"allow_search_indices": hasattr(provider_class, "supports_embeddings") and provider_class.supports_embeddings,
|
|
378
|
-
"allow_view": True,
|
|
379
|
-
"allow_fine_tuning": False,
|
|
380
|
-
"organization": "*",
|
|
381
|
-
"group": None,
|
|
382
|
-
"is_blocking": False
|
|
383
|
-
}]
|
|
384
|
-
)
|
|
385
|
-
models_data.append(model)
|
|
386
|
-
|
|
387
|
-
# Return as ModelList for proper formatting
|
|
388
|
-
response = ModelList(data=models_data)
|
|
389
|
-
return response.to_dict()
|
|
390
|
-
|
|
391
|
-
@self.app.get("/v1/models/{model_name}")
|
|
392
|
-
async def get_model(model_name: str):
|
|
393
|
-
"""Get information about a specific model"""
|
|
394
|
-
from webscout.Provider.OPENAI.utils import ModelData
|
|
395
|
-
created_time = int(time.time())
|
|
396
|
-
|
|
397
|
-
# Check if the model exists in our provider map
|
|
398
|
-
if model_name in AppConfig.provider_map:
|
|
399
|
-
provider_class = AppConfig.provider_map[model_name]
|
|
400
|
-
|
|
401
|
-
# Create a proper OpenAI-compatible model response
|
|
402
|
-
model = ModelData(
|
|
403
|
-
id=model_name,
|
|
404
|
-
created=created_time,
|
|
405
|
-
owned_by=getattr(provider_class, "__name__", "webscout"),
|
|
406
|
-
permission=[{
|
|
407
|
-
"id": f"modelperm-{model_name}",
|
|
408
|
-
"object": "model_permission",
|
|
409
|
-
"created": created_time,
|
|
410
|
-
"allow_create_engine": False,
|
|
411
|
-
"allow_sampling": True,
|
|
412
|
-
"allow_logprobs": True,
|
|
413
|
-
"allow_search_indices": hasattr(provider_class, "supports_embeddings") and provider_class.supports_embeddings,
|
|
414
|
-
"allow_view": True,
|
|
415
|
-
"allow_fine_tuning": False,
|
|
416
|
-
"organization": "*",
|
|
417
|
-
"group": None,
|
|
418
|
-
"is_blocking": False
|
|
419
|
-
}]
|
|
420
|
-
)
|
|
421
|
-
return model.to_dict()
|
|
422
|
-
|
|
423
|
-
# If we reached here, the model was not found
|
|
424
|
-
return ErrorResponse.from_message(f"Model '{model_name}' not found", HTTP_404_NOT_FOUND)
|
|
425
|
-
|
|
426
|
-
@self.app.post("/v1/chat/completions")
|
|
427
|
-
async def chat_completions(request: Request):
|
|
428
|
-
"""Create a chat completion"""
|
|
429
|
-
# First manually extract the request body to better handle parsing errors
|
|
430
|
-
try:
|
|
431
|
-
# Note: We don't need to parse JSON here as our middleware already handles that
|
|
432
|
-
# and fixes PowerShell JSON issues
|
|
433
|
-
body = await request.json()
|
|
434
|
-
logger.debug(f"Request body parsed successfully: {body}")
|
|
435
|
-
|
|
436
|
-
# Check for required fields
|
|
437
|
-
if "model" not in body:
|
|
438
|
-
return JSONResponse(
|
|
439
|
-
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
440
|
-
content=jsonable_encoder({
|
|
441
|
-
"detail": [
|
|
442
|
-
{
|
|
443
|
-
"loc": ["body", "model"],
|
|
444
|
-
"message": "Field 'model' is required",
|
|
445
|
-
"type": "missing"
|
|
446
|
-
}
|
|
447
|
-
]
|
|
448
|
-
}),
|
|
449
|
-
)
|
|
450
|
-
|
|
451
|
-
if "messages" not in body or not isinstance(body["messages"], list) or len(body["messages"]) == 0:
|
|
452
|
-
return JSONResponse(
|
|
453
|
-
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
454
|
-
content=jsonable_encoder({
|
|
455
|
-
"detail": [
|
|
456
|
-
{
|
|
457
|
-
"loc": ["body", "messages"],
|
|
458
|
-
"message": "Field 'messages' must be a non-empty array",
|
|
459
|
-
"type": "missing"
|
|
460
|
-
}
|
|
461
|
-
]
|
|
462
|
-
}),
|
|
463
|
-
)
|
|
464
|
-
|
|
465
|
-
# Now parse it through Pydantic model
|
|
466
|
-
try:
|
|
467
|
-
chat_request = ChatCompletionRequest(**body)
|
|
468
|
-
except Exception as validation_error:
|
|
469
|
-
logger.warning(f"Validation error: {validation_error}")
|
|
470
|
-
# Try to provide helpful error messages for common validation issues
|
|
471
|
-
error_msg = str(validation_error)
|
|
472
|
-
if "role" in error_msg:
|
|
473
|
-
return JSONResponse(
|
|
474
|
-
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
475
|
-
content=jsonable_encoder({
|
|
476
|
-
"detail": [
|
|
477
|
-
{
|
|
478
|
-
"loc": ["body", "messages", 0, "role"],
|
|
479
|
-
"message": "Each message must have a 'role' field with one of these values: 'system', 'user', 'assistant'",
|
|
480
|
-
"type": "value_error"
|
|
481
|
-
}
|
|
482
|
-
]
|
|
483
|
-
}),
|
|
484
|
-
)
|
|
485
|
-
elif "content" in error_msg:
|
|
486
|
-
return JSONResponse(
|
|
487
|
-
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
488
|
-
content=jsonable_encoder({
|
|
489
|
-
"detail": [
|
|
490
|
-
{
|
|
491
|
-
"loc": ["body", "messages", 0, "content"],
|
|
492
|
-
"message": "Each message must have a 'content' field with string value",
|
|
493
|
-
"type": "value_error"
|
|
494
|
-
}
|
|
495
|
-
]
|
|
496
|
-
}),
|
|
497
|
-
)
|
|
498
|
-
else:
|
|
499
|
-
return JSONResponse(
|
|
500
|
-
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
|
|
501
|
-
content=jsonable_encoder({
|
|
502
|
-
"detail": [
|
|
503
|
-
{
|
|
504
|
-
"loc": ["body"],
|
|
505
|
-
"message": f"Validation error: {error_msg}",
|
|
506
|
-
"type": "value_error"
|
|
507
|
-
}
|
|
508
|
-
]
|
|
509
|
-
}),
|
|
510
|
-
)
|
|
511
|
-
|
|
512
|
-
except json.JSONDecodeError as e:
|
|
513
|
-
logger.error(f"JSON decode error in chat_completions: {e}")
|
|
514
|
-
example = json.dumps({
|
|
515
|
-
"model": "gpt-4",
|
|
516
|
-
"messages": [{"role": "user", "content": "Hello"}]
|
|
529
|
+
if "/" not in model_name:
|
|
530
|
+
continue # Skip provider names
|
|
531
|
+
if any(m["id"] == model_name for m in models):
|
|
532
|
+
continue
|
|
533
|
+
models.append({
|
|
534
|
+
"id": model_name,
|
|
535
|
+
"object": "model",
|
|
536
|
+
"created": int(time.time()),
|
|
537
|
+
"owned_by": provider_class.__name__
|
|
517
538
|
})
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
539
|
+
return {
|
|
540
|
+
"object": "list",
|
|
541
|
+
"data": models
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
@self.app.post(
|
|
545
|
+
"/v1/chat/completions",
|
|
546
|
+
response_model_exclude_none=True,
|
|
547
|
+
response_model_exclude_unset=True,
|
|
548
|
+
openapi_extra={ # This ensures the example is shown in docs
|
|
549
|
+
"requestBody": {
|
|
550
|
+
"content": {
|
|
551
|
+
"application/json": {
|
|
552
|
+
"schema": {
|
|
553
|
+
"$ref": "#/components/schemas/ChatCompletionRequest" # Relies on custom_openapi
|
|
554
|
+
},
|
|
555
|
+
"example": ChatCompletionRequest.Config.schema_extra["example"]
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
)
|
|
561
|
+
async def chat_completions(
|
|
562
|
+
chat_request: ChatCompletionRequest = Body(...)
|
|
563
|
+
):
|
|
564
|
+
"""Handle chat completion requests with comprehensive error handling."""
|
|
565
|
+
start_time = time.time()
|
|
566
|
+
request_id = f"chatcmpl-{uuid.uuid4()}"
|
|
567
|
+
|
|
537
568
|
try:
|
|
538
|
-
|
|
539
|
-
provider_class = None
|
|
540
|
-
model = chat_request.model
|
|
541
|
-
logger.info(f"Chat completion request for model: {model}")
|
|
542
|
-
|
|
543
|
-
if model in AppConfig.provider_map:
|
|
544
|
-
provider_class = AppConfig.provider_map[model]
|
|
545
|
-
logger.info(f"Found provider class for model {model}: {provider_class.__name__}")
|
|
546
|
-
else:
|
|
547
|
-
# Use default provider if specific provider not found
|
|
548
|
-
provider_class = AppConfig.provider_map.get(AppConfig.default_provider)
|
|
549
|
-
logger.info(f"Using default provider {AppConfig.default_provider} for model {model}")
|
|
550
|
-
|
|
551
|
-
if not provider_class:
|
|
552
|
-
logger.error(f"No provider available for model {model}. Available models: {list(AppConfig.provider_map.keys())}")
|
|
553
|
-
return ErrorResponse.from_message(
|
|
554
|
-
f"Model '{model}' not supported. Available models: {list(AppConfig.provider_map.keys())}",
|
|
555
|
-
HTTP_404_NOT_FOUND
|
|
556
|
-
)
|
|
569
|
+
logger.info(f"Processing chat completion request {request_id} for model: {chat_request.model}")
|
|
557
570
|
|
|
558
|
-
#
|
|
559
|
-
|
|
571
|
+
# Resolve provider and model
|
|
572
|
+
provider_class, model_name = resolve_provider_and_model(chat_request.model)
|
|
573
|
+
|
|
574
|
+
# Initialize provider with error handling
|
|
560
575
|
try:
|
|
561
576
|
provider = provider_class()
|
|
577
|
+
logger.debug(f"Initialized provider: {provider_class.__name__}")
|
|
562
578
|
except Exception as e:
|
|
563
|
-
logger.
|
|
564
|
-
|
|
579
|
+
logger.error(f"Failed to initialize provider {provider_class.__name__}: {e}")
|
|
580
|
+
raise APIError(
|
|
565
581
|
f"Failed to initialize provider {provider_class.__name__}: {e}",
|
|
566
|
-
HTTP_500_INTERNAL_SERVER_ERROR
|
|
582
|
+
HTTP_500_INTERNAL_SERVER_ERROR,
|
|
583
|
+
"provider_error"
|
|
567
584
|
)
|
|
568
585
|
|
|
569
|
-
#
|
|
570
|
-
|
|
571
|
-
messages = []
|
|
572
|
-
for msg in chat_request.messages:
|
|
573
|
-
message_dict = {
|
|
574
|
-
"role": msg.role,
|
|
575
|
-
"content": msg.content
|
|
576
|
-
}
|
|
577
|
-
# Add name field if present
|
|
578
|
-
if msg.name:
|
|
579
|
-
message_dict["name"] = msg.name
|
|
580
|
-
messages.append(message_dict)
|
|
581
|
-
|
|
582
|
-
params = {
|
|
583
|
-
"model": model,
|
|
584
|
-
"messages": messages,
|
|
585
|
-
"stream": chat_request.stream,
|
|
586
|
-
}
|
|
586
|
+
# Process and validate messages
|
|
587
|
+
processed_messages = process_messages(chat_request.messages)
|
|
587
588
|
|
|
588
|
-
#
|
|
589
|
-
|
|
590
|
-
params["temperature"] = chat_request.temperature
|
|
591
|
-
if chat_request.max_tokens is not None:
|
|
592
|
-
params["max_tokens"] = chat_request.max_tokens
|
|
593
|
-
if chat_request.top_p is not None:
|
|
594
|
-
params["top_p"] = chat_request.top_p
|
|
589
|
+
# Prepare parameters for provider
|
|
590
|
+
params = prepare_provider_params(chat_request, model_name, processed_messages)
|
|
595
591
|
|
|
596
|
-
#
|
|
592
|
+
# Handle streaming vs non-streaming
|
|
597
593
|
if chat_request.stream:
|
|
598
|
-
|
|
599
|
-
try:
|
|
600
|
-
logger.info(f"Creating streaming completion with {provider_class.__name__}")
|
|
601
|
-
completion_stream = provider.chat.completions.create(**params)
|
|
602
|
-
logger.info(f"Got streaming response: {type(completion_stream)}")
|
|
603
|
-
|
|
604
|
-
if isinstance(completion_stream, Generator):
|
|
605
|
-
for chunk in completion_stream:
|
|
606
|
-
logger.debug(f"Streaming chunk: {type(chunk)}")
|
|
607
|
-
if hasattr(chunk, 'to_dict'):
|
|
608
|
-
# Use to_dict() for our custom dataclasses
|
|
609
|
-
yield f"data: {json.dumps(chunk.to_dict())}\n\n"
|
|
610
|
-
elif hasattr(chunk, 'model_dump'):
|
|
611
|
-
# For Pydantic models
|
|
612
|
-
yield f"data: {json.dumps(chunk.model_dump())}\n\n"
|
|
613
|
-
else:
|
|
614
|
-
# For dictionaries or other JSON-serializable objects
|
|
615
|
-
yield f"data: {json.dumps(chunk)}\n\n"
|
|
616
|
-
else:
|
|
617
|
-
# If the provider doesn't implement streaming but stream=True,
|
|
618
|
-
# simulate streaming with a single chunk
|
|
619
|
-
logger.info(f"Provider returned non-streaming response, simulating stream")
|
|
620
|
-
yield f"data: {json.dumps(completion_stream)}\n\n"
|
|
621
|
-
except Exception as e:
|
|
622
|
-
logger.exception(f"Error in streaming: {e}")
|
|
623
|
-
yield f"data: {format_exception(e)}\n\n"
|
|
624
|
-
yield "data: [DONE]\n\n"
|
|
625
|
-
|
|
626
|
-
return StreamingResponse(streaming(), media_type="text/event-stream")
|
|
594
|
+
return await handle_streaming_response(provider, params, request_id)
|
|
627
595
|
else:
|
|
628
|
-
|
|
629
|
-
try:
|
|
630
|
-
completion = provider.chat.completions.create(**params)
|
|
631
|
-
logger.info(f"Got completion response: {type(completion)}")
|
|
632
|
-
|
|
633
|
-
# If the response is empty or None, create a default response
|
|
634
|
-
if completion is None:
|
|
635
|
-
logger.warning(f"Provider {provider_class.__name__} returned None for completion")
|
|
636
|
-
return {
|
|
637
|
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
|
638
|
-
"created": int(time.time()),
|
|
639
|
-
"model": model,
|
|
640
|
-
"choices": [
|
|
641
|
-
{
|
|
642
|
-
"index": 0,
|
|
643
|
-
"message": {
|
|
644
|
-
"role": "assistant",
|
|
645
|
-
"content": "I apologize, but I couldn't generate a response. Please try again or try a different model.",
|
|
646
|
-
},
|
|
647
|
-
"finish_reason": "stop",
|
|
648
|
-
}
|
|
649
|
-
],
|
|
650
|
-
"usage": {
|
|
651
|
-
"prompt_tokens": 0,
|
|
652
|
-
"completion_tokens": 0,
|
|
653
|
-
"total_tokens": 0,
|
|
654
|
-
},
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
# Return the response in the appropriate format
|
|
658
|
-
if isinstance(completion, dict):
|
|
659
|
-
return completion
|
|
660
|
-
elif hasattr(completion, "model_dump"):
|
|
661
|
-
return completion.model_dump()
|
|
662
|
-
else:
|
|
663
|
-
return completion
|
|
664
|
-
except Exception as e:
|
|
665
|
-
logger.exception(f"Error in completion: {e}")
|
|
666
|
-
return ErrorResponse.from_exception(e, HTTP_500_INTERNAL_SERVER_ERROR)
|
|
596
|
+
return await handle_non_streaming_response(provider, params, request_id, start_time)
|
|
667
597
|
|
|
598
|
+
except APIError:
|
|
599
|
+
# Re-raise API errors as-is
|
|
600
|
+
raise
|
|
668
601
|
except Exception as e:
|
|
669
|
-
logger.
|
|
670
|
-
|
|
602
|
+
logger.error(f"Unexpected error in chat completion {request_id}: {e}")
|
|
603
|
+
raise APIError(
|
|
604
|
+
f"Internal server error: {str(e)}",
|
|
605
|
+
HTTP_500_INTERNAL_SERVER_ERROR,
|
|
606
|
+
"internal_error"
|
|
607
|
+
)
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
def resolve_provider_and_model(model_identifier: str) -> tuple[Any, str]:
|
|
611
|
+
"""Resolve provider class and model name from model identifier."""
|
|
612
|
+
provider_class = None
|
|
613
|
+
model_name = None
|
|
614
|
+
|
|
615
|
+
# Check for explicit provider/model syntax
|
|
616
|
+
if model_identifier in AppConfig.provider_map and "/" in model_identifier:
|
|
617
|
+
provider_class = AppConfig.provider_map[model_identifier]
|
|
618
|
+
_, model_name = model_identifier.split("/", 1)
|
|
619
|
+
elif "/" in model_identifier:
|
|
620
|
+
provider_name, model_name = model_identifier.split("/", 1)
|
|
621
|
+
provider_class = AppConfig.provider_map.get(provider_name)
|
|
622
|
+
else:
|
|
623
|
+
provider_class = AppConfig.provider_map.get(AppConfig.default_provider)
|
|
624
|
+
model_name = model_identifier
|
|
625
|
+
|
|
626
|
+
if not provider_class:
|
|
627
|
+
available_providers = list(set(v.__name__ for v in AppConfig.provider_map.values()))
|
|
628
|
+
raise APIError(
|
|
629
|
+
f"Provider for model '{model_identifier}' not found. Available providers: {available_providers}",
|
|
630
|
+
HTTP_404_NOT_FOUND,
|
|
631
|
+
"model_not_found",
|
|
632
|
+
param="model"
|
|
633
|
+
)
|
|
634
|
+
|
|
635
|
+
# Validate model availability
|
|
636
|
+
if hasattr(provider_class, "AVAILABLE_MODELS") and model_name is not None:
|
|
637
|
+
available = getattr(provider_class, "AVAILABLE_MODELS", [])
|
|
638
|
+
if available and model_name not in available:
|
|
639
|
+
raise APIError(
|
|
640
|
+
f"Model '{model_name}' not supported by provider '{provider_class.__name__}'. Available models: {available}",
|
|
641
|
+
HTTP_404_NOT_FOUND,
|
|
642
|
+
"model_not_found",
|
|
643
|
+
param="model"
|
|
644
|
+
)
|
|
645
|
+
|
|
646
|
+
return provider_class, model_name
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
def process_messages(messages: List[Message]) -> List[Dict[str, Any]]:
|
|
650
|
+
"""Process and validate chat messages."""
|
|
651
|
+
processed_messages = []
|
|
652
|
+
|
|
653
|
+
for i, msg_in in enumerate(messages):
|
|
654
|
+
try:
|
|
655
|
+
message_dict_out = {"role": msg_in.role}
|
|
656
|
+
|
|
657
|
+
if msg_in.content is None:
|
|
658
|
+
message_dict_out["content"] = None
|
|
659
|
+
elif isinstance(msg_in.content, str):
|
|
660
|
+
message_dict_out["content"] = msg_in.content
|
|
661
|
+
else: # List[MessageContentParts]
|
|
662
|
+
message_dict_out["content"] = [
|
|
663
|
+
part.model_dump(exclude_none=True) for part in msg_in.content
|
|
664
|
+
]
|
|
665
|
+
|
|
666
|
+
if msg_in.name:
|
|
667
|
+
message_dict_out["name"] = msg_in.name
|
|
668
|
+
|
|
669
|
+
processed_messages.append(message_dict_out)
|
|
670
|
+
|
|
671
|
+
except Exception as e:
|
|
672
|
+
raise APIError(
|
|
673
|
+
f"Invalid message at index {i}: {str(e)}",
|
|
674
|
+
HTTP_422_UNPROCESSABLE_ENTITY,
|
|
675
|
+
"invalid_request_error",
|
|
676
|
+
param=f"messages[{i}]"
|
|
677
|
+
)
|
|
678
|
+
|
|
679
|
+
return processed_messages
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
def prepare_provider_params(chat_request: ChatCompletionRequest, model_name: str,
|
|
683
|
+
processed_messages: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
684
|
+
"""Prepare parameters for the provider."""
|
|
685
|
+
params = {
|
|
686
|
+
"model": model_name,
|
|
687
|
+
"messages": processed_messages,
|
|
688
|
+
"stream": chat_request.stream,
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
# Add optional parameters if present
|
|
692
|
+
optional_params = [
|
|
693
|
+
"temperature", "max_tokens", "top_p", "presence_penalty",
|
|
694
|
+
"frequency_penalty", "stop", "user"
|
|
695
|
+
]
|
|
696
|
+
|
|
697
|
+
for param in optional_params:
|
|
698
|
+
value = getattr(chat_request, param, None)
|
|
699
|
+
if value is not None:
|
|
700
|
+
params[param] = value
|
|
701
|
+
|
|
702
|
+
return params
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
async def handle_streaming_response(provider: Any, params: Dict[str, Any], request_id: str) -> StreamingResponse:
|
|
706
|
+
"""Handle streaming chat completion response."""
|
|
707
|
+
async def streaming():
|
|
708
|
+
try:
|
|
709
|
+
logger.debug(f"Starting streaming response for request {request_id}")
|
|
710
|
+
completion_stream = provider.chat.completions.create(**params)
|
|
711
|
+
|
|
712
|
+
# Check if it's iterable (generator, iterator, or other iterable types)
|
|
713
|
+
if hasattr(completion_stream, '__iter__') and not isinstance(completion_stream, (str, bytes, dict)):
|
|
714
|
+
try:
|
|
715
|
+
for chunk in completion_stream:
|
|
716
|
+
# Standardize chunk format before sending
|
|
717
|
+
if hasattr(chunk, 'model_dump'): # Pydantic v2
|
|
718
|
+
chunk_data = chunk.model_dump(exclude_none=True)
|
|
719
|
+
elif hasattr(chunk, 'dict'): # Pydantic v1
|
|
720
|
+
chunk_data = chunk.dict(exclude_none=True)
|
|
721
|
+
elif isinstance(chunk, dict):
|
|
722
|
+
chunk_data = chunk
|
|
723
|
+
else: # Fallback for unknown chunk types
|
|
724
|
+
chunk_data = chunk
|
|
725
|
+
yield f"data: {json.dumps(chunk_data)}\n\n"
|
|
726
|
+
except TypeError as te:
|
|
727
|
+
logger.error(f"Error iterating over completion_stream: {te}")
|
|
728
|
+
# Fall back to treating as non-generator response
|
|
729
|
+
if hasattr(completion_stream, 'model_dump'):
|
|
730
|
+
yield f"data: {json.dumps(completion_stream.model_dump(exclude_none=True))}\n\n"
|
|
731
|
+
elif hasattr(completion_stream, 'dict'):
|
|
732
|
+
yield f"data: {json.dumps(completion_stream.dict(exclude_none=True))}\n\n"
|
|
733
|
+
else:
|
|
734
|
+
yield f"data: {json.dumps(completion_stream)}\n\n"
|
|
735
|
+
else: # Non-generator response
|
|
736
|
+
if hasattr(completion_stream, 'model_dump'):
|
|
737
|
+
yield f"data: {json.dumps(completion_stream.model_dump(exclude_none=True))}\n\n"
|
|
738
|
+
elif hasattr(completion_stream, 'dict'):
|
|
739
|
+
yield f"data: {json.dumps(completion_stream.dict(exclude_none=True))}\n\n"
|
|
740
|
+
else:
|
|
741
|
+
yield f"data: {json.dumps(completion_stream)}\n\n"
|
|
742
|
+
|
|
743
|
+
except Exception as e:
|
|
744
|
+
logger.error(f"Error in streaming response for request {request_id}: {e}")
|
|
745
|
+
error_data = {
|
|
746
|
+
"error": {
|
|
747
|
+
"message": str(e),
|
|
748
|
+
"type": "server_error",
|
|
749
|
+
"code": "streaming_error"
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
yield f"data: {json.dumps(error_data)}\n\n"
|
|
753
|
+
finally:
|
|
754
|
+
yield "data: [DONE]\n\n"
|
|
755
|
+
return StreamingResponse(streaming(), media_type="text/event-stream")
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
async def handle_non_streaming_response(provider: Any, params: Dict[str, Any],
|
|
759
|
+
request_id: str, start_time: float) -> Dict[str, Any]:
|
|
760
|
+
"""Handle non-streaming chat completion response."""
|
|
761
|
+
try:
|
|
762
|
+
logger.debug(f"Starting non-streaming response for request {request_id}")
|
|
763
|
+
completion = provider.chat.completions.create(**params)
|
|
764
|
+
|
|
765
|
+
if completion is None:
|
|
766
|
+
# Return a valid OpenAI-compatible error response
|
|
767
|
+
return ChatCompletion(
|
|
768
|
+
id=request_id,
|
|
769
|
+
created=int(time.time()),
|
|
770
|
+
model=params.get("model", "unknown"),
|
|
771
|
+
choices=[Choice(
|
|
772
|
+
index=0,
|
|
773
|
+
message=ChatCompletionMessage(role="assistant", content="No response generated."),
|
|
774
|
+
finish_reason="error"
|
|
775
|
+
)],
|
|
776
|
+
usage=CompletionUsage(prompt_tokens=0, completion_tokens=0, total_tokens=0)
|
|
777
|
+
).model_dump(exclude_none=True)
|
|
778
|
+
|
|
779
|
+
# Standardize response format
|
|
780
|
+
if hasattr(completion, "model_dump"): # Pydantic v2
|
|
781
|
+
response_data = completion.model_dump(exclude_none=True)
|
|
782
|
+
elif hasattr(completion, "dict"): # Pydantic v1
|
|
783
|
+
response_data = completion.dict(exclude_none=True)
|
|
784
|
+
elif isinstance(completion, dict):
|
|
785
|
+
response_data = completion
|
|
786
|
+
else:
|
|
787
|
+
raise APIError(
|
|
788
|
+
"Invalid response format from provider",
|
|
789
|
+
HTTP_500_INTERNAL_SERVER_ERROR,
|
|
790
|
+
"provider_error"
|
|
791
|
+
)
|
|
792
|
+
|
|
793
|
+
elapsed = time.time() - start_time
|
|
794
|
+
logger.info(f"Completed non-streaming request {request_id} in {elapsed:.2f}s")
|
|
795
|
+
|
|
796
|
+
return response_data
|
|
797
|
+
|
|
798
|
+
except Exception as e:
|
|
799
|
+
logger.error(f"Error in non-streaming response for request {request_id}: {e}")
|
|
800
|
+
raise APIError(
|
|
801
|
+
f"Provider error: {str(e)}",
|
|
802
|
+
HTTP_500_INTERNAL_SERVER_ERROR,
|
|
803
|
+
"provider_error"
|
|
804
|
+
)
|
|
671
805
|
|
|
672
806
|
def format_exception(e: Union[Exception, str]) -> str:
|
|
673
|
-
"""Format exception into a JSON string"""
|
|
674
807
|
if isinstance(e, str):
|
|
675
808
|
message = e
|
|
676
809
|
else:
|
|
677
|
-
message = f"{e.__class__.__name__}: {e}"
|
|
810
|
+
message = f"{e.__class__.__name__}: {str(e)}" # Keep it concise
|
|
678
811
|
return json.dumps({
|
|
679
812
|
"error": {
|
|
680
813
|
"message": message,
|
|
681
|
-
"type": "server_error",
|
|
814
|
+
"type": "server_error", # Or more specific if possible
|
|
682
815
|
"param": None,
|
|
683
|
-
"code": "internal_server_error"
|
|
816
|
+
"code": "internal_server_error" # Or more specific
|
|
684
817
|
}
|
|
685
818
|
})
|
|
686
819
|
|
|
687
|
-
def start_server(
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
from webscout.Provider.OPENAI.api import start_server
|
|
699
|
-
|
|
700
|
-
# Start server with default settings
|
|
701
|
-
start_server()
|
|
702
|
-
|
|
703
|
-
# Start server with custom settings
|
|
704
|
-
start_server(port=8080, api_key="your-api-key", default_provider="Claude")
|
|
705
|
-
```
|
|
706
|
-
"""
|
|
820
|
+
def start_server(
|
|
821
|
+
port: int = DEFAULT_PORT,
|
|
822
|
+
host: str = DEFAULT_HOST,
|
|
823
|
+
api_key: str = None,
|
|
824
|
+
default_provider: str = None,
|
|
825
|
+
base_url: str = None,
|
|
826
|
+
workers: int = 1,
|
|
827
|
+
log_level: str = 'info',
|
|
828
|
+
debug: bool = False
|
|
829
|
+
):
|
|
830
|
+
"""Start the API server with the given configuration."""
|
|
707
831
|
run_api(
|
|
708
|
-
host=
|
|
832
|
+
host=host,
|
|
709
833
|
port=port,
|
|
710
834
|
api_key=api_key,
|
|
711
835
|
default_provider=default_provider,
|
|
712
|
-
|
|
836
|
+
base_url=base_url,
|
|
837
|
+
workers=workers,
|
|
838
|
+
log_level=log_level,
|
|
839
|
+
debug=debug,
|
|
713
840
|
)
|
|
714
841
|
|
|
715
842
|
def run_api(
|
|
@@ -717,94 +844,126 @@ def run_api(
|
|
|
717
844
|
port: int = None,
|
|
718
845
|
api_key: str = None,
|
|
719
846
|
default_provider: str = None,
|
|
847
|
+
base_url: str = None,
|
|
720
848
|
debug: bool = False,
|
|
849
|
+
workers: int = 1,
|
|
850
|
+
log_level: str = 'info',
|
|
721
851
|
show_available_providers: bool = True,
|
|
722
852
|
) -> None:
|
|
723
|
-
"
|
|
724
|
-
|
|
725
|
-
Args:
|
|
726
|
-
host: Host to bind the server to
|
|
727
|
-
port: Port to bind the server to
|
|
728
|
-
api_key: API key for authentication (optional)
|
|
729
|
-
default_provider: Default provider to use if no provider is specified
|
|
730
|
-
debug: Whether to run in debug mode
|
|
731
|
-
show_available_providers: Whether to display available providers on startup
|
|
732
|
-
"""
|
|
733
|
-
print(f"Starting Webscout OpenAI API server...")
|
|
734
|
-
|
|
853
|
+
print("Starting Webscout OpenAI API server...")
|
|
735
854
|
if port is None:
|
|
736
855
|
port = DEFAULT_PORT
|
|
737
|
-
|
|
738
|
-
# Set configuration
|
|
739
856
|
AppConfig.set_config(
|
|
740
857
|
api_key=api_key,
|
|
741
|
-
default_provider=default_provider or AppConfig.default_provider
|
|
858
|
+
default_provider=default_provider or AppConfig.default_provider,
|
|
859
|
+
base_url=base_url
|
|
742
860
|
)
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
861
|
+
# initialize_provider_map() # This is called inside create_app now.
|
|
862
|
+
# Call here if create_app doesn't exist yet or for early info.
|
|
863
|
+
# For showing providers, it needs to be called before printing.
|
|
864
|
+
if show_available_providers: # Initialize map if needed for display before app creation
|
|
865
|
+
if not AppConfig.provider_map: # Avoid re-initializing if already done by app creation logic path
|
|
866
|
+
initialize_provider_map()
|
|
867
|
+
|
|
868
|
+
print("\n=== Webscout OpenAI API Server ===")
|
|
869
|
+
print(f"Server URL: http://{host if host != '0.0.0.0' else 'localhost'}:{port}")
|
|
870
|
+
if AppConfig.base_url:
|
|
871
|
+
print(f"Base Path: {AppConfig.base_url}")
|
|
872
|
+
api_endpoint_base = f"http://{host if host != '0.0.0.0' else 'localhost'}:{port}{AppConfig.base_url}"
|
|
873
|
+
else:
|
|
874
|
+
api_endpoint_base = f"http://{host if host != '0.0.0.0' else 'localhost'}:{port}"
|
|
875
|
+
|
|
876
|
+
print(f"API Endpoint: {api_endpoint_base}/v1/chat/completions")
|
|
877
|
+
print(f"Docs URL: {api_endpoint_base}/docs") # Adjusted for potential base_url in display
|
|
878
|
+
print(f"API Authentication: {'Enabled' if api_key else 'Disabled'}")
|
|
879
|
+
print(f"Default Provider: {AppConfig.default_provider}")
|
|
880
|
+
print(f"Workers: {workers}")
|
|
881
|
+
print(f"Log Level: {log_level}")
|
|
882
|
+
print(f"Debug Mode: {'Enabled' if debug else 'Disabled'}")
|
|
883
|
+
|
|
749
884
|
providers = list(set(v.__name__ for v in AppConfig.provider_map.values()))
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
# Filter out provider class names from the model list
|
|
885
|
+
print(f"\n--- Available Providers ({len(providers)}) ---")
|
|
886
|
+
for i, provider_name in enumerate(sorted(providers), 1):
|
|
887
|
+
print(f"{i}. {provider_name}")
|
|
888
|
+
|
|
755
889
|
provider_class_names = set(v.__name__ for v in AppConfig.provider_map.values())
|
|
756
|
-
models = [model for model in AppConfig.provider_map.keys() if model not in provider_class_names]
|
|
757
|
-
|
|
758
|
-
# Display models in a more organized way
|
|
890
|
+
models = sorted([model for model in AppConfig.provider_map.keys() if model not in provider_class_names])
|
|
759
891
|
if models:
|
|
760
|
-
|
|
761
|
-
|
|
892
|
+
print(f"\n--- Available Models ({len(models)}) ---")
|
|
893
|
+
for i, model_name in enumerate(models, 1):
|
|
894
|
+
print(f"{i}. {model_name} (via {AppConfig.provider_map[model_name].__name__})")
|
|
762
895
|
else:
|
|
763
|
-
print("
|
|
764
|
-
|
|
765
|
-
print(
|
|
766
|
-
print(
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
"
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
896
|
+
print("\nNo specific models registered. Use provider names as models.")
|
|
897
|
+
|
|
898
|
+
print("\nUse Ctrl+C to stop the server.")
|
|
899
|
+
print("=" * 40 + "\n")
|
|
900
|
+
|
|
901
|
+
uvicorn_app_str = "webscout.Provider.OPENAI.api:create_app_debug" if debug else "webscout.Provider.OPENAI.api:create_app"
|
|
902
|
+
|
|
903
|
+
# Configure uvicorn settings
|
|
904
|
+
uvicorn_config = {
|
|
905
|
+
"app": uvicorn_app_str,
|
|
906
|
+
"host": host,
|
|
907
|
+
"port": int(port),
|
|
908
|
+
"factory": True,
|
|
909
|
+
"reload": debug, # Enable reload only in debug mode for stability
|
|
910
|
+
"log_level": log_level.lower() if log_level else ("debug" if debug else "info"),
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
# Add workers only if not in debug mode (reload and workers are incompatible)
|
|
914
|
+
if not debug and workers > 1:
|
|
915
|
+
uvicorn_config["workers"] = workers
|
|
916
|
+
print(f"Starting with {workers} workers...")
|
|
917
|
+
elif debug:
|
|
918
|
+
print("Debug mode enabled - using single worker with reload...")
|
|
919
|
+
|
|
920
|
+
# Note: Logs show "werkzeug". If /docs 404s persist, ensure Uvicorn is the actual server running.
|
|
921
|
+
# The script uses uvicorn.run, so "werkzeug" logs are unexpected for this file.
|
|
922
|
+
uvicorn.run(**uvicorn_config)
|
|
780
923
|
|
|
781
|
-
# Command line interface
|
|
782
924
|
if __name__ == "__main__":
|
|
783
925
|
import argparse
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
926
|
+
|
|
927
|
+
# Read environment variables with fallbacks
|
|
928
|
+
default_port = int(os.getenv('WEBSCOUT_PORT', os.getenv('PORT', DEFAULT_PORT)))
|
|
929
|
+
default_host = os.getenv('WEBSCOUT_HOST', DEFAULT_HOST)
|
|
930
|
+
default_workers = int(os.getenv('WEBSCOUT_WORKERS', '1'))
|
|
931
|
+
default_log_level = os.getenv('WEBSCOUT_LOG_LEVEL', 'info')
|
|
932
|
+
default_api_key = os.getenv('WEBSCOUT_API_KEY', os.getenv('API_KEY'))
|
|
933
|
+
default_provider = os.getenv('WEBSCOUT_DEFAULT_PROVIDER', os.getenv('DEFAULT_PROVIDER'))
|
|
934
|
+
default_base_url = os.getenv('WEBSCOUT_BASE_URL', os.getenv('BASE_URL'))
|
|
935
|
+
default_debug = os.getenv('WEBSCOUT_DEBUG', os.getenv('DEBUG', 'false')).lower() == 'true'
|
|
936
|
+
|
|
937
|
+
parser = argparse.ArgumentParser(description='Start Webscout OpenAI-compatible API server')
|
|
938
|
+
parser.add_argument('--port', type=int, default=default_port, help=f'Port to run the server on (default: {default_port})')
|
|
939
|
+
parser.add_argument('--host', type=str, default=default_host, help=f'Host to bind the server to (default: {default_host})')
|
|
940
|
+
parser.add_argument('--workers', type=int, default=default_workers, help=f'Number of worker processes (default: {default_workers})')
|
|
941
|
+
parser.add_argument('--log-level', type=str, default=default_log_level, choices=['debug', 'info', 'warning', 'error', 'critical'], help=f'Log level (default: {default_log_level})')
|
|
942
|
+
parser.add_argument('--api-key', type=str, default=default_api_key, help='API key for authentication (optional)')
|
|
943
|
+
parser.add_argument('--default-provider', type=str, default=default_provider, help='Default provider to use (optional)')
|
|
944
|
+
parser.add_argument('--base-url', type=str, default=default_base_url, help='Base URL for the API (optional, e.g., /api/v1)')
|
|
945
|
+
parser.add_argument('--debug', action='store_true', default=default_debug, help='Run in debug mode')
|
|
793
946
|
args = parser.parse_args()
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
947
|
+
|
|
948
|
+
# Print configuration summary
|
|
949
|
+
print(f"Configuration:")
|
|
950
|
+
print(f" Host: {args.host}")
|
|
951
|
+
print(f" Port: {args.port}")
|
|
952
|
+
print(f" Workers: {args.workers}")
|
|
953
|
+
print(f" Log Level: {args.log_level}")
|
|
954
|
+
print(f" Debug Mode: {args.debug}")
|
|
955
|
+
print(f" API Key: {'Set' if args.api_key else 'Not set'}")
|
|
956
|
+
print(f" Default Provider: {args.default_provider or 'Not set'}")
|
|
957
|
+
print(f" Base URL: {args.base_url or 'Not set'}")
|
|
958
|
+
print()
|
|
959
|
+
|
|
960
|
+
run_api(
|
|
961
|
+
host=args.host,
|
|
962
|
+
port=args.port,
|
|
963
|
+
workers=args.workers,
|
|
964
|
+
log_level=args.log_level,
|
|
965
|
+
api_key=args.api_key,
|
|
966
|
+
default_provider=args.default_provider,
|
|
967
|
+
base_url=args.base_url,
|
|
968
|
+
debug=args.debug
|
|
969
|
+
)
|