Views
No views yet
1import torch
2import random
3
4# Загрузка модели и токенизатора
5model_path = "./fine_tuned_model"
6model = AutoModelForCausalLM.from_pretrained(model_path)
7tokenizer = AutoTokenizer.from_pretrained(model_path)
8
9device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10model.to(device)
11
12
13# Функция для генерации текста с использованием Instruct шаблона
14def generate_text(prompt, max_length=100):
15 # Создаем Instruct шаблон
16 instruct_prompt = f"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
17
18 input_ids = tokenizer.encode(instruct_prompt, return_tensors="pt").to(device)
19
20 # Генерируем случайные значения для параметров
21 temperature = random.uniform(0.5, 1.0)
22 top_p = random.uniform(0.9, 1.0)
23 top_k = random.randint(40, 60)
24
25 output = model.generate(
26 input_ids,
27 max_length=max_length,
28 num_return_sequences=1,
29 no_repeat_ngram_size=2,
30 do_sample=True,
31 top_k=top_k,
32 top_p=top_p,
33 temperature=temperature
34 )
35
36 generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
37 # Извлекаем только ответ ассистента
38 assistant_response = generated_text.split("<|im_start|>assistant\n")[-1].split("<|im_end|>")[0].strip()
39 return assistant_response
40
41
42# Пример использования
43prompt = "Hello!"
44print(f"Prompt: {prompt}")
45
46# Генерируем несколько ответов
47for i in range(3):
48 generated_text = generate_text(prompt)
49 print(f"\nGenerated text {i + 1}: {generated_text}")1Generated text 1: system
2You are a helpful assistant.
3 user
4Hello!
5 assistant
6 I am sorry for the confusion, but as an AI language model, I do not have access to any real-time information about your personal information or personal data that you may have provided or requested. However, it is important to ensure that any information you provide is accurate and up-to-date. It is also important that the information that I provide you is not disclosed
7
8Generated text 2: system
9You are a helpful assistant.
10 user
11Hello!
12 assistant
13 Hello!
14 How can I assist you today? It is important to remain calm and patient throughout this difficult time. Thank you for your time and consideration! We appreciate your support and guidance throughout the difficult period. Is there anything else you would like to know or discuss further? Additionally, would you like me to assist with any other activities or tasks?
15
16Generated text 3: system
17You are a helpful assistant.
18 user
19Hello!
20 assistant
21 Hello! How can I assist you today?
22 I'm sorry, but I can't assist with that. Is there anything else I could help you with? Is that the correct place for you? Is it a question or a comment? Let me know if you need anything further. Overall, I am sorry for any confusion you may have