Aakash Vardhan commited on
Commit
e1e15b4
·
1 Parent(s): f66a6e7
Files changed (1) hide show
  1. app.py +12 -45
app.py CHANGED
@@ -33,14 +33,10 @@ tokenizer.padding_side = "right"
33
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
34
 
35
 
36
- def respond(
37
- message,
38
- history,
39
- system_message,
40
- max_tokens=256,
41
- temperature=0.3,
42
- top_p=0.95,
43
- ):
44
  # Construct the chat list
45
  chat_list = [{"role": "system", "content": system_message}]
46
  for user, assistant in history:
@@ -52,37 +48,23 @@ def respond(
52
  )
53
  chat_list.append({"role": "user", "content": message})
54
 
55
- # Manually construct the prompt
56
- prompt = ""
57
- for chat in chat_list:
58
- prompt += f"{chat['role']}: {chat['content']}\n"
59
- prompt += "assistant:"
60
-
61
- # Get the input length
62
- input_ids = pipe.tokenizer.encode(prompt, return_tensors="pt")
63
- input_length = input_ids.shape[1]
64
-
65
- # Ensure max_tokens is a positive integer
66
- max_tokens = max(1, int(max_tokens))
67
-
68
- # Adjust max_length to be greater than input_length
69
- max_length = input_length + max_tokens
70
 
71
  outputs = pipe(
72
  prompt,
73
- max_new_tokens=max_tokens,
74
- max_length=max_length,
75
  num_beams=1,
76
  do_sample=True,
77
- temperature=temperature,
78
- top_p=top_p,
79
- top_k=50
80
  )
81
- new_text = outputs[0]['generated_text'][len(prompt):]
82
  return new_text.strip()
83
 
84
 
85
-
86
  examples = [
87
  ["Suggest some breeds that get along with each other"],
88
  ["Explain LLM in AI"],
@@ -95,21 +77,6 @@ demo = gr.ChatInterface(
95
  placeholder="Enter your message here...", container=False, scale=7
96
  ),
97
  examples=examples,
98
- additional_inputs=[
99
- gr.Textbox(
100
- value="You are General Knowledge Assistant. Answer the questions based on the provided information. Be succinct and use first-principles thinking to answer the questions.",
101
- label="System message",
102
- ),
103
- gr.Slider(minimum=1, maximum=2048, value=256, step=1, label="Max new tokens"),
104
- gr.Slider(minimum=0.1, maximum=2.0, value=0.3, step=0.1, label="Temperature"),
105
- gr.Slider(
106
- minimum=0.1,
107
- maximum=1.0,
108
- value=0.95,
109
- step=0.05,
110
- label="Top-p (nucleus sampling)",
111
- ),
112
- ],
113
  title="General Knowledge Assistant",
114
  description="Ask me anything about general knowledge. I'll try to answer succinctly using first principles.",
115
  )
 
33
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
34
 
35
 
36
+ def respond(message, history):
37
+ system_message = """You are General Knowledge Assistant.
38
+ Answer the questions based on the provided information.
39
+ Be succinct and use first-principles thinking to answer the questions."""
 
 
 
 
40
  # Construct the chat list
41
  chat_list = [{"role": "system", "content": system_message}]
42
  for user, assistant in history:
 
48
  )
49
  chat_list.append({"role": "user", "content": message})
50
 
51
+ prompt = pipe.tokenizer.apply_chat_template(
52
+ chat_list, tokenize=False, add_generation_prompt=True
53
+ )
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  outputs = pipe(
56
  prompt,
57
+ max_new_tokens=256,
 
58
  num_beams=1,
59
  do_sample=True,
60
+ temperature=0.3,
61
+ top_p=0.95,
62
+ top_k=50,
63
  )
64
+ new_text = outputs[0]["generated_text"][len(prompt) :]
65
  return new_text.strip()
66
 
67
 
 
68
  examples = [
69
  ["Suggest some breeds that get along with each other"],
70
  ["Explain LLM in AI"],
 
77
  placeholder="Enter your message here...", container=False, scale=7
78
  ),
79
  examples=examples,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  title="General Knowledge Assistant",
81
  description="Ask me anything about general knowledge. I'll try to answer succinctly using first principles.",
82
  )