Ktransformers0.3框架的api访问接口程序
Ktransformers0.3框架通过接口访问容易报错报错内容是:
AttributeError: ‘DeepseekV3ForCausalLM’ object has no attribute ‘_get_logits_warper’
解决方案:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple transformers==4.43.2
#!/usr/bin/env python3
"""
简单KTransformers客户端 - 用于调用ktransformers模型的简易脚本
"""import requests
import json
import sys# 服务器配置
BASE_URL = "http://192.168.3.137:10002/v1"
HEADERS = {"Content-Type": "application/json","Authorization": "Bearer dummy-token"
}
MODEL = "deepseek-r1" # 替换为实际模型名称def call_model(prompt, system_message="你是一个有帮助的助手。", stream=True):"""调用模型并获取回复参数:prompt: 用户提问内容system_message: 系统提示信息stream: 是否使用流式输出返回:模型回复内容"""url = f"{BASE_URL}/chat/completions"# 构建消息messages = [{"role": "system", "content": system_message},{"role": "user", "content": prompt}]# 构建请求payload = {"model": MODEL,"messages": messages,"stream": stream}try:print(f"发送请求到: {url}")response = requests.post(url,headers=HEADERS,json=payload,stream=stream,timeout=60 # 增加超时时间)# 检查响应状态if response.status_code != 200:print(f"错误: {response.status_code}")print(f"响应内容: {response.text}")return None# 处理流式响应if stream:full_response = ""for line in response.iter_lines():if line:line = line.decode('utf-8')if line.startswith("data:") and line != "data: [DONE]":try:# 删除 "data: " 前缀并解析JSONjson_str = line[5:].strip()if json_str:data = json.loads(json_str)# 判断结构并提取内容if "choices" in data and len(data["choices"]) > 0:choice = data["choices"][0]if "delta" in choice and "content" in choice["delta"]:content = choice["delta"]["content"]print(content, end="", flush=True)full_response += contentexcept json.JSONDecodeError:passelif line == "data: [DONE]":print() # 结束时换行return full_responseelse:# 非流式输出try:result = response.json()if "choices" in result and len(result["choices"]) > 0:response_text = result["choices"][0]["message"]["content"]print(response_text)return response_textelse:print("未找到回复内容")return str(result)except Exception as e:print(f"解析响应时出错: {str(e)}")print(f"原始响应: {response.text}")return response.textexcept Exception as e:print(f"发生错误: {str(e)}")return Noneif __name__ == "__main__":# 获取用户输入或使用默认值if len(sys.argv) > 1 and sys.argv[1] != "--stream":prompt = sys.argv[1]else:prompt = "你好,你是谁?"# 调用模型 (默认使用流式输出)call_model(prompt)