Update app.py
Browse files
app.py
CHANGED
@@ -15,10 +15,10 @@ nltk.download('punkt_tab')
|
|
15 |
|
16 |
|
17 |
@st.cache_resource
|
18 |
-
def init_agent(namespace1: str, namespace2: str):
|
19 |
"""
|
20 |
Initialize the LangGraph agent with two Pinecone retriever tools,
|
21 |
-
each configured with a
|
22 |
"""
|
23 |
# Retrieve API keys from environment variables
|
24 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
@@ -37,39 +37,39 @@ def init_agent(namespace1: str, namespace2: str):
|
|
37 |
index_name = 'autogen'
|
38 |
pc = Pinecone(api_key=PINE_API_KEY)
|
39 |
index = pc.Index(index_name)
|
40 |
-
# Allow the index to connect
|
41 |
time.sleep(1)
|
42 |
index.describe_index_stats()
|
43 |
|
44 |
# --- BM25 Sparse Encoder ---
|
45 |
bm25_encoder = BM25Encoder().default()
|
46 |
|
47 |
-
# --- Create first Pinecone Hybrid Retriever Tool using namespace1 ---
|
48 |
retriever1 = PineconeHybridSearchRetriever(
|
49 |
embeddings=embed,
|
50 |
sparse_encoder=bm25_encoder,
|
51 |
index=index,
|
52 |
namespace=namespace1,
|
53 |
-
top_k=
|
54 |
)
|
55 |
retriever_tool1 = create_retriever_tool(
|
56 |
retriever1,
|
57 |
"retrieve_context_1",
|
58 |
-
f"Search and return information from Autogen's codebase and documentation using namespace '{namespace1}'.",
|
59 |
)
|
60 |
|
61 |
-
# --- Create second
|
62 |
retriever2 = PineconeHybridSearchRetriever(
|
63 |
embeddings=embed,
|
64 |
sparse_encoder=bm25_encoder,
|
65 |
index=index,
|
66 |
namespace=namespace2,
|
67 |
-
top_k=
|
68 |
)
|
69 |
retriever_tool2 = create_retriever_tool(
|
70 |
retriever2,
|
71 |
"retrieve_context_2",
|
72 |
-
f"Search and return information from Autogen's codebase and documentation using namespace '{namespace2}'.",
|
73 |
)
|
74 |
|
75 |
# Both retriever tools are added to the list of available tools.
|
@@ -127,14 +127,20 @@ Now, please help the user with their coding query for LangGraph:
|
|
127 |
return graph
|
128 |
|
129 |
|
130 |
-
# ----------------- Sidebar: Namespace Selection ----------------- #
|
131 |
-
st.sidebar.header("
|
|
|
|
|
132 |
namespace_options = ["langgraph-main", "autogen"]
|
133 |
namespace1 = st.sidebar.selectbox("Select namespace for Retriever Tool 1:", namespace_options, index=0)
|
|
|
|
|
|
|
134 |
namespace2 = st.sidebar.selectbox("Select namespace for Retriever Tool 2:", namespace_options, index=0)
|
|
|
135 |
|
136 |
-
# Initialize the agent with the selected namespaces.
|
137 |
-
graph = init_agent(namespace1, namespace2)
|
138 |
|
139 |
|
140 |
# ----------------- Main Chat App UI ----------------- #
|
@@ -145,7 +151,7 @@ if "chat_history" not in st.session_state:
|
|
145 |
st.session_state.chat_history = [] # Each entry is a tuple: (role, message)
|
146 |
|
147 |
def display_conversation():
|
148 |
-
"""Display the chat
|
149 |
for role, message in st.session_state.chat_history:
|
150 |
if role == "user":
|
151 |
st.markdown(f"**You:** {message}")
|
@@ -167,7 +173,7 @@ with st.form("chat_form", clear_on_submit=True):
|
|
167 |
if st.session_state.chat_history and st.session_state.chat_history[-1][0] == "user":
|
168 |
inputs = {"messages": st.session_state.chat_history}
|
169 |
|
170 |
-
# Placeholder
|
171 |
response_placeholder = st.empty()
|
172 |
assistant_message = ""
|
173 |
|
@@ -182,6 +188,6 @@ if st.session_state.chat_history and st.session_state.chat_history[-1][0] == "us
|
|
182 |
assistant_message += text
|
183 |
response_placeholder.markdown(f"**Assistant:** {assistant_message}")
|
184 |
|
185 |
-
# Append the
|
186 |
st.session_state.chat_history.append(("assistant", assistant_message))
|
187 |
st.experimental_rerun()
|
|
|
15 |
|
16 |
|
17 |
@st.cache_resource
|
18 |
+
def init_agent(namespace1: str, top_k1: int, namespace2: str, top_k2: int):
|
19 |
"""
|
20 |
Initialize the LangGraph agent with two Pinecone retriever tools,
|
21 |
+
each configured with a specified namespace and top_k value.
|
22 |
"""
|
23 |
# Retrieve API keys from environment variables
|
24 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
|
|
37 |
index_name = 'autogen'
|
38 |
pc = Pinecone(api_key=PINE_API_KEY)
|
39 |
index = pc.Index(index_name)
|
40 |
+
# Allow a moment for the index to connect
|
41 |
time.sleep(1)
|
42 |
index.describe_index_stats()
|
43 |
|
44 |
# --- BM25 Sparse Encoder ---
|
45 |
bm25_encoder = BM25Encoder().default()
|
46 |
|
47 |
+
# --- Create first Pinecone Hybrid Retriever Tool using namespace1 and top_k1 ---
|
48 |
retriever1 = PineconeHybridSearchRetriever(
|
49 |
embeddings=embed,
|
50 |
sparse_encoder=bm25_encoder,
|
51 |
index=index,
|
52 |
namespace=namespace1,
|
53 |
+
top_k=top_k1
|
54 |
)
|
55 |
retriever_tool1 = create_retriever_tool(
|
56 |
retriever1,
|
57 |
"retrieve_context_1",
|
58 |
+
f"Search and return information from Autogen's codebase and documentation using namespace '{namespace1}' with top_k = {top_k1}.",
|
59 |
)
|
60 |
|
61 |
+
# --- Create second Pinecone Hybrid Retriever Tool using namespace2 and top_k2 ---
|
62 |
retriever2 = PineconeHybridSearchRetriever(
|
63 |
embeddings=embed,
|
64 |
sparse_encoder=bm25_encoder,
|
65 |
index=index,
|
66 |
namespace=namespace2,
|
67 |
+
top_k=top_k2
|
68 |
)
|
69 |
retriever_tool2 = create_retriever_tool(
|
70 |
retriever2,
|
71 |
"retrieve_context_2",
|
72 |
+
f"Search and return information from Autogen's codebase and documentation using namespace '{namespace2}' with top_k = {top_k2}.",
|
73 |
)
|
74 |
|
75 |
# Both retriever tools are added to the list of available tools.
|
|
|
127 |
return graph
|
128 |
|
129 |
|
130 |
+
# ----------------- Sidebar: Namespace & Top_K Selection ----------------- #
|
131 |
+
st.sidebar.header("Retriever Tool Settings")
|
132 |
+
|
133 |
+
# Dropdown and slider for Retriever Tool 1
|
134 |
namespace_options = ["langgraph-main", "autogen"]
|
135 |
namespace1 = st.sidebar.selectbox("Select namespace for Retriever Tool 1:", namespace_options, index=0)
|
136 |
+
top_k1 = st.sidebar.slider("Select top_k for Retriever Tool 1:", min_value=1, max_value=4, value=1, step=1)
|
137 |
+
|
138 |
+
# Dropdown and slider for Retriever Tool 2
|
139 |
namespace2 = st.sidebar.selectbox("Select namespace for Retriever Tool 2:", namespace_options, index=0)
|
140 |
+
top_k2 = st.sidebar.slider("Select top_k for Retriever Tool 2:", min_value=1, max_value=4, value=1, step=1)
|
141 |
|
142 |
+
# Initialize the agent with the selected namespaces and top_k values.
|
143 |
+
graph = init_agent(namespace1, top_k1, namespace2, top_k2)
|
144 |
|
145 |
|
146 |
# ----------------- Main Chat App UI ----------------- #
|
|
|
151 |
st.session_state.chat_history = [] # Each entry is a tuple: (role, message)
|
152 |
|
153 |
def display_conversation():
|
154 |
+
"""Display the chat conversation."""
|
155 |
for role, message in st.session_state.chat_history:
|
156 |
if role == "user":
|
157 |
st.markdown(f"**You:** {message}")
|
|
|
173 |
if st.session_state.chat_history and st.session_state.chat_history[-1][0] == "user":
|
174 |
inputs = {"messages": st.session_state.chat_history}
|
175 |
|
176 |
+
# Placeholder for real-time streaming of the response.
|
177 |
response_placeholder = st.empty()
|
178 |
assistant_message = ""
|
179 |
|
|
|
188 |
assistant_message += text
|
189 |
response_placeholder.markdown(f"**Assistant:** {assistant_message}")
|
190 |
|
191 |
+
# Append the complete assistant response to the chat history.
|
192 |
st.session_state.chat_history.append(("assistant", assistant_message))
|
193 |
st.experimental_rerun()
|