chatgpt_public_messages (view)
7 rows where conversation = "Starlette CORS Forwarding App 08a446e2-51a2-4abe-89ed-64b7887e12a0"
This data as json, copyable, CSV (advanced)
Suggested facets: author_role, create_time, create_time (date)
id | conversation | author_role | content | create_time |
---|---|---|---|---|
534a10c5-b320-4b12-a2c2-a56eb1c84dfd | Starlette CORS Forwarding App 08a446e2-51a2-4abe-89ed-64b7887e12a0 | system | You are ChatGPT, a large language model trained by OpenAI. Knowledge cutoff: 2021-09 Current date: 2023-03-25 | 2023-03-26 04:10:49 |
395b6bcd-d87d-4dda-9879-51200da31272 | Starlette CORS Forwarding App 08a446e2-51a2-4abe-89ed-64b7887e12a0 | user | Write a web app in python Starlette which has CORS enabled - including for the auth header - and forwards all incoming requests to any path to another server host specified in an environment variable | 2023-03-26 04:10:49 |
33a1dbc3-24c9-47cb-9f82-1aeb6364b3b4 | Starlette CORS Forwarding App 08a446e2-51a2-4abe-89ed-64b7887e12a0 | assistant | To build a web app in Python using the Starlette framework with CORS enabled, you need to follow these steps: 1. Install the required packages: Starlette, HTTPX, and python-dotenv 2. Create a `.env` file to store the environment variable for the target server 3. Write the main application script Here's a step-by-step guide: 1. Install the required packages: Create a virtual environment (optional, but recommended) and then run: ```bash pip install starlette httpx python-dotenv uvicorn aiohttp ``` 2. Create a `.env` file to store the environment variable for the target server: Create a `.env` file in the same directory as your main application script, and add the following line: ``` TARGET_SERVER_HOST=http://your-target-server.com ``` Replace `http://your-target-server.com` with the actual target server host. 3. Write the main application script: Create a new Python file, `main.py`, and add the following code: ```python from starlette.applications import Starlette from starlette.middleware import Middleware from starlette.middleware.cors import CORSMiddleware from starlette.routing import Route from starlette.requests import Request from starlette.responses import JSONResponse from starlette.config import Config import httpx from aiohttp.client_exceptions import ClientConnectorError import json config = Config(".env") TARGET_SERVER_HOST = config("TARGET_SERVER_HOST") middleware = [ Middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) ] app = Starlette(middleware=middleware) async def forward_request(request: Request): try: async with httpx.AsyncClient() as client: target_url = f"{TARGET_SERVER_HOST}{request.url.path}" method = request.method headers = dict(request.headers) if "host" in headers: del headers["host"] if request.method in ["POST", "PUT", "PATCH"]: request_body = await reque… | 2023-03-26 04:12:11 |
81860729-2e12-4fc5-87b9-858315b0b0b5 | Starlette CORS Forwarding App 08a446e2-51a2-4abe-89ed-64b7887e12a0 | user | Rewrite that to use os.environ | 2023-03-26 04:19:27 |
296feb16-86a1-4fde-9c73-fc81b03fe0cd | Starlette CORS Forwarding App 08a446e2-51a2-4abe-89ed-64b7887e12a0 | assistant | Here's the updated version of `main.py` using `os.environ` instead of `python-dotenv`: ```python import os from starlette.applications import Starlette from starlette.middleware import Middleware from starlette.middleware.cors import CORSMiddleware from starlette.routing import Route from starlette.requests import Request from starlette.responses import JSONResponse import httpx from aiohttp.client_exceptions import ClientConnectorError import json TARGET_SERVER_HOST = os.environ.get("TARGET_SERVER_HOST") middleware = [ Middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) ] app = Starlette(middleware=middleware) async def forward_request(request: Request): try: async with httpx.AsyncClient() as client: target_url = f"{TARGET_SERVER_HOST}{request.url.path}" method = request.method headers = dict(request.headers) if "host" in headers: del headers["host"] if request.method in ["POST", "PUT", "PATCH"]: request_body = await request.json() else: request_body = None response = await client.request( method, target_url, headers=headers, json=request_body ) return JSONResponse(response.json(), status_code=response.status_code) except ClientConnectorError: return JSONResponse({"error": "Target server connection error."}, status_code=503) except Exception as e: return JSONResponse({"error": str(e)}, status_code=500) app.add_route("/{path:path}", forward_request, methods=["GET", "POST", "PUT", "PATCH", "DELETE"]) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000) ``` Before running the app, you need to set the environment variable `TARGET_SERVER_HOST` in your terminal: For Linux/Mac: ```bash export TARGET_SERVER_HOST=http://your-target-server.com ``` For Wi… | 2023-03-26 04:20:17 |
e276c040-0bbb-4d2b-9b13-b2f4d676ceee | Starlette CORS Forwarding App 08a446e2-51a2-4abe-89ed-64b7887e12a0 | user | Catch httpx.HTTPError as the only exception you care about | 2023-03-26 04:20:46 |
e36825f0-d641-4578-88da-cbd0f26303b3 | Starlette CORS Forwarding App 08a446e2-51a2-4abe-89ed-64b7887e12a0 | assistant | Here's the updated version of `main.py` with only `httpx.HTTPError` as the exception we care about: ```python import os from starlette.applications import Starlette from starlette.middleware import Middleware from starlette.middleware.cors import CORSMiddleware from starlette.routing import Route from starlette.requests import Request from starlette.responses import JSONResponse import httpx TARGET_SERVER_HOST = os.environ.get("TARGET_SERVER_HOST") middleware = [ Middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) ] app = Starlette(middleware=middleware) async def forward_request(request: Request): try: async with httpx.AsyncClient() as client: target_url = f"{TARGET_SERVER_HOST}{request.url.path}" method = request.method headers = dict(request.headers) if "host" in headers: del headers["host"] if request.method in ["POST", "PUT", "PATCH"]: request_body = await request.json() else: request_body = None response = await client.request( method, target_url, headers=headers, json=request_body ) return JSONResponse(response.json(), status_code=response.status_code) except httpx.HTTPError as e: return JSONResponse({"error": str(e)}, status_code=500) app.add_route("/{path:path}", forward_request, methods=["GET", "POST", "PUT", "PATCH", "DELETE"]) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000) ``` Now, the application will only catch `httpx.HTTPError` exceptions and return a JSON response with the error message and a 500 status code. | 2023-03-26 04:21:28 |
Advanced export
JSON shape: default, array, newline-delimited
CREATE VIEW chatgpt_public_messages as select chatgpt_message.id, chatgpt_conversation.title || char(10) || chatgpt_conversation.id as conversation, chatgpt_message.author_role, chatgpt_message.content, datetime(chatgpt_message.create_time, 'unixepoch') as create_time from chatgpt_message join chatgpt_conversation on conversation_id = chatgpt_conversation.id where chatgpt_message.create_time is not null and conversation_id in (select id from chatgpt_public) order by chatgpt_message.create_time;