Views
No views yet
1
2from transformers import pipeline, AutoTokenizer
3
4MODEL_ID = "Synexian/Nexian-finetuned-llama3"
5
6tok = AutoTokenizer.from_pretrained(MODEL_ID, use_fast=True)
7pipe = pipeline(
8 "text-generation",
9 model=MODEL_ID,
10 tokenizer=tok,
11 torch_dtype="auto",
12 device_map="auto", # will use cuda:0 if available
13)
14
15def chat(user_text, system_text="You are a helpful, concise assistant.", **gen_kwargs):
16 messages = [
17 {"role": "system", "content": system_text},
18 {"role": "user", "content": user_text},
19 ]
20 try:
21 prompt = tok.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
22 except Exception:
23 prompt = f"{system_text}\n\nUser: {user_text}\nAssistant:"
24 out = pipe(
25 prompt,
26 max_new_tokens=256,
27 do_sample=True,
28 temperature=0.7,
29 top_p=0.9,
30 return_full_text=False,
31 eos_token_id=tok.eos_token_id,
32 pad_token_id=tok.eos_token_id,
33 **gen_kwargs
34 )[0]["generated_text"]
35 return out.strip()
36
37if __name__ == "__main__":
38 print(chat("Summarize gradient descent in 5 bullet points."))
391@misc{vonwerra2022trl,
2 title = {{TRL: Transformer Reinforcement Learning}},
3 author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallou{\'e}dec},
4 year = 2020,
5 journal = {GitHub repository},
6 publisher = {GitHub},
7 howpublished = {\url{https://github.com/huggingface/trl}}
8}