Views
No views yet

1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
3
4# Load the HelpingAI-3B-coder model
5model = AutoModelForCausalLM.from_pretrained("OEvortex/HelpingAI-3B-coder", trust_remote_code=True).to("cuda")
6
7# Load the tokenizer
8tokenizer = AutoTokenizer.from_pretrained("OEvortex/HelpingAI-3B-coder", trust_remote_code=True)
9
10# Initialize TextStreamer for smooth conversation flow
11streamer = TextStreamer(tokenizer)
12
13# Define the chat input
14chat = [
15 { "role": "system", "content": "You are HelpingAI, an emotionally intelligent AI. Always respond in the HelpingAI style. Provide concise and to-the-point answers." },
16 { "role": "user", "content": "Can you help me write a Python function to reverse a string?" }
17]
18
19# Apply the chat template
20chat_text = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
21
22# Tokenize the text
23inputs = tokenizer(chat_text, return_tensors="pt", return_attention_mask=False).to("cuda")
24
25# Generate text
26generated_text = model.generate(
27 **inputs,
28 max_length=500,
29 top_p=0.95,
30 do_sample=True,
31 temperature=0.7,
32 use_cache=True,
33 eos_token_id=tokenizer.eos_token_id,
34 streamer=streamer
35)
36
37# # Decode the generated text
38# output_text = tokenizer.decode(generated_text[0], skip_special_tokens=True)
39
40# # Print the generated text
41# print(output_text)
42
43# System:
44# You are HelpingAI, an emotional AI that always answers questions in HelpingAI style and always be to the point and answer as short as possible.
45
46# Question:
47# Can you help me write a Python function to reverse a string?
48
49# Answer:
50# Sure! Here's a Python function that reverses a string:
51
52# ```python
53# def reverse_string(input_string):
54# return input_string[::-1]
55# ```
56
57# This function uses slicing with a negative step to reverse the input string. The `[::-1]` syntax reverses the string by starting from the end and going backwards.
58
59# You can use this function like this:
60
61# ```python
62# print(reverse_string("Hello, World!"))
63# ```
64
65# This will output: `!dlroW,olleH`
66
67# I hope this helps! Let me know if you have any other questions.
68