mrolando commited on
Commit
527456d
1 Parent(s): 4add049

added word streaming

Browse files
Files changed (1) hide show
  1. app.py +26 -20
app.py CHANGED
@@ -70,26 +70,32 @@ def respond(message, chat_history):
70
  model="gpt-3.5-turbo",
71
  messages= prompt,
72
  temperature=0.5,
73
- max_tokens=1000
74
- ).choices[0].message.content
75
- chat_history.append((message, response))
76
- return "",chat_history
77
- #Streaming the tokens
78
- # for idx, response in enumerate(stream):
79
- # text_token = response.token.text
80
-
81
- # if response.details:
82
- # return
83
-
84
- # if idx == 0 and text_token.startswith(" "):
85
- # text_token = text_token[1:]
86
-
87
- # acc_text += text_token
88
- # last_turn = list(chat_history.pop(-1))
89
- # last_turn[-1] += acc_text
90
- # chat_history = chat_history + [last_turn]
91
- # yield "", chat_history
92
- # acc_text = ""
 
 
 
 
 
 
93
 
94
  with gr.Blocks() as demo:
95
  gr.Markdown("""
 
70
  model="gpt-3.5-turbo",
71
  messages= prompt,
72
  temperature=0.5,
73
+ max_tokens=1000,
74
+ stream = True
75
+ )#.choices[0].message.content
76
+ # chat_history.append((message, response))
77
+
78
+ token_counter = 0
79
+ partial_words = ""
80
+
81
+ for chunk in response:
82
+ chunk_message = chunk['choices'][0]['delta']
83
+ if(len(chat_history))<1:
84
+ # print("entró acaá")
85
+ partial_words += chunk_message.content
86
+ chat_history.append([message,chunk_message.content])
87
+ else:
88
+ # print("antes", chat_history)
89
+ if(len(chunk_message)!=0):
90
+ if(len(chunk_message)==2):
91
+ partial_words += chunk_message.content
92
+ chat_history.append([message,chunk_message.content])
93
+ else:
94
+ partial_words += chunk_message.content
95
+ chat_history[-1] =([message,partial_words])
96
+ yield "",chat_history
97
+
98
+ # return "",chat_history
99
 
100
  with gr.Blocks() as demo:
101
  gr.Markdown("""