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