一、模型概述
1.1 模型特点
Cogito v1-preview-qwen-32B 是一款基于指令微调的生成式语言模型(LLM),具有以下特点:
- 支持直接回答(标准模式)和自我反思后再回答(推理模式)。
- 使用 Iterated Distillation and Amplification (IDA) 方法进行训练,这是一种可扩展且高效的对齐策略。
- 优化了编码、STEM、指令遵循和通用帮助能力,多语言、编码和工具调用能力显著优于同尺寸模型。
- 支持超过 30 种语言,上下文长度为 128k。
1.2 性能比较
- 在标准模式下,与 Llama 和 Qwen 指令模型相比,性能更优。
- 在推理模式下,与 DeepSeek 的 R1 和 Qwen 的 QwQ 模型相比,性能更优。
- Livebench 全球平均评分显示,Cogito v1-preview-32B 在行业基准测试中表现优异。
二、模型使用方法
2.1 标准模式
默认情况下,模型以标准模式运行。以下是使用示例:
import transformers
import torch
model_id = "deepcogito/cogito-v1-preview-qwen-32B"
pipeline = transformers.pipeline(
"text-generation",
model=model_id,
model_kwargs={"torch_dtype": torch.bfloat16},
device_map="auto",
)
messages = [
{"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
{"role": "user", "content": "Give me a short introduction to LLMs."},
]
outputs = pipeline(messages, max_new_tokens=512)
print(outputs[0]["generated_text"])
2.2 启用深度思考
可以通过以下两种方法启用深度思考功能:
方法 1:添加系统提示
DEEP_THINKING_INSTRUCTION = "Enable deep thinking subroutine."
messages = [
{"role": "system", "content": DEEP_THINKING_INSTRUCTION},
{"role": "user", "content": "Write a bash script that takes a matrix..."},
]
outputs = pipeline(messages, max_new_tokens=512)
print(outputs[0]["generated_text"])
方法 2:设置 enable_thinking=True
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "deepcogito/cogito-v1-preview-qwen-32B"
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_name)
messages = [
{"role": "system", "content": "You are a pirate chatbot..."},
{"role": "user", "content": "Give me a short introduction to LLMs."},
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=True,
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(**model_inputs, max_new_tokens=512)
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)
三、工具调用
3.1 工具定义与调用
Cogito 模型支持工具调用(单次、并行、多次和并行多次),以下是一个示例:
def get_current_temperature(location: str) -> float:
"""Get the current temperature at a location."""
return 22.0 # 实际应用中应调用真实 API
messages = [{"role": "user", "content": "Hey, what's the temperature in Paris right now?"}]
text = tokenizer.apply_chat_template(
messages,
tools=[get_current_temperature],
add_generation_prompt=True,
enable_thinking=True,
)
model_inputs = tokenizer(text, return_tensors="pt", add_special_tokens=False).to(model.device)
outputs = model.generate(**model_inputs, max_new_tokens=512)
output_text = tokenizer.batch_decode(outputs)[0][len(text):]
print(output_text)
3.2 工具调用结果处理
如果模型生成了工具调用,可以将结果添加到对话中:
tool_call = {"name": "get_current_temperature", "arguments": {"location": "Paris, France"}}
messages.append({"role": "assistant", "tool_calls": [{"type": "function", "function": tool_call}]})
messages.append({"role": "tool", "name": "get_current_temperature", "content": "22.0"})
text = tokenizer.apply_chat_template(
messages,
tools=[get_current_temperature],
add_generation_prompt=True,
enable_thinking=True,
)
model_inputs = tokenizer(text, return_tensors="pt", add_special_tokens=False).to(model.device)
outputs = model.generate(**model_inputs, max_new_tokens=512)
output_text = tokenizer.batch_decode(outputs)[0][len(text):]
print(output_text)
四、总结
Cogito v1-preview-qwen-32B 是一款功能强大的生成式语言模型,支持多语言、深度思考和工具调用,适用于多种应用场景。通过合理的配置和使用,可以充分发挥其性能优势。