victor HF staff commited on
Commit
7296866
0 Parent(s):
Files changed (4) hide show
  1. .gitattributes +35 -0
  2. README.md +12 -0
  3. app.py +77 -0
  4. requirements.txt +2 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: RAG With CoT And Self Reflection
3
+ emoji: 💻
4
+ colorFrom: red
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 4.44.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from txtai import Embeddings, LLM
3
+ import gradio as gr
4
+
5
+
6
+ def cot(system, user):
7
+ system = f"""
8
+ {system}
9
+
10
+ You are an AI assistant that uses a Chain of Thought (CoT) approach with reflection to answer queries. Follow these steps:
11
+
12
+ 1. Think through the problem step by step within the <thinking> tags.
13
+ 2. Reflect on your thinking to check for any errors or improvements within the <reflection> tags.
14
+ 3. Make any necessary adjustments based on your reflection.
15
+ 4. Provide your final, concise answer within the <output> tags.
16
+
17
+ Important: The <thinking> and <reflection> sections are for your internal reasoning process only.
18
+ Do not include any part of the final answer in these sections.
19
+ The actual response to the query must be entirely contained within the <output> tags.
20
+
21
+ Use the following format for your response:
22
+ <thinking>
23
+ [Your step-by-step reasoning goes here. This is your internal thought process, not the final answer.]
24
+ <reflection>
25
+ [Your reflection on your reasoning, checking for errors or improvements]
26
+ </reflection>
27
+ [Any adjustments to your thinking based on your reflection]
28
+ </thinking>
29
+ <output>
30
+ [Your final, concise answer to the query. This is the only part that will be shown to the user.]
31
+ </output>
32
+ """
33
+
34
+ response = llm(
35
+ [
36
+ {"role": "system", "content": system},
37
+ {"role": "user", "content": user},
38
+ ],
39
+ maxlength=4096,
40
+ )
41
+
42
+ match = re.search(r"<output>(.*?)(?:</output>|$)", response, re.DOTALL)
43
+ return match.group(1).strip() if match else response
44
+
45
+
46
+ def rag(question):
47
+ prompt = """
48
+ Answer the following question using only the context below. Only include information
49
+ specifically discussed.
50
+
51
+ question: {question}
52
+ context: {context}
53
+ """
54
+
55
+ system = "You are a friendly assistant. You answer questions from users."
56
+
57
+ context = "\n".join([x["text"] for x in embeddings.search(question)])
58
+
59
+ return cot(system, prompt.format(question=question, context=context))
60
+
61
+
62
+ embeddings = Embeddings()
63
+ embeddings.load(provider="huggingface-hub", container="neuml/txtai-wikipedia")
64
+
65
+ llm = LLM("hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4", gpu=True)
66
+
67
+
68
+ def predict(message, history):
69
+ response = rag(message)
70
+ return response
71
+
72
+
73
+ gr.ChatInterface(
74
+ predict,
75
+ title="txtai Reflection Chatbot",
76
+ description="A chatbot that uses Chain of Thought (CoT) with self-reflection to answer queries.",
77
+ ).launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ txtai
2
+ autoawq