本项目实现了链式草稿(Chain of Draft, CoD)推理方法,此方法能让大语言模型(LLM)生成简洁且信息丰富的中间推理步骤,在解决任务时显著降低令牌使用量,为相关任务的处理带来了新的优化方案。
本MCP服务器实现了论文《链式草稿:通过写作减少思考》中所描述的链式草稿(CoD)推理方法。CoD是一种新颖的方法,允许LLM生成简洁但信息丰富的中间推理步骤,解决任务时显著降低令牌使用量。
环境配置
anthropic, dotenv等。代码实现
from client import ChainOfDraftClient
# 创建客户端
cod_client = ChainOfDraftClient()
# 直接使用
result = await cod_client.solve_with_reasoning(
problem="求解:247 + 394 = ?",
domain="数学"
)
print(f"答案:{result['final_answer']}")
print(f"推理步骤:{result['reasoning_steps']}")
print(f"使用的令牌数:{result['token_count']}")
环境配置
@anthropic-ai/sdk, dotenv等。代码实现
import { Anthropic } from "@anthropic-ai/sdk";
import dotenv from "dotenv";
// 加载环境变量
dotenv.config();
// 创建Anthropic客户端
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
// 导入链式草稿客户端
import chainOfDraftClient from './lib/chain-of-draft-client.js';
// 使用客户端
async function solveMathProblem() {
const result = await chainOfDraftClient.solveWithReasoning({
problem: "求解:247 + 394 = ?",
domain: "数学",
max_words_per_step: 5
});
console.log(`答案:${result.final_answer}`);
console.log(`推理步骤:${result.reasoning_steps}`);
console.log(`使用的令牌数:${result.token_count}`);
}
solveMathProblem();
from client import ChainOfDraftClient
# 创建客户端
cod_client = ChainOfDraftClient()
# 直接使用
result = await cod_client.solve_with_reasoning(
problem="求解:247 + 394 = ?",
domain="数学"
)
print(f"答案:{result['final_answer']}")
print(f"推理步骤:{result['reasoning_steps']}")
print(f"使用的令牌数:{result['token_count']}")
import { Anthropic } from "@anthropic-ai/sdk";
import dotenv from "dotenv";
// 加载环境变量
dotenv.config();
// 创建Anthropic客户端
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
// 导入链式草稿客户端
import chainOfDraftClient from './lib/chain-of-draft-client.js';
// 使用客户端
async function solveMathProblem() {
const result = await chainOfDraftClient.solveWithReasoning({
problem: "求解:247 + 394 = ?",
domain: "数学",
max_words_per_step: 5
});
console.log(`答案:${result.final_answer}`);
console.log(`推理步骤:${result.reasoning_steps}`);
console.log(`使用的令牌数:${result.token_count}`);
}
solveMathProblem();
两种实现遵循相同的核心原则,并提供相同的MCP工具,使得它们在大多数用例中可以互换使用。
本项目采用MIT许可证,你可以自由地使用、修改和分发代码。
欢迎贡献!请参考CONTRIBUTING.md了解如何为该项目做出贡献。
在使用过程中遇到问题?请提交到GitHub Issues,我们会尽快解决。