File size: 1,644 Bytes
63b2429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a880d8c
5b6144a
a880d8c
5b6144a
a880d8c
5b6144a
 
 
 
 
a880d8c
 
 
 
37200eb
762431e
63b2429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, Depends, Body
from typing import List, Dict
from RAG.Retriever import Retriever, load_vector_store
from RAG.llm import GeminiLLM
import os

app = FastAPI()

# Retrieve API keys from environment variables
userdata = {
    "GEMINI_API_KEY":os.getenv("GEMINI_API_KEY"),
}

GEMINI_KEY = userdata.get("GEMINI_API_KEY")


# import sqlite3

# DATABASE_PATH = "/app/RAG/chroma.sqlite3"

# try:
#     conn = sqlite3.connect(DATABASE_PATH, check_same_thread=False)
#     print("Database connection successful!")
# except sqlite3.OperationalError as e:
#     print(f"Database connection failed: {e}")




PERSIST_DIR = "/app/RAG"
v_store = load_vector_store(GEMINI_KEY, PERSIST_DIR)
retriever = Retriever(v_store)
gemini_llm = GeminiLLM(GEMINI_KEY)


@app.post("/rag")
async def rag_endpoint(query: str = Body(...)):
    # First retrieve relevant documents
    docs = retriever.retrieve_documents(query)

    # Create a clean message list with only role and content keys
    messages = [
        {
            "role": "user",
            "content": str(query)
        },
        {
            "role": "assistant",
            "content": f"Based on the retrieved documents: {str(docs)}, I will now answer your question."
        },
        {
            "role": "user",
            "content": "Please provide a clear and concise answer based on the above documents."
        }
    ]

    # Generate response using the formatted messages
    # formatted_messages = gemini_llm.format_messages(messages)
    response = gemini_llm.generate_response(messages)

    return {
        "query": query,
        "response": response
    }