Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,14 +11,13 @@ def get_api_key():
|
|
11 |
return None
|
12 |
|
13 |
# Function to interact with the OpenAI API with streaming
|
14 |
-
def generate_response(
|
15 |
try:
|
16 |
client = openai.OpenAI(api_key=api_key) # Instantiate OpenAI client with api_key
|
17 |
|
18 |
-
messages = [{"role": "user", "content": prompt + "\n" + extra_instructions}]
|
19 |
stream = client.chat.completions.create(
|
20 |
model=model_name,
|
21 |
-
messages=messages,
|
22 |
stream=True,
|
23 |
)
|
24 |
return stream
|
@@ -66,8 +65,10 @@ def main():
|
|
66 |
return
|
67 |
|
68 |
full_response = ""
|
|
|
|
|
69 |
for model in models:
|
70 |
-
stream = generate_response(
|
71 |
if stream:
|
72 |
with st.chat_message("assistant"):
|
73 |
message_placeholder = st.empty()
|
|
|
11 |
return None
|
12 |
|
13 |
# Function to interact with the OpenAI API with streaming
|
14 |
+
def generate_response(messages, model_name, api_key): # Modified to accept 'messages'
|
15 |
try:
|
16 |
client = openai.OpenAI(api_key=api_key) # Instantiate OpenAI client with api_key
|
17 |
|
|
|
18 |
stream = client.chat.completions.create(
|
19 |
model=model_name,
|
20 |
+
messages=messages, # Use the entire conversation history
|
21 |
stream=True,
|
22 |
)
|
23 |
return stream
|
|
|
65 |
return
|
66 |
|
67 |
full_response = ""
|
68 |
+
# Prepare messages for OpenAI:
|
69 |
+
openai_messages = st.session_state.messages
|
70 |
for model in models:
|
71 |
+
stream = generate_response(openai_messages, model, api_key) # Pass the messages
|
72 |
if stream:
|
73 |
with st.chat_message("assistant"):
|
74 |
message_placeholder = st.empty()
|