fix the async mess
This commit is contained in:
13
app.py
13
app.py
@@ -8,7 +8,14 @@ import os
|
||||
import logging
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import FastAPI, Request, WebSocket, Depends, HTTPException, status
|
||||
from fastapi import (
|
||||
FastAPI,
|
||||
Request,
|
||||
WebSocket,
|
||||
Depends,
|
||||
HTTPException,
|
||||
status,
|
||||
)
|
||||
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
@@ -67,7 +74,7 @@ templates = Jinja2Templates(directory=os.path.join(STATIC_DIR, TEMPLATES_DIR))
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def home(request: Request, username: Annotated[str, Depends(verify_credentials)]):
|
||||
def home(request: Request, username: Annotated[str, Depends(verify_credentials)]):
|
||||
"""Render the main index page.
|
||||
|
||||
Args:
|
||||
@@ -91,7 +98,7 @@ async def websocket_endpoint(websocket: WebSocket):
|
||||
while True:
|
||||
data = await websocket.receive_text()
|
||||
|
||||
for chunk in generate(data):
|
||||
async for chunk in generate(data):
|
||||
await websocket.send_text(chunk)
|
||||
|
||||
await websocket.send_text("<<END>>")
|
||||
|
||||
17
lib.py
17
lib.py
@@ -12,13 +12,15 @@ from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
# Vertex AI RAG Corpus resource path
|
||||
CORPUS: str = "projects/520464122471/locations/europe-west3/ragCorpora/2305843009213693952"
|
||||
CORPUS: str = (
|
||||
"projects/520464122471/locations/europe-west3/ragCorpora/2305843009213693952"
|
||||
)
|
||||
|
||||
# Gemini model name
|
||||
GEMINI_MODEL: str = "gemini-3-pro-preview"
|
||||
|
||||
|
||||
def generate(prompt: str):
|
||||
async def generate(prompt: str):
|
||||
"""Generate content using Gemini model with RAG retrieval.
|
||||
|
||||
This function creates a streaming response from the Gemini model,
|
||||
@@ -42,8 +44,7 @@ def generate(prompt: str):
|
||||
types.Tool(
|
||||
retrieval=types.Retrieval(
|
||||
vertex_rag_store=types.VertexRagStore(
|
||||
rag_resources=[
|
||||
types.VertexRagStoreRagResource(rag_corpus=CORPUS)],
|
||||
rag_resources=[types.VertexRagStoreRagResource(rag_corpus=CORPUS)],
|
||||
)
|
||||
)
|
||||
)
|
||||
@@ -54,16 +55,14 @@ def generate(prompt: str):
|
||||
top_p=0.95,
|
||||
max_output_tokens=65535,
|
||||
safety_settings=[
|
||||
types.SafetySetting(
|
||||
category="HARM_CATEGORY_HATE_SPEECH", threshold="OFF"),
|
||||
types.SafetySetting(category="HARM_CATEGORY_HATE_SPEECH", threshold="OFF"),
|
||||
types.SafetySetting(
|
||||
category="HARM_CATEGORY_DANGEROUS_CONTENT", threshold="OFF"
|
||||
),
|
||||
types.SafetySetting(
|
||||
category="HARM_CATEGORY_SEXUALLY_EXPLICIT", threshold="OFF"
|
||||
),
|
||||
types.SafetySetting(
|
||||
category="HARM_CATEGORY_HARASSMENT", threshold="OFF"),
|
||||
types.SafetySetting(category="HARM_CATEGORY_HARASSMENT", threshold="OFF"),
|
||||
],
|
||||
tools=tools,
|
||||
thinking_config=types.ThinkingConfig(
|
||||
@@ -76,6 +75,8 @@ def generate(prompt: str):
|
||||
contents=contents,
|
||||
config=generate_content_config,
|
||||
):
|
||||
# DEBUG: Log chunk type to confirm generator behavior
|
||||
print(f"[DEBUG] Chunk type: {type(chunk)}")
|
||||
if (
|
||||
not chunk.candidates
|
||||
or not chunk.candidates[0].content
|
||||
|
||||
Reference in New Issue
Block a user