Spaces:
Sleeping
Sleeping
Upload app.py
Browse files113 unmodified lines1141151161171171181191204 unmodified lines12512612712812812913013122 unmodified lines1541551561571571581591603 unmodified lines164165166167168169170171113 unmodified lines############################################
# 呼叫模型 API (OpenAI / Claude)
############################################
def call_openai_api(prompt: str, model_name: str = "gpt-3.5-turbo") -> str:
def call_openai_api(prompt: str, model_name: str) -> str:
"""參考新版 usage: from openai import OpenAI -> client.chat.completions.create()"""
if not client.api_key:
return "[OpenAI API Key 未設定,無法呼叫 API]"4 unmodified lines messages=[{"role": "user", "content": prompt}],
model=model_name,
temperature=0.7,
max_tokens=1024,
max_completion_tokens=2048,
)
return chat_completion.choices[0].message.content
except Exception as e:22 unmodified lines messages=[
{"role": "user", "content": prompt}
],
max_tokens=1024,
max_tokens=2048,
temperature=0.7,
)
text = response.content3 unmodified lines # 移除或替換 TextBlock(...) 的外層結構,只保留其中文字
import re
text = re.sub(r"TextBlock\(.*?text='(.*?)'.*?\)", r"\1", text, flags=re.DOTALL)
# 先將原本的 "\n"(backslash-n) 字串轉成真正的換行符
text = text.replace('\\n', '\n')
return text.strip()
except Exception as e:
return f"[Claude API 呼叫失敗]: {str(e)}"app.py+5-3