Spaces:
Sleeping
Sleeping
Update app.py
Browse files15 unmodified lines161718191920212214 unmodified lines373839404140414243444546474849505152424344454647484950515253545556575859606162636415 unmodified lines
def gpt_call(history, user_message, model="gpt-4o", max_tokens=1500, # Increased from 512 to 1500 to prevent truncation max_tokens=3000, # Increased to 3000 to prevent truncation temperature=0.7, top_p=0.95): """14 unmodified lines # 3) Add the user's new message messages.append({"role": "user", "content": user_message})
# 4) Call OpenAI API (with continuation handling) full_response = "" # 4) Call OpenAI API completion = client.chat.completions.create( model=model, messages=messages, max_tokens=max_tokens, # Increased to allow longer responses temperature=temperature, top_p=top_p )
# 5) Ensure full response is returned without being cut off full_response = "".join(choice.message.content for choice in completion.choices).strip()
return full_response while True: completion = client.chat.completions.create( model=model, messages=messages, max_tokens=max_tokens, # Increased to allow longer responses temperature=temperature, top_p=top_p ) response_part = completion.choices[0].message.content.strip() full_response += " " + response_part
# If the response looks incomplete, force the AI to continue if len(response_part) < max_tokens - 50: # Ensures near full completion break # Stop loop if response is complete
# Add last response back into conversation history to continue it messages.append({"role": "assistant", "content": response_part})
return full_response.strip()
def respond(user_message, history):app.py+23-14