Srinivasulu kethanaboina commited on
Commit
52bd5b0
1 Parent(s): 7622824

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -1
app.py CHANGED
@@ -5,6 +5,11 @@ from llama_index.core import StorageContext, load_index_from_storage, VectorStor
5
  from llama_index.llms.huggingface import HuggingFaceInferenceAPI
6
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
7
  from sentence_transformers import SentenceTransformer
 
 
 
 
 
8
 
9
  # Load environment variables
10
  load_dotenv()
@@ -41,6 +46,9 @@ def data_ingestion_from_directory():
41
  index.storage_context.persist(persist_dir=PERSIST_DIR)
42
 
43
  def handle_query(query):
 
 
 
44
  chat_text_qa_msgs = [
45
  (
46
  "user",
@@ -60,7 +68,7 @@ def handle_query(query):
60
  storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
61
  index = load_index_from_storage(storage_context)
62
 
63
- # Use chat history to enhance response
64
  context_str = ""
65
  for past_query, response in reversed(current_chat_history):
66
  if past_query.strip():
@@ -79,6 +87,11 @@ def handle_query(query):
79
  # Update current chat history
80
  current_chat_history.append((query, response))
81
 
 
 
 
 
 
82
  return response
83
 
84
  # Example usage: Process PDF ingestion from directory
 
5
  from llama_index.llms.huggingface import HuggingFaceInferenceAPI
6
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
7
  from sentence_transformers import SentenceTransformer
8
+ import csv
9
+ import os
10
+
11
+ PERSIST_DIR = "history" # Replace with your actual directory path
12
+ CSV_FILE = os.path.join(PERSIST_DIR, "chat_history.csv")
13
 
14
  # Load environment variables
15
  load_dotenv()
 
46
  index.storage_context.persist(persist_dir=PERSIST_DIR)
47
 
48
  def handle_query(query):
49
+ # Ensure the directory exists or create it
50
+ os.makedirs(PERSIST_DIR, exist_ok=True)
51
+
52
  chat_text_qa_msgs = [
53
  (
54
  "user",
 
68
  storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
69
  index = load_index_from_storage(storage_context)
70
 
71
+ # Use chat history to enhance response (assuming current_chat_history is defined)
72
  context_str = ""
73
  for past_query, response in reversed(current_chat_history):
74
  if past_query.strip():
 
87
  # Update current chat history
88
  current_chat_history.append((query, response))
89
 
90
+ # Save chat history to CSV
91
+ with open(CSV_FILE, 'a', newline='', encoding='utf-8') as file:
92
+ csv_writer = csv.writer(file)
93
+ csv_writer.writerow([query, response])
94
+
95
  return response
96
 
97
  # Example usage: Process PDF ingestion from directory