Manavraj commited on
Commit
aa652bb
·
verified ·
1 Parent(s): ed736a1

Initial Commit

Browse files
Files changed (8) hide show
  1. .env +1 -0
  2. Dockerfile +0 -0
  3. data.txt +9 -0
  4. faiss_index/index.faiss +0 -0
  5. faiss_index/index.pkl +3 -0
  6. ingest.py +62 -0
  7. main.py +70 -0
  8. requirements.txt +0 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ GEMINI_API_KEY="AIzaSyC-46PxhMR-cze_KwETlwBj6fA_ETmbFSk"
Dockerfile ADDED
File without changes
data.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ The Iridium 9603 is a very small satellite communication module used for sending and receiving short data messages. It works through the Iridium satellite network, which covers the entire Earth — including oceans, mountains, and polar regions. Because of this, the module can be used in remote areas where there is no mobile network.
2
+
3
+ One of the main advantages of the Iridium 9603 is its small size. It is one of the smallest satellite transceivers available. This makes it suitable for compact devices, portable trackers, and battery-powered equipment. It is commonly used in GPS trackers, fleet monitoring, emergency alert devices, and environmental sensors.
4
+
5
+ The device works on the Iridium Short Burst Data (SBD) service. This allows it to send small packets of data very quickly, with low delay, because the satellites are in low Earth orbit. Messages travel faster than with high-altitude satellites, making communication more responsive.
6
+
7
+ The module does not require a SIM card, and it uses simple AT commands, making it easy to integrate with microcontrollers and embedded systems. It also has a single connector for power and data control, which simplifies hardware design. It can also notify the user automatically when new messages are waiting.
8
+
9
+ The Iridium 9603 can operate in harsh conditions — from -40°C to +85°C — and works even in humid environments. It meets regulatory standards like FCC and CE, meaning it is approved for use in major regions around the world.
faiss_index/index.faiss ADDED
Binary file (24.6 kB). View file
 
faiss_index/index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f882e7230ffd50b4275bc0d529f1bbc6f43282e3302a0b65165e2a5172405ff7
3
+ size 1973
ingest.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ from dotenv import load_dotenv
4
+ from langchain_community.document_loaders import TextLoader
5
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
6
+ from langchain_community.vectorstores import FAISS
7
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
8
+
9
+ load_dotenv()
10
+ api_key = os.getenv("GEMINI_API_KEY")
11
+
12
+ # --- 1. Load Document ---
13
+ print("Loading document...")
14
+ loader = TextLoader("data.txt")
15
+ documents = loader.load()
16
+
17
+ # --- 2. Split Document ---
18
+ print("Splitting text...")
19
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
20
+ texts = text_splitter.split_documents(documents)
21
+
22
+ if not texts:
23
+ print("No text found in 'data.txt'. Exiting.")
24
+ exit()
25
+
26
+ # --- 3. Create Embeddings & Vector Store ---
27
+ print("Initializing embeddings...")
28
+ embeddings = GoogleGenerativeAIEmbeddings(
29
+ model="gemini-embedding-001",
30
+ google_api_key=api_key
31
+ )
32
+
33
+ # Set a reasonable batch size (Google's API limit is 100)
34
+ batch_size = 90
35
+ db = None
36
+
37
+ try:
38
+ # Create the vector store with the first batch
39
+ first_batch = texts[0:batch_size]
40
+ print(f"Creating vector store with initial batch (0 to {len(first_batch)})...")
41
+ db = FAISS.from_documents(first_batch, embeddings)
42
+
43
+ # Now, add the rest of the batches
44
+ for i in range(batch_size, len(texts), batch_size):
45
+ batch = texts[i:i+batch_size]
46
+ print(f"Adding batch {i} to {i+len(batch)}...")
47
+
48
+ # Use add_documents to add to the existing index
49
+ db.add_documents(batch)
50
+
51
+ # Optional: Add a small delay if you still see rate limit errors
52
+ # time.sleep(1)
53
+
54
+ # --- 4. Save the final vector store ---
55
+ db.save_local("faiss_index")
56
+ print("\nDone. Vector store saved as 'faiss_index'")
57
+
58
+ except Exception as e:
59
+ print(f"\n--- AN ERROR OCCURRED ---")
60
+ print(f"{e}")
61
+ print("\nThis was likely a network timeout or API issue.")
62
+ print("Please check your firewall/VPN settings and try running the script again.")
main.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from fastapi import FastAPI
4
+ from pydantic import BaseModel
5
+
6
+ # --- Imports for LCEL ---
7
+ # We replace the create_..._chain imports with these building blocks
8
+ from operator import itemgetter # A handy tool to get a value from a dict
9
+ from langchain_core.runnables import RunnablePassthrough, RunnableParallel
10
+ from langchain_core.output_parsers import StrOutputParser
11
+ # --- End of new imports ---
12
+
13
+ from langchain_community.vectorstores import FAISS
14
+ from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
15
+ from langchain_core.prompts import PromptTemplate
16
+
17
+ # --- 1. SETUP (Your code is perfect) ---
18
+ load_dotenv()
19
+ api_key = os.getenv("GEMINI_API_KEY")
20
+
21
+ app = FastAPI()
22
+
23
+ # Initialize your models and retriever
24
+ embeddings = GoogleGenerativeAIEmbeddings(
25
+ model="gemini-embedding-001", google_api_key=api_key
26
+ )
27
+ vector_store = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
28
+ retriever = vector_store.as_retriever()
29
+
30
+ llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", google_api_key=api_key)
31
+
32
+ # Your prompt template
33
+ template = """
34
+ You are a helpful AI assistant. Answer the user's question based on the
35
+ following context. If you don't know the answer, just say "I don't know."
36
+
37
+ Context: {context}
38
+ Question: {input}
39
+ """
40
+ prompt = PromptTemplate.from_template(template)
41
+
42
+
43
+ # --- 2. BUILD YOUR CHAIN WITH LCEL ---
44
+
45
+ # This is the equivalent of 'create_stuff_documents_chain'
46
+ # It "stuffs" the context and input into the prompt, then calls the model.
47
+ document_chain = prompt | llm | StrOutputParser()
48
+
49
+ # This is the equivalent of 'create_retrieval_chain'
50
+ # It defines the full RAG process.
51
+ retrieval_chain = RunnableParallel(
52
+ # "context": Run the retriever on the user's "input"
53
+ context=(itemgetter("input") | retriever),
54
+ # "input": Pass the user's "input" straight through
55
+ input=itemgetter("input")
56
+ ) | document_chain # Pipe the resulting {context, input} dict into our document_chain
57
+
58
+
59
+ # --- 3. YOUR API (No changes needed) ---
60
+
61
+ class Query(BaseModel):
62
+ query: str
63
+
64
+ @app.post("/ask")
65
+ async def ask_query(query: Query):
66
+ # Use the .invoke() method on your new LCEL chain
67
+ # It expects a dictionary matching the 'itemgetter' keys
68
+ response = retrieval_chain.invoke({"input": query.query})
69
+
70
+ return {"answer": response}
requirements.txt ADDED
Binary file (466 Bytes). View file