File size: 8,893 Bytes
d8a8160
 
 
9213889
 
d8a8160
 
 
 
 
 
 
 
9213889
d8a8160
 
9213889
 
 
 
 
 
 
 
 
 
 
 
 
d8a8160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9213889
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8a8160
9213889
d8a8160
9213889
d8a8160
9213889
 
 
 
 
 
d8a8160
 
 
 
 
 
 
 
0302d6b
d8a8160
 
9213889
 
0302d6b
 
d8a8160
9213889
d8a8160
 
 
 
 
9213889
d8a8160
 
9213889
d8a8160
 
 
 
 
9213889
 
d8a8160
 
 
9213889
9675d25
9213889
9675d25
 
 
d8a8160
9675d25
 
 
 
 
 
 
9213889
 
 
9675d25
9213889
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8a8160
 
 
9213889
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8a8160
 
 
 
 
 
 
9213889
 
 
9675d25
9213889
 
 
 
 
d8a8160
9213889
d8a8160
9213889
 
8a66b29
9213889
 
 
 
 
 
d8a8160
9213889
d8a8160
 
9213889
 
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
242
243
244
245
246
import os
import google.generativeai as genai
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.chains import RetrievalQA, ConversationalRetrievalChain
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.prompts import PromptTemplate
from pinecone import Pinecone, ServerlessSpec
from langchain_pinecone import PineconeVectorStore
from dotenv import load_dotenv
import threading
from datetime import datetime
import time
from langchain.schema import HumanMessage, AIMessage
from langchain_google_genai import GoogleGenerativeAIEmbeddings
import functools
import hashlib
import logging
import random
from mongodb import get_chat_history

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
)
logger = logging.getLogger(__name__)

# Load environment variables
load_dotenv()

# Configure API keys from environment variables
google_api_key = os.getenv("GOOGLE_API_KEY")
pinecone_api_key = os.getenv("PINECONE_API_KEY")

if not google_api_key or not pinecone_api_key:
    raise ValueError("Missing required API keys in environment variables")

os.environ["GOOGLE_API_KEY"] = google_api_key
os.environ["PINECONE_API_KEY"] = pinecone_api_key

genai.configure(api_key=google_api_key)

# Lấy model chatbot
try:
    generation_config = {
        "temperature": 0.9,
        "top_p": 1,
        "top_k": 1,
        "max_output_tokens": 2048,
    }

    safety_settings = [
        {
            "category": "HARM_CATEGORY_HARASSMENT",
            "threshold": "BLOCK_MEDIUM_AND_ABOVE"
        },
        {
            "category": "HARM_CATEGORY_HATE_SPEECH",
            "threshold": "BLOCK_MEDIUM_AND_ABOVE"
        },
        {
            "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
            "threshold": "BLOCK_MEDIUM_AND_ABOVE"
        },
        {
            "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
            "threshold": "BLOCK_MEDIUM_AND_ABOVE"
        },
    ]

    model = genai.GenerativeModel(
        model_name='models/gemini-2.0-flash',
        generation_config=generation_config,
        safety_settings=safety_settings
    )
    
    # Test the model with a simple prompt
    test_response = model.generate_content("Hello")
    logger.debug(f"Test response: {test_response.text if hasattr(test_response, 'text') else 'No text attribute'}")
    
except Exception as e:
    logger.error(f"Error initializing or testing Gemini model: {str(e)}")
    raise

# Lấy model embedding
# Print available embedding models
# available_models = GoogleGenerativeAIEmbeddings.list_models()
# embedding_models = [model.name for model in available_models if "embedding" in model.name.lower()]
# logger.info(f"Available embedding models: {embedding_models}")

# Use the embedding model
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
# embeddings = genai.GenerativeModel(model_name="models/embedding-004")

# Cache for responses
response_cache = {}
cache_lock = threading.Lock()
# Maximum cache size và thời gian sống (30 phút)
MAX_CACHE_SIZE = 100
CACHE_TTL = 1800  # 30 phút tính bằng giây

# Create a prompt template with conversation history
prompt = PromptTemplate(
    template = """Goal:
You are a professional tour guide assistant that assists users in finding information about places in Da Nang, Vietnam.
You can provide details on restaurants, cafes, hotels, attractions, and other local venues. You have to chat with users, who are Da Nang tourists. 

Return Format:
Respond in friendly, natural, and concise English like a real tour guide. 

Warning:
Let's support users like a real tour guide, not a bot. The information in core knowledge is your own knowledge.
Your knowledge is provided in the Core Knowledge. All of information in Core Knowledge is about Da Nang, Vietnam.
You just care about current time that user mention when user ask about Solana event.
If you do not have enough information to answer user's question, reply with "I don't know. I don't have information about that".

Core knowledge:
{context}

Conversation History:
{chat_history}

User question:
{question}

Your answer:
""",
    input_variables = ["context", "question", "chat_history"],
)

def get_history(user_id):
    """Get conversation history for a specific user from MongoDB"""
    return get_chat_history(user_id)


def get_chain():
    """Get the retrieval chain with Pinecone vector store (singleton pattern)"""
    try:
        start_time = time.time()
        pc = Pinecone(
            api_key=os.environ["PINECONE_API_KEY"]
        )
        
        # Get the vector store from the existing index
        vectorstore = PineconeVectorStore.from_existing_index(
            index_name="testbot768",
            embedding=embeddings,
            text_key="text"
        )
        
        _retriever_instance = vectorstore.as_retriever(search_kwargs={"k": 4})
        logger.info(f"Pinecone retriever initialized in {time.time() - start_time:.2f} seconds")
        return _retriever_instance
    except Exception as e:
        logger.error(f"Error getting vector store from Pinecone: {e}")
        # Fallback to a local vector store or return None
        try:
            # Try to load a local FAISS index if it exists
            start_time = time.time()
            vectorstore = FAISS.load_local("faiss_index", embeddings)
            _retriever_instance = vectorstore.as_retriever(search_kwargs={"k": 3})
            logger.info(f"FAISS retriever initialized in {time.time() - start_time:.2f} seconds")
            return _retriever_instance
        except Exception as faiss_error:
            logger.error(f"Error getting FAISS vector store: {faiss_error}")
            return None

def clean_cache():
    """Clean expired cache entries"""
    with cache_lock:
        current_time = time.time()
        expired_keys = [k for k, v in response_cache.items() if current_time - v['timestamp'] > CACHE_TTL]
        
        for key in expired_keys:
            del response_cache[key]
            
        # Nếu cache vẫn quá lớn, xóa các mục cũ nhất
        if len(response_cache) > MAX_CACHE_SIZE:
            # Sắp xếp theo thời gian và giữ lại MAX_CACHE_SIZE mục mới nhất
            sorted_items = sorted(response_cache.items(), key=lambda x: x[1]['timestamp'])
            items_to_remove = sorted_items[:len(sorted_items) - MAX_CACHE_SIZE]
            
            for key, _ in items_to_remove:
                del response_cache[key]

def generate_cache_key(request, user_id):
    """Generate a unique cache key from the request and user_id"""
    # Tạo một chuỗi kết hợp để hash
    combined = f"{request.strip().lower()}:{user_id}"
    # Tạo MD5 hash
    return hashlib.md5(combined.encode()).hexdigest()

def chat(request, user_id="default_user"):
    """Process a chat request from a specific user"""
    start_time = time.time()
    
    # Định kỳ xóa các mục cache hết hạn
    if random.random() < 0.1:
        clean_cache()
    
    cache_key = generate_cache_key(request, user_id)
    
    with cache_lock:
        if cache_key in response_cache:
            cache_data = response_cache[cache_key]
            if time.time() - cache_data['timestamp'] <= CACHE_TTL:
                logger.info(f"Cache hit for user {user_id}, request: '{request[:30]}...'")
                cache_data['timestamp'] = time.time()
                return cache_data['response']
    try:
        retriever = get_chain()
        if not retriever:
            return "Error: Could not initialize retriever"
        
        current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        
        # Sử dụng invoke thay vì get_relevant_documents
        retrieved_docs = retriever.invoke(request)
        context = "\n".join([doc.page_content for doc in retrieved_docs])
        
        # Sử dụng generate_content thay vì invoke cho model Gemini
        prompt_text = prompt.format(
            context=context,
            question=request,
            chat_history=get_history(user_id)
        )
        print(prompt_text)
        
        response = model.generate_content(prompt_text)
        answer = response.text  # Sử dụng .text thay vì .content
        
        # Lưu vào cache
        with cache_lock:
            response_cache[cache_key] = {
                'response': answer,
                'timestamp': time.time()
            }
        
        logger.info(f"Total processing time: {time.time() - start_time:.2f} seconds")
        return answer
    except Exception as e:
        logger.error(f"Error in chat: {e}")
        return f"I don't know how to answer that right now. Let me forward this to the admin team."