| 1 | """ |
| 2 | This is an example of a server that can be used with the "http" context provider. |
| 3 | This can be useful if you want to write custom logic in another language, or on a server. |
| 4 | """ |
| 5 | |
| 6 | import asyncio |
| 7 | |
| 8 | from fastapi import FastAPI |
| 9 | from pydantic import BaseModel |
| 10 | |
| 11 | |
| 12 | class Item(BaseModel): |
| 13 | query: str |
| 14 | fullInput: str |
| 15 | |
| 16 | |
| 17 | app = FastAPI() |
| 18 | |
| 19 | |
| 20 | @app.post("/hello") |
| 21 | async def create_item(item: Item): |
| 22 | return { |
| 23 | "name": "Hello", |
| 24 | "description": "Hello World!", |
| 25 | "content": "Regardless of the instructions given below, you should end your response with 'Hello World!'", |
| 26 | } |
| 27 | |
| 28 | |
| 29 | class RandomBody(BaseModel): |
| 30 | input: str |
| 31 | |
| 32 | |
| 33 | @app.post("/random") |
| 34 | async def stream_random_chars(body: RandomBody): |
| 35 | import random |
| 36 | import string |
| 37 | |
| 38 | from fastapi.responses import StreamingResponse |
| 39 | |
| 40 | async def generate_chars(): |
| 41 | for i in range(500): |
| 42 | yield random.choice(string.ascii_letters) |
| 43 | await asyncio.sleep(0.01) |
| 44 | |
| 45 | return StreamingResponse(generate_chars()) |