forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Serve][docs] Refresh http-guide (ray-project#27779)
- Moved most code snippet to doc_code - Added section about DAGDriver - Added section discussing when should you use each abstraction layer. Signed-off-by: Stefan van der Kleij <[email protected]>
- Loading branch information
Showing
4 changed files
with
165 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# flake8: noqa | ||
|
||
# __begin_starlette__ | ||
import starlette.requests | ||
import requests | ||
from ray import serve | ||
|
||
|
||
@serve.deployment | ||
class Counter: | ||
def __call__(self, request: starlette.requests.Request): | ||
return request.query_params | ||
|
||
|
||
serve.run(Counter.bind()) | ||
resp = requests.get("http:https://localhost:8000?a=b&c=d") | ||
assert resp.json() == {"a": "b", "c": "d"} | ||
# __end_starlette__ | ||
|
||
# __begin_dagdriver__ | ||
import numpy as np | ||
import requests | ||
from ray import serve | ||
from ray.serve.drivers import DAGDriver | ||
from ray.serve.http_adapters import json_to_ndarray | ||
|
||
|
||
@serve.deployment | ||
class Model: | ||
def __call__(self, arr: np.ndarray): | ||
return arr.sum() | ||
|
||
|
||
serve.run(DAGDriver.bind(Model.bind(), http_adapter=json_to_ndarray)) | ||
resp = requests.post("http:https://localhost:8000", json={"array": [[1, 2], [2, 3]]}) | ||
assert resp.json() == 8 | ||
|
||
# __end_dagdriver__ | ||
|
||
# __begin_fastapi__ | ||
import ray | ||
import requests | ||
from fastapi import FastAPI | ||
from ray import serve | ||
|
||
app = FastAPI() | ||
|
||
|
||
@serve.deployment(route_prefix="/hello") | ||
@serve.ingress(app) | ||
class MyFastAPIDeployment: | ||
@app.get("/") | ||
def root(self): | ||
return "Hello, world!" | ||
|
||
|
||
serve.run(MyFastAPIDeployment.bind()) | ||
resp = requests.get("http:https://localhost:8000/hello") | ||
assert resp.json() == "Hello, world!" | ||
# __end_fastapi__ | ||
|
||
|
||
# __begin_fastapi_multi_routes__ | ||
import ray | ||
import requests | ||
from fastapi import FastAPI | ||
from ray import serve | ||
|
||
app = FastAPI() | ||
|
||
|
||
@serve.deployment(route_prefix="/hello") | ||
@serve.ingress(app) | ||
class MyFastAPIDeployment: | ||
@app.get("/") | ||
def root(self): | ||
return "Hello, world!" | ||
|
||
@app.post("/{subpath}") | ||
def root(self, subpath: str): | ||
return f"Hello from {subpath}!" | ||
|
||
|
||
serve.run(MyFastAPIDeployment.bind()) | ||
resp = requests.post("http:https://localhost:8000/hello/Serve") | ||
assert resp.json() == "Hello from Serve!" | ||
# __end_fastapi_multi_routes__ | ||
|
||
# __begin_byo_fastapi__ | ||
import ray | ||
import requests | ||
from fastapi import FastAPI | ||
from ray import serve | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/") | ||
def f(): | ||
return "Hello from the root!" | ||
|
||
|
||
@serve.deployment(route_prefix="/") | ||
@serve.ingress(app) | ||
class FastAPIWrapper: | ||
pass | ||
|
||
|
||
serve.run(FastAPIWrapper.bind()) | ||
resp = requests.get("http:https://localhost:8000/") | ||
assert resp.json() == "Hello from the root!" | ||
# __end_byo_fastapi__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters