Spaces:
Sleeping
Sleeping
Update services.py
Browse files- services.py +53 -27
services.py
CHANGED
|
@@ -10,35 +10,37 @@ load_dotenv()
|
|
| 10 |
|
| 11 |
class GeminiService:
|
| 12 |
def __init__(self):
|
| 13 |
-
# 從環境變數讀取 Key,兼容本地 .env 與 Hugging Face Secrets
|
| 14 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 15 |
if not api_key:
|
| 16 |
-
# 為了避免佈署時報錯,這裡僅印出警告,讓 UI 層處理
|
| 17 |
print("警告:找不到 GEMINI_API_KEY")
|
| 18 |
|
| 19 |
self.client = genai.Client(api_key=api_key) if api_key else None
|
|
|
|
| 20 |
self.model_id = os.getenv("GEMINI_MODEL_ID", "gemini-2.0-flash")
|
| 21 |
|
| 22 |
def _check_client(self):
|
| 23 |
if not self.client:
|
| 24 |
-
raise ValueError("API Key
|
| 25 |
|
| 26 |
-
def
|
|
|
|
|
|
|
|
|
|
| 27 |
self._check_client()
|
| 28 |
exclusion_prompt = ""
|
| 29 |
if exclude_names:
|
| 30 |
exclusion_prompt = f"IMPORTANT: Do not include: {', '.join(exclude_names)}."
|
| 31 |
|
| 32 |
-
# Phase 1: Search (
|
| 33 |
search_prompt = f"""
|
| 34 |
-
Using Google Search, find 10 prominent
|
| 35 |
|
| 36 |
-
CRITICAL
|
| 37 |
-
1.
|
| 38 |
-
2.
|
| 39 |
{exclusion_prompt}
|
| 40 |
|
| 41 |
-
List them (Name -
|
| 42 |
"""
|
| 43 |
|
| 44 |
search_response = self.client.models.generate_content(
|
|
@@ -50,12 +52,12 @@ class GeminiService:
|
|
| 50 |
)
|
| 51 |
raw_text = search_response.text
|
| 52 |
|
| 53 |
-
# Phase 2: Extract JSON
|
| 54 |
extract_prompt = f"""
|
| 55 |
-
From the text below, extract
|
| 56 |
Calculate a Relevance Score (0-100) based on query: "{query}".
|
| 57 |
|
| 58 |
-
Return ONLY a JSON array: [{{"name": "...", "
|
| 59 |
|
| 60 |
Text:
|
| 61 |
---
|
|
@@ -77,21 +79,45 @@ class GeminiService:
|
|
| 77 |
print(f"JSON Parse Error: {e}")
|
| 78 |
return []
|
| 79 |
|
| 80 |
-
def
|
|
|
|
|
|
|
|
|
|
| 81 |
self._check_client()
|
| 82 |
-
name =
|
| 83 |
-
uni = professor.get('university')
|
| 84 |
-
dept = professor.get('department')
|
| 85 |
-
|
| 86 |
-
prompt = f"""
|
| 87 |
-
Act as an academic consultant. Investigate Professor {name} from {dept} at {uni}.
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
"""
|
| 96 |
|
| 97 |
response = self.client.models.generate_content(
|
|
@@ -119,7 +145,7 @@ class GeminiService:
|
|
| 119 |
|
| 120 |
def chat_with_ai(self, history: List[Dict], new_message: str, context: str) -> str:
|
| 121 |
self._check_client()
|
| 122 |
-
system_instruction = f"
|
| 123 |
|
| 124 |
chat_history = []
|
| 125 |
for h in history:
|
|
|
|
| 10 |
|
| 11 |
class GeminiService:
|
| 12 |
def __init__(self):
|
|
|
|
| 13 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 14 |
if not api_key:
|
|
|
|
| 15 |
print("警告:找不到 GEMINI_API_KEY")
|
| 16 |
|
| 17 |
self.client = genai.Client(api_key=api_key) if api_key else None
|
| 18 |
+
# 建議使用最新模型以獲得最佳分析能力
|
| 19 |
self.model_id = os.getenv("GEMINI_MODEL_ID", "gemini-2.0-flash")
|
| 20 |
|
| 21 |
def _check_client(self):
|
| 22 |
if not self.client:
|
| 23 |
+
raise ValueError("API Key 未設定")
|
| 24 |
|
| 25 |
+
def search_companies(self, query: str, exclude_names: List[str] = []) -> List[Dict]:
|
| 26 |
+
"""
|
| 27 |
+
Step 1: 搜尋台灣公司
|
| 28 |
+
"""
|
| 29 |
self._check_client()
|
| 30 |
exclusion_prompt = ""
|
| 31 |
if exclude_names:
|
| 32 |
exclusion_prompt = f"IMPORTANT: Do not include: {', '.join(exclude_names)}."
|
| 33 |
|
| 34 |
+
# Phase 1: Google Search (廣泛搜尋)
|
| 35 |
search_prompt = f"""
|
| 36 |
+
Using Google Search, find 5 to 10 prominent companies in Taiwan related to the query: "{query}".
|
| 37 |
|
| 38 |
+
**CRITICAL INSTRUCTIONS:**
|
| 39 |
+
1. **TARGET:** Focus on Taiwanese companies (or global companies with a major branch in Taiwan).
|
| 40 |
+
2. **IDENTIFIERS:** Try to find their distinct "Company Name" (e.g., 台積電 / 台灣積體電路製造股份有限公司).
|
| 41 |
{exclusion_prompt}
|
| 42 |
|
| 43 |
+
List them (Full Name - Industry/Sector) in Traditional Chinese.
|
| 44 |
"""
|
| 45 |
|
| 46 |
search_response = self.client.models.generate_content(
|
|
|
|
| 52 |
)
|
| 53 |
raw_text = search_response.text
|
| 54 |
|
| 55 |
+
# Phase 2: Extract JSON (結構化)
|
| 56 |
extract_prompt = f"""
|
| 57 |
+
From the text below, extract company names and their industry.
|
| 58 |
Calculate a Relevance Score (0-100) based on query: "{query}".
|
| 59 |
|
| 60 |
+
Return ONLY a JSON array: [{{"name": "...", "industry": "...", "relevanceScore": 85}}]
|
| 61 |
|
| 62 |
Text:
|
| 63 |
---
|
|
|
|
| 79 |
print(f"JSON Parse Error: {e}")
|
| 80 |
return []
|
| 81 |
|
| 82 |
+
def get_company_details(self, company: Dict) -> Dict:
|
| 83 |
+
"""
|
| 84 |
+
Step 2: 進行商業徵信調查 (Deep Dive)
|
| 85 |
+
"""
|
| 86 |
self._check_client()
|
| 87 |
+
name = company.get('name')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
prompt = f"""
|
| 90 |
+
Act as a professional "Business Due Diligence Analyst" (商業徵信分析師).
|
| 91 |
+
Conduct a comprehensive investigation on the Taiwanese company: "{name}".
|
| 92 |
+
|
| 93 |
+
**Investigation Targets (Must search for these specifically):**
|
| 94 |
+
|
| 95 |
+
1. **Corporate Identity (基本資料)**:
|
| 96 |
+
- Find the **Tax ID (統一編號)**.
|
| 97 |
+
- **Registered Capital (資本額)**.
|
| 98 |
+
- **Representative (代表人)**.
|
| 99 |
+
- **Establishment Date (設立日期)**.
|
| 100 |
+
- *Source Hint: Ministry of Economic Affairs (經濟部商業司), Datagovtw.*
|
| 101 |
+
|
| 102 |
+
2. **Scale & Business (規模與業務)**:
|
| 103 |
+
- **Employee Count**: Estimated number of employees.
|
| 104 |
+
- **Core Products/Services**: What do they actually sell or do?
|
| 105 |
+
- *Source Hint: 104 Job Bank, Company Website, LinkedIn.*
|
| 106 |
+
|
| 107 |
+
3. **Market Reputation & Culture (評價與文化 - KEY PART)**:
|
| 108 |
+
- Search for employee reviews on **PTT (Tech_Job, Soft_Job, Salary)**, **Dcard (Work board)**, **Qollie (求職天眼通)**, or **Google Maps**.
|
| 109 |
+
- Summarize the **Pros** (e.g., high pay, free snacks) and **Cons** (e.g., toxic management, forced overtime, family business style).
|
| 110 |
+
- *Tone:* Be objective but highlight recurring complaints.
|
| 111 |
+
|
| 112 |
+
4. **Legal & Risk Assessment (法律與風險 - CRITICAL)**:
|
| 113 |
+
- Search for keywords: "{name} 判決", "{name} 勞資糾紛", "{name} 違反勞基法", "{name} 詐騙", "{name} 吸金", "{name} 罰款".
|
| 114 |
+
- List any major lawsuits, fines, or controversies found in news or government records.
|
| 115 |
+
- If clean, state "No major public legal disputes found."
|
| 116 |
+
|
| 117 |
+
**Format Requirements**:
|
| 118 |
+
- Structure the output as a clean, readable report using Markdown.
|
| 119 |
+
- Use clear headings.
|
| 120 |
+
- **Language**: Traditional Chinese (繁體中文).
|
| 121 |
"""
|
| 122 |
|
| 123 |
response = self.client.models.generate_content(
|
|
|
|
| 145 |
|
| 146 |
def chat_with_ai(self, history: List[Dict], new_message: str, context: str) -> str:
|
| 147 |
self._check_client()
|
| 148 |
+
system_instruction = f"You are a sharp Business Analyst. Answer based on this due diligence report:\n{context}"
|
| 149 |
|
| 150 |
chat_history = []
|
| 151 |
for h in history:
|