2424@app .get ("/GenerateQuiz" )
2525async def generate_quiz_endpoint (request : Request ) -> JSONResponse :
2626 """
27- FastAPI App to generate an image based on a provided prompt .
27+ FastAPI endpoint to generate a quiz based on topic, difficulty, and model .
2828
29- The function expects a 'prompt' parameter in the HTTP request query
30- or body. If a valid prompt is received, the function uses the
31- generate_image() function to create an image URL corresponding to
32- the prompt and returns it in the HTTP response.
33-
34- Parameters:
35- - request (Request): The FastAPI request object containing the client request.
29+ Query Parameters:
30+ - topic: The subject for the quiz (e.g., "UK History").
31+ - difficulty: The desired difficulty (e.g., "easy", "medium").
32+ - n_questions: (Optional) Number of questions to generate (defaults to 10).
33+ - model: (Optional) The model to use. If not provided, the default from QuizGenerator is used.
3634
3735 Returns:
38- - JSONResponse: The HTTP response object containing the generated quiz or
39- an appropriate error message .
36+ - StreamingResponse: Streams quiz questions in SSE format.
37+ - JSONResponse: Error message if required parameters are missing .
4038 """
41-
39+ # Retrieve query parameters
4240 topic = request .query_params .get ("topic" )
4341 difficulty = request .query_params .get ("difficulty" )
4442 n_questions = request .query_params .get ("n_questions" )
43+ model = request .query_params .get ("model" )
4544
4645 logging .info (
47- f"Python HTTP trigger function processed a request with { topic = } { difficulty = } , { n_questions = } ."
46+ f"Python HTTP trigger function processed a request with { topic = } { difficulty = } , { n_questions = } , model= { model } ."
4847 )
4948
50- # If either 'topic' or 'difficulty' is not provided in the request,
51- # the function will return an error message and a 400 status code.
52- # n_questions is optional
49+ # If either 'topic' or 'difficulty' is missing, return an error.
5350 if not topic or not difficulty :
5451 error_message = "Please provide a topic and difficulty in the query string or in the request body to generate a quiz."
5552 logging .error (error_message )
@@ -58,52 +55,53 @@ async def generate_quiz_endpoint(request: Request) -> JSONResponse:
5855 status_code = 400 ,
5956 )
6057
61- # Set default value if not set
58+ # Set default number of questions if not provided.
6259 if not n_questions :
6360 n_questions = 10
61+ else :
62+ # Convert n_questions to an integer if provided as string.
63+ try :
64+ n_questions = int (n_questions )
65+ except ValueError :
66+ error_message = "n_questions must be an integer."
67+ logging .error (error_message )
68+ return JSONResponse (
69+ content = {"error" : error_message },
70+ status_code = 400 ,
71+ )
6472
6573 logging .info (
66- f"Generating quiz for topic : { topic } with difficulty: { difficulty } with number of questions: { n_questions } "
74+ f"Generating quiz with : { topic = } , { difficulty = } , { n_questions = } , { model = } . "
6775 )
6876
69- # TODO: rename to quiz creator
70- # TODO: Fix - currently doesnt actually stream, but returns all items at once.
71- # Need to look into the azure functions streaming capability
72- # Or think about hosting the fastapi in another method e.g. ACI
73- quiz_generator = QuizGenerator ()
77+ # Create a QuizGenerator instance.
78+ # TODO: rename to quiz creator ?
79+ quiz_generator = QuizGenerator (model = model )
7480 generator = quiz_generator .generate_quiz (topic , difficulty , n_questions )
7581
82+ # Return the quiz as a streaming response in SSE format.
7683 return StreamingResponse (generator , media_type = "text/event-stream" )
7784
7885
7986@app .get ("/GenerateImage" )
8087async def generate_image_endpoint (request : Request ) -> JSONResponse :
8188 """
82- FastAPI App to generate an image based on a provided prompt.
83-
84- The function expects a 'prompt' parameter in the HTTP request query
85- or body. If a valid prompt is received, the function uses the
86- generate_image() function to create an image URL corresponding to
87- the prompt and returns it in the HTTP response.
89+ FastAPI endpoint to generate an image based on a provided prompt.
8890
89- Parameters:
90- - request (Request) : The FastAPI request object containing the client request .
91+ Query Parameters:
92+ - prompt : The prompt for image generation .
9193
9294 Returns:
93- - JSONResponse: The HTTP response object containing the image URL or
94- an appropriate error message.
95+ - JSONResponse: Contains the generated image URL or an error message.
9596 """
96-
97- logging .info ("Python HTTP trigger function processed a request." )
98-
97+ logging .info ("Processing image generation request." )
9998 prompt = request .query_params .get ("prompt" )
100-
10199 if not prompt :
102100 error_message = "No prompt query param provided for image generation."
103101 logging .warning (error_message )
104102 return JSONResponse (content = {"error" : error_message }, status_code = 400 )
105103
106- logging .info (f"Received prompt: { prompt } " )
104+ logging .info (f"Received image prompt: { prompt } " )
107105 image_generator = ImageGenerator ()
108106 image_url = image_generator .generate_image (prompt )
109107
@@ -112,8 +110,7 @@ async def generate_image_endpoint(request: Request) -> JSONResponse:
112110 logging .error (error_message )
113111 return JSONResponse (content = {"error" : error_message }, status_code = 500 )
114112
115- # Return the image URL in the HTTP response
116- logging .info (f"Generated image for prompt { prompt } : { image_url } " )
113+ logging .info (f"Generated image for prompt '{ prompt } ': { image_url } " )
117114 return JSONResponse (content = {"image_url" : image_url }, status_code = 200 )
118115
119116
0 commit comments