Views
No views yet
Note: This repository contains theGGUF4-bit quantized variant ofhalbihn/NeuralHermes-2.5-Mistral-7Bfor the full version visit the link





1import transformers
2from transformers import AutoTokenizer
3
4model_id = "halbihn/NeuralHermes-2.5-Mistral-7B"
5
6# Format prompt
7message = [
8 {"role": "system", "content": "You are a helpful assistant chatbot."},
9 {"role": "user", "content": "What is a Large Language Model?"}
10]
11tokenizer = AutoTokenizer.from_pretrained(model_id)
12prompt = tokenizer.apply_chat_template(message, add_generation_prompt=True, tokenize=False)
13
14# Create pipeline
15pipeline = transformers.pipeline(
16 "text-generation",
17 model=model_id,
18 tokenizer=tokenizer
19)
20
21# Generate text
22sequences = pipeline(
23 prompt,
24 do_sample=True,
25 temperature=0.7,
26 top_p=0.9,
27 num_return_sequences=1,
28 max_length=200,
29)
30response = sequences[0]['generated_text'].split("<|im_start|>assistant")[-1].strip()
31print(response)
32
33
34# streaming example
35from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
36import torch
37
38model_id = "halbihn/NeuralHermes-2.5-Mistral-7B"
39
40model = AutoModelForCausalLM.from_pretrained(model_id)
41tokenizer = AutoTokenizer.from_pretrained(model_id)
42device = "cuda:0" if torch.cuda.is_available() else "cpu"
43model.to(device)
44
45def stream(
46 user_prompt: str,
47 max_tokens: int = 200,
48) -> None:
49 """Text streaming example
50 """
51
52 system_prompt = 'Below is a conversation between Human and AI assistant named Mistral\n'
53
54 message = [
55 {"role": "system", "content": system_prompt},
56 {"role": "user", "content": user_prompt}
57 ]
58 prompt = tokenizer.apply_chat_template(
59 message,
60 add_generation_prompt=True,
61 tokenize=False,
62 )
63
64 inputs = tokenizer([prompt], return_tensors="pt").to(device)
65
66 streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
67
68 _ = model.generate(**inputs, streamer=streamer, max_new_tokens=max_tokens)
69
70stream("Tell me about the future")
71
72>>> The future is a vast and uncertain expanse, shaped by the collective actions and innovations of humanity. It is a blend of possibilities, technological advancements, and societal changes. Some potential aspects of the future include:
73>>>
74>>> 1. Technological advancements: Artificial intelligence, quantum computing, and biotechnology are expected to continue evolving, leading to breakthroughs in fields like medicine, energy, and communication.
75>>>
76>>> 2. Space exploration: As technology progresses, space travel may become more accessible, enabling humans to establish colonies on other planets and explore the cosmos further.
77>>>
78>>> 3. Climate change mitigation: The future will likely see increased efforts to combat climate change through renewable energy sources, carbon capture technologies, and sustainable practices.
79>>>
80>>> 4. Artificial intelligence integration: AI will likely become more integrated into daily life, assisting with tasks, automating jobs, and even influencing decision-making processes in various industries.