deepseek-recipe README

项目的 README,来自 github.com/deepseek-ai/deepseek-recipe。合并为一个 markdown 文件:README.md。

DeepSeek

deepseek-recipe

English | 中文

deepseek-recipe is a collection of Rust libraries and Python bindings that uniformly convert API requests in different formats into the Conversation format, render conversations into prompts for DeepSeek models, encode conversations into token IDs with an attached tokenizer, and convert model output into responses in the corresponding format. Use these components to connect an inference backend to API services that support multiple formats. The caller must provide model inference, tool execution, and HTTP transport.

Getting started · Streaming response · Use with tokenizer · License

Supported scope

Using deepseek-recipe

The examples below convert a Chat Completions request into a DeepSeek V4.1 prompt. The library resolves the model version from the request's model name and accepts DeepSeek-V4-Pro and DeepSeek-V4.1-Flash by default; register other names, such as deepseek-flash, with ConversionOptions::with_model_alias.

Python

Installation

Python 3.10+:

python3 -m pip install deepseek-recipe

Example

from deepseek_recipe import ChatCompletionRequest, ConversionOptions, DeepseekV41Encoding

request = ChatCompletionRequest({
    "model": "DeepSeek-V4.1-Flash",
    "messages": [{"role": "user", "content": "Hello"}],
})
converted = request.convert(ConversionOptions())
rendered = DeepseekV41Encoding().render_conversation(converted.conversation)
print(rendered.prompt)

Rust

Installation

cargo add [email protected] [email protected] serde_json@1

See the development guide for source builds and image dependencies.

Example

use deepseek_recipe::openai::ChatCompletionRequest;
use deepseek_recipe::request::{ConversionOptions, ProtocolRequest};
use deepseek_recipe_encoding::PromptEncoding;
use deepseek_recipe_encoding::v4::dsv41::DeepseekV41Encoding;
use serde_json::json;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let request: ChatCompletionRequest = serde_json::from_value(json!({
        "model": "DeepSeek-V4.1-Flash",
        "messages": [{"role": "user", "content": "Hello"}],
    }))?;
    let converted = request.convert(ConversionOptions::default())?;
    let rendered = DeepseekV41Encoding::new().render_conversation(&converted.conversation);
    println!("{}", rendered.prompt);
    Ok(())
}

Encoding & Decoding Demo

Run the demo from the repository root to render prompts from requests and parse model output into protocol responses:

cargo run -p encoding-decoding-demo --locked

Open http://127.0.0.1:7778.

More examples

Rust and Python both support converting model output into streaming responses. See the Streaming response. To encode conversations into token IDs or decode backend token IDs, see use with tokenizer.

Packages and example projects

Package Purpose
deepseek-recipe Protocol conversion and model output parsing.
deepseek-recipe-core Shared conversation, message, image, and tool types.
deepseek-recipe-encoding DeepSeek V4 and V4.1 prompt rendering and token encoding.
deepseek-recipe-image Image fetching and preprocessing.
deepseek-recipe-macros The prompt! macro used by prompt rendering.
deepseek-recipe-python Python bindings, imported as deepseek_recipe.
encoding-decoding-demo A web interface that renders prompts, highlights special tokens, and parses complete model output into Chat Completions, Responses, or Messages responses.
server-rs An Axum API example with mock inference.
server-py A FastAPI example with mock inference.

License

Project code and public documentation are licensed under the MIT License. Bundled tokenizer notices are in static/tokenizers/README.md.

deepseek-recipe (deepseek-ai/deepseek-recipe)

正在搜索数据库…

这是什么?deepseek-recipe 是一组 Rust 库(附带 Python 绑定),可以把不同格式的 API 请求转换为 DeepSeek 内部的 Conversation 格式,再将其渲染成提示词、编码为 token ID,并把模型输出转换回与原始请求格式一致的响应。
它是一个库,还是完整的推理服务器?它是一个可以嵌入你自己服务中的库。它只负责协议转换和提示词渲染;模型推理、工具执行和 HTTP 传输仍需调用方自行提供。
如何把它添加到项目中?可以使用 Python 包(需要 Python 3.10+),也可以通过 Cargo 添加 Rust crates;两种方式都能把 Chat Completions 请求转换为 DeepSeek V4 或 V4.1 的提示词。此外还有一个 encoding-decoding-demo 网页界面,无需写代码即可体验提示词渲染和输出解析。
要真正运行推理,我还需要自己准备什么?你自己的推理后端、工具执行和 HTTP 传输。本库只负责协议转换和提示词渲染,并不运行模型。
每个 DeepSeek 模型都支持图片吗?不支持。图片仅针对 DeepSeek V4.1 使用 OpenCV 进行预处理;DeepSeek V4 完全不接受图片,因此转换时会将每张图片替换为文本 '[Unsupported Image]'。
它可以在哪些请求格式之间转换?可在 Messages、Chat Completions 和 Responses 格式之间相互转换,同时支持流式响应和完整响应。
它是否支持工具调用和结构化输出?支持。包括函数工具(以及 Responses 的工具命名空间(tool namespaces)和 apply_patch 自定义工具),还支持通过 JSON Schema 或正则表达式响应格式实现的结构化输出,这些输出会经过校验后注入提示词。
它能否报告 token 用量和对数概率?如果你附加了分词器(tokenizer),它可以统计推理 token 数,并输出 Chat Completions/Responses 的对数概率字段;但它本身从不计算概率,这些值必须由后端提供。
各个子包分别是做什么的?deepseek-recipe-core 存放共享类型,deepseek-recipe-encoding 负责提示词渲染和 token 编码,deepseek-recipe-image 负责图片的获取与预处理,deepseek-recipe-python 提供 Python 绑定;server-rs/server-py 是带模拟推理功能的示例 API 服务器。
大小
2.2 MB
星标
2.9K
更新
提交Oct 19
时长
2个月
提交 · 90 天