Views
No views yet
1from unsloth import FastLanguageModel
2from transformers import TextStreamer
3import torch
4
5model_name = "jpacifico/Chocolatine-Cook-3B-combined-SFT-DPO-v0.1"
6
7model, tokenizer = FastLanguageModel.from_pretrained(
8 model_name,
9 max_seq_length=2048,
10 dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
11 load_in_4bit=False
12)
13
14FastLanguageModel.for_inference(model)
15model.eval()
16
17def generate_response(user_question: str):
18 messages = [
19 {"role": "system", "content": "Tu es un assistant IA spécialisé dans le langage culinaire français. Une question te sera posée. Tu dois générer une réponse précise et concise."},
20 {"role": "user", "content": "En cuisine "+user_question},
21 ]
22
23 inputs = tokenizer.apply_chat_template(
24 messages,
25 tokenize=True,
26 add_generation_prompt=True,
27 return_tensors="pt",
28 ).to("cuda")
29
30 attention_mask = (inputs != tokenizer.pad_token_id).long()
31
32 text_streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
33
34 with torch.no_grad():
35 _ = model.generate(
36 input_ids=inputs,
37 attention_mask=attention_mask,
38 max_new_tokens=128,
39 use_cache=True,
40 streamer=text_streamer,
41 do_sample=False,
42 temperature=0.7,
43 )