Spaces:
Sleeping
Sleeping
Kyryll Kochkin commited on
Commit ·
698373a
1
Parent(s): 2ad2929
added GPT4-dev-177M-1511-Instruct
Browse filesFiles changed (5)
9 unmodified lines1011121314151695 unmodified lines1121131141151161171189 unmodified lines# GPT3dev OpenAI-Compatible API**more detailed documentation is hoeeted on [DeepWiki](https://deepwiki.com/krll-corp/gpt3dev-api)**A production-ready FastAPI server that mirrors the OpenAI REST API surface while proxying requests to Hugging Face causal language models. The service implements the `/v1/completions`, `/v1/models`, and `/v1/embeddings` endpoints with full support for streaming Server-Sent Events (SSE) and OpenAI-style usage accounting. A `/v1/chat/completions` stub is included but currently returns a structured 501 error because the available models are completion-only.## The API is hosted on HuggingFace Spaces:```bash95 unmodified lines### Chat CompletionsThe `/v1/chat/completions` endpoint is currently disabled and returns a 501 Not Implemented error instructing clients to use `/v1/completions` instead. I don't have any chat-tuned models now, but I plan to enable this endpoint later with openai harmony - tuned models.### Embeddings9 unmodified lines1011121314151695 unmodified lines1121131141151161171181191201211221231241251261271281291301311321331349 unmodified lines# GPT3dev OpenAI-Compatible API**more detailed documentation is hoeeted on [DeepWiki](https://deepwiki.com/krll-corp/gpt3dev-api)**A production-ready FastAPI server that mirrors the OpenAI REST API surface while proxying requests to Hugging Face causal language models. The service implements the `/v1/completions`, `/v1/chat/completions`, `/v1/models`, and `/v1/embeddings` endpoints with full support for streaming Server-Sent Events (SSE) and OpenAI-style usage accounting. Chat completions are available for instruct-tuned models like `GPT4-dev-177M-1511-Instruct`.## The API is hosted on HuggingFace Spaces:```bash95 unmodified lines### Chat CompletionsThe `/v1/chat/completions` endpoint is available for instruct-tuned models. Currently supported instruct models:- `GPT4-dev-177M-1511-Instruct` - Instruction-tuned GPT-4-style model fine-tuned on HuggingFaceH4/no_robots```bashcurl http://localhost:7860/v1/chat/completions \-H "Content-Type: application/json" \-d '{"model": "GPT4-dev-177M-1511-Instruct","messages": [{"role": "user", "content": "Write a short welcome message for new contributors."}],"max_tokens": 128}'```Non-instruct models will return an error directing users to use `/v1/completions` instead.### Embeddings
139 unmodified lines14014114214314414514614714814915015115215329 unmodified lines18318418518618718818919019119219318 unmodified lines2122132142152162172182192202212222 unmodified lines2252262272282292302312 unmodified lines23423523623723823952 unmodified lines292293294295296297139 unmodified lineslogger.info("Loading tokenizer for %s", spec.hf_repo)tokenizer = AutoTokenizer.from_pretrained(spec.hf_repo,use_auth_token=token,trust_remote_code=True,)if tokenizer.pad_token_id is None:tokenizer.pad_token = tokenizer.eos_tokenlogger.info("Tokenizer ready in %.2fs", time.perf_counter() - t0)model_kwargs = {"use_auth_token": token,"trust_remote_code": True,}# Resolve preferred device early so we can adjust dtype if needed29 unmodified linesdevice_pref," (device_map=auto)" if device_map else "",)model = AutoModelForCausalLM.from_pretrained(spec.hf_repo,device_map=device_map,**model_kwargs,)logger.info("Model ready in %.2fs", time.perf_counter() - t1)if device_map is None:model = model.to(device_pref)18 unmodified lineskwargs.pop("cache_position", None)kwargs.pop("encoder_attention_mask", None)kwargs.pop("attention_mask", None)return _orig_forward(*args, **kwargs)model.forward = MethodType(_forward_compat, model)# Also patch submodules whose forward signatures include# encoder_attention_mask to avoid duplicate passing (positional+kw)for _, module in model.named_modules():fwd = getattr(module, "forward", None)if not callable(fwd):2 unmodified linessig = inspect.signature(fwd)except Exception:continueif "encoder_attention_mask" not in sig.parameters:continueorig_fwd = fwd2 unmodified lineskwargs.pop("encoder_attention_mask", None)kwargs.pop("attention_mask", None)kwargs.pop("cache_position", None)return orig(*args, **kwargs)return _sub_forward_compat52 unmodified linesreturn text, "length"def _prepare_inputs(handle: _ModelHandle,prompt: str,139 unmodified lines14014114214314414514614714814915015115215329 unmodified lines18318418518618718818919019119219319419519619719819920020120220320420520620720820921021121218 unmodified lines2312322332342352362372382392402412422 unmodified lines2452462472482492502512522532542552562 unmodified lines25926026126226326426552 unmodified lines318319320321322323324325326327328329330331332333334335336337338339340341342343344139 unmodified lineslogger.info("Loading tokenizer for %s", spec.hf_repo)tokenizer = AutoTokenizer.from_pretrained(spec.hf_repo,token=token,trust_remote_code=True,)if tokenizer.pad_token_id is None:tokenizer.pad_token = tokenizer.eos_tokenlogger.info("Tokenizer ready in %.2fs", time.perf_counter() - t0)model_kwargs = {"token": token,"trust_remote_code": True,}# Resolve preferred device early so we can adjust dtype if needed29 unmodified linesdevice_pref," (device_map=auto)" if device_map else "",)# Patch _load_pretrained_model to fix tie_weights incompatibility# with newer transformers that pass missing_keys keyword argumentfrom transformers import modeling_utils_orig_load_pretrained_func = modeling_utils.PreTrainedModel._load_pretrained_model.__func__def _patched_load_pretrained_func(cls, model, *args, **kwargs):# Patch model.tie_weights to accept and ignore unexpected kwargsorig_tie_weights = model.tie_weightsdef _compat_tie_weights(*tw_args, **tw_kwargs):tw_kwargs.pop("missing_keys", None)tw_kwargs.pop("recompute_mapping", None)return orig_tie_weights(*tw_args, **tw_kwargs)model.tie_weights = _compat_tie_weightsreturn _orig_load_pretrained_func(cls, model, *args, **kwargs)modeling_utils.PreTrainedModel._load_pretrained_model = classmethod(_patched_load_pretrained_func)try:model = AutoModelForCausalLM.from_pretrained(spec.hf_repo,device_map=device_map,**model_kwargs,)finally:modeling_utils.PreTrainedModel._load_pretrained_model = classmethod(_orig_load_pretrained_func)logger.info("Model ready in %.2fs", time.perf_counter() - t1)if device_map is None:model = model.to(device_pref)18 unmodified lineskwargs.pop("cache_position", None)kwargs.pop("encoder_attention_mask", None)kwargs.pop("attention_mask", None)kwargs.pop("head_mask", None)return _orig_forward(*args, **kwargs)model.forward = MethodType(_forward_compat, model)# Also patch submodules whose forward signatures include# encoder_attention_mask or head_mask to avoid duplicate passing (positional+kw)for _, module in model.named_modules():fwd = getattr(module, "forward", None)if not callable(fwd):2 unmodified linessig = inspect.signature(fwd)except Exception:continue# Patch modules that have problematic parametersneeds_patch = any(p in sig.parametersfor p in ("encoder_attention_mask", "head_mask"))if not needs_patch:continueorig_fwd = fwd2 unmodified lineskwargs.pop("encoder_attention_mask", None)kwargs.pop("attention_mask", None)kwargs.pop("cache_position", None)kwargs.pop("head_mask", None)return orig(*args, **kwargs)return _sub_forward_compat52 unmodified linesreturn text, "length"def apply_chat_template(model_name: str,messages: List[dict],add_generation_prompt: bool = True,) -> str:"""Apply the tokenizer's chat template to format messages for instruct models."""handle = _get_handle(model_name)tokenizer = handle.tokenizerif hasattr(tokenizer, "apply_chat_template"):return tokenizer.apply_chat_template(messages,tokenize=False,add_generation_prompt=add_generation_prompt,)# Fallback for tokenizers without chat_templatefrom .prompting import render_chat_promptfrom ..schemas.chat import ChatMessagechat_messages = [ChatMessage(role=m["role"], content=m["content"]) for m in messages]return render_chat_prompt(chat_messages)def _prepare_inputs(handle: _ModelHandle,prompt: str,
54 unmodified lines555657585960616263166 unmodified lines23023123223323423554 unmodified linesdevice: Optional[str] = Nonemax_context_tokens: Optional[int] = Nonemetadata: Optional[ModelMetadata] = None_DEFAULT_MODELS: List[ModelSpec] = [ModelSpec(name="GPT4-dev-177M-1511",hf_repo="k050506koch/GPT4-dev-177M-1511",166 unmodified linesdevice=entry.get("device"),max_context_tokens=entry.get("max_context_tokens"),metadata=metadata,))return specs54 unmodified lines5556575859606162636465666768697071727374757677787980818283166 unmodified lines25025125225325425525654 unmodified linesdevice: Optional[str] = Nonemax_context_tokens: Optional[int] = Nonemetadata: Optional[ModelMetadata] = Noneis_instruct: bool = False_DEFAULT_MODELS: List[ModelSpec] = [ModelSpec(name="GPT4-dev-177M-1511-Instruct",hf_repo="k050506koch/GPT4-dev-177M-1511-Instruct",dtype="float16",device="auto",max_context_tokens=512,is_instruct=True,metadata=ModelMetadata(description="Instruction-tuned GPT-4-style model fine-tuned on HuggingFaceH4/no_robots conversational dataset.",parameter_count="177M",training_datasets="HuggingFaceH4/no_robots",training_steps="1,200 SFT steps · AdamW optimizer · cosine LR schedule · assistant-only loss masking",evaluation="25.75% MMLU, 34.20% HellaSwag (author reported)",notes="First instruct model. Uses Harmony-style chat formatting with apply_chat_template. Requires trust_remote_code.",sources=("https://huggingface.co/k050506koch/GPT4-dev-177M-1511-Instruct",),),),ModelSpec(name="GPT4-dev-177M-1511",hf_repo="k050506koch/GPT4-dev-177M-1511",166 unmodified linesdevice=entry.get("device"),max_context_tokens=entry.get("max_context_tokens"),metadata=metadata,is_instruct=entry.get("is_instruct", False),))return specs
12345678910111213141516171819"""Chat completions endpoint."""from __future__ import annotationsfrom fastapi import APIRouterfrom ..core.errors import feature_not_availablefrom ..schemas.chat import ChatCompletionRequest, ChatCompletionResponserouter = APIRouter(prefix="/v1", tags=["chat"])@router.post("/chat/completions", response_model=ChatCompletionResponse)async def create_chat_completion(payload: ChatCompletionRequest) -> ChatCompletionResponse:"""Return a structured error while chat completions are disabled."""raise feature_not_available("chat_completions","Chat completions are currently disabled; please use /v1/completions instead.",)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165"""Chat completions endpoint."""from __future__ import annotationsimport asyncioimport jsonimport timeimport uuidfrom typing import Generator, Listfrom fastapi import APIRouterfrom fastapi.responses import StreamingResponsefrom ..core import enginefrom ..core.errors import model_not_found, openai_http_errorfrom ..core.model_registry import get_model_specfrom ..schemas.chat import (ChatCompletionChoice,ChatCompletionChunk,ChatCompletionChunkChoice,ChatCompletionChunkChoiceDelta,ChatCompletionRequest,ChatCompletionResponse,ChatMessage,)from ..schemas.common import UsageInforouter = APIRouter(prefix="/v1", tags=["chat"])@router.post("/chat/completions", response_model=ChatCompletionResponse)async def create_chat_completion(payload: ChatCompletionRequest) -> ChatCompletionResponse:"""Generate chat completions using instruct-tuned models."""try:spec = get_model_spec(payload.model)except KeyError:raise model_not_found(payload.model)if not spec.is_instruct:raise openai_http_error(400,f"Model '{payload.model}' is not an instruct model and cannot be used with chat completions. ""Please use /v1/completions instead, or choose an instruct model like 'GPT4-dev-177M-1511-Instruct'.",error_type="invalid_request_error",param="model",)# Convert messages to dict format for apply_chat_templatemessages_dict = [{"role": m.role, "content": m.content} for m in payload.messages]# Apply chat template using tokenizerprompt = engine.apply_chat_template(payload.model, messages_dict)stop_sequences = payload.stop if isinstance(payload.stop, list) else ([payload.stop] if payload.stop else [])if payload.stream:return _streaming_chat_completion(payload, prompt, stop_sequences)try:result = await asyncio.to_thread(engine.generate,payload.model,prompt,temperature=payload.temperature,top_p=payload.top_p,max_tokens=payload.max_tokens,stop=stop_sequences,n=payload.n,)except Exception as exc:raise openai_http_error(500,f"Generation error: {exc}",error_type="server_error",code="generation_error",)choices: List[ChatCompletionChoice] = []total_completion_tokens = 0for idx, item in enumerate(result.completions):total_completion_tokens += item.tokenschoices.append(ChatCompletionChoice(index=idx,message=ChatMessage(role="assistant", content=item.text.strip()),finish_reason=item.finish_reason,))usage = UsageInfo(prompt_tokens=result.prompt_tokens,completion_tokens=total_completion_tokens,total_tokens=result.prompt_tokens + total_completion_tokens,)return ChatCompletionResponse(model=payload.model, choices=choices, usage=usage)def _streaming_chat_completion(payload: ChatCompletionRequest,prompt: str,stop_sequences: List[str],) -> StreamingResponse:completion_id = f"chatcmpl-{uuid.uuid4().hex}"def event_stream() -> Generator[bytes, None, None]:stream = engine.create_stream(payload.model,prompt,temperature=payload.temperature,top_p=payload.top_p,max_tokens=payload.max_tokens,stop=stop_sequences,)# Send initial role deltainitial_chunk = ChatCompletionChunk(id=completion_id,created=int(time.time()),model=payload.model,choices=[ChatCompletionChunkChoice(index=0,delta=ChatCompletionChunkChoiceDelta(role="assistant"),finish_reason=None,)],)yield f"data: {initial_chunk.model_dump_json()}\n\n".encode()for token in stream.iter_tokens():chunk = ChatCompletionChunk(id=completion_id,created=int(time.time()),model=payload.model,choices=[ChatCompletionChunkChoice(index=0,delta=ChatCompletionChunkChoiceDelta(content=token),finish_reason=None,)],)yield f"data: {chunk.model_dump_json()}\n\n".encode()# Send final chunk with finish_reasonfinal_chunk = ChatCompletionChunk(id=completion_id,created=int(time.time()),model=payload.model,choices=[ChatCompletionChunkChoice(index=0,delta=ChatCompletionChunkChoiceDelta(),finish_reason=stream.finish_reason,)],)yield f"data: {final_chunk.model_dump_json()}\n\n".encode()yield b"data: [DONE]\n\n"return StreamingResponse(event_stream(),media_type="text/event-stream",)
219 unmodified lines2202212222232242252263 unmodified lines230231232233234235236237219 unmodified linesassert body["usage"]["prompt_tokens"] == len("Hello") + len("World")def test_chat_disabled() -> None:payload = ChatCompletionRequest.model_validate({"model": "GPT3-dev","messages": [3 unmodified lineswith pytest.raises(HTTPException) as exc:asyncio.run(chat.create_chat_completion(payload))assert exc.value.status_code == 501assert exc.value.detail["code"] == "chat_completions_not_available"def test_embeddings_not_implemented() -> None:219 unmodified lines2202212222232242252262272282292302312322332342352363 unmodified lines240241242243244245246247219 unmodified linesassert body["usage"]["prompt_tokens"] == len("Hello") + len("World")def test_chat_rejects_non_instruct_model(monkeypatch: pytest.MonkeyPatch) -> None:"""Chat completions should reject non-instruct models with a 400 error."""from app.core import model_registry# Register a non-instruct modelmonkeypatch.setattr(model_registry,"_registry",{"GPT3-dev": ModelSpec(name="GPT3-dev", hf_repo="k050506koch/GPT3-dev", is_instruct=False)},)payload = ChatCompletionRequest.model_validate({"model": "GPT3-dev","messages": [3 unmodified lineswith pytest.raises(HTTPException) as exc:asyncio.run(chat.create_chat_completion(payload))assert exc.value.status_code == 400assert "not an instruct model" in exc.value.detail["message"]def test_embeddings_not_implemented() -> None:
README.md+18-2
app/core/engine.py+56-9
app/core/model_registry.py+21
app/routers/chat.py+152-6
tests/test_openai_compat.py+13-3