mirror of
https://github.com/theroyallab/tabbyAPI.git
synced 2026-03-15 00:07:28 +00:00
* improve validation * remove to_gen_params functions * update changes for all endpoint types * OAI: Fix calls to generation Chat completion and completion need to have prompt split out before pushing to the backend. Signed-off-by: kingbri <bdashore3@proton.me> * Sampling: Convert Top-K values of -1 to 0 Some OAI implementations use -1 as disabled instead of 0. Therefore, add a coalesce case. Signed-off-by: kingbri <bdashore3@proton.me> * Sampling: Format and space out Make the code more readable. Signed-off-by: kingbri <bdashore3@proton.me> * Sampling: Fix mirostat Field items are nested in data within a Pydantic FieldInfo Signed-off-by: kingbri <bdashore3@proton.me> * Sampling: Format Signed-off-by: kingbri <bdashore3@proton.me> * Sampling: Fix banned_tokens and allowed_tokens conversion If the provided string has whitespace, trim it before splitting. Signed-off-by: kingbri <bdashore3@proton.me> * Sampling: Add helpful log to dry_sequence_breakers Let the user know if the sequence errors out. Signed-off-by: kingbri <bdashore3@proton.me> * Sampling: Apply validators in right order Validators need to be applied in order from top to bottom, this is why the after validator was not being applied properly. Set the model to validate default params for sampler override purposes. This can be turned off if there are unclear errors. Signed-off-by: kingbri <bdashore3@proton.me> * Endpoints: Format Cleanup and semantically fix field validators Signed-off-by: kingbri <bdashore3@proton.me> * Kobold: Update validators and fix parameter application Validators on parent fields cannot see child fields. Therefore, validate using the child fields instead and alter the parent field data from there. Also fix badwordsids casting. Signed-off-by: kingbri <bdashore3@proton.me> * Sampling: Remove validate defaults and fix mirostat If a user sets an override to a non-default value, that's their own fault. Run validator on the actual mirostat_mode parameter rather than the alternate mirostat parameter. Signed-off-by: kingbri <bdashore3@proton.me> * Kobold: Rework badwordsids Currently, this serves to ban the EOS token. All other functionality was legacy, so remove it. Signed-off-by: kingbri <bdashore3@proton.me> * Model: Remove HuggingfaceConfig This was only necessary for badwordsids. All other fields are handled by exl2. Keep the class as a stub if it's needed again. Signed-off-by: kingbri <bdashore3@proton.me> * Kobold: Bump kcpp impersonation TabbyAPI supports XTC now. Signed-off-by: kingbri <bdashore3@proton.me> * Sampling: Change alias to validation_alias Reduces the probability for errors and makes the class consistent. Signed-off-by: kingbri <bdashore3@proton.me> * OAI: Use constraints for validation Instead of adding a model_validator, use greater than or equal to constraints provided by Pydantic. Signed-off-by: kingbri <bdashore3@proton.me> * Tree: Lint Signed-off-by: kingbri <bdashore3@proton.me> --------- Co-authored-by: SecretiveShell <84923604+SecretiveShell@users.noreply.github.com> Co-authored-by: kingbri <bdashore3@proton.me>
152 lines
4.5 KiB
Python
152 lines
4.5 KiB
Python
import asyncio
|
|
from asyncio import CancelledError
|
|
from fastapi import HTTPException, Request
|
|
from loguru import logger
|
|
from sse_starlette import ServerSentEvent
|
|
|
|
from common import model
|
|
from common.networking import (
|
|
get_generator_error,
|
|
handle_request_disconnect,
|
|
handle_request_error,
|
|
request_disconnect_loop,
|
|
)
|
|
from common.utils import unwrap
|
|
from endpoints.Kobold.types.generation import (
|
|
AbortResponse,
|
|
GenerateRequest,
|
|
GenerateResponse,
|
|
GenerateResponseResult,
|
|
StreamGenerateChunk,
|
|
)
|
|
|
|
|
|
generation_cache = {}
|
|
|
|
|
|
async def override_request_id(request: Request, data: GenerateRequest):
|
|
"""Overrides the request ID with a KAI genkey if present."""
|
|
|
|
if data.genkey:
|
|
request.state.id = data.genkey
|
|
|
|
|
|
def _create_response(text: str):
|
|
results = [GenerateResponseResult(text=text)]
|
|
return GenerateResponse(results=results)
|
|
|
|
|
|
def _create_stream_chunk(text: str):
|
|
return StreamGenerateChunk(token=text)
|
|
|
|
|
|
async def _stream_collector(data: GenerateRequest, request: Request):
|
|
"""Common async generator for generation streams."""
|
|
|
|
abort_event = asyncio.Event()
|
|
disconnect_task = asyncio.create_task(request_disconnect_loop(request))
|
|
|
|
# Create a new entry in the cache
|
|
generation_cache[data.genkey] = {"abort": abort_event, "text": ""}
|
|
|
|
try:
|
|
logger.info(f"Received Kobold generation request {data.genkey}")
|
|
|
|
generator = model.container.generate_gen(
|
|
request_id=data.genkey, abort_event=abort_event, **data.model_dump()
|
|
)
|
|
async for generation in generator:
|
|
if disconnect_task.done():
|
|
abort_event.set()
|
|
handle_request_disconnect(
|
|
f"Kobold generation {data.genkey} cancelled by user."
|
|
)
|
|
|
|
text = generation.get("text")
|
|
|
|
# Update the generation cache with the new chunk
|
|
if text:
|
|
generation_cache[data.genkey]["text"] += text
|
|
yield text
|
|
|
|
if "finish_reason" in generation:
|
|
logger.info(f"Finished streaming Kobold request {data.genkey}")
|
|
break
|
|
except CancelledError:
|
|
# If the request disconnects, break out
|
|
if not disconnect_task.done():
|
|
abort_event.set()
|
|
handle_request_disconnect(
|
|
f"Kobold generation {data.genkey} cancelled by user."
|
|
)
|
|
finally:
|
|
# Cleanup the cache
|
|
del generation_cache[data.genkey]
|
|
|
|
|
|
async def stream_generation(data: GenerateRequest, request: Request):
|
|
"""Wrapper for stream generations."""
|
|
|
|
# If the genkey doesn't exist, set it to the request ID
|
|
if not data.genkey:
|
|
data.genkey = request.state.id
|
|
|
|
try:
|
|
async for chunk in _stream_collector(data, request):
|
|
response = _create_stream_chunk(chunk)
|
|
yield ServerSentEvent(
|
|
event="message", data=response.model_dump_json(), sep="\n"
|
|
)
|
|
except Exception:
|
|
yield get_generator_error(
|
|
f"Kobold generation {data.genkey} aborted. "
|
|
"Please check the server console."
|
|
)
|
|
|
|
|
|
async def get_generation(data: GenerateRequest, request: Request):
|
|
"""Wrapper to get a static generation."""
|
|
|
|
# If the genkey doesn't exist, set it to the request ID
|
|
if not data.genkey:
|
|
data.genkey = request.state.id
|
|
|
|
try:
|
|
full_text = ""
|
|
async for chunk in _stream_collector(data, request):
|
|
full_text += chunk
|
|
|
|
response = _create_response(full_text)
|
|
return response
|
|
except Exception as exc:
|
|
error_message = handle_request_error(
|
|
f"Completion {request.state.id} aborted. Maybe the model was unloaded? "
|
|
"Please check the server console."
|
|
).error.message
|
|
|
|
# Server error if there's a generation exception
|
|
raise HTTPException(503, error_message) from exc
|
|
|
|
|
|
async def abort_generation(genkey: str):
|
|
"""Aborts a generation from the cache."""
|
|
|
|
abort_event = unwrap(generation_cache.get(genkey), {}).get("abort")
|
|
if abort_event:
|
|
abort_event.set()
|
|
handle_request_disconnect(f"Kobold generation {genkey} cancelled by user.")
|
|
|
|
return AbortResponse(success=True)
|
|
|
|
|
|
async def generation_status(genkey: str):
|
|
"""Fetches the status of a generation from the cache."""
|
|
|
|
current_text = unwrap(generation_cache.get(genkey), {}).get("text")
|
|
if current_text:
|
|
response = _create_response(current_text)
|
|
else:
|
|
response = GenerateResponse()
|
|
|
|
return response
|