-
Notifications
You must be signed in to change notification settings - Fork 227
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
Conversation
Signed-off-by: jupyterjazz <saba.sturua@jina.ai>
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>
docs/integrations/fastapi.md
Outdated
@@ -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`: |
There was a problem hiding this comment.
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
docs/integrations/fastapi.md
Outdated
```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()) | ||
``` |
There was a problem hiding this comment.
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
docs/integrations/fastapi.md
Outdated
> **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) |
There was a problem hiding this comment.
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
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>
📝 Docs are deployed on https://ft-docs-fastapi-integration--jina-docs.netlify.app 🎉 |
Define an "Integrations" section describing Docarray integration with FastAPI
https://ft-docs-fastapi-integration--jina-docs.netlify.app/