Spaces:
Sleeping
Sleeping
| import os | |
| import openai | |
| # 設定 API 金鑰 | |
| openai.api_key = os.environ.get("OPENAI_API_KEY") | |
| # Chat 模型初始化(不需要 create chat session) | |
| def query(prompt: str) -> str: | |
| """ | |
| 模擬 Google Gemini 的 chat.send_message 功能: | |
| 接收文字 prompt,回傳 GPT 模型生成的回應。 | |
| """ | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=[{"role": "user", "content": prompt}], | |
| temperature=0.7, | |
| max_tokens=1000, | |
| ) | |
| return response.choices[0].message.content.strip() | |
| except Exception as e: | |
| return f"⚠️ GPT 發生錯誤:{e}" | |
| if __name__ == "__main__": | |
| # 測試輸入 | |
| while True: | |
| user_input = input("你:") | |
| reply = query(user_input) | |
| print(f"🤖:{reply}\n") | |