fix(gtm): Replace risky ternary operators in f-strings with explicit if/else blocks to fix SyntaxError
This commit is contained in:
@@ -51,15 +51,20 @@ Das Projekt nutzt ein geteiltes `helpers.py`, das auf der alten OpenAI Python Li
|
|||||||
openai==0.28.1
|
openai==0.28.1
|
||||||
# weitere deps...
|
# weitere deps...
|
||||||
### 1.5 Python Syntax & F-Strings
|
### 1.5 Python Syntax & F-Strings
|
||||||
Multi-Line f-Strings mit JSON-Beispielen (`{...}`) sind extrem fehleranfällig (Escaping, Einrückung).
|
Multi-Line f-Strings sind in Docker-Umgebungen fehleranfällig.
|
||||||
* **Best Practice:** Komplexe JSON-Daten oder Strings **vorher** in Variablen speichern und dann einfügen.
|
* **Vermeide:** Ternary Operators (`... if x else ...`) innerhalb von Multi-Line f-Strings. Dies führt oft zu `SyntaxError: invalid decimal literal`.
|
||||||
|
* **Best Practice:** Nutze explizite `if/else` Blöcke zur Definition des Prompts.
|
||||||
|
* **Vermeide:** Verschachtelte JSON-Dumps (`{...}`) direkt im f-String. Nutze vorher definierte Variablen.
|
||||||
```python
|
```python
|
||||||
# Schlecht:
|
# Schlecht:
|
||||||
prompt = f"""Daten: {json.dumps(data.get('nested'))}"""
|
prompt = f"""Daten: {json.dumps(data)}""" if lang=='de' else f"""Data: ..."""
|
||||||
|
|
||||||
# Gut:
|
# Gut:
|
||||||
nested_json = json.dumps(data.get('nested'))
|
json_data = json.dumps(data)
|
||||||
prompt = f"""Daten: {nested_json}"""
|
if lang == 'de':
|
||||||
|
prompt = f"""Daten: {json_data}"""
|
||||||
|
else:
|
||||||
|
prompt = f"""Data: {json_data}"""
|
||||||
```
|
```
|
||||||
* **Signaturen prüfen:** Shared Libraries (`helpers.py`) haben oft ältere Signaturen.
|
* **Signaturen prüfen:** Shared Libraries (`helpers.py`) haben oft ältere Signaturen.
|
||||||
* Beispiel: `call_openai_chat` unterstützt oft kein `system_message` Argument. Stattdessen Prompt manuell zusammenbauen (`sys_instr + "\n\n" + prompt`).
|
* Beispiel: `call_openai_chat` unterstützt oft kein `system_message` Argument. Stattdessen Prompt manuell zusammenbauen (`sys_instr + "\n\n" + prompt`).
|
||||||
|
|||||||
@@ -131,7 +131,8 @@ def analyze_product(data):
|
|||||||
lang = data.get('language', 'de')
|
lang = data.get('language', 'de')
|
||||||
sys_instr = get_system_instruction(lang)
|
sys_instr = get_system_instruction(lang)
|
||||||
|
|
||||||
extraction_prompt = f"""
|
if lang == 'en':
|
||||||
|
extraction_prompt = f"""
|
||||||
PHASE 1-A: TECHNICAL EXTRACTION
|
PHASE 1-A: TECHNICAL EXTRACTION
|
||||||
Input Product Description: "{product_input[:25000]}..."
|
Input Product Description: "{product_input[:25000]}..."
|
||||||
|
|
||||||
@@ -141,7 +142,9 @@ Task:
|
|||||||
3. Create a short raw analysis summary.
|
3. Create a short raw analysis summary.
|
||||||
|
|
||||||
Output JSON format ONLY.
|
Output JSON format ONLY.
|
||||||
""" if lang == 'en' else f"""
|
"""
|
||||||
|
else:
|
||||||
|
extraction_prompt = f"""
|
||||||
PHASE 1-A: TECHNICAL EXTRACTION
|
PHASE 1-A: TECHNICAL EXTRACTION
|
||||||
Input Product Description: "{product_input[:25000]}..."
|
Input Product Description: "{product_input[:25000]}..."
|
||||||
|
|
||||||
@@ -152,15 +155,18 @@ Aufgabe:
|
|||||||
|
|
||||||
Output JSON format ONLY.
|
Output JSON format ONLY.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Fix: Prepend system instruction manually
|
# Fix: Prepend system instruction manually
|
||||||
full_extraction_prompt = sys_instr + "\n\n" + extraction_prompt
|
full_extraction_prompt = sys_instr + "\n\n" + extraction_prompt
|
||||||
|
# Using response_format_json=True since helpers.py supports it (for logging/prompt hint)
|
||||||
extraction_response = call_openai_chat(full_extraction_prompt, response_format_json=True)
|
extraction_response = call_openai_chat(full_extraction_prompt, response_format_json=True)
|
||||||
extraction_data = json.loads(extraction_response)
|
extraction_data = json.loads(extraction_response)
|
||||||
|
|
||||||
features_json = json.dumps(extraction_data.get('features'))
|
features_json = json.dumps(extraction_data.get('features'))
|
||||||
constraints_json = json.dumps(extraction_data.get('constraints'))
|
constraints_json = json.dumps(extraction_data.get('constraints'))
|
||||||
|
|
||||||
conflict_prompt = f"""
|
if lang == 'en':
|
||||||
|
conflict_prompt = f"""
|
||||||
PHASE 1-B: PORTFOLIO CONFLICT CHECK
|
PHASE 1-B: PORTFOLIO CONFLICT CHECK
|
||||||
|
|
||||||
New Product Features: {features_json}
|
New Product Features: {features_json}
|
||||||
@@ -174,7 +180,9 @@ Task:
|
|||||||
Check if the new product overlaps significantly with existing ones (is it just a clone?).
|
Check if the new product overlaps significantly with existing ones (is it just a clone?).
|
||||||
|
|
||||||
Output JSON format ONLY.
|
Output JSON format ONLY.
|
||||||
""" if lang == 'en' else f"""
|
"""
|
||||||
|
else:
|
||||||
|
conflict_prompt = f"""
|
||||||
PHASE 1-B: PORTFOLIO CONFLICT CHECK
|
PHASE 1-B: PORTFOLIO CONFLICT CHECK
|
||||||
|
|
||||||
Neue Produkt-Features: {features_json}
|
Neue Produkt-Features: {features_json}
|
||||||
@@ -189,6 +197,7 @@ Prüfe, ob das neue Produkt signifikant mit bestehenden Produkten überlappt (Is
|
|||||||
|
|
||||||
Output JSON format ONLY.
|
Output JSON format ONLY.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Fix: Prepend system instruction manually
|
# Fix: Prepend system instruction manually
|
||||||
full_conflict_prompt = sys_instr + "\n\n" + conflict_prompt
|
full_conflict_prompt = sys_instr + "\n\n" + conflict_prompt
|
||||||
conflict_response = call_openai_chat(full_conflict_prompt, response_format_json=True)
|
conflict_response = call_openai_chat(full_conflict_prompt, response_format_json=True)
|
||||||
@@ -205,7 +214,8 @@ def discover_icps(data):
|
|||||||
features_json = json.dumps(phase1_result.get('features'))
|
features_json = json.dumps(phase1_result.get('features'))
|
||||||
constraints_json = json.dumps(phase1_result.get('constraints'))
|
constraints_json = json.dumps(phase1_result.get('constraints'))
|
||||||
|
|
||||||
prompt = f"""
|
if lang == 'en':
|
||||||
|
prompt = f"""
|
||||||
PHASE 2: ICP DISCOVERY & DATA PROXIES
|
PHASE 2: ICP DISCOVERY & DATA PROXIES
|
||||||
Based on the product features: {features_json}
|
Based on the product features: {features_json}
|
||||||
And constraints: {constraints_json}
|
And constraints: {constraints_json}
|
||||||
@@ -215,7 +225,9 @@ Output JSON format ONLY:
|
|||||||
"icps": [ {{ "name": "Industry Name", "rationale": "Rationale" }} ],
|
"icps": [ {{ "name": "Industry Name", "rationale": "Rationale" }} ],
|
||||||
"dataProxies": [ {{ "target": "Criteria", "method": "How" }} ]
|
"dataProxies": [ {{ "target": "Criteria", "method": "How" }} ]
|
||||||
}}
|
}}
|
||||||
""" if lang == 'en' else f"""
|
"""
|
||||||
|
else:
|
||||||
|
prompt = f"""
|
||||||
PHASE 2: ICP DISCOVERY & DATA PROXIES
|
PHASE 2: ICP DISCOVERY & DATA PROXIES
|
||||||
Basierend auf Features: {features_json}
|
Basierend auf Features: {features_json}
|
||||||
Und Constraints: {constraints_json}
|
Und Constraints: {constraints_json}
|
||||||
@@ -226,6 +238,7 @@ Output JSON format ONLY:
|
|||||||
"dataProxies": [ {{ "target": "Kriterium", "method": "Methode" }} ]
|
"dataProxies": [ {{ "target": "Kriterium", "method": "Methode" }} ]
|
||||||
}}
|
}}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Fix: Prepend system instruction manually
|
# Fix: Prepend system instruction manually
|
||||||
full_prompt = sys_instr + "\n\n" + prompt
|
full_prompt = sys_instr + "\n\n" + prompt
|
||||||
response = call_openai_chat(full_prompt, response_format_json=True)
|
response = call_openai_chat(full_prompt, response_format_json=True)
|
||||||
@@ -283,11 +296,11 @@ def main():
|
|||||||
try:
|
try:
|
||||||
data = json.loads(sys.stdin.read())
|
data = json.loads(sys.stdin.read())
|
||||||
except:
|
except:
|
||||||
data = {}
|
data = {{}}
|
||||||
else:
|
else:
|
||||||
data = {}
|
data = {{}}
|
||||||
|
|
||||||
modes = {
|
modes = {{
|
||||||
"analyze_product": analyze_product,
|
"analyze_product": analyze_product,
|
||||||
"discover_icps": discover_icps,
|
"discover_icps": discover_icps,
|
||||||
"hunt_whales": hunt_whales,
|
"hunt_whales": hunt_whales,
|
||||||
|
|||||||
Reference in New Issue
Block a user