当前位置: 首页 > news >正文

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)

相关文章:

  • vue中excel文件 打包后不展示问题
  • 【云实验】Excel文件转存到RDS数据库
  • PDF 合并测试:性能与内容完整性
  • 确保高质量的音视频通话,如何最大化利用视频带宽
  • android双屏之副屏待机显示图片
  • std::ranges::views::as_const 和 std::ranges::as_const_view
  • 多卡跑ollama run deepseek-r1
  • Android Kotlin权限管理最佳实践
  • 看之前熟悉双亲委派加载机制,看之后了解双亲委派加载机制
  • 最大子树和--树形dp
  • Linux基础第四天
  • fastadmin 数据导出,设置excel行高和限制图片大小
  • 在Excel中使用函数公式时,常见错误对应不同的典型问题
  • Python学习笔记--使用Django操作mysql
  • 一键启动多个 Chrome 实例并自动清理的 Bash 脚本分享!
  • AWS EKS IP 耗尽:原因、解决方案和最佳实践
  • 【AWS入门】AWS身份验证和访问管理(IAM)
  • 【Windows系统】向量数据库Milvus安装教程
  • Win10 安装单机版ES(elasticsearch),整合IK分词器和安装Kibana
  • 鸿蒙PC操作系统:从Linux到自研微内核的蜕变
  • 国家发改委谈整治“内卷式”竞争:加力破除地方保护和市场分割,遏制落后产能无序扩张
  • 张核子“限高”次日即被解除,前员工的执行款3个月后仍未到账
  • 体坛联播|利物浦三轮不胜,孙颖莎/王楚钦晋级混双八强
  • 国家统计局:4月全国规模以上工业增加值同比增长6.1%
  • 西藏普兰县公安局网安大队向自媒体人宣传网络安全知识
  • 上海青少年书法学习园开园:少年以巨笔书写《祖国万岁》