-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨[DEV] Support OpenAI API Format for transformers model.
- Loading branch information
1 parent
021c638
commit be7d1b5
Showing
9 changed files
with
131 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from .basic import get_basic_router | ||
from .model import get_model_router | ||
from .openai import get_openai_router | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from pydantic import BaseModel, ConfigDict | ||
from fastapi import APIRouter, Request | ||
|
||
PREFIX = "/openai" | ||
|
||
class ChatMessage(BaseModel): | ||
role: str | ||
content: str | ||
|
||
class ChatRequest(BaseModel): | ||
model: str | ||
messages: list[ChatMessage] | ||
max_completion_tokens: int = None | ||
|
||
model_config=ConfigDict(protected_namespaces=()) | ||
|
||
|
||
def chat_completions(request: Request, item: ChatRequest): | ||
server = request.app.state.server | ||
try: | ||
assert item.model in server.module["model"].loaded_models | ||
except AssertionError: | ||
return f"【Error】: {item.model} is not loaded." | ||
|
||
outputs = server.module["model"].loaded_models[item.model].chat( | ||
messages=item.messages, | ||
max_completion_tokens=item.max_completion_tokens | ||
) | ||
return outputs | ||
|
||
def get_openai_router(): | ||
router = APIRouter(prefix=PREFIX) | ||
|
||
router.add_api_route("/chat/completions", chat_completions, methods=["POST"]) | ||
return router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters