Remove RAG functionality
Browse files
app.py
CHANGED
@@ -626,92 +626,4 @@ def find_documents_batch(request: BatchDocRequest):
|
|
626 |
results=results,
|
627 |
missing=missing,
|
628 |
search_time=time.time() - start_time
|
629 |
-
)
|
630 |
-
|
631 |
-
def generate_keywords_from_rag_query(question: str):
|
632 |
-
llm = openai.OpenAI(
|
633 |
-
api_key=os.environ.get("GROQ_API_KEY"),
|
634 |
-
base_url="https://api.groq.com/openai/v1",
|
635 |
-
http_client=httpx.Client(verify=False)
|
636 |
-
)
|
637 |
-
system_prompt = """
|
638 |
-
You are a keyword extraction assistant specialized in technical documentation and knowledge retrieval.
|
639 |
-
Your task is to convert a natural language question into a concise set of search-friendly keywords that combine technical terms, abbreviations, and general descriptors.
|
640 |
-
Focus on terminology used in standards, technical specifications, or protocol documentation. Avoid full sentences, keep it short and focused.
|
641 |
-
|
642 |
-
Return the result as a single string, suitable for use in vector search or RAG pipelines.
|
643 |
-
|
644 |
-
Input (example):
|
645 |
-
"Explain the procedure for network slice selection"
|
646 |
-
|
647 |
-
Output:
|
648 |
-
"NSSF network slice selection"
|
649 |
-
"""
|
650 |
-
|
651 |
-
messages = [{
|
652 |
-
"role": "system",
|
653 |
-
"content": system_prompt
|
654 |
-
}, {
|
655 |
-
"role": "user",
|
656 |
-
"content": f"Now process the following input: {question}"
|
657 |
-
}]
|
658 |
-
|
659 |
-
response = llm.chat.completions.create(messages=messages, model="llama-3.3-70b-versatile")
|
660 |
-
return response.choices[0].message.content
|
661 |
-
|
662 |
-
class RAGRequest(BaseModel):
|
663 |
-
question: str
|
664 |
-
threshold: int
|
665 |
-
release: Optional[str] = None
|
666 |
-
working_group: Optional[str] = None
|
667 |
-
spec_type: Optional[Literal["TS", "TR"]] = None
|
668 |
-
|
669 |
-
|
670 |
-
@app.post("/list-rag-docs")
|
671 |
-
def get_docs_for_rag(req: RAGRequest):
|
672 |
-
keywords = generate_keywords_from_rag_query(req.question)
|
673 |
-
print(keywords)
|
674 |
-
doc_data = finder_spec.indexer_documents
|
675 |
-
unique_specs = []
|
676 |
-
documents = {}
|
677 |
-
results = search_spec_bm25(KeywordRequest2(keywords=keywords, threshold=req.threshold, release=req.release, working_group=req.working_group, spec_type=req.spec_type))
|
678 |
-
|
679 |
-
for result in results.results:
|
680 |
-
if result['id'] in unique_specs: continue
|
681 |
-
if result['id'] not in unique_specs:
|
682 |
-
unique_specs.append(result['id'])
|
683 |
-
content = dict(doc_data[result['id']])
|
684 |
-
content_bak = dict(doc_data[result['id']])
|
685 |
-
if isinstance(content, str): continue
|
686 |
-
for chapter in content_bak.keys():
|
687 |
-
if any(kw in chapter.lower() for kw in ["reference", "void"]) or any(kw in content_bak[chapter].lower() for kw in ["annex"]):
|
688 |
-
content.pop(chapter)
|
689 |
-
documents[f"{result['id']}*-*{result['title']}"] = content
|
690 |
-
|
691 |
-
faiss_index = faiss.IndexFlatIP(384)
|
692 |
-
meta = {}
|
693 |
-
contents = []
|
694 |
-
index_counter = 0
|
695 |
-
for spec in documents.keys():
|
696 |
-
for chapter, content in documents[spec].items():
|
697 |
-
contents.append(content)
|
698 |
-
meta[index_counter] = (spec.split("*-*")[0], spec.split("*-*")[1], chapter, content)
|
699 |
-
index_counter += 1
|
700 |
-
|
701 |
-
print("Done contents")
|
702 |
-
|
703 |
-
embedding = model.encode(contents, convert_to_numpy=True, normalize_embeddings=True, show_progress_bar=True).astype('float32')
|
704 |
-
embedding = embedding.reshape(-1, 384) # Forme (1, 384)
|
705 |
-
print(embedding.shape)
|
706 |
-
faiss_index.add(embedding)
|
707 |
-
|
708 |
-
embedding_query = model.encode(req.question, convert_to_numpy=True, normalize_embeddings=True).astype('float32')
|
709 |
-
embedding_query = embedding_query.reshape(1, -1)
|
710 |
-
distances, indices = faiss_index.search(embedding_query, 15)
|
711 |
-
|
712 |
-
outputs = []
|
713 |
-
for i, idx in enumerate(indices[0]):
|
714 |
-
if idx in meta:
|
715 |
-
outputs.append(f"{meta[idx]}")
|
716 |
-
|
717 |
-
return {"output": "\n".join(outputs)}
|
|
|
626 |
results=results,
|
627 |
missing=missing,
|
628 |
search_time=time.time() - start_time
|
629 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|