Update api.py
Browse files
api.py
CHANGED
@@ -55,7 +55,7 @@ class SpecificityEvaluationResponse(BaseModel):
|
|
55 |
specificity: SpecificityScore
|
56 |
|
57 |
class ProblemDescriptionRequest(BaseModel):
|
58 |
-
|
59 |
technical_topic: str
|
60 |
|
61 |
class ProblemDescriptionResponse(BaseModel):
|
@@ -140,6 +140,63 @@ app = FastAPI(
|
|
140 |
lifespan=lifespan # Use the lifespan context manager
|
141 |
)
|
142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
# --- API Endpoint ---
|
144 |
# API state check route
|
145 |
@app.get("/")
|
@@ -349,34 +406,33 @@ async def evaluation(request: SpecificityEvaluationRequest):
|
|
349 |
@app.post("/create-problem-description", response_model=ProblemDescriptionResponse)
|
350 |
async def gen_problem_description(request: ProblemDescriptionRequest):
|
351 |
"""
|
352 |
-
Generates a problem description using Gemini based on
|
353 |
-
and technical topic.
|
354 |
"""
|
355 |
-
|
|
|
|
|
|
|
|
|
|
|
356 |
technical_topic = request.technical_topic
|
357 |
|
358 |
-
|
359 |
-
|
|
|
|
|
|
|
360 |
|
361 |
-
logger.info("Received request for problem description generation.")
|
362 |
-
logger.debug(f"Topic: {technical_topic}")
|
363 |
|
364 |
-
# ---
|
365 |
-
prompt =
|
366 |
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
As the output, I only want you to provide the problem description found, nothing else.
|
372 |
-
|
373 |
-
Here are examples of problem descriptions that you could create, it shows the level of specificity that you should aim for:
|
374 |
-
|
375 |
-
Example 1: 'I am working on enhancing security in 4G and 5G telecommunications networks, specifically in the area of network slicing. My goal is to address vulnerabilities in the isolation of slices and improve the integrity of data transmission across different network slices, ensuring they are properly protected against unauthorized access and attacks.'
|
376 |
-
Example 2: 'I am working on improving user authentication in the context of 3GPP (3rd Generation Partnership Project) networks. Specifically, I need to solve issues related to the security and efficiency of the authentication process in the 5G network architecture, focusing on the use of cryptographic keys and identity management.'
|
377 |
-
|
378 |
-
As far as possible, avoid using acronyms in the problem description.
|
379 |
-
Try to be about the same length as the examples if possible."""
|
380 |
|
381 |
try:
|
382 |
logger.info("Calling Gemini API to generate problem description...")
|
@@ -402,7 +458,6 @@ async def gen_problem_description(request: ProblemDescriptionRequest):
|
|
402 |
# --- Return Result ---
|
403 |
# Return as JSON using the Pydantic model
|
404 |
return ProblemDescriptionResponse(problem_description=result_text)
|
405 |
-
|
406 |
# Alternative: Return as plain text
|
407 |
# return PlainTextResponse(content=result_text)
|
408 |
|
|
|
55 |
specificity: SpecificityScore
|
56 |
|
57 |
class ProblemDescriptionRequest(BaseModel):
|
58 |
+
descriptions: List[str]
|
59 |
technical_topic: str
|
60 |
|
61 |
class ProblemDescriptionResponse(BaseModel):
|
|
|
140 |
lifespan=lifespan # Use the lifespan context manager
|
141 |
)
|
142 |
|
143 |
+
# --- Helper Functions for Prompt Building ---
|
144 |
+
|
145 |
+
def format_ki_descriptions(descriptions: list[str]) -> str:
|
146 |
+
formatted_descriptions = ""
|
147 |
+
for ind in range(len(sample)):
|
148 |
+
ki_number = ind + 1
|
149 |
+
formatted_description = f"Here is the description of the key issue {ki_number}: <description{ki_number}>{descriptions[ind]}<description{ki_number}>"
|
150 |
+
if ind == len(sample) - 1:
|
151 |
+
formatted_descriptions += formatted_description
|
152 |
+
else:
|
153 |
+
formatted_descriptions += formatted_description + "\n"
|
154 |
+
return formatted_descriptions
|
155 |
+
|
156 |
+
def build_prompt(descriptions: list[str], technical_topic: str) -> str | None:
|
157 |
+
prompt = None
|
158 |
+
if len(descriptions) == 1:
|
159 |
+
prompt = f"""I want you to create a problem description using a key issue explained in a detailed description.
|
160 |
+
Here is the description of the key issue: <description>{descriptions[0]}</description>
|
161 |
+
|
162 |
+
This key issue is part of the following technical topic: <technical_topic>{technical_topic}</technical_topic>
|
163 |
+
|
164 |
+
As the output, I only want you to provide the problem description found, nothing else.
|
165 |
+
|
166 |
+
Here are examples of problem descriptions that you could create, it shows the level of specificity that you should aim for:
|
167 |
+
|
168 |
+
Example 1: 'I am working on enhancing security in 4G and 5G telecommunications networks, specifically in the area of network slicing. My goal is to address vulnerabilities in the isolation of slices and improve the integrity of data transmission across different network slices, ensuring they are properly protected against unauthorized access and attacks.'
|
169 |
+
Example 2: 'I am working on improving user authentication in the context of 3GPP (3rd Generation Partnership Project) networks. Specifically, I need to solve issues related to the security and efficiency of the authentication process in the 5G network architecture, focusing on the use of cryptographic keys and identity management.'
|
170 |
+
|
171 |
+
As far as possible, avoid using acronyms in the problem description.
|
172 |
+
Try to be about the same length as the examples if possible."""
|
173 |
+
|
174 |
+
elif len(descriptions) > 1:
|
175 |
+
formatted_descriptions = format_ki_descriptions(descriptions)
|
176 |
+
prompt = f"""I want you to create a problem description using several key issues, each explained with a detailed description.
|
177 |
+
|
178 |
+
{formatted_descriptions}
|
179 |
+
|
180 |
+
These key issues are part of the following technical topic: <technical_topic>{technical_topic}<technical_topic>
|
181 |
+
|
182 |
+
As the output, I only want you to provide the problem description found, nothing else.
|
183 |
+
|
184 |
+
Here are examples of problem descriptions that you could create, it shows the level of specificity that you should aim for:
|
185 |
+
|
186 |
+
Example 1: 'I am working on enhancing security in 4G and 5G telecommunications networks, specifically in the area of network slicing.
|
187 |
+
My goal is to address vulnerabilities in the isolation of slices and improve the integrity of data transmission across different network slices,
|
188 |
+
ensuring they are properly protected against unauthorized access and attacks.'
|
189 |
+
|
190 |
+
Example 2: 'I am working on improving user authentication in the context of 3GPP (3rd Generation Partnership Project) networks.
|
191 |
+
Specifically, I need to solve issues related to the security and efficiency of the authentication process in the 5G network architecture,
|
192 |
+
focusing on the use of cryptographic keys and identity management.'
|
193 |
+
|
194 |
+
As far as possible, avoid using acronyms in the problem description.
|
195 |
+
When creating the problem description that combines the topics of the key issues, be as intelligible as possible and ensure that the mix of key issues makes sense.
|
196 |
+
The description of the problem should not exceed 100 words or so."""
|
197 |
+
|
198 |
+
return prompt
|
199 |
+
|
200 |
# --- API Endpoint ---
|
201 |
# API state check route
|
202 |
@app.get("/")
|
|
|
406 |
@app.post("/create-problem-description", response_model=ProblemDescriptionResponse)
|
407 |
async def gen_problem_description(request: ProblemDescriptionRequest):
|
408 |
"""
|
409 |
+
Generates a problem description using Gemini based on one or more detailed
|
410 |
+
descriptions and a technical topic.
|
411 |
"""
|
412 |
+
# Check if Gemini library was imported and configured
|
413 |
+
if not genai or not genai.is_configured():
|
414 |
+
logger.error("Gemini client is not available or configured.")
|
415 |
+
raise HTTPException(status_code=503, detail="Service Unavailable: Gemini client not configured")
|
416 |
+
|
417 |
+
descriptions = request.descriptions
|
418 |
technical_topic = request.technical_topic
|
419 |
|
420 |
+
# Validate input: need at least one description and a topic
|
421 |
+
if not descriptions: # Check if the list is empty
|
422 |
+
raise HTTPException(status_code=400, detail="Field 'descriptions' cannot be empty.")
|
423 |
+
if not technical_topic:
|
424 |
+
raise HTTPException(status_code=400, detail="Missing required field: technical_topic.")
|
425 |
|
426 |
+
logger.info(f"Received request for problem description generation with {len(descriptions)} description(s).")
|
427 |
+
logger.debug(f"Topic: {technical_topic}")
|
428 |
|
429 |
+
# --- Build Prompt using helper function ---
|
430 |
+
prompt = build_prompt(descriptions, technical_topic)
|
431 |
|
432 |
+
if prompt is None:
|
433 |
+
# This case should theoretically be caught by the validation above, but good practice to check
|
434 |
+
logger.error("Failed to build prompt (likely empty description list).")
|
435 |
+
raise HTTPException(status_code=500, detail="Internal error: Could not build prompt.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
436 |
|
437 |
try:
|
438 |
logger.info("Calling Gemini API to generate problem description...")
|
|
|
458 |
# --- Return Result ---
|
459 |
# Return as JSON using the Pydantic model
|
460 |
return ProblemDescriptionResponse(problem_description=result_text)
|
|
|
461 |
# Alternative: Return as plain text
|
462 |
# return PlainTextResponse(content=result_text)
|
463 |
|