Shreyas094 commited on
Commit
6e76606
1 Parent(s): ff13ccd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -40,17 +40,25 @@ def get_model():
40
  huggingfacehub_api_token=huggingface_token
41
  )
42
 
 
 
 
 
 
 
 
 
 
43
  def response(database, model, question):
44
  prompt_val = ChatPromptTemplate.from_template(prompt)
45
  retriever = database.as_retriever()
46
- parser = StrOutputParser()
47
- chain = (
48
- {'context': retriever, 'question': RunnablePassthrough()}
49
- | prompt_val
50
- | model
51
- | parser
52
- )
53
- ans = chain.invoke(question)
54
  return ans
55
 
56
  def update_vectors(file):
 
40
  huggingfacehub_api_token=huggingface_token
41
  )
42
 
43
+ def generate_chunked_response(model, prompt, max_tokens=500, max_chunks=5):
44
+ full_response = ""
45
+ for i in range(max_chunks):
46
+ chunk = model(prompt + full_response, max_new_tokens=max_tokens)
47
+ full_response += chunk
48
+ if chunk.strip().endswith((".", "!", "?")):
49
+ break
50
+ return full_response
51
+
52
  def response(database, model, question):
53
  prompt_val = ChatPromptTemplate.from_template(prompt)
54
  retriever = database.as_retriever()
55
+
56
+ context = retriever.get_relevant_documents(question)
57
+ context_str = "\n".join([doc.page_content for doc in context])
58
+
59
+ formatted_prompt = prompt_val.format(context=context_str, question=question)
60
+
61
+ ans = generate_chunked_response(model, formatted_prompt)
 
62
  return ans
63
 
64
  def update_vectors(file):