当前位置: 首页 > 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)
http://www.xdnf.cn/news/534421.html

相关文章:

  • vue2.0 组件生命周期
  • LLaMA-Factory:了解webUI参数
  • Mysql 刷题 day06
  • Image and depth from a conventional camera with a coded aperture论文阅读
  • “保证医疗器械信息来源合法 真实、安全的保障措施、情况说明及相关证明”模板
  • 滑动窗口算法详解:从理论到实战(LeetCode 3 438)
  • 自动化测试的框架有哪些?原理是什么?
  • 深入掌握MyBatis:连接池、动态SQL、多表查询与缓存
  • springboot+mybatis或mybatisplus在进行%name%的前后模糊查询时如何放防止sql注入
  • 汇川MD810-20M4110GXXX变频器为什么要加GRJ9000S电源滤波器?
  • C# 深入理解类(属性)
  • python打卡day30
  • Navicat连接开启sm3认证的瀚高数据库
  • 网络请求和状态管理
  • SAP学习笔记 - 开发13 - CAP 之 添加数据库支持(Sqlite)
  • 《虚实共生:双向映射重塑具身智能决策逻辑》
  • 5.19 打卡
  • 存储系统02——Libevent事件循环
  • Interrupt 2025 大会回顾:关于LangChain 的 AI Agent会议内容总结
  • anythingLLM支持本地大模型嵌入知识库后进行api调用
  • Linux 系统异常触发后自动重启配置指南
  • 深入解析PyTorch中MultiheadAttention的参数key_padding_mask与attn_mask
  • 【AI时代】Java程序员大模型应用开发详细教程(上)
  • ALTER AGGREGATE使用场景
  • Pod 节点数量
  • 【Game】Powerful——Punch and Kick(12)
  • 阿里世界偏好模型:WorldPM-72B论文速读
  • LangChain框架核心技术:从链式工作流到结构化输出的全栈指南
  • Spring的后置处理器是干什么用的?扩展点又是什么?
  • 数据结构学习笔记—初识数据结构