delcenjo commited on
Commit
58cb88e
·
verified ·
1 Parent(s): d34cd2e

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import sys
 
3
 
4
  sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "src"))
5
 
@@ -32,15 +33,19 @@ def answer(question):
32
  context_block = "\n\n".join(
33
  f"[{chunk['source']}]\n{chunk['text']}" for chunk, _ in contexts
34
  )
35
- response = client.chat_completion(
36
- messages=[
37
- {"role": "system", "content": "Answer the question using only the provided context. If the answer is not in the context, say you don't know. Cite the source filename in square brackets after each fact you use."},
38
- {"role": "user", "content": f"Context:\n{context_block}\n\nQuestion: {question}"},
39
- ],
40
- model=MODEL,
41
- max_tokens=400,
42
- )
43
- return response.choices[0].message.content.strip()
 
 
 
 
44
  except Exception:
45
  return "The service is busy or unavailable right now. Please try again in a moment."
46
 
@@ -57,6 +62,7 @@ demo = gr.Interface(
57
  "from them, citing the source file for each fact."
58
  ),
59
  article="Code: https://github.com/delcenjo/rag-document-assistant",
 
60
  examples=[
61
  ["What is the refund policy?"],
62
  ["How do I authenticate with the API?"],
 
1
  import os
2
  import sys
3
+ import time
4
 
5
  sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "src"))
6
 
 
33
  context_block = "\n\n".join(
34
  f"[{chunk['source']}]\n{chunk['text']}" for chunk, _ in contexts
35
  )
36
+ messages = [
37
+ {"role": "system", "content": "Answer the question using only the provided context. If the answer is not in the context, say you don't know. Cite the source filename in square brackets after each fact you use."},
38
+ {"role": "user", "content": f"Context:\n{context_block}\n\nQuestion: {question}"},
39
+ ]
40
+ last_error = None
41
+ for attempt in range(3):
42
+ try:
43
+ response = client.chat_completion(messages=messages, model=MODEL, max_tokens=400)
44
+ return response.choices[0].message.content.strip()
45
+ except Exception as error:
46
+ last_error = error
47
+ time.sleep(1.5 * (attempt + 1))
48
+ raise last_error
49
  except Exception:
50
  return "The service is busy or unavailable right now. Please try again in a moment."
51
 
 
62
  "from them, citing the source file for each fact."
63
  ),
64
  article="Code: https://github.com/delcenjo/rag-document-assistant",
65
+ cache_examples=False,
66
  examples=[
67
  ["What is the refund policy?"],
68
  ["How do I authenticate with the API?"],