'''
else:
error_msg = result.get('error', '未知错误')
return f'
❌ 错误: {error_msg}
'
@app.route('/api/chat', methods=['POST'])
@app.route('/api/chat', methods=['POST'])
def chat():
user_message = request.form.get('message', '')
def generate():
# Do not yield user message here; frontend handles it.
# Use proxy + baseurl + endpoint concatenation
target_url = f"{Config.BASE_URL.rstrip('/')}/v1/chat/completions"
url = f"{Config.PROXY}{target_url}"
headers = {
"Authorization": f"Bearer {Config.API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": "gemini-3-flash-preview",
"messages": [
{"role": "system", "content": "You are a professional Design Assistant (设计助理) for NoirFlow. Your goal is to help users maintain a consistent, premium, and aesthetic design language in their workflows. You provide advice on color theory, layout, and visual harmony, while also assisting with the technical node configuration when asked. Keep your tone professional, artistic, and concise."},
{"role": "user", "content": user_message}
],
"stream": True
}
try:
with requests.post(url, headers=headers, json=payload, stream=True, timeout=60) as r:
r.raise_for_status()
for line in r.iter_lines():
if line:
decoded_line = line.decode('utf-8')
if decoded_line.startswith('data: '):
json_str = decoded_line[6:]
if json_str == '[DONE]':
break
try:
data = json.loads(json_str)
content = data['choices'][0]['delta'].get('content', '')
if content:
# Basic formatting
formatted_content = content.replace('\n', ' ')
yield formatted_content
except:
pass
except Exception as e:
yield f"[Error: {str(e)}]"
return app.response_class(generate(), mimetype='text/html')
if __name__ == '__main__':
app.run(debug=True, port=5000)