-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path02_rag_doc_querier.py
More file actions
65 lines (49 loc) · 1.86 KB
/
Copy path02_rag_doc_querier.py
File metadata and controls
65 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import argparse
import os
import sys
from google import genai
from dotenv import load_dotenv
load_dotenv()
def chunk_text(text, chunk_size=2000):
return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
def query_doc(file_path):
if not os.path.exists(file_path):
print(f"Error: File {file_path} not found.")
return
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
chunks = chunk_text(content)
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
print(f"Loaded {file_path} and split into {len(chunks)} chunks.")
print("Type 'exit' to quit.")
while True:
query = input("\nAsk a question about the document: ")
if query.lower() == 'exit':
break
# In a real RAG we would use embeddings, but for a simple CLI tool,
# we'll provide the most relevant context or just the whole thing if small.
# Here we'll just send the query with the context of the file.
prompt = f"""
Use the following document content to answer the user's question.
Document Content:
{content[:15000]} # Simple truncation to fit context
Question: {query}
"""
try:
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=prompt
)
print(f"\nAnswer: {response.text}")
except Exception as e:
print(f"Error: {e}")
def main():
parser = argparse.ArgumentParser(description="RAG Document Querier CLI")
parser.add_argument("file", help="Path to the text or markdown file")
args = parser.parse_args()
if not os.getenv("GEMINI_API_KEY"):
print("Error: GEMINI_API_KEY not found.")
sys.exit(1)
query_doc(args.file)
if __name__ == "__main__":
main()