docs: polish sglang launch & add python request examples#16
Conversation
Summary of ChangesHello @alphabetc1, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refines the SGLang documentation by adjusting a worker port to prevent conflicts and enhance usability. Additionally, it introduces practical Python code examples, making it easier for developers to integrate and test the SGLang router's functionalities programmatically. Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request effectively resolves a port conflict issue by shifting the second worker's port to 30002 and updates the documentation to reflect this change. It also introduces a Python example section. My feedback focuses on improving the Python examples by using the project's existing dependency (httpx) instead of adding a new one (requests), and adding basic error handling to make the examples more reliable for users.
| import requests | ||
| import base64 | ||
|
|
||
| ROUTER = "http://localhost:30081" | ||
|
|
||
| # Check router health | ||
| resp = requests.get(f"{ROUTER}/health") | ||
| print(resp.json()) | ||
|
|
||
| # List registered workers | ||
| resp = requests.get(f"{ROUTER}/list_workers") | ||
| print(resp.json()) | ||
|
|
||
| # Image generation request (returns base64-encoded image) | ||
| resp = requests.post(f"{ROUTER}/generate", json={ | ||
| "model": "Qwen/Qwen-Image", | ||
| "prompt": "a cute cat", | ||
| "num_images": 1, | ||
| "response_format": "b64_json", | ||
| }) | ||
| data = resp.json() | ||
| print(data) | ||
|
|
||
| # Decode and save the image locally | ||
| img = base64.b64decode(data["data"][0]["b64_json"]) | ||
| with open("output.png", "wb") as f: | ||
| f.write(img) | ||
| print("Saved to output.png") | ||
|
|
||
| # Video generation request | ||
| resp = requests.post(f"{ROUTER}/generate_video", json={ | ||
| "model": "Qwen/Qwen-Image", | ||
| "prompt": "a flowing river", | ||
| }) | ||
| print(resp.json()) | ||
|
|
||
| # Check per-worker health and load | ||
| resp = requests.get(f"{ROUTER}/health_workers") | ||
| print(resp.json()) |
There was a problem hiding this comment.
The Python example currently uses the requests library, which is not a dependency of this project (the router uses httpx). Users following the installation guide will have httpx available, but might not have requests. Switching to httpx ensures the example works out-of-the-box. Additionally, incorporating resp.raise_for_status() provides better error handling for connection or API issues.
import httpx
import base64
ROUTER = "http://localhost:30081"
with httpx.Client(base_url=ROUTER) as client:
# Check router health
resp = client.get("/health")
resp.raise_for_status()
print(resp.json())
# List registered workers
resp = client.get("/list_workers")
resp.raise_for_status()
print(resp.json())
# Image generation request (returns base64-encoded image)
resp = client.post("/generate", json={
"model": "Qwen/Qwen-Image",
"prompt": "a cute cat",
"num_images": 1,
"response_format": "b64_json",
})
resp.raise_for_status()
data = resp.json()
print(data)
# Decode and save the image locally
img = base64.b64decode(data["data"][0]["b64_json"])
with open("output.png", "wb") as f:
f.write(img)
print("Saved to output.png")
# Video generation request
resp = client.post("/generate_video", json={
"model": "Qwen/Qwen-Image",
"prompt": "a flowing river",
})
resp.raise_for_status()
print(resp.json())
# Check per-worker health and load
resp = client.get("/health_workers")
resp.raise_for_status()
print(resp.json())
to #11