File size: 10,508 Bytes
bc15143 63df3f2 bc15143 11f96c1 bc15143 63df3f2 43c94a5 d9309f4 bc15143 4ef8a97 63df3f2 11f96c1 63df3f2 d9309f4 63df3f2 11f96c1 63df3f2 11f96c1 63df3f2 11f96c1 63df3f2 de5a712 63df3f2 11f96c1 63df3f2 de5a712 63df3f2 11f96c1 63df3f2 11f96c1 63df3f2 11f96c1 63df3f2 11f96c1 63df3f2 43c94a5 d9309f4 e392631 d9309f4 2142606 d9309f4 2142606 d9309f4 2142606 d9309f4 2142606 bc15143 2142606 d9309f4 bc15143 d9309f4 a3e0c24 d9309f4 bc15143 e392631 d9309f4 e392631 34f4e14 e392631 21487ff 2142606 bc15143 2142606 bc15143 2142606 b1911fe bc15143 d9309f4 2142606 43c94a5 63df3f2 b1911fe |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
from fastapi import FastAPI, HTTPException, Header, Depends, BackgroundTasks, Query
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
from typing import List, Optional, Dict, AsyncGenerator
import json
import os
import logging
from txtai.embeddings import Embeddings
import pandas as pd
import glob
import uuid
import httpx
import asyncio
import requests
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI(
title="Embeddings API",
description="An API for creating and querying text embeddings indexes.",
version="1.0.0"
)
CHAT_AUTH_KEY = os.environ.get("CHAT_AUTH_KEY", "default_secret_key")
# Enable CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
embeddings = Embeddings({"path": "avsolatorio/GIST-all-MiniLM-L6-v2"})
class DocumentRequest(BaseModel):
index_id: str = Field(..., description="Unique identifier for the index")
documents: List[str] = Field(..., description="List of documents to be indexed")
class QueryRequest(BaseModel):
index_id: str = Field(..., description="Unique identifier for the index to query")
query: str = Field(..., description="The search query")
num_results: int = Field(..., description="Number of results to return", ge=1)
def save_embeddings(index_id: str, document_list: List[str]):
try:
folder_path = f"/app/indexes/{index_id}"
os.makedirs(folder_path, exist_ok=True)
# Save embeddings
embeddings.save(f"{folder_path}/embeddings")
# Save document_list
with open(f"{folder_path}/document_list.json", "w") as f:
json.dump(document_list, f)
logger.info(f"Embeddings and document list saved for index_id: {index_id}")
except Exception as e:
logger.error(f"Error saving embeddings for index_id {index_id}: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error saving embeddings: {str(e)}")
def load_embeddings(index_id: str) -> List[str]:
try:
folder_path = f"/app/indexes/{index_id}"
if not os.path.exists(folder_path):
logger.error(f"Index not found for index_id: {index_id}")
raise HTTPException(status_code=404, detail="Index not found")
# Load embeddings
embeddings.load(f"{folder_path}/embeddings")
# Load document_list
with open(f"{folder_path}/document_list.json", "r") as f:
document_list = json.load(f)
logger.info(f"Embeddings and document list loaded for index_id: {index_id}")
return document_list
except Exception as e:
logger.error(f"Error loading embeddings for index_id {index_id}: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error loading embeddings: {str(e)}")
@app.post("/create_index/", response_model=dict, tags=["Index Operations"])
async def create_index(request: DocumentRequest):
"""
Create a new index with the given documents.
- **index_id**: Unique identifier for the index
- **documents**: List of documents to be indexed
"""
try:
document_list = [(i, text, None) for i, text in enumerate(request.documents)]
embeddings.index(document_list)
save_embeddings(request.index_id, request.documents) # Save the original documents
logger.info(f"Index created successfully for index_id: {request.index_id}")
return {"message": "Index created successfully"}
except Exception as e:
logger.error(f"Error creating index: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error creating index: {str(e)}")
@app.post("/query_index/", response_model=dict, tags=["Index Operations"])
async def query_index(request: QueryRequest):
"""
Query an existing index with the given search query.
- **index_id**: Unique identifier for the index to query
- **query**: The search query
- **num_results**: Number of results to return
"""
try:
document_list = load_embeddings(request.index_id)
results = embeddings.search(request.query, request.num_results)
queried_texts = [document_list[idx[0]] for idx in results]
logger.info(f"Query executed successfully for index_id: {request.index_id}")
return {"queried_texts": queried_texts}
except Exception as e:
logger.error(f"Error querying index: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error querying index: {str(e)}")
def process_csv_file(file_path):
try:
df = pd.read_csv(file_path)
df_rows = df.apply(lambda row: ' '.join(row.values.astype(str)), axis=1)
txtai_data = [(i, row, None) for i, row in enumerate(df_rows)]
return txtai_data, df_rows.tolist()
except Exception as e:
logger.error(f"Error processing CSV file {file_path}: {str(e)}")
return None, None
def check_and_index_csv_files():
index_data_folder = "/app/index_data"
if not os.path.exists(index_data_folder):
logger.warning(f"index_data folder not found: {index_data_folder}")
return
csv_files = glob.glob(os.path.join(index_data_folder, "*.csv"))
for csv_file in csv_files:
index_id = os.path.splitext(os.path.basename(csv_file))[0]
if not os.path.exists(f"/app/indexes/{index_id}"):
logger.info(f"Processing CSV file: {csv_file}")
txtai_data, documents = process_csv_file(csv_file)
if txtai_data and documents:
embeddings.index(txtai_data)
save_embeddings(index_id, documents)
logger.info(f"CSV file indexed successfully: {csv_file}")
else:
logger.warning(f"Failed to process CSV file: {csv_file}")
else:
logger.info(f"Index already exists for: {csv_file}")
# ... [Previous code for DocumentRequest, QueryRequest, save_embeddings, load_embeddings, create_index, query_index, process_csv_file, check_and_index_csv_files remains the same]
class ChatRequest(BaseModel):
query: str = Field(..., description="The user's query")
index_id: str = Field(..., description="Unique identifier for the index to query")
conversation_id: Optional[str] = Field(None, description="Unique identifier for the conversation")
model_id: str = Field(..., description="Identifier for the LLM model to use")
user_id: str = Field(..., description="Unique identifier for the user")
enable_followup: bool = Field(default=False, description="Flag to enable follow-up questions")
def get_api_key(x_api_key: str = Header(...)) -> str:
if x_api_key != CHAT_AUTH_KEY:
raise HTTPException(status_code=403, detail="Invalid API key")
return x_api_key
def stream_llm_request(api_key: str, llm_request: dict, endpoint_url: str):
"""
Make a streaming request to the LLM service using requests.
"""
try:
headers = {
"accept": "text/event-stream",
"X-API-Key": api_key,
"Content-Type": "application/json"
}
with requests.post(endpoint_url, headers=headers, json=llm_request, stream=True) as response:
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail="Error from LLM service")
for chunk in response.iter_content(chunk_size=None, decode_unicode=True):
if chunk:
yield chunk
except requests.RequestException as e:
logger.error(f"HTTP error occurred while making LLM request: {str(e)}")
raise HTTPException(status_code=500, detail=f"HTTP error occurred while making LLM request: {str(e)}")
except Exception as e:
logger.error(f"Unexpected error occurred while making LLM request: {str(e)}")
raise HTTPException(status_code=500, detail=f"Unexpected error occurred while making LLM request: {str(e)}")
@app.post("/chat/", response_class=StreamingResponse, tags=["Chat"])
async def chat(request: ChatRequest, background_tasks: BackgroundTasks, api_key: str = Depends(get_api_key)):
try:
document_list = load_embeddings(request.index_id)
search_results = embeddings.search(request.query, 6)
context = "\n".join([document_list[idx[0]] for idx in search_results])
rag_prompt = f"Based on the following context, please answer the user's question:\n\nContext:\n{context}\n\nUser's question: {request.query}\n\nAnswer:"
system_prompt = "You are a helpful assistant tasked with providing answers using the context provided"
conversation_id = request.conversation_id or str(uuid.uuid4())
if request.enable_followup:
llm_request = {
"query": rag_prompt,
"model_id": 'openai/gpt-4o-mini',
"conversation_id": conversation_id,
"user_id": request.user_id
}
endpoint_url = "https://pvanand-general-chat.hf.space/v2/followup-agent"
else:
llm_request = {
"prompt": rag_prompt,
"system_message": system_prompt,
"model_id": request.model_id,
"conversation_id": conversation_id,
"user_id": request.user_id
}
endpoint_url = "https://pvanand-audio-chat.hf.space/llm-agent"
logger.info(f"Starting chat response generation for user: {request.user_id} Full request: {llm_request}")
def response_generator():
full_response = ""
for chunk in stream_llm_request(api_key, llm_request, endpoint_url):
full_response += chunk
yield chunk
logger.info(f"Finished chat response generation for user: {request.user_id} Full response: {full_response}")
return StreamingResponse(response_generator(), media_type="text/event-stream")
except Exception as e:
logger.error(f"Error in chat endpoint: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error in chat endpoint: {str(e)}")
@app.on_event("startup")
async def startup_event():
check_and_index_csv_files()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860) |