File size: 1,178 Bytes
20d3dd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import json
import google.generativeai as genai
from google.generativeai.types import GenerationConfig, HarmCategory, HarmBlockThreshold

def run_gemini_json_completion(model, prompt: str, temperature: float):
    """Runs a Gemini call expecting a JSON response."""
    try:
        response = model.generate_content(prompt, generation_config=GenerationConfig(response_mime_type="application/json", temperature=temperature))
        return json.loads(response.text)
    except Exception as e:
        print(f"Warning: Failed to parse JSON from Gemini. Error: {e}")
        return {}

def run_gemini_text_completion(model, prompt: str, temperature: float):
    """Runs a standard Gemini text completion call."""
    generation_config = GenerationConfig(temperature=temperature)
    safety_settings = [{"category": HarmCategory.HARM_CATEGORY_HARASSMENT, "threshold": HarmBlockThreshold.BLOCK_ONLY_HIGH}]
    try:
        response = model.generate_content(prompt, generation_config=generation_config, safety_settings=safety_settings)
        return response.text
    except Exception as e:
        return f"[Error: Could not generate response. Details: {e}]"