Carlos Rosas commited on
Commit
67cb741
·
verified ·
1 Parent(s): ec95a4c

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +85 -13
README.md CHANGED
@@ -1,6 +1,7 @@
1
  # Cassandre-RAG
2
 
3
- Cassandre-RAG is a fine-tuned llama-3.1-8b model, built for RAG on French administrative documents, with a focus on sources from school administration.
 
4
 
5
  ## Training
6
 
@@ -46,11 +47,57 @@ Quantization:
46
  Cassandre-RAG uses a custom syntax for parsing sources and generating sourced output.
47
  Each source should be preceded by an ID encapsulated in double asterisks (e.g., \*\*SOURCE_ID\*\*).
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  ### Example Usage
50
 
 
 
51
  ```python
52
- import pandas as pd
53
  from vllm import LLM, SamplingParams
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  # Load the model
56
  model_name = "PleIAs/Cassandre-RAG"
@@ -65,17 +112,24 @@ sampling_params = SamplingParams(
65
  stop=["#END#"]
66
  )
67
 
68
- # Prepare the input data
 
 
 
 
 
 
 
 
 
69
  def prepare_prompt(query, sources):
70
- sources_text = "\n\n".join([f"**{src_id}**\n{content}" for src_id, content in sources])
71
- return f"### Query ###\n{query}\n\n### Source ###\n{sources_text}\n\n### Analysis ###\n"
72
-
73
- # Example query and sources
74
- query = "Quelles sont les procédures pour inscrire un enfant à l'école primaire?"
75
- sources = [
76
- ("SOURCE_001", "L'inscription à l'école primaire se fait généralement à la mairie..."),
77
- ("SOURCE_002", "Les documents nécessaires pour l'inscription scolaire incluent..."),
78
- ]
79
 
80
  # Prepare the prompt
81
  prompt = prepare_prompt(query, sources)
@@ -85,5 +139,23 @@ outputs = llm.generate([prompt], sampling_params)
85
  generated_text = outputs[0].outputs[0].text
86
 
87
  print("Query:", query)
 
 
88
  print("\nGenerated Response:")
89
- print(generated_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Cassandre-RAG
2
 
3
+ Cassandre-RAG is a fine-tuned **llama-3.1-8b model**, built for RAG on French administrative documents, with a focus on sources from school administration.
4
+ The model has been trained to expect a predifined input structure, that allows it to very efficiently perform RAG tasks while clearly citing the specific exceprts and the source documents used in the generation of anwsers.
5
 
6
  ## Training
7
 
 
47
  Cassandre-RAG uses a custom syntax for parsing sources and generating sourced output.
48
  Each source should be preceded by an ID encapsulated in double asterisks (e.g., \*\*SOURCE_ID\*\*).
49
 
50
+ The input structure expected by Cassandre is the following
51
+
52
+ ```yaml
53
+ prompt = f"""### Query ###\n{user_message}\n\n### Source ###\n{fiches}\n\n### Answer ###\n"""
54
+ ```
55
+
56
+ The **"Query"** section consists on the question or keywords that the user inputs.
57
+
58
+ The **"Source"** part consists of the documents that have been retrieved using a vector database, like duckdb, lancedb or others.
59
+
60
+ The **"Answer"** indicates the model where it should insert the generated answer to the query, based on the retrieved documents.
61
+ This answer will also contain the excerpts of the documents used and the ID of those documents, using this format:
62
+
63
+ ```yaml
64
+ <ref text="[Quoted text from source]">[Source ID]</ref>
65
+ ```
66
+
67
  ### Example Usage
68
 
69
+ In this example, we will be using lancedb for the retrieval part. Lancedb creates the embeddings of the documents added to the database, and its hybrid search feature allows us to combine vector search with keyword search for better retrieval.
70
+
71
  ```python
72
+ import lancedb
73
  from vllm import LLM, SamplingParams
74
+ import pandas as pd
75
+
76
+ # Initialize LanceDB
77
+ db = lancedb.connect("lancedb_data")
78
+ table = db.open_table("education")
79
+
80
+ # Create fictitious education documents
81
+ documents = [
82
+ {
83
+ "hash": "DOC001",
84
+ "main_title": "Inscription à l'école primaire",
85
+ "text": "L'inscription à l'école primaire en France se fait en deux étapes. Premièrement, les parents doivent se rendre à la mairie avec un justificatif de domicile, le livret de famille et le carnet de santé de l'enfant. Ensuite, ils doivent finaliser l'inscription directement à l'école. L'âge minimal pour l'inscription est de 3 ans."
86
+ },
87
+ {
88
+ "hash": "DOC002",
89
+ "main_title": "Calendrier des inscriptions scolaires",
90
+ "text": "Les inscriptions à l'école primaire doivent être effectuées au plus tard au mois de juin précédant la rentrée scolaire. Il est conseillé de s'y prendre à l'avance car certaines communes ont des périodes d'inscription spécifiques. La rentrée scolaire a généralement lieu début septembre."
91
+ },
92
+ {
93
+ "hash": "DOC003",
94
+ "main_title": "Documents requis pour l'inscription scolaire",
95
+ "text": "Pour inscrire un enfant à l'école primaire, les documents suivants sont généralement requis : justificatif de domicile de moins de 3 mois, livret de famille ou extrait d'acte de naissance, carnet de santé avec vaccinations à jour, et éventuellement le certificat de radiation si l'enfant était précédemment inscrit dans une autre école."
96
+ }
97
+ ]
98
+
99
+ # Add documents to LanceDB (in a real scenario, this would be done separately)
100
+ table.add(documents)
101
 
102
  # Load the model
103
  model_name = "PleIAs/Cassandre-RAG"
 
112
  stop=["#END#"]
113
  )
114
 
115
+ def hybrid_search(text):
116
+ results = table.search(text, query_type="hybrid").limit(3).to_pandas()
117
+ document = []
118
+ for _, row in results.iterrows():
119
+ hash_id = str(row['hash'])
120
+ title = row['main_title']
121
+ content = row['text']
122
+ document.append(f"**{hash_id}**\n{title}\n{content}")
123
+ return "\n\n".join(document)
124
+
125
  def prepare_prompt(query, sources):
126
+ return f"### Query ###\n{query}\n\n### Source ###\n{sources}\n\n### Answer ###\n"
127
+
128
+ # Example query
129
+ query = "Quelles sont les démarches pour inscrire un enfant à l'école primaire en France?"
130
+
131
+ # Perform hybrid search
132
+ sources = hybrid_search(query)
 
 
133
 
134
  # Prepare the prompt
135
  prompt = prepare_prompt(query, sources)
 
139
  generated_text = outputs[0].outputs[0].text
140
 
141
  print("Query:", query)
142
+ print("\nSources:")
143
+ print(sources)
144
  print("\nGenerated Response:")
145
+ print(generated_text)
146
+
147
+ # Simple post-processing to display references (a simplified version of format_references)
148
+ def simple_format_references(text):
149
+ while '<ref text="' in text:
150
+ start = text.index('<ref text="') + 10
151
+ end = text.index('">', start)
152
+ ref_text = text[start:end]
153
+ ref_id_start = end + 2
154
+ ref_id_end = text.index('</ref>', ref_id_start)
155
+ ref_id = text[ref_id_start:ref_id_end]
156
+ text = text.replace(f'<ref text="{ref_text}">{ref_id}</ref>', f'[{ref_id}: {ref_text}]')
157
+ return text
158
+
159
+ print("\nFormatted Response:")
160
+ print(simple_format_references(generated_text))
161
+ ```