Skip to content

feat: docarray fastapi simple integration#1320

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

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

Conversation

@jupyterjazz

@jupyterjazz jupyterjazz commented Mar 30, 2023

Copy link
Copy Markdown
Contributor

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

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
@jupyterjazz jupyterjazz linked an issue Mar 31, 2023 that may be closed by this pull request
Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
@jupyterjazz jupyterjazz marked this pull request as ready for review March 31, 2023 06:43
Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
Comment thread docarray/array/array/io.py
Comment thread docarray/array/array/io.py Outdated
Comment thread docarray/base_doc/doc.py

@samsja samsja left a comment

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.

minor comment

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
@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

@jupyterjazz jupyterjazz Mar 31, 2023

Copy link
Copy Markdown
Contributor Author

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 <saba.sturua@jina.ai>

* refactor: custom orjson response class

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>

* refactor: docarray response name

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>

* refactor: simplify from json

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>

* test: refactor tests

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>

* refactor: adjust type hint

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>

* refactor: use orjson

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>

* style: mypy errors

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>

* refactor: abstract method

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>

---------

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
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