|
1 | 1 | import os |
2 | 2 | import time |
3 | 3 |
|
| 4 | +from dotenv import load_dotenv |
4 | 5 | from elasticsearch import Elasticsearch, helpers |
5 | 6 | from openai import OpenAI |
6 | 7 |
|
7 | | -ES_URL = "http://localhost:9200" |
8 | | -ES_API_KEY = "NDdDQWM1b0JPSDBFTV9JQzA0WVo6eHFXcWFJQmFYNzBwS1RjUllpRUNHZw==" |
9 | | -INDEX_NAME = "team-data" |
10 | | -LOCAL_AI_URL = "http://localhost:8080/v1" # Local AI server URL |
11 | | -DATASET_FOLDER = "./Dataset" |
| 8 | +load_dotenv() |
| 9 | + |
| 10 | +ES_URL = os.getenv("ES_URL", "http://localhost:9200") |
| 11 | +ES_API_KEY = os.getenv("ES_API_KEY") |
| 12 | +INDEX_NAME = os.getenv("INDEX_NAME", "team-data") |
| 13 | +LOCAL_AI_URL = os.getenv("LOCAL_AI_URL", "http://localhost:8080/v1") |
| 14 | +DATASET_FOLDER = os.getenv("DATASET_FOLDER", "./Dataset") |
12 | 15 |
|
13 | 16 |
|
14 | 17 | es_client = Elasticsearch(ES_URL, api_key=ES_API_KEY) |
@@ -177,23 +180,34 @@ def query_local_ai(prompt, model): |
177 | 180 | print(f"🔍 Search: '{query}'") |
178 | 181 | search_results, search_latency = semantic_search(query) |
179 | 182 |
|
180 | | - context = "Information found:\n" |
181 | | - for hit in search_results: |
| 183 | + context = "" |
| 184 | + citations = [] |
| 185 | + for idx, hit in enumerate(search_results, 1): |
182 | 186 | source = hit["_source"] |
183 | | - context += f"File: {source['file_title']}\n" |
| 187 | + context += f"[{idx}] File: {source['file_title']}\n" |
184 | 188 | context += f"Content: {source['file_content']}\n\n" |
| 189 | + citations.append(f"[{idx}] {source['file_title']}") |
| 190 | + |
| 191 | + prompt = f"""Based on the following documents, answer the user's question. |
| 192 | + You MUST cite your sources using the format [1], [2], etc. when referencing information from the documents. |
| 193 | +
|
| 194 | + Documents: |
| 195 | + {context} |
| 196 | +
|
| 197 | + User Question: {query} |
185 | 198 |
|
186 | | - prompt = f"{context}\nQuestion: {query}\nAnswer:" |
| 199 | + Answer (remember to include citations [1], [2], etc. when referencing specific information) |
| 200 | + """ |
187 | 201 |
|
188 | | - # ai_model = "llama-smoltalk-3.2-1b-instruct" |
189 | | - # ai_model = "dolphin3.0-qwen2.5-0.5b" |
190 | | - # ai_model = "fastllama-3.2-1b-instruct" |
191 | 202 | ai_model = "smollm2-1.7b-instruct" |
192 | 203 |
|
193 | 204 | print(f"🤖 Asking to model: {ai_model}") |
194 | 205 | response, ai_latency, tokens_per_second = query_local_ai(prompt, ai_model) |
195 | 206 |
|
196 | 207 | print(f"\n💡 Question: {query}\n📝 Answer: {response}") |
| 208 | + print("\n📚 Citations:") |
| 209 | + for citation in citations: |
| 210 | + print(f" {citation}") |
197 | 211 |
|
198 | 212 | print(f"✅ Indexed {success} documents in {bulk_latency:.0f}ms") |
199 | 213 | print(f"🔍 Search Latency: {search_latency:.0f}ms") |
|
0 commit comments