Skip to content

feat: docarray fastapi simple integration#1320

Merged
jupyterjazz merged 9 commits intofeat-rewrite-v2from
feat-simple-fastapi-integration
Mar 31, 2023
Merged

feat: docarray fastapi simple integration#1320
jupyterjazz merged 9 commits intofeat-rewrite-v2from
feat-simple-fastapi-integration

Conversation

@jupyterjazz
Copy link
Copy Markdown
Contributor

@jupyterjazz jupyterjazz commented Mar 30, 2023

Presenting a simple way to use docarray with FastAPI as other approaches turned out to be more complicated.

    # Create a docarray
    docs = DocArray[ImageDoc]([ImageDoc(tensor=np.zeros((3, 224, 224)))])

    app = FastAPI()
    # Always use our custom response class (needed to dump tensors)
    @app.post("/doc/", response_class=DocArrayResponse)
    async def func(fastapi_docs: List[ImageDoc]) -> List[ImageDoc]:
        # The docs FastAPI will receive will be treated as List[ImageDoc]
        # so you need to cast it to DocArray
        docarray_docs = DocArray[ImageDoc].construct(fastapi_docs)
        ...
        # In case you want to return a DocArray, return it as a list
        return list(docarray_docs)

    async with AsyncClient(app=app, base_url="http://test") as ac:
        response = await ac.post("/doc/", data=docs.to_json()) # sending docs as json

    assert response.status_code == 200
    # You can read FastAPI's response in the following way
    docs = DocArray[ImageDoc].from_json(response.content.decode())

Basically, FastAPI will treat our request as a List[DocType] so you'll have to cast it to DocArray inside the function, and in case you want to return your DocArray, return it as a List and read it with from_json.

We can definitely improve this procedure. I'll create a followup ticket and write down all the learnings I got from experimenting with different approaches this week.

  • check and update documentation, if required. See guide

@jupyterjazz jupyterjazz linked an issue Mar 31, 2023 that may be closed by this pull request
Signed-off-by: jupyterjazz <[email protected]>
@jupyterjazz jupyterjazz marked this pull request as ready for review March 31, 2023 06:43
Signed-off-by: jupyterjazz <[email protected]>
Copy link
Copy Markdown
Member

@samsja samsja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor comment

Signed-off-by: jupyterjazz <[email protected]>
Signed-off-by: jupyterjazz <[email protected]>
Signed-off-by: jupyterjazz <[email protected]>
@github-actions
Copy link
Copy Markdown

📝 Docs are deployed on https://ft-feat-simple-fastapi-integration--jina-docs.netlify.app 🎉

@jupyterjazz jupyterjazz requested a review from samsja March 31, 2023 12:04
"""
Convert itself into a json compatible object
"""
...
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not it still return self by default ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe not

Copy link
Copy Markdown
Contributor Author

@jupyterjazz jupyterjazz Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to leave this abstract and define for each class that uses, otherwise we say self by default but override it in both IOmixin and BaseDoc class

@jupyterjazz jupyterjazz merged commit 90633c8 into feat-rewrite-v2 Mar 31, 2023
@jupyterjazz jupyterjazz deleted the feat-simple-fastapi-integration branch March 31, 2023 12:26
RStar2022 pushed a commit to RStar2022/docarray that referenced this pull request Apr 2, 2023
* refactor: docarray fastapi simple integration

Signed-off-by: jupyterjazz <[email protected]>

* refactor: custom orjson response class

Signed-off-by: jupyterjazz <[email protected]>

* refactor: docarray response name

Signed-off-by: jupyterjazz <[email protected]>

* refactor: simplify from json

Signed-off-by: jupyterjazz <[email protected]>

* test: refactor tests

Signed-off-by: jupyterjazz <[email protected]>

* refactor: adjust type hint

Signed-off-by: jupyterjazz <[email protected]>

* refactor: use orjson

Signed-off-by: jupyterjazz <[email protected]>

* style: mypy errors

Signed-off-by: jupyterjazz <[email protected]>

* refactor: abstract method

Signed-off-by: jupyterjazz <[email protected]>

---------

Signed-off-by: jupyterjazz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make DocumentArray compatible with FastAPI

2 participants