This commit is contained in:
hailin 2025-07-25 16:05:44 +08:00
parent 2e621b202d
commit 222c46ef15
1 changed files with 8 additions and 5 deletions

View File

@ -1,14 +1,15 @@
import gradio as gr
import requests
API_URL = "http://localhost:30000/v1/completions"
API_URL = "http://localhost:30000/generate" # ✅ 使用原生 generate 接口
API_KEY = "token-abc123"
MODEL_NAME = "Qwen3-14b-base"
# 🚫 不再格式化拼接历史,只取最后一条用户输入
# 🚫 不再拼接上下文,只保留用户当前输入
def build_prompt(history, user_message):
return user_message # 原样作为 prompt 送入模型
return user_message
# 主对话函数
def chat(user_message, history, max_tokens, temperature):
prompt = build_prompt(history, user_message)
@ -26,12 +27,13 @@ def chat(user_message, history, max_tokens, temperature):
try:
response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
result = response.json()
reply = result["choices"][0]["text"].strip()
reply = result["text"].strip() # ✅ /generate 接口返回字段是 text
except Exception as e:
reply = f"[请求失败] {e}"
return reply
# 测试 API 连通性
def test_api_connection(max_tokens, temperature):
headers = {
"Authorization": f"Bearer {API_KEY}",
@ -46,11 +48,12 @@ def test_api_connection(max_tokens, temperature):
try:
resp = requests.post(API_URL, headers=headers, json=payload, timeout=10)
out = resp.json()["choices"][0]["text"].strip()
out = resp.json()["text"].strip() # ✅ 修改这里
return f"✅ API 可用,响应: {out}"
except Exception as e:
return f"❌ API 请求失败: {e}"
# Gradio 界面
with gr.Blocks(title="Base 模型测试 UI") as demo:
gr.Markdown("# 💬 Base 模型对话界面")