Views
No views yet



1import transformers
2import torch
3
4model_id = "deepcogito/cogito-v1-preview-qwen-14B"
5
6pipeline = transformers.pipeline(
7 "text-generation",
8 model=model_id,
9 model_kwargs={"torch_dtype": torch.bfloat16},
10 device_map="auto",
11)
12
13messages = [
14 {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
15 {"role": "user", "content": "Give me a short introduction to LLMs."},
16]
17
18outputs = pipeline(
19 messages,
20 max_new_tokens=512,
21)
22
23print(outputs[0]["generated_text"][-1])enable_thinking=True while applying the chat template.system_instruction = 'Enable deep thinking subroutine.'system_instruction = 'Enable deep thinking subroutine.' + '\n\n' + system_instruction.1import transformers
2import torch
3
4model_id = "deepcogito/cogito-v1-preview-qwen-14B"
5
6pipeline = transformers.pipeline(
7 "text-generation",
8 model=model_id,
9 model_kwargs={"torch_dtype": torch.bfloat16},
10 device_map="auto",
11)
12
13DEEP_THINKING_INSTRUCTION = "Enable deep thinking subroutine."
14
15messages = [
16 {"role": "system", "content": DEEP_THINKING_INSTRUCTION},
17 {"role": "user", "content": "Write a bash script that takes a matrix represented as a string with format '[1,2],[3,4],[5,6]' and prints the transpose in the same format."},
18]
19
20outputs = pipeline(
21 messages,
22 max_new_tokens=512,
23)
24
25print(outputs[0]["generated_text"][-1])DEEP_THINKING_INSTRUCTION to the beginning in this way -1DEEP_THINKING_INSTRUCTION = "Enable deep thinking subroutine."
2
3system_prompt = "Reply to each prompt with only the actual code - no explanations."
4prompt = "Write a bash script that takes a matrix represented as a string with format '[1,2],[3,4],[5,6]' and prints the transpose in the same format."
5
6messages = [
7 {"role": "system", "content": DEEP_THINKING_INSTRUCTION + '\n\n' + system_prompt},
8 {"role": "user", "content": prompt}
9]enable_thinking=True to the tokenization (this option is added to the chat template).1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "deepcogito/cogito-v1-preview-qwen-14B"
4
5model = AutoModelForCausalLM.from_pretrained(
6 model_name,
7 torch_dtype="auto",
8 device_map="auto"
9)
10tokenizer = AutoTokenizer.from_pretrained(model_name)
11
12prompt = "Give me a short introduction to LLMs."
13messages = [
14 {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
15 {"role": "user", "content": prompt}
16]
17
18text = tokenizer.apply_chat_template(
19 messages,
20 tokenize=False,
21 add_generation_prompt=True,
22 enable_thinking=True
23)
24model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
25
26generated_ids = model.generate(
27 **model_inputs,
28 max_new_tokens=512
29)
30generated_ids = [
31 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
32]
33
34response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
35print(response)1# First, define a tool
2def get_current_temperature(location: str) -> float:
3 """
4 Get the current temperature at a location.
5
6 Args:
7 location: The location to get the temperature for, in the format "City, Country"
8 Returns:
9 The current temperature at the specified location in the specified units, as a float.
10 """
11 return 22. # A real function should probably actually get the temperature!
12
13# Next, create a chat and apply the chat template
14messages = [
15 {"role": "user", "content": "Hey, what's the temperature in Paris right now?"}
16]
17
18model_inputs = tokenizer.apply_chat_template(messages, tools=[get_current_temperature], add_generation_prompt=True)
19
20text = tokenizer.apply_chat_template(messages, tools=[get_current_temperature], add_generation_prompt=True, tokenize=False)
21inputs = tokenizer(text, return_tensors="pt", add_special_tokens=False).to(model.device)
22outputs = model.generate(**inputs, max_new_tokens=512)
23output_text = tokenizer.batch_decode(outputs)[0][len(text):]
24print(output_text)<tool_call>
{"name": "get_current_temperature", "arguments": {"location": "Paris, France"}}
</tool_call><|im_end|>1tool_call = {"name": "get_current_temperature", "arguments": {"location": "Paris, France"}}
2messages.append({"role": "assistant", "tool_calls": [{"type": "function", "function": tool_call}]})tool role, like so:messages.append({"role": "tool", "name": "get_current_temperature", "content": "22.0"})generate() again to let the model use the tool result in the chat:1text = tokenizer.apply_chat_template(messages, tools=[get_current_temperature], add_generation_prompt=True, tokenize=False)
2inputs = tokenizer(text, return_tensors="pt", add_special_tokens=False).to(model.device)
3outputs = model.generate(**inputs, max_new_tokens=512)
4output_text = tokenizer.batch_decode(outputs)[0][len(text):]'The current temperature in Paris is 22.0 degrees.<|im_end|>'