Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,70 +1,74 @@
|
|
| 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 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
return {"answer": response}
|
|
|
|
| 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 |
+
@app.get("/")
|
| 62 |
+
def read_root():
|
| 63 |
+
return {"Hello": "Welcome to the Gemini RAG API. Go to /docs to test."}
|
| 64 |
+
|
| 65 |
+
class Query(BaseModel):
|
| 66 |
+
query: str
|
| 67 |
+
|
| 68 |
+
@app.post("/ask")
|
| 69 |
+
async def ask_query(query: Query):
|
| 70 |
+
# Use the .invoke() method on your new LCEL chain
|
| 71 |
+
# It expects a dictionary matching the 'itemgetter' keys
|
| 72 |
+
response = retrieval_chain.invoke({"input": query.query})
|
| 73 |
+
|
| 74 |
return {"answer": response}
|