|
import os |
|
import json |
|
from google import genai |
|
from google.genai import types |
|
from dotenv import load_dotenv |
|
from prompts import * |
|
|
|
try: |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
except ImportError: |
|
pass |
|
|
|
def format_search_results(results): |
|
"""Format Search Results""" |
|
formatted_output = "" |
|
|
|
for i in range(1, 4): |
|
formatted_output += f"### {i}. {results[f'keyword{i}']}\n" |
|
formatted_output += f"{results[f'summary{i}']}" |
|
formatted_output += "\n" |
|
|
|
return formatted_output |
|
|
|
def grounding_with_google_search(stt_data, content_type = "국민의힘"): |
|
""" |
|
Extract Keywords and Perform Google Search |
|
""" |
|
|
|
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY")) |
|
|
|
|
|
sample_schema = { |
|
"keyword1": "Core Keyword 1", |
|
"summary1": "Summarize Search Result about Core Keyword 1", |
|
"keyword2": "Core Keyword 2", |
|
"summary2": "Summarize Search Result about Core Keyword 2", |
|
"keyword3": "Core Keyword 3", |
|
"summary3": "Summarize Search Result about Core Keyword 3" |
|
} |
|
|
|
|
|
if content_type == "Agents for Amazon Bedrock": |
|
system_prompt = BEDROCK_SEARCH_PROMPT |
|
elif content_type == "Bundesliga Fan Experience": |
|
system_prompt = BUNDESLIGA_SEARCH_PROMPT |
|
elif content_type == "AWS_2024_recap": |
|
system_prompt = AWS_SEARCH_PROMPT |
|
|
|
|
|
system_prompt = system_prompt.format(sample_schema=sample_schema) |
|
|
|
|
|
human_message = f""" |
|
## Input Script |
|
{stt_data} |
|
""" |
|
|
|
|
|
response = client.models.generate_content( |
|
model="gemini-2.0-flash-001", |
|
contents=human_message, |
|
config=types.GenerateContentConfig( |
|
system_instruction=system_prompt, |
|
response_mime_type="application/json", |
|
tools=[ |
|
types.Tool( |
|
google_search=types.GoogleSearchRetrieval( |
|
dynamic_retrieval_config = types.DynamicRetrievalConfig( |
|
mode=types.DynamicRetrievalConfigMode.MODE_UNSPECIFIED, |
|
dynamic_threshold=0.0 |
|
) |
|
) |
|
) |
|
] |
|
) |
|
) |
|
|
|
|
|
text = response.text |
|
results = json.loads(text) |
|
return format_search_results(results) |