Xylor commited on
Commit
3f83509
·
verified ·
1 Parent(s): 947552f

Add stream kwarg; add dict history support

Browse files
Files changed (1) hide show
  1. app.py +33 -20
app.py CHANGED
@@ -14,31 +14,43 @@ def respond(
14
  max_tokens,
15
  temperature,
16
  top_p,
 
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
 
20
  for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
 
 
 
26
  messages.append({"role": "user", "content": message})
27
 
28
  response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
 
 
 
 
 
 
 
42
 
43
  """
44
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
@@ -56,6 +68,7 @@ demo = gr.ChatInterface(
56
  step=0.05,
57
  label="Top-p (nucleus sampling)",
58
  ),
 
59
  ],
60
  )
61
 
 
14
  max_tokens,
15
  temperature,
16
  top_p,
17
+ stream: bool = True,
18
  ):
19
+ messages = []
20
+ if system_message:
21
+ messages.append({"role": "system", "content": system_message})
22
  for val in history:
23
+ if isinstance(val, dict) and "role" in val and "content" in val:
24
+ messages.append(val)
25
+ elif isinstance(val, (tuple, list)):
26
+ messages.append(
27
+ {"role": "user", "content": val[0]}
28
+ if val[0] else
29
+ {"role": "assistant", "content": val[1]}
30
+ )
31
  messages.append({"role": "user", "content": message})
32
 
33
  response = ""
34
+ if stream:
35
+ for message in client.chat_completion(
36
+ messages,
37
+ max_tokens=max_tokens,
38
+ stream=True,
39
+ temperature=temperature,
40
+ top_p=top_p,
41
+ ):
42
+ token = message.choices[0].delta.content
43
+
44
+ response += token
45
+ yield response
46
+ else:
47
+ completion = client.chat_completion(
48
+ messages,
49
+ max_tokens=max_tokens,
50
+ temperature=temperature,
51
+ top_p=top_p,
52
+ )
53
+ return completion.choices[0].message
54
 
55
  """
56
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
68
  step=0.05,
69
  label="Top-p (nucleus sampling)",
70
  ),
71
+ gr.Checkbox(value=True, label="Streaming", info="Streaming response vs full completion"),
72
  ],
73
  )
74