Spaces:
Running
Running
File size: 1,098 Bytes
e2a3751 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import os
from openai import OpenAI
import time
# 修正 1:將 BASE_URL 設為官方地址
BASE_URL = "https://hsuwill000-llamacpp-webui01.hf.space/v1"
MODEL_ID = "LFM2-8B-A1B-UD-Q4_K_XL" # 官方 API 需要指定模型
TIMEOUT_SECONDS = 60
try:
# 修正 2:使用同步客戶端 (OpenAI)
client = OpenAI(
base_url=BASE_URL,
api_key="sk-local", # llama.cpp 不檢查內容,只要有就行
timeout=600
)
start_time = time.time()
response = client.chat.completions.create(
model=MODEL_ID,
messages=[
{"role": "system", "content": "You are a helpful and brief assistant."},
{"role": "user", "content": "hello"}
]
)
end_time = time.time()
# 輸出結果
response_content = response.choices[0].message.content
print("\n--- 🤖 模型回覆 ---")
print(f"✅ 請求成功!耗時: {end_time - start_time:.2f} 秒")
print(response_content.strip())
print("--------------------")
except Exception as e:
print(f"\n❌ 發生錯誤: {type(e).__name__}: {e}") |