mjbuehler commited on
Commit
d6cca84
·
verified ·
1 Parent(s): b7a94e8

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +28 -25
README.md CHANGED
@@ -23,52 +23,55 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
23
  #### Function to interact with the model
24
 
25
  ```
26
-
27
- def generate_response (text_input="Biology offers amazing",system_prompt='You are a materials scientist.',
28
- num_return_sequences=1,
29
- temperature=1., #the higher the temperature, the more creative the model becomes
30
- max_new_tokens=127,device='cuda',
31
- num_beams=1,eos_token_id= [
 
32
  128001,
33
  128008,
34
  128009
35
- ],
36
- top_k = 50,
37
- top_p =0.9,repetition_penalty=1.1,
38
- messages=[],
 
39
  ):
40
 
41
- if messages==[]:
42
- messages=[{"role": "system", "content":system_prompt},
43
- {"role": "user", "content":text_input}]
44
- else:
45
- messages.append ({"role": "user", "content":text_input})
 
 
46
 
47
  text_input = tokenizer.apply_chat_template(
48
  messages,
49
  tokenize=False,
50
  add_generation_prompt=True
51
  )
52
- inputs = tokenizer([text_input], add_special_tokens =True, return_tensors ='pt' ).to(device)
 
 
53
  with torch.no_grad():
54
  outputs = model.generate(**inputs,
55
  max_new_tokens=max_new_tokens,
56
  temperature=temperature,
57
  num_beams=num_beams,
58
- top_k = top_k,
59
- top_p = top_p,
60
- num_return_sequences = num_return_sequences,
61
- eos_token_id=eos_token_id,
62
- do_sample =True,repetition_penalty=repetition_penalty,
63
  )
64
-
65
  outputs=outputs[:, inputs["input_ids"].shape[1]:]
66
-
67
- return tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True), messages
68
  ```
69
  Usage:
70
  ```
71
- res,_= generate_response (text_input = "What is collagen?", system_prompt = 'You are a materials scientist. ',
72
  num_return_sequences=1,
73
  temperature=1., #the higher the temperature, the more creative the model becomes
74
  max_new_tokens=127,
 
23
  #### Function to interact with the model
24
 
25
  ```
26
+ def generate_response (text_input="What is spider silk?",
27
+ system_prompt='',
28
+ num_return_sequences=1,
29
+ temperature=1., #the higher the temperature, the more creative the model becomes
30
+ max_new_tokens=127,device='cuda',
31
+ add_special_tokens = False, #since tokenizer.apply_chat_template adds <|begin_of_text|> template already, set to False
32
+ num_beams=1,eos_token_id= [
33
  128001,
34
  128008,
35
  128009
36
+ ], verbatim=False,
37
+ top_k = 50,
38
+ top_p = 0.9,
39
+ repetition_penalty=1.1,
40
+ messages=[],
41
  ):
42
 
43
+ if messages==[]: #start new messages dictionary
44
+ if system_prompt != '': #include system prompt if provided
45
+ messages.extend ([ {"role": "system", "content": system_prompt}, ])
46
+ messages.extend ( [ {"role": "user", "content": text_input}, ])
47
+
48
+ else: #if messages provided, will extend (make sure to add previous response as assistant message)
49
+ messages.append ({"role": "user", "content": text_input})
50
 
51
  text_input = tokenizer.apply_chat_template(
52
  messages,
53
  tokenize=False,
54
  add_generation_prompt=True
55
  )
56
+ inputs = tokenizer([text_input], add_special_tokens = add_special_tokens, return_tensors ='pt' ).to(device)
57
+ if verbatim:
58
+ print (inputs)
59
  with torch.no_grad():
60
  outputs = model.generate(**inputs,
61
  max_new_tokens=max_new_tokens,
62
  temperature=temperature,
63
  num_beams=num_beams,
64
+ top_k = top_k,eos_token_id=eos_token_id,
65
+ top_p =top_p,
66
+ num_return_sequences = num_return_sequences,
67
+ do_sample =True, repetition_penalty=repetition_penalty,
 
68
  )
 
69
  outputs=outputs[:, inputs["input_ids"].shape[1]:]
70
+ return tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True), messages
 
71
  ```
72
  Usage:
73
  ```
74
+ res,_= generate_response (text_input = "What is collagen?", system_prompt = 'You are a materials scientist.',
75
  num_return_sequences=1,
76
  temperature=1., #the higher the temperature, the more creative the model becomes
77
  max_new_tokens=127,