adrienbrdne commited on
Commit
399006c
·
verified ·
1 Parent(s): 50e92f4

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +84 -0
api.py CHANGED
@@ -482,6 +482,90 @@ async def return_key_issue():
482
  # We use the response_model for validation and documentation
483
  return result
484
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
485
  # --- How to Run ---
486
  if __name__ == "__main__":
487
  # Make sure to set environment variables for config (NEO4J_URI, NEO4J_PASSWORD, GEMINI_API_KEY, etc.)
 
482
  # We use the response_model for validation and documentation
483
  return result
484
 
485
+ @app.post("/create-several-probdesc", response_model=CreateSeveralProbDescResponse)
486
+ async def create_several_probdesc(request: CreateSeveralProbDescRequest):
487
+ """
488
+ Generates multiple problem descriptions, each focused on a specific challenge
489
+ related to a key issue description. Executes calls concurrently.
490
+ """
491
+ # Check if Gemini library was imported and configured
492
+ if not genai:
493
+ logger.error("Gemini client is not available or configured.")
494
+ raise HTTPException(status_code=503, detail="Service Unavailable: Gemini client not configured")
495
+
496
+ descriptions_map = request.descriptions
497
+ challenges_map = request.challenges
498
+ technical_topic = request.technical_topic
499
+
500
+ # Basic input validation
501
+ if not descriptions_map or not challenges_map or not technical_topic:
502
+ raise HTTPException(status_code=400, detail="Missing required fields: descriptions, challenges, or technical_topic.")
503
+
504
+ logger.info(f"Received request to generate multiple problem descriptions for topic: {technical_topic}")
505
+
506
+ # --- Prepare and Execute Concurrent Gemini Calls ---
507
+ model_name = "gemini-2.5-flash-preview-04-17"
508
+ model = genai.GenerativeModel(model_name)
509
+ generated_descriptions = []
510
+
511
+ # Iterate through challenges to create tasks
512
+ for key_issue_id, list_of_challenges in challenges_map.items():
513
+ # Get the corresponding description
514
+ description = descriptions_map.get(key_issue_id)
515
+ successful_count = 0
516
+ failed_count = 0
517
+ if description is None:
518
+ logger.warning(f"No description found for key_issue_id {key_issue_id}. Skipping its challenges.")
519
+ continue # Skip challenges for this ID if description is missing
520
+
521
+ if not list_of_challenges:
522
+ logger.warning(f"Empty challenge list for key_issue_id {key_issue_id}. Skipping.")
523
+ continue
524
+
525
+ # Create a task for each challenge associated with this description
526
+ for ind, challenge in enumerate(list_of_challenges):
527
+ if not challenge: # Skip empty challenges
528
+ logger.warning(f"Skipping empty challenge string for key_issue_id {key_issue_id}.")
529
+ continue
530
+ else:
531
+ prompt = f"""I want you to create a problem description using a key issue explained in a detailed description.
532
+ Here is the description of the key issue: <description>{description}</description>
533
+ This key issue is part of the following technical topic: <technical_topic>{technical_topic}</technical_topic>
534
+ And the main focus of the problem description must be the following: <focus>{challenge}</focus>
535
+
536
+ As the output, I only want you to provide the problem description found, nothing else.
537
+
538
+ Here are examples of problem descriptions that you could create, it shows the level of specificity that you should aim for:
539
+
540
+ 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.'
541
+ 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.'
542
+
543
+ As far as possible, avoid using acronyms in the problem description.
544
+ Try to be about the same length as the examples if possible."""
545
+
546
+ try:
547
+ logger.info(f"Calling Gemini API to generate problem description {ind} ...")
548
+
549
+ response = model.generate_content(prompt)
550
+ # Extract the result
551
+ result_text = response.text.strip()
552
+ generated_descriptions.append(result_text)
553
+ successful_count += 1
554
+ logger.info(f"Successfully generated problem description {ind+1} from Gemini.")
555
+ logger.debug(f"Generated description: {result_text[:200]}...") # Log snippet
556
+
557
+ except Exception as e:
558
+ failed_count += 1
559
+ logger.error(f"Error calling Gemini API for problem description {ind+1}: {e}", exc_info=True)
560
+ # Check for specific Gemini API errors if possible
561
+ raise HTTPException(status_code=502, detail=f"Failed to generate problem description using LLM: {e}")
562
+
563
+ logger.info(f"Successfully generated descriptions: {successful_count}/{len(list_of_challenges)} ")
564
+
565
+ # --- Return Result ---
566
+ return CreateSeveralProbDescResponse(problem_descriptions=generated_descriptions)
567
+
568
+
569
  # --- How to Run ---
570
  if __name__ == "__main__":
571
  # Make sure to set environment variables for config (NEO4J_URI, NEO4J_PASSWORD, GEMINI_API_KEY, etc.)