-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement OpenAI to OpenAI adapter for gateway (#860)
* Implement openai-to-openai adapter * Disable SSE buffering, adjust OpenAI models for vLLM responses
- Loading branch information
Showing
11 changed files
with
125 additions
and
43 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
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 |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
ChatCompletionsResponse, | ||
) | ||
|
||
DEFAULT_TIMEOUT = 60 | ||
|
||
|
||
class ChatCompletionsClient(ABC): | ||
@abstractmethod | ||
|
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,29 @@ | ||
from typing import AsyncIterator, Optional | ||
|
||
from dstack.gateway.common import AsyncClientWrapper | ||
from dstack.gateway.errors import GatewayError | ||
from dstack.gateway.openai.clients import DEFAULT_TIMEOUT, ChatCompletionsClient | ||
from dstack.gateway.openai.schemas import ( | ||
ChatCompletionsChunk, | ||
ChatCompletionsRequest, | ||
ChatCompletionsResponse, | ||
) | ||
|
||
|
||
class OpenAIChatCompletions(ChatCompletionsClient): | ||
def __init__(self, base_url: str, host: Optional[str] = None): | ||
self.client = AsyncClientWrapper( | ||
base_url=base_url.rstrip("/"), | ||
headers={} if host is None else {"Host": host}, | ||
timeout=DEFAULT_TIMEOUT, | ||
) | ||
|
||
async def generate(self, request: ChatCompletionsRequest) -> ChatCompletionsResponse: | ||
resp = await self.client.post("/chat/completions", json=request.model_dump()) | ||
if resp.status_code != 200: | ||
raise GatewayError(resp.text) | ||
return ChatCompletionsResponse.model_validate(resp.json()) | ||
|
||
async def stream(self, request: ChatCompletionsRequest) -> AsyncIterator[ChatCompletionsChunk]: | ||
async for data in self.client.stream_sse("/chat/completions", json=request.model_dump()): | ||
yield ChatCompletionsChunk.model_validate(data) |
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
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
22 changes: 13 additions & 9 deletions
22
src/dstack/_internal/server/services/jobs/configurators/extensions/openai.py
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