-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add legacy completions API support (#124)
* Add new type of parser * add completions endpoint * -add prompt tepmlate to legacy completions -move env variables to distinct file * hotfix * another hotfix * pr fixes * refactor due to pr comment * another refactor due to PR comments * another minor refactor * Fix model field ingestion for embeddings endpoint
- Loading branch information
1 parent
bb5403e
commit 29c1684
Showing
5 changed files
with
286 additions
and
49 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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from typing import Any, Dict | ||
|
||
from aidial_sdk.exceptions import HTTPException as DialException | ||
from fastapi.responses import JSONResponse, StreamingResponse | ||
from openai import AsyncStream | ||
from openai.types import Completion | ||
|
||
from aidial_adapter_openai.env import COMPLETION_DEPLOYMENTS_PROMPT_TEMPLATES | ||
from aidial_adapter_openai.utils.auth import OpenAICreds | ||
from aidial_adapter_openai.utils.parsers import ( | ||
AzureOpenAIEndpoint, | ||
OpenAIEndpoint, | ||
) | ||
from aidial_adapter_openai.utils.reflection import call_with_extra_body | ||
from aidial_adapter_openai.utils.sse_stream import to_openai_sse_stream | ||
from aidial_adapter_openai.utils.streaming import build_chunk, map_stream | ||
|
||
|
||
def convert_to_chat_completions_response( | ||
chunk: Completion, is_stream: bool | ||
) -> Dict[str, Any]: | ||
return build_chunk( | ||
id=chunk.id, | ||
finish_reason=chunk.choices[0].finish_reason, | ||
delta=chunk.choices[0].text, | ||
created=str(chunk.created), | ||
is_stream=is_stream, | ||
usage=chunk.usage.to_dict() if chunk.usage else None, | ||
) | ||
|
||
|
||
async def chat_completion( | ||
data: Dict[str, Any], | ||
endpoint: OpenAIEndpoint | AzureOpenAIEndpoint, | ||
creds: OpenAICreds, | ||
api_version: str, | ||
deployment_id: str, | ||
) -> Any: | ||
|
||
if data.get("n", 1) > 1: # type: ignore | ||
raise DialException( | ||
status_code=422, | ||
message="The deployment doesn't support n > 1", | ||
type="invalid_request_error", | ||
) | ||
|
||
client = endpoint.get_client({**creds, "api_version": api_version}) | ||
|
||
messages = data.get("messages", []) | ||
if len(messages) == 0: | ||
raise DialException( | ||
status_code=422, | ||
message="The request doesn't contain any messages", | ||
type="invalid_request_error", | ||
) | ||
|
||
prompt = messages[-1].get("content") or "" | ||
|
||
if ( | ||
template := COMPLETION_DEPLOYMENTS_PROMPT_TEMPLATES.get(deployment_id) | ||
) is not None: | ||
prompt = template.format(prompt=prompt) | ||
|
||
del data["messages"] | ||
response = await call_with_extra_body( | ||
client.completions.create, | ||
{"prompt": prompt, **data}, | ||
) | ||
|
||
if isinstance(response, AsyncStream): | ||
return StreamingResponse( | ||
to_openai_sse_stream( | ||
map_stream( | ||
lambda item: convert_to_chat_completions_response( | ||
item, is_stream=True | ||
), | ||
response, | ||
) | ||
) | ||
) | ||
else: | ||
return JSONResponse( | ||
convert_to_chat_completions_response(response, is_stream=False) | ||
) |
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,27 @@ | ||
import json | ||
import os | ||
from typing import Dict | ||
|
||
from aidial_adapter_openai.utils.parsers import parse_deployment_list | ||
|
||
MODEL_ALIASES: Dict[str, str] = json.loads(os.getenv("MODEL_ALIASES", "{}")) | ||
DALLE3_DEPLOYMENTS = parse_deployment_list( | ||
os.getenv("DALLE3_DEPLOYMENTS") or "" | ||
) | ||
GPT4_VISION_DEPLOYMENTS = parse_deployment_list( | ||
os.getenv("GPT4_VISION_DEPLOYMENTS") or "" | ||
) | ||
MISTRAL_DEPLOYMENTS = parse_deployment_list( | ||
os.getenv("MISTRAL_DEPLOYMENTS") or "" | ||
) | ||
DATABRICKS_DEPLOYMENTS = parse_deployment_list( | ||
os.getenv("DATABRICKS_DEPLOYMENTS") or "" | ||
) | ||
GPT4O_DEPLOYMENTS = parse_deployment_list(os.getenv("GPT4O_DEPLOYMENTS") or "") | ||
API_VERSIONS_MAPPING: Dict[str, str] = json.loads( | ||
os.getenv("API_VERSIONS_MAPPING", "{}") | ||
) | ||
COMPLETION_DEPLOYMENTS_PROMPT_TEMPLATES: Dict[str, str] = json.loads( | ||
os.getenv("COMPLETION_DEPLOYMENTS_PROMPT_TEMPLATES") or "{}" | ||
) | ||
DALLE3_AZURE_API_VERSION = os.getenv("DALLE3_AZURE_API_VERSION", "2024-02-01") |
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
Oops, something went wrong.