Update app.py
Browse files88 unmodified lines89909192939495969798991001011021031041051061071081 unmodified line1101111121131141151161171 unmodified line11912012112212312412512612712812913013113213313413513629 unmodified lines16616716816917017117217317417588 unmodified lines{"role": "user", "content": user_message}]# Include chat history if providedif history:messages = history + messagestry:for call in range(num_calls):try:for response in client.chat_completion(messages=messages,max_tokens=6000,temperature=temperature,stream=True,top_p=0.8,):if isinstance(response, dict) and "choices" in response:for choice in response["choices"]:if "delta" in choice and "content" in choice["delta"]:1 unmodified linefull_response += chunkyield full_response, ""else:logging.error("Unexpected response format or missing attributes in the response object.")breakexcept Exception as e:logging.error(f"Error in API call {call + 1}: {str(e)}")if "422 Client Error" in str(e):1 unmodified line# You might want to adjust parameters here, e.g., reduce max_tokensyield f"An error occurred during API call {call + 1}. Retrying...", ""# Add a small delay between API callsawait asyncio.sleep(1) # 1 second delayexcept asyncio.CancelledError:logging.warning("The operation was cancelled.")yield "The operation was cancelled. Please try again.", ""if not full_response:logging.warning("No response generated from the model")yield "No response generated from the model.", ""yield f"{full_response}\n\nSources:\n{source_list_str}", ""async def respond(message, system_prompt, history, model, temperature, num_calls, use_embeddings):logging.info(f"User Query: {message}")29 unmodified linesnum_calls=num_calls,temperature=temperature):# Yield only the new contentnew_content = main_content[len(full_response):]full_response = main_contentyield new_content# Yield the sources as a separate messageif sources:88 unmodified lines89909192939495969798991001011021031041051061071081091101111121131141 unmodified line1161171181191201211221231241251261 unmodified line12812913013113213313413513613713813914014114214314414514614729 unmodified lines17717817918018118218318418518618718818919088 unmodified lines{"role": "user", "content": user_message}]if history:messages = history + messagestry:for call in range(num_calls):try:response_stream = client.chat_completion(messages=messages,max_tokens=6000,temperature=temperature,stream=True,top_p=0.8,)if response_stream is None:logging.error(f"API call {call + 1} returned None")yield "The API returned an empty response. Please try again.", ""continuefor response in response_stream:if isinstance(response, dict) and "choices" in response:for choice in response["choices"]:if "delta" in choice and "content" in choice["delta"]:1 unmodified linefull_response += chunkyield full_response, ""else:logging.error(f"Unexpected response format in API call {call + 1}: {response}")if full_response:break # If we got a valid response, exit the loopexcept Exception as e:logging.error(f"Error in API call {call + 1}: {str(e)}")if "422 Client Error" in str(e):1 unmodified line# You might want to adjust parameters here, e.g., reduce max_tokensyield f"An error occurred during API call {call + 1}. Retrying...", ""await asyncio.sleep(1) # 1 second delay between callsexcept asyncio.CancelledError:logging.warning("The operation was cancelled.")yield "The operation was cancelled. Please try again.", ""except Exception as e:logging.error(f"Unexpected error in get_response_with_search: {str(e)}")yield f"An unexpected error occurred: {str(e)}", ""if not full_response:logging.warning("No response generated from the model")yield "No response generated from the model. Please try again.", ""else:yield f"{full_response}\n\nSources:\n{source_list_str}", ""async def respond(message, system_prompt, history, model, temperature, num_calls, use_embeddings):logging.info(f"User Query: {message}")29 unmodified linesnum_calls=num_calls,temperature=temperature):if "error" in main_content.lower() or "no response" in main_content.lower():# If it's an error message, yield it as isyield main_contentelse:# Otherwise, yield only the new contentnew_content = main_content[len(full_response):]full_response = main_contentyield new_content# Yield the sources as a separate messageif sources:
app.py+29-14