Update app.py
Browse files
app.py
CHANGED
|
@@ -58,22 +58,34 @@ After writing the document, please provide a list of sources used in your respon
|
|
| 58 |
|
| 59 |
# Use Hugging Face API
|
| 60 |
client = InferenceClient(model, token=huggingface_token)
|
| 61 |
-
|
|
|
|
| 62 |
try:
|
| 63 |
-
for
|
| 64 |
-
response
|
| 65 |
messages=[{"role": "user", "content": prompt}],
|
| 66 |
max_tokens=6000,
|
| 67 |
temperature=temperature,
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
logging.error(f"Error in get_response_with_search: {str(e)}")
|
| 75 |
yield f"An error occurred while processing your request: {str(e)}", ""
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
async def respond(message, history, model, temperature, num_calls, use_embeddings):
|
| 78 |
logging.info(f"User Query: {message}")
|
| 79 |
logging.info(f"Model Used: {model}")
|
|
|
|
| 58 |
|
| 59 |
# Use Hugging Face API
|
| 60 |
client = InferenceClient(model, token=huggingface_token)
|
| 61 |
+
full_response = ""
|
| 62 |
+
|
| 63 |
try:
|
| 64 |
+
for _ in range(num_calls):
|
| 65 |
+
for response in client.chat_completion(
|
| 66 |
messages=[{"role": "user", "content": prompt}],
|
| 67 |
max_tokens=6000,
|
| 68 |
temperature=temperature,
|
| 69 |
+
stream=True,
|
| 70 |
+
top_p=0.9,
|
| 71 |
+
):
|
| 72 |
+
if isinstance(response, dict) and "choices" in response:
|
| 73 |
+
for choice in response["choices"]:
|
| 74 |
+
if "delta" in choice and "content" in choice["delta"]:
|
| 75 |
+
chunk = choice["delta"]["content"]
|
| 76 |
+
full_response += chunk
|
| 77 |
+
yield full_response, ""
|
| 78 |
+
else:
|
| 79 |
+
logging.error("Unexpected response format or missing attributes in the response object.")
|
| 80 |
+
break
|
| 81 |
except Exception as e:
|
| 82 |
logging.error(f"Error in get_response_with_search: {str(e)}")
|
| 83 |
yield f"An error occurred while processing your request: {str(e)}", ""
|
| 84 |
|
| 85 |
+
if not full_response:
|
| 86 |
+
logging.warning("No response generated from the model")
|
| 87 |
+
yield "No response generated from the model.", ""
|
| 88 |
+
|
| 89 |
async def respond(message, history, model, temperature, num_calls, use_embeddings):
|
| 90 |
logging.info(f"User Query: {message}")
|
| 91 |
logging.info(f"Model Used: {model}")
|