Upload 2 files
Browse files- app.py +112 -0
- requirements.txt +17 -0
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import os
|
| 4 |
+
import dotenv
|
| 5 |
+
from llama_index.core.indices.vector_store.base import VectorStoreIndex
|
| 6 |
+
from llama_index.vector_stores.qdrant import QdrantVectorStore
|
| 7 |
+
from llama_index.embeddings.fastembed import FastEmbedEmbedding
|
| 8 |
+
from llama_index.core import Settings
|
| 9 |
+
import qdrant_client
|
| 10 |
+
from llama_index.llms.gemini import Gemini
|
| 11 |
+
from llama_index.core.memory import ChatMemoryBuffer
|
| 12 |
+
from llama_index.readers.web import FireCrawlWebReader
|
| 13 |
+
|
| 14 |
+
dotenv.load_dotenv()
|
| 15 |
+
|
| 16 |
+
app = FastAPI()
|
| 17 |
+
|
| 18 |
+
# Initialize session state equivalent
|
| 19 |
+
state = {
|
| 20 |
+
'setup_complete': False,
|
| 21 |
+
'documents': None,
|
| 22 |
+
'chat_history': [],
|
| 23 |
+
'index': None,
|
| 24 |
+
'url': "",
|
| 25 |
+
'collection_name': "",
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
| 29 |
+
|
| 30 |
+
# Setup functions
|
| 31 |
+
def embed_setup():
|
| 32 |
+
Settings.embed_model = FastEmbedEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
| 33 |
+
Settings.llm = Gemini(temperature=0.1, model_name="models/gemini-pro")
|
| 34 |
+
|
| 35 |
+
def qdrant_setup():
|
| 36 |
+
client = qdrant_client.QdrantClient(
|
| 37 |
+
os.getenv("QDRANT_URL"),
|
| 38 |
+
api_key=os.getenv("QDRANT_API_KEY"),
|
| 39 |
+
)
|
| 40 |
+
return client
|
| 41 |
+
|
| 42 |
+
def llm_setup():
|
| 43 |
+
llm = Gemini(api_key=os.getenv("GOOGLE_API_KEY"), temperature=0.1, model_name="models/gemini-pro")
|
| 44 |
+
return llm
|
| 45 |
+
|
| 46 |
+
def ingest_documents(url):
|
| 47 |
+
firecrawl_reader = FireCrawlWebReader(
|
| 48 |
+
api_key=os.getenv("FIRECRAWL_API_KEY"),
|
| 49 |
+
mode="scrape",
|
| 50 |
+
)
|
| 51 |
+
documents = firecrawl_reader.load_data(url=url)
|
| 52 |
+
return documents
|
| 53 |
+
|
| 54 |
+
class SetupRequest(BaseModel):
|
| 55 |
+
url: str = None
|
| 56 |
+
collection_name: str
|
| 57 |
+
|
| 58 |
+
class QueryRequest(BaseModel):
|
| 59 |
+
query: str
|
| 60 |
+
|
| 61 |
+
@app.post("/setup/")
|
| 62 |
+
async def setup(request: SetupRequest):
|
| 63 |
+
state['url'] = request.url
|
| 64 |
+
state['collection_name'] = request.collection_name
|
| 65 |
+
|
| 66 |
+
embed_setup()
|
| 67 |
+
client = qdrant_setup()
|
| 68 |
+
llm = llm_setup()
|
| 69 |
+
vector_store = QdrantVectorStore(client=client, collection_name=state['collection_name'])
|
| 70 |
+
|
| 71 |
+
if state['url']:
|
| 72 |
+
state['documents'] = ingest_documents(state['url'])
|
| 73 |
+
state['index'] = VectorStoreIndex.from_documents(state['documents'], vector_store=vector_store)
|
| 74 |
+
state['setup_complete'] = True
|
| 75 |
+
return {"message": f"Documents ingested from {state['url']} and query engine setup completed successfully!"}
|
| 76 |
+
else:
|
| 77 |
+
state['index'] = VectorStoreIndex.from_vector_store(vector_store=vector_store)
|
| 78 |
+
state['setup_complete'] = True
|
| 79 |
+
return {"message": f"Query engine setup completed successfully using existing collection: {state['collection_name']}"}
|
| 80 |
+
|
| 81 |
+
@app.post("/query/")
|
| 82 |
+
async def query(request: QueryRequest):
|
| 83 |
+
if not state['setup_complete']:
|
| 84 |
+
raise HTTPException(status_code=400, detail="Please complete the setup first")
|
| 85 |
+
|
| 86 |
+
memory = ChatMemoryBuffer.from_defaults(token_limit=4000)
|
| 87 |
+
chat_engine = state['index'].as_chat_engine(
|
| 88 |
+
chat_mode="context",
|
| 89 |
+
memory=memory,
|
| 90 |
+
system_prompt=(
|
| 91 |
+
"""You are an AI assistant for developers, specializing in technical documentation.
|
| 92 |
+
Your task is to provide accurate, concise, and helpful responses based on the given documentation context.
|
| 93 |
+
Given this context, please respond to the following user query:
|
| 94 |
+
{query_str}
|
| 95 |
+
Your response:"""
|
| 96 |
+
),
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
response = chat_engine.chat(request.query)
|
| 100 |
+
state['chat_history'].append(("User", request.query))
|
| 101 |
+
state['chat_history'].append(("Assistant", str(response.response)))
|
| 102 |
+
|
| 103 |
+
return {"response": response.response}
|
| 104 |
+
|
| 105 |
+
@app.get("/chat-history/")
|
| 106 |
+
async def get_chat_history():
|
| 107 |
+
return {"chat_history": state['chat_history']}
|
| 108 |
+
|
| 109 |
+
@app.post("/clear-chat/")
|
| 110 |
+
async def clear_chat():
|
| 111 |
+
state['chat_history'] = []
|
| 112 |
+
return {"message": "Chat history cleared!"}
|
requirements.txt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
llama-index==0.10.17
|
| 2 |
+
llama-index-vector-stores-qdrant
|
| 3 |
+
fastembed
|
| 4 |
+
llama-index-embeddings-fastembed
|
| 5 |
+
llama-index-llms-gemini
|
| 6 |
+
llama-index-embeddings-gemini
|
| 7 |
+
gradio
|
| 8 |
+
llama-index-readers-web
|
| 9 |
+
qdrant-client
|
| 10 |
+
firecrawl-py
|
| 11 |
+
streamlit-analytics2
|
| 12 |
+
llama-index-readers-youtube-transcript
|
| 13 |
+
pypdf2
|
| 14 |
+
python-dotenv
|
| 15 |
+
fastapi
|
| 16 |
+
pydantic
|
| 17 |
+
uvicorn
|