File size: 11,708 Bytes
ca5d3ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76fc11a
 
 
 
 
 
 
 
 
 
 
 
 
5f516b5
76fc11a
 
ca5d3ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76fc11a
 
 
 
 
 
ca5d3ff
 
 
 
 
76fc11a
ca5d3ff
 
 
 
 
 
76fc11a
ca5d3ff
 
 
76fc11a
 
 
 
ca5d3ff
76fc11a
 
 
 
 
 
 
 
 
 
 
 
 
 
ca5d3ff
76fc11a
 
 
731bda8
 
 
 
 
 
 
 
ca5d3ff
731bda8
 
 
76fc11a
 
731bda8
76fc11a
731bda8
76fc11a
 
731bda8
76fc11a
 
 
 
 
731bda8
ca5d3ff
 
 
76fc11a
 
731bda8
 
 
ca5d3ff
76fc11a
 
 
ca5d3ff
 
731bda8
ca5d3ff
76fc11a
c1a716e
76fc11a
 
 
731bda8
 
76fc11a
 
 
731bda8
76fc11a
ca5d3ff
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# # This file will contain the core logic for the TurBot agent,
# # including the LangGraph setup, state management, and tool definition. 
# 
# import os
# from typing import List
# from dotenv import load_dotenv
# from huggingface_hub.inference._client import InferenceClient
# from langchain_community.vectorstores import FAISS
# from langchain_huggingface import HuggingFaceEmbeddings
# from langchain_core.messages import HumanMessage, AIMessage
# 
# # Load environment variables
# load_dotenv()
# 
# # Initialize the LLM (using a free model from Hugging Face)
# client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct")
# 
# # Load the vector store
# DB_FAISS_PATH = "../vectorstore/db_faiss"
# 
# def search_travel_packages(query: str) -> str:
#     """
#     Search for travel packages based on the user's query.
#     This function searches through the travel package documents to find relevant information.
#     """
#     print(f"BURAAAZ STA SAM JA USAO U SEARCH TRAVEL PACKAGES OVO STVARNO NISAM OCEKIVAO")
#     try:
#         # Load the vector store
#         embeddings = HuggingFaceEmbeddings(
#             model_name='sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2',
#             model_kwargs={'device': 'cpu'}
#         )
#         
#         if os.path.exists(DB_FAISS_PATH):
#             vectorstore = FAISS.load_local(DB_FAISS_PATH, embeddings)
#             
# 
#             print(f"JOOOO USPEJO SAM")
# 
#             # Search for relevant documents
#             docs = vectorstore.similarity_search(query, k=3)
#             
#             # Combine the relevant information
#             context = "\n\n".join([doc.page_content for doc in docs])
#             return f"Found relevant travel information:\n\n{context}"
#         else:
#             return "Travel package database not found. Please ensure the vector store has been created."
#     except Exception as e:
#         return f"Error searching travel packages: {str(e)}"
# 
# def should_search_travel_info(message: str) -> bool:
#     print(f"BURAZENGIJA SRA CU JA U SHOULD SEARCH TRAVEL INFU")
#     """Decide whether to search for travel information based on the message content."""
#     message_lower = message.lower()
#     
#     # Keywords that indicate we should search for travel information
#     travel_keywords = [
#         "destinacija", "let", "hotel", "putovanje", "mediteran", "grčka", "italija", 
#         "turska", "budžet", "porodica", "all inclusive", "preporučite", "ponuda",
#         "destination", "flight", "travel", "mediterranean", "greece", "italy", 
#         "turkey", "budget", "family", "recommend", "offer"
#     ]
#     
#     return any(keyword in message_lower for keyword in travel_keywords)
# 
# def chat_with_turbot(message: str, history: List[List[str]]) -> str:
#     print(f"BURAZ KOJI JE OVO KITA MOJA")
#     """
#     Main function to chat with TurBot.
#     """
#     # Prepare the system message first
#     system_message = """Ti si TurBot, digitalni asistent za turističku agenciju. 
#     Odgovori na srpskom jeziku i budi koristan i prijateljski nastrojen. 
#     Ako ne znaš odgovor na pitanje, preporuči korisniku da pita o putovanjima, letovima, hotelima ili destinacijama."""
#     
#     # Convert messages to the format expected by InferenceClient
#     messages_for_client = []
#     
#     # Add system message first (only once)
#     messages_for_client.append({"role": "system", "content": system_message})
#     
#     # Add conversation history
#     for human, ai in history:
#         if human:
#             messages_for_client.append({"role": "user", "content": human})
#         if ai:
#             messages_for_client.append({"role": "assistant", "content": ai})
#     
#     # Add the current message
#     messages_for_client.append({"role": "user", "content": message})
#     
#     # Check if we should search for travel information
#     context = ""
#     if should_search_travel_info(message):
#         context = search_travel_packages(message)
#         if context:
#             # Add context as additional information
#             messages_for_client.append({"role": "user", "content": f"Kontekst o putovanjima: {context}"})
#     
#     try:
#         # Debug: Print messages being sent to model
#         print(f"\n🔍 DEBUG: Sending {len(messages_for_client)} messages to model:")
#         for i, msg in enumerate(messages_for_client):
#             print(f"  {i+1}. {msg['role']}: {msg['content'][:100]}...")
#         
#         # Generate response
#         response = ""
#         for message_chunk in client.chat_completion(
#             messages_for_client,
#             stream=True,
#             max_tokens=512,
#             temperature=0.7,
#             top_p=0.95,
#         ):
#             if hasattr(message_chunk, 'choices') and message_chunk.choices:
#                 token = message_chunk.choices[0].delta.content
#                 if token:
#                     response += token
#         
#         return response if response else "Izvinjavam se, nisam dobio odgovor od modela. Pokušajte ponovo."
#     except Exception as e:
#         return f"Izvinjavam se, došlo je do greške: {str(e)}" 

import os
from typing import List
from dotenv import load_dotenv
from huggingface_hub.inference._client import InferenceClient
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_core.messages import HumanMessage, AIMessage

# Load environment variables
load_dotenv()

# Initialize the LLM (using a free model from Hugging Face)
client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct")

# Load the vector store
DB_FAISS_PATH = "../vectorstore/db_faiss"

# --- KLJUČNA IZMENA: Učitaj embeddings model i FAISS bazu SAMO JEDNOM na početku ---
# Inicijalizuj embeddings model globalno (ili bar van funkcije search_travel_packages)
print("Initializing HuggingFaceEmbeddings...")
try:
    embeddings_model = HuggingFaceEmbeddings(
        model_name='sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2',
        model_kwargs={'device': 'cpu'} # Možeš probati 'cuda' ako imaš GPU i PyTorch sa CUDA
    )
    print("HuggingFaceEmbeddings initialized.")
except Exception as e:
    print(f"Error initializing embeddings: {str(e)}")
    embeddings_model = None # Postavi na None ako dođe do greške

# Učitaj FAISS vektor bazu globalno
vectorstore = None

if embeddings_model:
    print(f"JOOOOO EMBEDINGS POSTOJI BACI MOJ")

if embeddings_model and os.path.exists(DB_FAISS_PATH):
    print(f"Loading FAISS vector store from {DB_FAISS_PATH}...")
    try:
        vectorstore = FAISS.load_local(DB_FAISS_PATH, embeddings_model, allow_dangerous_deserialization=True)
        print("FAISS vector store loaded successfully.")
    except Exception as e:
        print(f"Error loading FAISS vector store: {str(e)}. Make sure 'faiss-cpu' is installed and vectorstore is valid.")
        vectorstore = None
else:
    print("FAISS vector store path does not exist or embeddings model failed to initialize. RAG will not function.")

# --- KRAJ KLJUČNE IZMENE ---


def search_travel_packages(query: str) -> str:
    """
    Search for travel packages based on the user's query.
    This function searches through the travel package documents to find relevant information.
    """
    print(f"BURAAAZ STA SAM JA USAO U SEARCH TRAVEL PACKAGES OVO STVARNO NISAM OCEKIVAO")
    
    if vectorstore is None:
        return "Travel package database is not available. Please ensure the vector store has been created and loaded correctly."
        
    try:
        # Nema više učitavanja embeddings i vectorstore OVDE! Koristimo globalne instance.
        
        print(f"JOOOO USPEJO SAM (now searching)...") # Poruka je sada relevantnija

        # Search for relevant documents
        docs = vectorstore.similarity_search(query, k=3)
        
        # Combine the relevant information
        context = "\n\n".join([doc.page_content for doc in docs])
        return f"Found relevant travel information:\n\n{context}"
    except Exception as e:
        return f"Error searching travel packages: {str(e)}"

def should_search_travel_info(message: str) -> bool:
    print(f"BURAZENGIJA SRA CU JA U SHOULD SEARCH TRAVEL INFU")
    """Decide whether to search for travel information based on the message content."""
    message_lower = message.lower()
    
    # Keywords that indicate we should search for travel information
    travel_keywords = [
        "destinacija", "let", "hotel", "putovanje", "mediteran", "grčka", "italija", 
        "turska", "budžet", "porodica", "all inclusive", "preporučite", "ponuda",
        "destination", "flight", "travel", "mediterranean", "greece", "italy", 
        "turkey", "budget", "family", "recommend", "offer"
    ]
    
    return any(keyword in message_lower for keyword in travel_keywords)

def chat_with_turbot(message: str, history: List[List[str]]) -> str:
    print(f"BURAZ KOJI JE OVO KITA MOJA")
    """
    Main function to chat with TurBot.
    """
    # Prepare the system message first
    system_message = """Ti si TurBot, digitalni asistent za turističku agenciju. 
    Odgovori na srpskom jeziku i budi koristan i prijateljski nastrojen. 
    Ako ne znaš odgovor na pitanje, preporuči korisniku da pita o putovanjima, letovima, hotelima ili destinacijama."""
    
    # Convert messages to the format expected by InferenceClient
    messages_for_client = []
    
    # Add system message first (only once per interaction, model will handle context)
    messages_for_client.append({"role": "system", "content": system_message})
    
    # Add conversation history
    for human, ai in history:
        if human:
            messages_for_client.append({"role": "user", "content": human})
        if ai:
            messages_for_client.append({"role": "assistant", "content": ai})
    
    # Add the current message
    messages_for_client.append({"role": "user", "content": message})
    
    # Check if we should search for travel information
    context = ""
    if should_search_travel_info(message):
        context = search_travel_packages(message)
        if context:
            # Add context as additional information for the model
            # It's better to add it as a user message or a tool output for the model to use
            messages_for_client.append({"role": "user", "content": f"Relevantne informacije iz baze podataka: {context}"})
    
    try:
        # Debug: Print messages being sent to model
        print(f"\n🔍 DEBUG: Sending {len(messages_for_client)} messages to model:")
        for i, msg in enumerate(messages_for_client):
            print(f"   {i+1}. {msg['role']}: {msg['content'][:150]}...") # Povećan prikaz karaktera za debug
        
        # Generate response
        response = ""
        # The `client.chat_completion` now expects `messages` in a list of dicts.
        # This is already correctly prepared as `messages_for_client`.
        for message_chunk in client.chat_completion(
            messages_for_client, # Prosleđujemo celu listu poruka
            stream=True,
            max_tokens=512,
            temperature=0.7,
            top_p=0.95,
        ):
            if hasattr(message_chunk, 'choices') and message_chunk.choices:
                token = message_chunk.choices[0].delta.content
                if token:
                    response += token
        
        return response if response else "Izvinjavam se, nisam dobio odgovor od modela. Pokušajte ponovo."
    except Exception as e:
        return f"Izvinjavam se, došlo je do greške u pozivu LLM-a: {str(e)}"