Views
No views yet
[!NOTE] Includes Unsloth chat template fixes!
Forllama.cpp, use--jinja
[!WARNING] See https://docs.unsloth.ai/models/tutorials-how-to-fine-tune-and-run-llms/cogito-v2-how-to-run-locally
for how to run Cogito v2.1 671B locally via llama.cpp!
deepcogito/cogito-671b-v2.1-FP8-Dynamic.1pip install transformers hf_transfer accelerate vllm
2hf download deepcogito/cogito-671b-v2.11import torch
2from transformers import pipeline
3
4model_id = "deepcogito/cogito-671b-v2.1"
5pipe = pipeline("text-generation", model=model_id, model_kwargs={"dtype": "auto"}, device_map="auto")
6
7messages = [
8 {"role": "system", "content": "Always respond in 1-2 words."},
9 {"role": "user", "content": "Who created you?"},
10]
11
12## without reasoning
13outputs = pipe(messages, max_new_tokens=512, tokenizer_encode_kwargs={"enable_thinking": False})
14print(outputs[0]["generated_text"][-1])
15# {'role': 'assistant', 'content': 'Deep Cogito'}
16
17## with reasoning
18outputs = pipe(messages, max_new_tokens=512, tokenizer_encode_kwargs={"enable_thinking": True})
19print(outputs[0]["generated_text"][-1])
20# {'role': 'assistant', 'content': 'The question is asking about my creator. I know that I\'m Cogito, an AI assistant created by Deep Cogito, which is an AI research lab. The question is very direct and can be answered very briefly. Since the user has specified to always respond in 1-2 words, I should keep my answer extremely concise.\n\nThe most accurate 2-word answer would be "Deep Cogito" - this names the organization that created me without any unnecessary details. "Deep Cogito" is two words, so it fits the requirement perfectly.\n</think>\nDeep Cogito'}1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "deepcogito/cogito-671b-v2.1"
4
5model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto")
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7
8messages = [
9 {"role": "system", "content": "Always respond in 1-2 words."},
10 {"role": "user", "content": "Who created you?"}
11]
12
13text = tokenizer.apply_chat_template(
14 messages,
15 tokenize=False,
16 add_generation_prompt=True,
17 enable_thinking=False,
18)
19# To enable self-reflection or reasoning, set `enable_thinking=True` above.
20
21model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
22
23generated_ids = model.generate(**model_inputs, max_new_tokens=512)
24generated_ids = [
25 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
26]
27response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
28print(response)1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_id = "deepcogito/cogito-671b-v2.1"
5
6model = AutoModelForCausalLM.from_pretrained(model_id, dtype="auto", device_map="auto")
7tokenizer = AutoTokenizer.from_pretrained(model_id)
8
9def get_current_temperature(location: str) -> float:
10 """
11 Get the current temperature at a location.
12
13 Args:
14 location: The location to get the temperature for, in the format "City, Country"
15 Returns:
16 The current temperature at the specified location in the specified units, as a float.
17 """
18 return 22.
19
20def generate(messages):
21 global tokenizer, model
22 prompt = tokenizer.apply_chat_template(
23 messages,
24 tools=[get_current_temperature],
25 tokenize=False,
26 add_generation_prompt=True,
27 enable_thinking=False,
28 )
29 # To enable self-reflection or reasoning, set `enable_thinking=True` above.
30
31 model_inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to(model.device)
32
33 generated_ids = model.generate(**model_inputs, max_new_tokens=512)
34 generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
35 response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
36 return response
37
38messages = [{"role": "user", "content": "whats the temperature in Paris?"}]
39response = generate(messages)<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>get_current_temperature
```json
{"location":"Paris, France"}
```<|tool▁call▁end|><|tool▁calls▁end|><|end▁of▁sentence|>1tool_call = {"name": "get_current_temperature", "arguments": {"location": "Paris, France"}}
2messages.append({"role": "assistant", "tool_calls": [{"type": "function", "function": tool_call}]})1messages.append({"role": "tool", "name": "get_current_temperature", "content": "22.0"})
2response = generate(messages)The current temperature in Paris is 22.0 degrees.<|end▁of▁sentence|>1from transformers import AutoTokenizer
2from vllm import SamplingParams, LLM
3
4model_id = "deepcogito/cogito-671b-v2.1"
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6llm = LLM(model=model_id, tensor_parallel_size=8, gpu_memory_utilization=0.95, max_model_len=16384)
7sampling_params = SamplingParams(temperature=0.6, max_tokens=8192)
8
9prompts = ["who created you?", "how are you doing?"]
10
11prompts = [
12 tokenizer.apply_chat_template(
13 [{"role": "system", "content": "Always respond in 1-2 words."}, {"role": "user", "content": prompt}],
14 tokenize=False,
15 add_generation_prompt=True,
16 enable_thinking=False,
17 )
18 for prompt in prompts
19]
20# To enable self-reflection or reasoning, set `enable_thinking=True` above.
21
22out = llm.generate(prompts, sampling_params=sampling_params)
23print([res.outputs[0].text for res in out])1from vllm import LLM, SamplingParams
2
3
4def get_current_temperature(location: str) -> float:
5 """
6 Get the current temperature at a location.
7
8 Args:
9 location: The location to get the temperature for, in the format "City, Country"
10 Returns:
11 The current temperature at the specified location in the specified units, as a float.
12 """
13 return 22. # A real function should probably actually get the temperature!
14
15
16model_id = "deepcogito/cogito-671b-v2.1"
17
18llm = LLM(model=model_id, gpu_memory_utilization=0.9, tensor_parallel_size=8, max_model_len=16384)
19sampling_params = SamplingParams(temperature=0.6, max_tokens=512)
20
21tokenizer = llm.get_tokenizer()
22
23def generate_output(messages):
24 global tokenizer, llm, sampling_params
25 prompt = tokenizer.apply_chat_template(
26 messages,
27 tools=[get_current_temperature],
28 tokenize=False,
29 add_generation_prompt=True,
30 enable_thinking=False,
31 )
32 response = llm.generate(prompt, sampling_params)
33 return response[0].outputs[0].text
34
35messages = [{"role": "user", "content": "whats the temperature today?"}]
36response = generate_output(messages)
37print(response)
38# 'I\'d be happy to check the temperature for you. Could you please let me know which location you\'re interested in? Please provide the city and country (e.g., "New York, USA").'
39
40messages.append({"role": "assistant", "content": 'I\'d be happy to check the temperature for you. Could you please let me know which location you\'re interested in? Please provide the city and country (e.g., "New York, USA").'})
41messages.append({"role": "user", "content": "I live in San Francisco."})
42
43response = generate_output(messages)
44print(response)
45# '<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>get_current_temperature<|tool▁sep|>{"location": "San Francisco, USA"}<|tool▁call▁end|><|tool▁calls▁end|>'
46
47tool_calls = [{"type": "function", "function": {"name": "get_current_temperature", "arguments": {"location": "San Francisco, USA"}}}]
48messages.append({"role": "assistant", "tool_calls": tool_calls})
49
50messages.append({"role": "tool", "name": "get_current_temperature", "content": "22.0"})
51response = generate_output(messages)
52print(response)
53# The current temperature in San Francisco, USA is 22°C.