cryogenic22 commited on
Commit
34000bf
·
verified ·
1 Parent(s): e7881d4

Update utils/database.py

Browse files
Files changed (1) hide show
  1. utils/database.py +12 -18
utils/database.py CHANGED
@@ -107,21 +107,8 @@ def format_chat_history(messages: list[BaseMessage]) -> list[dict]:
107
  formatted.append({"role": "system", "content": msg.content})
108
  return formatted
109
 
110
- # utils/database.py
111
- from langchain.memory import ConversationBufferWindowMemory
112
- from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
113
- from langchain.chains import ConversationalRetrievalChain
114
- from langchain.chat_models import ChatOpenAI
115
- from langchain.agents import AgentExecutor, Tool, create_openai_tools_agent
116
- from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
117
- from langchain.agents.format_scratchpad.tools import format_to_tool_messages
118
- from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser
119
- from langchain_core.runnables import RunnablePassthrough
120
- import os
121
- import streamlit as st
122
-
123
  def initialize_qa_system(vector_store):
124
- """Initialize QA system with proper chat handling"""
125
  try:
126
  llm = ChatOpenAI(
127
  temperature=0.5,
@@ -156,7 +143,7 @@ def initialize_qa_system(vector_store):
156
  "agent_scratchpad": lambda x: format_to_tool_messages(x["intermediate_steps"])
157
  }
158
  | prompt
159
- | llm.bind(stop=["\nHuman:"])
160
  | OpenAIToolsAgentOutputParser()
161
  )
162
 
@@ -183,7 +170,7 @@ def initialize_qa_system(vector_store):
183
  return None
184
 
185
  def initialize_faiss(embeddings, documents, document_names):
186
- """Initialize FAISS vector store"""
187
  try:
188
  from langchain.vectorstores import FAISS
189
 
@@ -200,7 +187,7 @@ def initialize_faiss(embeddings, documents, document_names):
200
 
201
  @st.cache_resource
202
  def get_embeddings_model():
203
- """Get the embeddings model"""
204
  try:
205
  from langchain.embeddings import HuggingFaceEmbeddings
206
 
@@ -209,4 +196,11 @@ def get_embeddings_model():
209
  return embeddings
210
  except Exception as e:
211
  st.error(f"Error loading embeddings model: {e}")
212
- return None
 
 
 
 
 
 
 
 
107
  formatted.append({"role": "system", "content": msg.content})
108
  return formatted
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  def initialize_qa_system(vector_store):
111
+
112
  try:
113
  llm = ChatOpenAI(
114
  temperature=0.5,
 
143
  "agent_scratchpad": lambda x: format_to_tool_messages(x["intermediate_steps"])
144
  }
145
  | prompt
146
+ | llm.bind(stop=["\\nHuman:"])
147
  | OpenAIToolsAgentOutputParser()
148
  )
149
 
 
170
  return None
171
 
172
  def initialize_faiss(embeddings, documents, document_names):
173
+
174
  try:
175
  from langchain.vectorstores import FAISS
176
 
 
187
 
188
  @st.cache_resource
189
  def get_embeddings_model():
190
+
191
  try:
192
  from langchain.embeddings import HuggingFaceEmbeddings
193
 
 
196
  return embeddings
197
  except Exception as e:
198
  st.error(f"Error loading embeddings model: {e}")
199
+ return None
200
+
201
+ # Writing the updated contents back to the original files
202
+ with open(components_chat_path, 'w') as chat_file:
203
+ chat_file.write(updated_components_chat_content)
204
+
205
+ with open(utils_database_path, 'w') as database_file:
206
+ database_file.write(updated_utils_database_content)