ashok2216 commited on
Commit
e4a8b4b
·
verified ·
1 Parent(s): 89a37d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -53
app.py CHANGED
@@ -60,34 +60,6 @@ for message in st.session_state.messages:
60
  with st.chat_message(message["role"]):
61
  st.markdown(message["content"])
62
 
63
- # def respond(message, history, max_tokens, temperature, top_p):
64
- # # Prepare the list of messages for the chat completion
65
- # messages = [{"role": "system", "content": st.session_state.messages[0]["content"]}]
66
-
67
- # for val in history:
68
- # if val["role"] == "user":
69
- # messages.append({"role": "user", "content": val["content"]})
70
- # elif val["role"] == "assistant":
71
- # messages.append({"role": "assistant", "content": val["content"]})
72
-
73
- # messages.append({"role": "user", "content": message})
74
-
75
- # # Generate response
76
- # response = ""
77
- # response_container = st.empty() # Placeholder to update the response text dynamically
78
-
79
- # for message in client.chat_completion(
80
- # messages,
81
- # max_tokens=max_tokens,
82
- # stream=True,
83
- # temperature=temperature,
84
- # top_p=top_p,
85
- # ):
86
- # token = message.choices[0].delta.content
87
- # response += token
88
- # response_container.text(response) # Stream the response
89
-
90
- # return response
91
  def respond(message, history, max_tokens, temperature, top_p):
92
  # Prepare the list of messages for the chat completion
93
  messages = [{"role": "system", "content": st.session_state.messages[0]["content"]}]
@@ -104,33 +76,61 @@ def respond(message, history, max_tokens, temperature, top_p):
104
  response = ""
105
  response_container = st.empty() # Placeholder to update the response text dynamically
106
 
107
- try:
108
- for item in client.chat_completion(
109
- messages,
110
- max_tokens=max_tokens,
111
- stream=True,
112
- temperature=temperature,
113
- top_p=top_p,
114
- ):
115
- # Get the payload from the response
116
- payload = item['choices'][0]['delta']['content']
117
- # Clean and log the payload
118
- cleaned_payload = payload.lstrip("data:").rstrip("\n")
119
- print(f"Payload received: {cleaned_payload}") # Debugging line
120
-
121
- try:
122
- token = json.loads(cleaned_payload) # Attempt to parse as JSON
123
- response += token
124
- response_container.text(response) # Stream the response
125
- except json.JSONDecodeError as e:
126
- print(f"JSON decoding failed: {e}")
127
- response_container.text("An error occurred while processing the response.")
128
-
129
- except Exception as e:
130
- print(f"Error during chat completion: {e}")
131
- response_container.text("An error occurred while generating a response.")
132
 
133
  return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  # User input
136
  if user_input := st.chat_input("Ask a health question..."):
 
60
  with st.chat_message(message["role"]):
61
  st.markdown(message["content"])
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  def respond(message, history, max_tokens, temperature, top_p):
64
  # Prepare the list of messages for the chat completion
65
  messages = [{"role": "system", "content": st.session_state.messages[0]["content"]}]
 
76
  response = ""
77
  response_container = st.empty() # Placeholder to update the response text dynamically
78
 
79
+ for message in client.chat_completion(
80
+ messages,
81
+ max_tokens=max_tokens,
82
+ stream=True,
83
+ temperature=temperature,
84
+ top_p=top_p,
85
+ ):
86
+ token = message.choices[0].delta.content
87
+ response += token
88
+ response_container.text(response) # Stream the response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  return response
91
+ # def respond(message, history, max_tokens, temperature, top_p):
92
+ # # Prepare the list of messages for the chat completion
93
+ # messages = [{"role": "system", "content": st.session_state.messages[0]["content"]}]
94
+
95
+ # for val in history:
96
+ # if val["role"] == "user":
97
+ # messages.append({"role": "user", "content": val["content"]})
98
+ # elif val["role"] == "assistant":
99
+ # messages.append({"role": "assistant", "content": val["content"]})
100
+
101
+ # messages.append({"role": "user", "content": message})
102
+
103
+ # # Generate response
104
+ # response = ""
105
+ # response_container = st.empty() # Placeholder to update the response text dynamically
106
+
107
+ # try:
108
+ # for item in client.chat_completion(
109
+ # messages,
110
+ # max_tokens=max_tokens,
111
+ # stream=True,
112
+ # temperature=temperature,
113
+ # top_p=top_p,
114
+ # ):
115
+ # # Get the payload from the response
116
+ # payload = item['choices'][0]['delta']['content']
117
+ # # Clean and log the payload
118
+ # cleaned_payload = payload.lstrip("data:").rstrip("\n")
119
+ # print(f"Payload received: {cleaned_payload}") # Debugging line
120
+
121
+ # try:
122
+ # token = json.loads(cleaned_payload) # Attempt to parse as JSON
123
+ # response += token
124
+ # response_container.text(response) # Stream the response
125
+ # except json.JSONDecodeError as e:
126
+ # print(f"JSON decoding failed: {e}")
127
+ # response_container.text("An error occurred while processing the response.")
128
+
129
+ # except Exception as e:
130
+ # print(f"Error during chat completion: {e}")
131
+ # response_container.text("An error occurred while generating a response.")
132
+
133
+ # return response
134
 
135
  # User input
136
  if user_input := st.chat_input("Ask a health question..."):