cryogenic22 commited on
Commit
e7881d4
·
verified ·
1 Parent(s): 89933fd

Update components/chat.py

Browse files
Files changed (1) hide show
  1. components/chat.py +0 -118
components/chat.py CHANGED
@@ -41,124 +41,6 @@ def display_chat_interface():
41
  st.error(f"Error generating response: {e}")
42
  import traceback
43
  st.error(f"Detailed error: {traceback.format_exc()}")
44
- """
45
 
46
- # Updating utils/database.py to ensure consistency in prompt variable names and agent handling.
47
 
48
- updated_utils_database_content = """
49
- # utils/database.py
50
- from langchain.memory import ConversationBufferWindowMemory
51
- from langchain_core.messages import (
52
- HumanMessage,
53
- AIMessage,
54
- SystemMessage,
55
- BaseMessage # Added this import
56
- )
57
- from langchain.chains import ConversationalRetrievalChain
58
- from langchain.chat_models import ChatOpenAI
59
- from langchain.agents import AgentExecutor, Tool, create_openai_tools_agent
60
- from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
61
- from langchain.agents.format_scratchpad.tools import format_to_tool_messages
62
- from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputParser
63
- from langchain_core.runnables import RunnablePassthrough
64
- import os
65
- import streamlit as st
66
-
67
- def initialize_qa_system(vector_store):
68
- \"\"\"Initialize QA system with proper chat handling\"\"\"
69
- try:
70
- llm = ChatOpenAI(
71
- temperature=0.5,
72
- model_name="gpt-4",
73
- api_key=os.environ.get("OPENAI_API_KEY")
74
- )
75
-
76
- # Create chat memory
77
- memory = ConversationBufferWindowMemory(
78
- memory_key="chat_history",
79
- return_messages=True,
80
- k=5
81
- )
82
-
83
- # Create the prompt template with the correct variable names
84
- prompt = ChatPromptTemplate.from_messages([
85
- ("system", "You are a helpful assistant analyzing RFP documents."),
86
- MessagesPlaceholder(variable_name="chat_history"),
87
- ("user", "{input}\nContext: {context}"),
88
- MessagesPlaceholder(variable_name="agent_scratchpad")
89
- ])
90
-
91
- # Create retriever function
92
- retriever = vector_store.as_retriever(search_kwargs={"k": 2})
93
-
94
- # Create the RAG pipeline
95
- rag_pipe = (
96
- {
97
- "context": lambda x: retriever.get_relevant_documents(x["input"]),
98
- "input": lambda x: x["input"],
99
- "chat_history": lambda x: memory.chat_memory.messages,
100
- "agent_scratchpad": lambda x: format_to_tool_messages(x["intermediate_steps"])
101
- }
102
- | prompt
103
- | llm.bind(stop=["\\nHuman:"])
104
- | OpenAIToolsAgentOutputParser()
105
- )
106
-
107
- # Create the agent executor
108
- agent_executor = AgentExecutor(
109
- agent=rag_pipe,
110
- tools=[
111
- Tool(
112
- name="RFP_Knowledge_Base",
113
- func=lambda x: retriever.get_relevant_documents(x),
114
- description="Use this tool to analyze RFP documents and answer questions about their content."
115
- )
116
- ],
117
- memory=memory,
118
- verbose=True,
119
- handle_parsing_errors=True,
120
- return_intermediate_steps=True
121
- )
122
-
123
- return agent_executor
124
-
125
- except Exception as e:
126
- st.error(f"Error initializing QA system: {e}")
127
- return None
128
-
129
- def initialize_faiss(embeddings, documents, document_names):
130
- \"\"\"Initialize FAISS vector store\"\"\"
131
- try:
132
- from langchain.vectorstores import FAISS
133
-
134
- vector_store = FAISS.from_texts(
135
- documents,
136
- embeddings,
137
- metadatas=[{"source": name} for name in document_names],
138
- )
139
- return vector_store
140
- except Exception as e:
141
- st.error(f"Error initializing FAISS: {e}")
142
- return None
143
-
144
-
145
- @st.cache_resource
146
- def get_embeddings_model():
147
- \"\"\"Get the embeddings model\"\"\"
148
- try:
149
- from langchain.embeddings import HuggingFaceEmbeddings
150
-
151
- model_name = "sentence-transformers/all-MiniLM-L6-v2"
152
- embeddings = HuggingFaceEmbeddings(model_name=model_name)
153
- return embeddings
154
- except Exception as e:
155
- st.error(f"Error loading embeddings model: {e}")
156
- return None
157
- """
158
-
159
- # Writing the updated contents back to the original files
160
- with open(components_chat_path, 'w') as chat_file:
161
- chat_file.write(updated_components_chat_content)
162
 
163
- with open(utils_database_path, 'w') as database_file:
164
- database_file.write(updated_utils_database_content)
 
41
  st.error(f"Error generating response: {e}")
42
  import traceback
43
  st.error(f"Detailed error: {traceback.format_exc()}")
 
44
 
 
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46