33import logging
44from werkzeug .utils import secure_filename
55
6- from rag .ingestion_pipeline import IngestionPipeline
7- from vector_database .qdrant_vdb import QdrantVDB
6+ from genai .rag .ingestion_pipeline import IngestionPipeline
7+ from genai .vector_database .qdrant_vdb import QdrantVDB
8+ from genai .rag .llm .chat_model import ChatModel
9+ from genai .service .rag_service import (
10+ retrieve_similar_docs ,
11+ prepare_prompt ,
12+ process_raw_messages
13+ )
14+
815
916# Set Logging
1017logging .getLogger ().setLevel (logging .INFO )
1118
19+ # Set ChatModel
20+ llm = ChatModel (model_name = "llama3.3:latest" )
21+
22+ # Set Vector Database
23+ qdrant = QdrantVDB ()
24+
1225generate_bp = Blueprint ('generate' , __name__ )
1326
1427
@@ -31,8 +44,6 @@ def upload_file():
3144
3245 try :
3346 collection_name = "recipes"
34- # Initialize vector database
35- qdrant = QdrantVDB ()
3647 # Check if the file already in the collection
3748 if (qdrant .client .collection_exists (collection_name )
3849 and qdrant .collection_contains_file (
@@ -69,6 +80,60 @@ def upload_file():
6980 os .remove (file_path )
7081
7182
72- @generate_bp .route ('/api /generate' , methods = ['POST' ])
83+ @generate_bp .route ('/genai /generate' , methods = ['POST' ])
7384def generate ():
74- return jsonify ({'output' : 'Hello World!' })
85+ """
86+ API Endpoint for generating recipe responses using retrieved context.
87+
88+ This endpoint processes a user query against a vector database of recipes
89+ and returns an AI-generated response using both retrieved context and
90+ the full conversation history provided in the request.
91+
92+ Request Body:
93+ query (str): The user's recipe-related query
94+ messages (List[Dict]): Full conversation history,
95+ each with 'role' and 'content'
96+ Example:
97+ [
98+ {"role": "USER", "content": "I have eggs and tomatoes."},
99+ {"role": "ASSISTANT", "content": "You could make shakshuka."}
100+ ]
101+
102+ Returns:
103+ JSON response containing:
104+ - 'response': The generated assistant reply
105+ """
106+ data = request .get_json ()
107+
108+ if not data or "query" not in data or "messages" not in data :
109+ return jsonify ({"error" : "Missing 'query' or 'messages'" }), 400
110+
111+ query = data ["query" ]
112+ messages_raw = data ["messages" ]
113+
114+ try :
115+ collection_name = "recipes"
116+
117+ if qdrant .client .collection_exists (collection_name ):
118+ # Get vector store
119+ vector_store = qdrant .create_and_get_vector_storage (
120+ collection_name
121+ )
122+ # turn raw message into BaseMessage type
123+ messages = process_raw_messages (messages_raw )
124+ retrieved_docs = retrieve_similar_docs (vector_store , query )
125+ prompt = prepare_prompt (
126+ llm .get_system_prompt (),
127+ query ,
128+ retrieved_docs ,
129+ messages
130+ )
131+
132+ response = llm .invoke (prompt )
133+
134+ return jsonify ({
135+ "response" : response .content ,
136+ }), 200
137+
138+ except Exception as e :
139+ return jsonify ({"error" : str (e )}), 500
0 commit comments