Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fastapi integration section #1326

Merged
merged 6 commits into from
Apr 12, 2023

Conversation

jupyterjazz
Copy link
Contributor

@jupyterjazz jupyterjazz commented Apr 3, 2023

Define an "Integrations" section describing Docarray integration with FastAPI

https://ft-docs-fastapi-integration--jina-docs.netlify.app/

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
@jupyterjazz jupyterjazz linked an issue Apr 3, 2023 that may be closed by this pull request
@samsja
Copy link
Member

samsja commented Apr 3, 2023

lets just do FastAPI in a first time, we can do the other one in other PR

Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
@jupyterjazz jupyterjazz marked this pull request as ready for review April 3, 2023 07:12
@@ -0,0 +1,125 @@
# Use Docarray with FastAPI

You might already know that `DocArray` documents are Pydantic Models (with a twist) [Reference relevant part], and as such they are fully compatible with `FastAPI`:
Copy link
Member

Choose a reason for hiding this comment

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

I think we need more introduction to what is FastAPI etccc

Comment on lines 5 to 43
```python
import numpy as np
from fastapi import FastAPI
from httpx import AsyncClient

from docarray import BaseDoc
from docarray.documents import ImageDoc
from docarray.typing import NdArray
from docarray.base_doc import DocumentResponse


class InputDoc(BaseDoc):
img: ImageDoc


class OutputDoc(BaseDoc):
embedding_clip: NdArray
embedding_bert: NdArray


input_doc = InputDoc(img=ImageDoc(tensor=np.zeros((3, 224, 224))))

app = FastAPI()


@app.post("/doc/", response_model=OutputDoc, response_class=DocumentResponse)
async def create_item(doc: InputDoc) -> OutputDoc:
## call my fancy model to generate the embeddings
doc = OutputDoc(
embedding_clip=np.zeros((100, 1)), embedding_bert=np.zeros((100, 1))
)
return doc


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

doc = OutputDoc.parse_raw(response.content.decode())
```
Copy link
Member

Choose a reason for hiding this comment

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

we need to break down this part in two. First schema creation then FastAPI app instatiation

Comment on lines 82 to 85
> **Note**
> Currently, `FastAPI` receives `DocArray` objects as lists, so you have to construct a DocArray inside the function.
> Also, if you want to return a `DocArray` object, first you have to convert it to a list.
> (Shown in the example below)
Copy link
Member

Choose a reason for hiding this comment

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

you can check the install documentaiton but to do note you need to do

!! note
smth

jupyterjazz and others added 4 commits April 4, 2023 09:07
Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
Signed-off-by: Saba Sturua <45267439+jupyterjazz@users.noreply.github.com>
@jupyterjazz jupyterjazz requested a review from samsja April 12, 2023 13:25
@github-actions
Copy link

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

@jupyterjazz jupyterjazz merged commit 9a13f93 into feat-rewrite-v2 Apr 12, 2023
@jupyterjazz jupyterjazz deleted the docs-fastapi-integration branch April 12, 2023 13:34
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.

DOCS: "integrations" section
2 participants