mirror of
https://github.com/kvcache-ai/ktransformers.git
synced 2026-11-05 01:07:28 +00:00
[GH-ISSUE #371] 不支持VSCode Cline生成的提示 #244
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
kvcache-ai/ktransformers#244
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @zhengjx-ai on GitHub (Feb 16, 2025).
Original GitHub issue: https://github.com/kvcache-ai/ktransformers/issues/371
使用Cline配合ktransformers-0.2 main版本server,DeepSeek-V2.5-IQ4_XS模型。
Cline会生成一个大约12K提示的/v1/chat/completion请求(见附件),server直接返回422 Unprocessable Entity。貌似是FastAPI的问题。
api.log
@laf070810 commented on GitHub (Feb 16, 2025):
我在ktransformers v0.2.1跑DeepSeek-R1-Q4_K_M模型时也碰到了这个问题
@DeerEyre commented on GitHub (Feb 18, 2025):
同样遇到了这个问题0.2.1版本
import os
import platform
import sys
import torch
import logging
from transformers import (
AutoTokenizer,
AutoConfig,
AutoModelForCausalLM,
GenerationConfig,
TextStreamer,
)
import json
import fire
from ktransformers.optimize.optimize import optimize_and_load_gguf
from ktransformers.models.modeling_deepseek import DeepseekV2ForCausalLM
from ktransformers.models.modeling_qwen2_moe import Qwen2MoeForCausalLM
from ktransformers.models.modeling_deepseek_v3 import DeepseekV3ForCausalLM
from ktransformers.models.modeling_llama import LlamaForCausalLM
from ktransformers.models.modeling_mixtral import MixtralForCausalLM
from ktransformers.util.utils import prefill_and_generate
from ktransformers.server.config.config import Config
from fastapi import FastAPI
原代码中的配置部分保持不变
project_dir = os.path.dirname(os.path.dirname(file))
sys.path.insert(0, project_dir)
custom_models = {
"DeepseekV2ForCausalLM": DeepseekV2ForCausalLM,
"DeepseekV3ForCausalLM": DeepseekV3ForCausalLM,
"Qwen2MoeForCausalLM": Qwen2MoeForCausalLM,
"LlamaForCausalLM": LlamaForCausalLM,
"MixtralForCausalLM": MixtralForCausalLM,
}
ktransformer_rules_dir = (
os.path.dirname(os.path.abspath(file)) + "/optimize/optimize_rules/"
)
default_optimize_rules = {
"DeepseekV2ForCausalLM": ktransformer_rules_dir + "DeepSeek-V2-Chat.yaml",
"DeepseekV3ForCausalLM": ktransformer_rules_dir + "DeepSeek-V3-Chat.yaml",
"Qwen2MoeForCausalLM": ktransformer_rules_dir + "Qwen2-57B-A14B-Instruct.yaml",
"LlamaForCausalLM": ktransformer_rules_dir + "Internlm2_5-7b-Chat-1m.yaml",
"MixtralForCausalLM": ktransformer_rules_dir + "Mixtral.yaml",
}
定义一个函数来初始化和运行模型
def init_model(
model_path: str,
optimize_rule_path: str = None,
gguf_path: str = None,
max_new_tokens: int = 300,
cpu_infer: int = Config().cpu_infer,
use_cuda_graph: bool = True,
mode: str = "normal",
force_think: bool = False,
):
torch.set_grad_enabled(False)
Config().cpu_infer = cpu_infer
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
创建FastAPI应用
app = FastAPI()
全局初始化模型和分词器
model, tokenizer = None, None
@app.on_event("startup")
async def startup_event():
global model, tokenizer
# 这里填入你的模型路径和gguf路径
model_path = "/data/llm_tuning/dpsk/R1-Q4-config"
gguf_path = "/data/llm_tuning/dpsk/DeepSeek-R1-GGUF"
optimize_rule_path = None
max_new_tokens = 2000
cpu_infer = 65
model, tokenizer = init_model(
model_path,
optimize_rule_path,
gguf_path,
max_new_tokens,
cpu_infer,
)
@app.post("/generate")
async def chat(prompt: str):
messages = [{"role": "user", "content": prompt}]
input_tensor = tokenizer.apply_chat_template(
messages, add_generation_prompt=True, return_tensors="pt"
)
max_new_tokens = 2000
use_cuda_graph = True
mode = "normal"
force_think = False
generated = prefill_and_generate(
model, tokenizer, input_tensor.cuda(), max_new_tokens, use_cuda_graph, mode, force_think
)
result = tokenizer.decode(generated[0], skip_special_tokens=True)
return {"result": result}
if name == "main":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=50711)
@git-shan commented on GitHub (Mar 13, 2025):
0.2.3 问题还在
:51735 - "POST /v1/chat/completions HTTP/1.1" 422 Unprocessable Entity
@knilink commented on GitHub (Apr 15, 2025):
应该是API兼容性问题,试试开启兼容Legacy API
新格式应该是为了图片支持messages.content从
string改成了{type: "text", text: string}similar issue https://github.com/kvcache-ai/ktransformers/issues/861