Shreyas094 commited on
Commit
5860470
·
verified ·
1 Parent(s): a20790a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -8
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
- main_content = ""
 
62
  try:
63
- for i in range(num_calls):
64
- response = await client.chat_completion(
65
  messages=[{"role": "user", "content": prompt}],
66
  max_tokens=6000,
67
  temperature=temperature,
68
- )
69
- if response.choices and response.choices[0].message:
70
- chunk = response.choices[0].message.content
71
- main_content += chunk
72
- yield main_content, ""
 
 
 
 
 
 
 
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}")