Skip to content

Commit

Permalink
Add OpenAI-compatible file-upload (#553)
Browse files Browse the repository at this point in the history
<!-- Thank you for your contribution! Please review
https://github.com/autonomi-ai/nos/blob/main/docs/CONTRIBUTING.md before
opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Summary

<!-- Please give a short summary of the change and the problem this
solves. -->

## Related issues

<!-- For example: "Closes #1234" -->

## Checks

- [ ] `make lint`: I've run `make lint` to lint the changes in this PR.
- [ ] `make test`: I've made sure the tests (`make test-cpu` or `make
test`) are passing.
- Additional tests:
   - [ ] Benchmark tests (when contributing new models)
   - [ ] GPU/HW tests
  • Loading branch information
spillai committed Jun 1, 2024
1 parent 55411c4 commit 2761f7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
20 changes: 12 additions & 8 deletions nos/server/http/_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from functools import lru_cache
from pathlib import Path
from tempfile import NamedTemporaryFile, SpooledTemporaryFile
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Literal, Optional

import requests
from fastapi import Depends, FastAPI, File, Form, HTTPException, UploadFile, status
Expand Down Expand Up @@ -39,6 +39,9 @@
Message,
Model,
)
from .integrations.openai.models import (
File as FileModel,
)


HTTP_API_VERSION = "v1"
Expand Down Expand Up @@ -228,11 +231,15 @@ def model_info(
raise HTTPException(status_code=400, detail=f"Invalid model {model}")

# TODO (delete file after processing)
@app.post(f"/{version}/file/upload", status_code=201)
def upload_file(file: UploadFile = File(...), client: Client = Depends(get_client)) -> JSONResponse:
@app.post(f"/{version}/files", response_model=FileModel, status_code=201)
def upload_file(
file: UploadFile = File(...),
purpose: Literal["assistants", "vision", "batch", "fine-tune"] = Form(...),
client: Client = Depends(get_client),
) -> JSONResponse:
try:
uid = uuid.uuid4()
basename = f"{uid}-{Path(file.filename).name}"
basename = f"file-{uid}-{Path(file.filename).name}"
path = NOS_TMP_FILES_DIR / basename
logger.debug(f"Uploading file: [local={file.filename}, path={path}]")
file.file.seek(0)
Expand All @@ -249,10 +256,7 @@ def upload_file(file: UploadFile = File(...), client: Client = Depends(get_clien
except Exception as exc:
logger.error(f"""Failed to upload file [file={file.filename}, exc={exc}]""")
raise HTTPException(status_code=500, detail="Failed to upload file.")
return {
"file_id": str(uid),
"filename": basename,
}
return FileModel(id=f"file-{uid}", bytes=path.stat().st_size, purpose=purpose, filename=f"file://{basename}")

@app.post(f"/{version}/chat/completions", status_code=status.HTTP_201_CREATED)
def chat(
Expand Down
22 changes: 22 additions & 0 deletions nos/server/http/integrations/openai/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ def list(cls) -> TModel:
pass


class File(BaseModel):
id: str
"""ID of the file"""

bytes: int
"""Size of the file in bytes"""

purpose: Optional[
Literal["assistants", "assistants_output", "batch", "batch_output", "fine-tune", "fine-tune-results", "vision"]
]
"""Purpose of the file"""

filename: str
"""Name of the file"""

created_at: int = Field(default_factory=lambda: int(datetime.datetime.utcnow().timestamp()))
"""UNIX timestamp (in seconds) of when the file was created"""

object: Literal["file"] = Field(default="file")
"""Type of the file"""


class Message(BaseModel):
role: Literal["user", "assistant", "system"] = Field(default="user")
"""Role of the message, either 'user', 'assistant' or 'system'"""
Expand Down

0 comments on commit 2761f7b

Please sign in to comment.