From 120527b3ee10550f89200209509c740047d6a9ca Mon Sep 17 00:00:00 2001 From: KasaNick Date: Sat, 24 Jan 2026 17:45:36 -0500 Subject: [PATCH] Gemeni Prompt --- backend/src/gemini/__init__.py | 63 ++++++++++++++------------------- backend/src/gemini/client.py | 18 ++++------ backend/src/gemini/reporting.py | 0 3 files changed, 33 insertions(+), 48 deletions(-) create mode 100644 backend/src/gemini/reporting.py diff --git a/backend/src/gemini/__init__.py b/backend/src/gemini/__init__.py index aa52ae2..aa2b5c5 100644 --- a/backend/src/gemini/__init__.py +++ b/backend/src/gemini/__init__.py @@ -1,6 +1,6 @@ -from src.rag.embeddings import get_embedding +import os +from google import genai from src.chroma.vector_store import search_documents -from .client import generate_content GREENWASHING_ANALYSIS_PROMPT = """ You are an expert Environmental, Social, and Governance (ESG) Analyst specialized in detecting 'Greenwashing'. @@ -18,40 +18,31 @@ Based on the context provided, give a final verdict: - VERDICT: [Clear/Suspect/High Risk of Greenwashing] - REASONING: [Explain your findings clearly] - EVIDENCE: [Quote specific parts of the context if possible] -- BETTER ALTERNATIVES: [Suggest 2-3 similar companies or products that have verified sustainability records or higher transparency in this specific area] """ -def analyze_greenwashing(query, category=None, num_results=10): - try: - query_embedding = get_embedding(query) - - filter_metadata = None - if category: - filter_metadata = {"category": category} - - search_results = search_documents( - query_embedding, - num_results=num_results, - filter_metadata=filter_metadata - ) - - context = "" - if search_results: - context = "--- START OF REPORT CONTEXT ---\n" - for res in search_results: - context += f"RELEVANT DATA CHUNK: {res['text']}\n\n" - context += "--- END OF REPORT CONTEXT ---\n" - - if context: - full_prompt = f"{GREENWASHING_ANALYSIS_PROMPT}\n\n{context}\n\nUSER QUERY/COMPANY FOCUS: {query}" - else: - return "No data found in the report to analyze. Please upload a report first." - - response = generate_content(full_prompt) - return response - - except Exception as e: - return f"Error in Analysis process: {str(e)}" +def ask(prompt): + client = genai.Client(api_key=os.environ.get("GOOGLE_API_KEY")) + return client.models.generate_content(model="gemini-2.0-flash-exp", contents=prompt).text -def ask_gemini_with_rag(query, category=None, num_results=5): - return analyze_greenwashing(query, category, num_results) +def analyze(query, query_embedding, num_results=5, num_alternatives=3): + results = search_documents(query_embedding, num_results=num_results + num_alternatives + 5) + + context = "--- START OF REPORT CONTEXT ---\n" + for res in results[:num_results]: + context += f"RELEVANT DATA CHUNK: {res['text']}\n\n" + context += "--- END OF REPORT CONTEXT ---\n" + + full_prompt = f"{GREENWASHING_ANALYSIS_PROMPT}\n\n{context}\n\nUSER QUERY/COMPANY FOCUS: {query}" + analysis_text = ask(full_prompt) + + alternatives = [] + seen_texts = set() + for res in results[num_results:]: + text_preview = res['text'][:200] + if text_preview not in seen_texts: + seen_texts.add(text_preview) + alternatives.append({"text": res['text'], "score": res.get('score'), "summary": text_preview}) + if len(alternatives) >= num_alternatives: + break + + return {"analysis": analysis_text, "alternatives": alternatives} diff --git a/backend/src/gemini/client.py b/backend/src/gemini/client.py index fef0733..29193a4 100644 --- a/backend/src/gemini/client.py +++ b/backend/src/gemini/client.py @@ -3,15 +3,9 @@ import os def generate_content(prompt, model_name="gemini-2.0-flash-exp"): api_key = os.environ.get("GOOGLE_API_KEY") - if not api_key: - return "Error: GOOGLE_API_KEY not found." - - try: - client = genai.Client(api_key=api_key) - response = client.models.generate_content( - model=model_name, - contents=prompt, - ) - return response.text - except Exception as e: - return f"Error interacting with Gemini API: {str(e)}" + client = genai.Client(api_key=api_key) + response = client.models.generate_content( + model=model_name, + contents=prompt, + ) + return response.text diff --git a/backend/src/gemini/reporting.py b/backend/src/gemini/reporting.py new file mode 100644 index 0000000..e69de29