Skip to content

Commit

Permalink
debugging chunk speed
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedkhaleel2004 committed Feb 15, 2025
1 parent 6736191 commit 008c4d2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 11 deletions.
6 changes: 1 addition & 5 deletions backend/app/routers/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ def replace_path(match):
@router.post("/stream")
async def generate_stream(request: Request, body: ApiRequest):
try:
print("generate_stream called!")
# Initial validation checks
if len(body.instructions) > 1000:
return {"error": "Instructions exceed maximum length of 1000 characters"}
Expand All @@ -291,12 +290,9 @@ async def generate_stream(request: Request, body: ApiRequest):
]:
return {"error": "Example repos cannot be regenerated"}

print("initial validation checks passed")

async def event_generator():
try:
# Get cached github data
print("backend should be processing if you see this")
github_data = get_cached_github_data(
body.username, body.repo, body.github_pat
)
Expand Down Expand Up @@ -350,6 +346,7 @@ async def event_generator():
reasoning_effort="medium",
):
explanation += chunk
print("sending explanation chunk", chunk)
yield f"data: {json.dumps({'status': 'explanation_chunk', 'chunk': chunk})}\n\n"

if "BAD_INSTRUCTIONS" in explanation:
Expand Down Expand Up @@ -419,7 +416,6 @@ async def event_generator():
except Exception as e:
yield f"data: {json.dumps({'error': str(e)})}\n\n"

print("returning streaming response")
return StreamingResponse(event_generator(), media_type="text/event-stream")
except Exception as e:
return {"error": str(e)}
7 changes: 1 addition & 6 deletions src/hooks/useDiagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,12 @@ export function useDiagram(username: string, repo: string) {

const generateDiagram = useCallback(
async (instructions = "", apiKey?: string, githubPat?: string) => {
console.log("generateDiagram called");
setState({
status: "started",
message: "Starting generation process...",
});
console.log("started state set");

try {
console.log("fetching stream api");
const baseUrl =
process.env.NEXT_PUBLIC_API_DEV_URL ?? "https://api.gitdiagram.com"; // keeping this here since its easier for streaming
const response = await fetch(`${baseUrl}/generate/stream`, {
Expand All @@ -75,13 +72,10 @@ export function useDiagram(username: string, repo: string) {
github_pat: githubPat,
}),
});
console.log("response", response);
if (!response.ok) {
throw new Error("Failed to start streaming");
}
console.log("accessing reader");
const reader = response.body?.getReader();
console.log("accessed reader");
if (!reader) {
throw new Error("No reader available");
}
Expand Down Expand Up @@ -131,6 +125,7 @@ export function useDiagram(username: string, repo: string) {
}));
break;
case "explanation_chunk":
console.log("received explanation chunk:", data.chunk);
if (data.chunk) {
explanation += data.chunk;
setState((prev) => ({ ...prev, explanation }));
Expand Down

0 comments on commit 008c4d2

Please sign in to comment.