Update README.md
Browse files
README.md
CHANGED
|
@@ -43,19 +43,46 @@ This is a specialized AI assistant for programming in **BlackBox Component Build
|
|
| 43 |
## How to Use
|
| 44 |
|
| 45 |
```python
|
| 46 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
outputs = model.generate(
|
| 55 |
-
inputs
|
| 56 |
max_new_tokens=256,
|
| 57 |
-
temperature=0.
|
| 58 |
-
top_p=0.
|
| 59 |
pad_token_id=tokenizer.eos_token_id
|
| 60 |
)
|
| 61 |
-
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
| 43 |
## How to Use
|
| 44 |
|
| 45 |
```python
|
| 46 |
+
from transformers import BitsAndBytesConfig, AutoModelForCausalLM, AutoTokenizer
|
| 47 |
+
import torch
|
| 48 |
+
from peft import PeftModel
|
| 49 |
|
| 50 |
+
assert torch.cuda.is_available(), "you need cuda for this part"
|
| 51 |
+
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
|
|
|
| 52 |
|
| 53 |
+
base_model_name = 'Qwen/Qwen2.5-Coder-3B-Instruct'
|
| 54 |
+
qlora_adapter = "hodza/BlackBox-Coder-3B"
|
| 55 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
| 56 |
+
|
| 57 |
+
bnb_config = BitsAndBytesConfig(
|
| 58 |
+
load_in_4bit=True,
|
| 59 |
+
bnb_4bit_quant_type="nf4",
|
| 60 |
+
bnb_4bit_use_double_quant=True,
|
| 61 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
| 62 |
+
)
|
| 63 |
+
base_model = AutoModelForCausalLM.from_pretrained(base_model_name, device_map=device,quantization_config=bnb_config,)
|
| 64 |
+
|
| 65 |
+
model = PeftModel.from_pretrained(base_model, qlora_adapter, device_map=device)
|
| 66 |
+
# Define the chat template
|
| 67 |
+
def format_chat_prompt(user_query):
|
| 68 |
+
return [
|
| 69 |
+
{"role": "system", "content": "You are a helpful coding assistant for BlackBox Component Builder using Component Pascal."},
|
| 70 |
+
{"role": "user", "content": user_query}
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
def get_assistant_response(user_query):
|
| 74 |
+
# Format the prompt using the chat template
|
| 75 |
+
chat_prompt = format_chat_prompt(user_query)
|
| 76 |
+
inputs = tokenizer.apply_chat_template(chat_prompt, return_tensors="pt").to(model.device)
|
| 77 |
+
|
| 78 |
+
# Generate the response
|
| 79 |
outputs = model.generate(
|
| 80 |
+
inputs,
|
| 81 |
max_new_tokens=256,
|
| 82 |
+
temperature=0.3,
|
| 83 |
+
top_p=0.3,
|
| 84 |
pad_token_id=tokenizer.eos_token_id
|
| 85 |
)
|
| 86 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 87 |
+
|
| 88 |
+
print(get_assistant_response("Как мне вывести массив в Log?"))
|