Spaces:
Sleeping
Sleeping
Update services.py
Browse files- services.py +29 -37
services.py
CHANGED
|
@@ -15,7 +15,6 @@ class GeminiService:
|
|
| 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):
|
|
@@ -24,23 +23,25 @@ class GeminiService:
|
|
| 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 |
-
**
|
| 39 |
-
1. **
|
| 40 |
-
2. **
|
|
|
|
| 41 |
{exclusion_prompt}
|
| 42 |
|
| 43 |
-
List them (Full Name - Industry/
|
| 44 |
"""
|
| 45 |
|
| 46 |
search_response = self.client.models.generate_content(
|
|
@@ -54,7 +55,7 @@ class GeminiService:
|
|
| 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}}]
|
|
@@ -87,37 +88,29 @@ class GeminiService:
|
|
| 87 |
name = company.get('name')
|
| 88 |
|
| 89 |
prompt = f"""
|
| 90 |
-
Act as a professional "Business
|
| 91 |
Conduct a comprehensive investigation on the Taiwanese company: "{name}".
|
| 92 |
|
| 93 |
-
**Investigation Targets
|
| 94 |
|
| 95 |
-
1. **
|
| 96 |
-
-
|
| 97 |
-
- **Registered Capital (資本額)**.
|
| 98 |
- **Representative (代表人)**.
|
| 99 |
-
- **
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
- **
|
| 104 |
-
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 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(
|
|
@@ -135,7 +128,6 @@ class GeminiService:
|
|
| 135 |
if chunk.web and chunk.web.uri and chunk.web.title:
|
| 136 |
sources.append({"title": chunk.web.title, "uri": chunk.web.uri})
|
| 137 |
|
| 138 |
-
# Deduplicate
|
| 139 |
unique_sources = {v['uri']: v for v in sources}.values()
|
| 140 |
|
| 141 |
return {
|
|
@@ -145,7 +137,7 @@ class GeminiService:
|
|
| 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
|
| 149 |
|
| 150 |
chat_history = []
|
| 151 |
for h in history:
|
|
|
|
| 15 |
print("警告:找不到 GEMINI_API_KEY")
|
| 16 |
|
| 17 |
self.client = genai.Client(api_key=api_key) if api_key else None
|
|
|
|
| 18 |
self.model_id = os.getenv("GEMINI_MODEL_ID", "gemini-2.0-flash")
|
| 19 |
|
| 20 |
def _check_client(self):
|
|
|
|
| 23 |
|
| 24 |
def search_companies(self, query: str, exclude_names: List[str] = []) -> List[Dict]:
|
| 25 |
"""
|
| 26 |
+
Step 1: 領域探索 -> 公司列表
|
| 27 |
"""
|
| 28 |
self._check_client()
|
| 29 |
exclusion_prompt = ""
|
| 30 |
if exclude_names:
|
| 31 |
exclusion_prompt = f"IMPORTANT: Do not include: {', '.join(exclude_names)}."
|
| 32 |
|
| 33 |
+
# Phase 1: Google Search (廣泛探索)
|
| 34 |
+
# 這裡的 Prompt 強調:如果使用者輸入的是「領域(如: AI)」,請列出該領域的台灣代表性公司。
|
| 35 |
search_prompt = f"""
|
| 36 |
Using Google Search, find 5 to 10 prominent companies in Taiwan related to the query: "{query}".
|
| 37 |
|
| 38 |
+
**Instructions:**
|
| 39 |
+
1. **Domain Search:** If "{query}" is an industry or technology (e.g., "AI", "Green Energy"), list the top representative Taiwanese companies in this field.
|
| 40 |
+
2. **Company Search:** If "{query}" is a specific name, list that company and its direct competitors.
|
| 41 |
+
3. **Target:** Focus on Taiwanese companies (or global companies with major R&D in Taiwan).
|
| 42 |
{exclusion_prompt}
|
| 43 |
|
| 44 |
+
List them (Full Name - Industry/Main Product) in Traditional Chinese.
|
| 45 |
"""
|
| 46 |
|
| 47 |
search_response = self.client.models.generate_content(
|
|
|
|
| 55 |
|
| 56 |
# Phase 2: Extract JSON (結構化)
|
| 57 |
extract_prompt = f"""
|
| 58 |
+
From the text below, extract company names and their industry/main product.
|
| 59 |
Calculate a Relevance Score (0-100) based on query: "{query}".
|
| 60 |
|
| 61 |
Return ONLY a JSON array: [{{"name": "...", "industry": "...", "relevanceScore": 85}}]
|
|
|
|
| 88 |
name = company.get('name')
|
| 89 |
|
| 90 |
prompt = f"""
|
| 91 |
+
Act as a professional "Business Analyst & Investigator".
|
| 92 |
Conduct a comprehensive investigation on the Taiwanese company: "{name}".
|
| 93 |
|
| 94 |
+
**Investigation Targets:**
|
| 95 |
|
| 96 |
+
1. **Overview (基本盤)**:
|
| 97 |
+
- **Tax ID (統編)** & **Capital (資本額)**. (Try to find specific numbers)
|
|
|
|
| 98 |
- **Representative (代表人)**.
|
| 99 |
+
- **Core Business**: What specific problem do they solve? What is their "Ace" product?
|
| 100 |
+
|
| 101 |
+
2. **Workforce & Culture (內部情報)**:
|
| 102 |
+
- **Employee Count**.
|
| 103 |
+
- **Reviews/Gossip**: Search **PTT (Tech_Job, Soft_Job)**, **Dcard**, **Qollie**.
|
| 104 |
+
- Summarize the *REAL* work vibe (e.g., "Good for juniors but low ceiling", "Free snacks but forced overtime").
|
| 105 |
+
|
| 106 |
+
3. **Legal & Risks (排雷專區)**:
|
| 107 |
+
- Search: "{name} 勞資糾紛", "{name} 違反勞基法", "{name} 判決", "{name} 罰款".
|
| 108 |
+
- List any red flags found in government records or news.
|
| 109 |
+
|
| 110 |
+
**Format**:
|
| 111 |
+
- Use Markdown.
|
| 112 |
+
- Language: Traditional Chinese (繁體中文).
|
| 113 |
+
- Be objective but don't sugarcoat potential risks.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
"""
|
| 115 |
|
| 116 |
response = self.client.models.generate_content(
|
|
|
|
| 128 |
if chunk.web and chunk.web.uri and chunk.web.title:
|
| 129 |
sources.append({"title": chunk.web.title, "uri": chunk.web.uri})
|
| 130 |
|
|
|
|
| 131 |
unique_sources = {v['uri']: v for v in sources}.values()
|
| 132 |
|
| 133 |
return {
|
|
|
|
| 137 |
|
| 138 |
def chat_with_ai(self, history: List[Dict], new_message: str, context: str) -> str:
|
| 139 |
self._check_client()
|
| 140 |
+
system_instruction = f"You are an expert Business Consultant. Answer based on this company report:\n{context}"
|
| 141 |
|
| 142 |
chat_history = []
|
| 143 |
for h in history:
|