Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,6 +21,8 @@ import tempfile
|
|
| 21 |
import os
|
| 22 |
from langchain.llms import OpenAI # Import the OpenAI class
|
| 23 |
from langchain.chat_models import ChatOpenAI # Import ChatOpenAI
|
|
|
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
|
|
@@ -188,17 +190,26 @@ def get_embeddings_model():
|
|
| 188 |
@st.cache_resource
|
| 189 |
def initialize_qa_system(_vector_store):
|
| 190 |
try:
|
| 191 |
-
# Use ChatOpenAI instead of OpenAI
|
| 192 |
llm = ChatOpenAI(
|
| 193 |
temperature=0,
|
| 194 |
model_name="gpt-4", # Or another OpenAI model like "gpt-3.5-turbo"
|
| 195 |
api_key=os.environ.get('OPENAI_API_KEY'),
|
| 196 |
)
|
| 197 |
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
| 199 |
llm=llm,
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
)
|
| 203 |
return qa_pipeline
|
| 204 |
except Exception as e:
|
|
|
|
| 21 |
import os
|
| 22 |
from langchain.llms import OpenAI # Import the OpenAI class
|
| 23 |
from langchain.chat_models import ChatOpenAI # Import ChatOpenAI
|
| 24 |
+
from langchain.agents import create_chat_conversational_react_description_agent
|
| 25 |
+
from langchain.memory import ConversationBufferMemory
|
| 26 |
|
| 27 |
|
| 28 |
|
|
|
|
| 190 |
@st.cache_resource
|
| 191 |
def initialize_qa_system(_vector_store):
|
| 192 |
try:
|
|
|
|
| 193 |
llm = ChatOpenAI(
|
| 194 |
temperature=0,
|
| 195 |
model_name="gpt-4", # Or another OpenAI model like "gpt-3.5-turbo"
|
| 196 |
api_key=os.environ.get('OPENAI_API_KEY'),
|
| 197 |
)
|
| 198 |
|
| 199 |
+
memory = ConversationBufferMemory(memory_key="chat_history") # Initialize memory
|
| 200 |
+
|
| 201 |
+
# Create the conversational agent
|
| 202 |
+
qa_pipeline = create_chat_conversational_react_description_agent(
|
| 203 |
llm=llm,
|
| 204 |
+
tools=[
|
| 205 |
+
Tool(
|
| 206 |
+
name="Search",
|
| 207 |
+
func=_vector_store.as_retriever(search_kwargs={"k": 2}).get_relevant_documents,
|
| 208 |
+
description="useful for when you need to answer questions about the documents you have been uploaded. Input should be a fully formed question.",
|
| 209 |
+
)
|
| 210 |
+
],
|
| 211 |
+
verbose=True,
|
| 212 |
+
memory=memory,
|
| 213 |
)
|
| 214 |
return qa_pipeline
|
| 215 |
except Exception as e:
|