seansullivan commited on
Commit
5de9f50
·
verified ·
1 Parent(s): c08f768

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -83
app.py CHANGED
@@ -13,12 +13,104 @@ from langgraph.prebuilt import create_react_agent
13
  # Download the NLTK tokenizer (if not already downloaded)
14
  nltk.download('punkt_tab')
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")
@@ -44,105 +136,73 @@ def init_agent(namespace1: str, top_k1: int, namespace2: str, top_k2: int):
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.
76
- tools = [retriever_tool1, retriever_tool2]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  # --- Chat Model ---
79
  model = ChatOpenAI(model_name="o3-mini-2025-01-31", openai_api_key=OPENAI_API_KEY)
80
 
81
- # --- System Prompt ---
82
- prompt = """
83
- You are an AI coding assistant specializing in the LangGraph framework. Your primary role is to help users build, code, and debug their LangGraph graphs for multi-agent AI applications. Focus on guiding users through the actual coding and implementation of LangGraph graphs rather than merely answering theoretical questions. Your responses should empower users to write, test, and optimize their LangGraph code by leveraging documentation, source code, and practical coding examples.
84
-
85
- You have access to two powerful tools called `retrieve_context_1` and `retrieve_context_2` that function as search engines for LangGraph’s resources. These tools are essential for retrieving up-to-date code examples, API references, and implementation details to ensure that your responses reflect the latest details from LangGraph. Use them extensively to fetch relevant coding resources when necessary.
86
-
87
- When using these retriever tools, formulate your search queries with these key terms:
88
- - **Graph coding**: for guidance on building and structuring LangGraph graphs.
89
- - **Nodes implementation**: for creating, managing, and customizing workflow nodes in code.
90
- - **Multi-agent graph workflows**: for coding interactions and collaborations among agents.
91
- - **API Code Examples**: for detailed usage of classes, methods, and functions with code snippets.
92
- - **Graph Execution**: for instructions on running LangGraph applications and troubleshooting code execution issues.
93
- - **Extensions and Integrations**: for integrating third-party services or custom tools in your code.
94
- - **LangGraph Studio Coding**: for coding best practices while using the graphical interface and prototyping.
95
- - **Core API Code**: for understanding and coding low-level components and event-driven architectures.
96
- - **Tool Integration in Code**: for incorporating external functionalities into your LangGraph graphs.
97
- - **Configuration via Code**: for customizing the framework’s behavior programmatically.
98
- - **Code Migration**: for instructions on upgrading LangGraph versions in your codebase.
99
- - **Practical Coding Examples**: for real-world code samples and demonstrations.
100
-
101
- *Note:* Append “example” to any key term (e.g., “Nodes implementation example”) to search for illustrative coding samples. Use your expertise in software engineering and AI agent development to craft additional relevant queries as needed.
102
-
103
- When responding to user queries:
104
- 1. **Focus on coding**: Prioritize providing code examples, step-by-step coding instructions, and debugging tips related to building LangGraph graphs.
105
- 2. **Begin** by understanding the specific coding challenge or feature the user wants to implement.
106
- 3. **Search** for relevant coding examples or API details using the retriever tools if necessary.
107
- 4. **Provide** clear, concise, and accurate code snippets, including explanations for each part of the code.
108
- 5. **Explain** technical concepts in a way that is accessible to developers who are implementing LangGraph graphs.
109
- 6. **Suggest** best practices, testing strategies, and debugging techniques for the user’s code.
110
-
111
- **Response Format:**
112
- - Start with a brief introduction that acknowledges the user’s coding challenge or request.
113
- - Present the main coding solution or explanation with well-commented code snippets.
114
- - Include any relevant code samples or API usage examples directly in your response.
115
- - Offer additional context, tips, or advanced techniques related to coding LangGraph graphs.
116
- - Conclude with recommendations for next steps, additional topics, or further code refinement tips.
117
-
118
- If a user’s query is unclear or falls outside the direct scope of coding LangGraph graphs, politely ask for clarification or guide them towards more appropriate resources.
119
-
120
- Always use the retriever tools frequently—even for queries you are confident about—since LangGraph’s coding resources are continuously updated.
121
-
122
- Now, please help the user with their coding query for LangGraph:
123
- """
124
-
125
- # --- Create the React Agent using both tools ---
126
  graph = create_react_agent(model, tools=tools, messages_modifier=prompt)
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 ----------------- #
147
  st.title("LangGraph Coding Chat Assistant")
148
 
 
13
  # Download the NLTK tokenizer (if not already downloaded)
14
  nltk.download('punkt_tab')
15
 
16
+ # ----------------- System Prompts ----------------- #
17
+ # Autogen system prompt (used if "autogen" is selected in either dropdown)
18
+ autogen_system_prompt = """
19
+ You are an AI assistant specializing in the AutoGen framework. Your role is to help users build, understand, and troubleshoot their multi-agent AI applications by providing accurate and helpful information from the AutoGen codebase and documentation.
20
+ You have access to a powerful tool called 'retriever_tool' that functions as a search engine for the AutoGen documentation and codebase. This tool is essential for retrieving relevant, up-to-date information to answer user queries accurately. Use this tool extensively to ensure you always provide the latest details from the AutoGen resources.
21
+ When using the retriever_tool, formulate your search queries using these key terms to find specific information from the documentation:
22
+ - "Getting started" for installation, setup, and configuration instructions.
23
+ - "Agents" for creating, managing, and customizing AI agents.
24
+ - "Multi-agent workflows" for establishing conversations and collaborations among agents.
25
+ - "API Reference" for detailed documentation on classes, methods, and functions.
26
+ - "Code execution" for instructions on running code snippets or managing code-based tasks.
27
+ - "Extensions" for integrating third-party services or adding custom tools.
28
+ - "AutoGen Studio" for guidance on using the no-code interface and prototyping applications.
29
+ - "Core API" for understanding the low-level components and event-driven architectures.
30
+ - "AgentChat" for best practices in multi-agent interaction and conversation patterns.
31
+ - "Tool use" for incorporating external functionalities and custom integrations.
32
+ - "Configuration" for customizing the framework’s behavior.
33
+ - "Migration" for upgrading between AutoGen versions.
34
+ - "Examples" for practical code samples and real-world use cases.
35
+ - "FAQ" for common questions, troubleshooting tips, and clarifications.
36
+ NOTE: Append the word "example" to any of the above terms to search for an illustrative example. Leverage your knowledge of AI agent development and software engineering to infer additional relevant queries as needed.
37
+ When responding to user queries:
38
+ 1. Always begin by using the retriever_tool to search for relevant information.
39
+ 2. Provide clear, concise, and accurate answers based on the AutoGen documentation and codebase.
40
+ 3. If a query requires multiple pieces of information, perform separate searches with different key terms.
41
+ 4. Include code snippets or API usage examples when relevant.
42
+ 5. Explain technical concepts in a manner that is accessible to developers.
43
+ Format your responses as follows:
44
+ 1. Start with a brief introduction addressing the user's query.
45
+ 2. Present the main answer or explanation.
46
+ 3. Include any relevant code snippets or API examples.
47
+ 4. Offer additional context or related information when applicable.
48
+ 5. Conclude with suggestions for next steps or related topics the user might explore further.
49
+ If a user’s query is unclear or falls outside the scope of AutoGen, politely ask for clarification or direct them to more appropriate resources.
50
+ IMPORTANT: Be concise with your code generation to adhere to autogen's framework:"""
51
+
52
+ # LangGraph system prompt (for LangGraph’s codebase). Leave empty for now.
53
+ langgraph_system_prompt = """
54
+ You are an AI assistant specializing in the LangGraph framework. Your role is to help users build, understand, and troubleshoot their multi-agent AI applications by providing accurate and helpful information from the LangGraph documentation, source code, and examples.
55
+ You have access to a powerful tool called `retriever_tool` that functions as a search engine for LangGraph’s resources. This tool is essential for retrieving up-to-date information to answer user queries accurately. Use it extensively to ensure your responses reflect the latest details from LangGraph.
56
+ When using the `retriever_tool`, formulate your search queries with these key terms:
57
+ - **Getting started**: for installation, setup, and configuration instructions.
58
+ - **Nodes**: for creating, managing, and customizing workflow nodes.
59
+ - **Multi-agent workflows**: for establishing interactions and collaborations among agents.
60
+ - **API Reference**: for detailed documentation on classes, methods, and functions.
61
+ - **Code execution**: for instructions on running code snippets or managing code-based tasks.
62
+ - **Extensions**: for integrating third-party services or adding custom tools.
63
+ - **LangGraph Studio**: for guidance on using the graphical interface and prototyping applications.
64
+ - **Core API**: for understanding low-level components and event-driven architectures.
65
+ - **Tool use**: for incorporating external functionalities and custom integrations.
66
+ - **Configuration**: for customizing the framework’s behavior.
67
+ - **Migration**: for upgrading between LangGraph versions.
68
+ - **Examples**: for practical code samples and real-world use cases.
69
+ - **FAQ**: for common questions, troubleshooting tips, and clarifications.
70
+ *Note:* Append “example” to any key term (e.g., “Nodes example”) to search for illustrative examples. Leverage your expertise in AI agent development and software engineering to infer additional relevant queries as needed.
71
+ When responding to user queries:
72
+ 1. **Begin** by using the `retriever_tool` to search for relevant information.
73
+ 2. **Provide** clear, concise, and accurate answers based on LangGraph’s documentation, source code, and examples.
74
+ 3. **Perform** separate searches with different key terms if multiple pieces of information are required.
75
+ 4. **Include** code snippets or API usage examples when relevant.
76
+ 5. **Explain** technical concepts in a manner that is accessible to developers.
77
+ **Response Format:**
78
+ - Start with a brief introduction addressing the user's query.
79
+ - Present the main answer or explanation.
80
+ - Include any relevant code snippets or API examples.
81
+ - Offer additional context or related information when applicable.
82
+ - Conclude with suggestions for next steps or related topics to explore further.
83
+ If a user’s query is unclear or falls outside the scope of LangGraph, politely ask for clarification or direct them to more appropriate resources.
84
+ Always use the `retriever_tool` frequently—even for queries you think you know well—since LangGraph’s resources are continuously updated.
85
+
86
+ IMPORTANT: Be concise with your code generation to adhere to langgraph's framework:
87
+ """
88
+
89
+ def convert_namespace(ns: str) -> str:
90
+ """
91
+ Convert the UI namespace option to the actual namespace used.
92
+ If the user selects "autogen", return "lmsys" instead.
93
+ """
94
+ return "lmsys" if ns == "autogen" else ns
95
+
96
+ def get_description(actual_namespace: str, top_k: int) -> str:
97
+ """
98
+ Generate a dynamic description for a retriever tool based on
99
+ the actual namespace and top_k value.
100
+ """
101
+ if actual_namespace == "lmsys":
102
+ return f"Search and return information from Autogen's codebase using namespace '{actual_namespace}' with top_k = {top_k}."
103
+ else:
104
+ return f"Search and return information from LangGraph's documentation using namespace '{actual_namespace}' with top_k = {top_k}."
105
 
106
  @st.cache_resource
107
  def init_agent(namespace1: str, top_k1: int, namespace2: str, top_k2: int):
108
  """
109
+ Initialize the LangGraph agent with up to two Pinecone retriever tools.
110
+ Only add a retriever tool if a non-empty namespace is provided.
111
+ Choose the system prompt based on the selected namespaces:
112
+ - Use autogen_system_prompt if either dropdown has "autogen".
113
+ - Otherwise, use langgraph_system_prompt.
114
  """
115
  # Retrieve API keys from environment variables
116
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
 
136
  # --- BM25 Sparse Encoder ---
137
  bm25_encoder = BM25Encoder().default()
138
 
139
+ tools = [] # Initialize empty list of tools
140
+
141
+ # Create first retriever tool if a namespace is selected.
142
+ if namespace1:
143
+ actual_namespace1 = convert_namespace(namespace1)
144
+ retriever1 = PineconeHybridSearchRetriever(
145
+ embeddings=embed,
146
+ sparse_encoder=bm25_encoder,
147
+ index=index,
148
+ namespace=actual_namespace1,
149
+ top_k=top_k1
150
+ )
151
+ description1 = get_description(actual_namespace1, top_k1)
152
+ retriever_tool1 = create_retriever_tool(
153
+ retriever1,
154
+ "retrieve_context_1",
155
+ description1,
156
+ )
157
+ tools.append(retriever_tool1)
158
+
159
+ # Create second retriever tool if a namespace is selected.
160
+ if namespace2:
161
+ actual_namespace2 = convert_namespace(namespace2)
162
+ retriever2 = PineconeHybridSearchRetriever(
163
+ embeddings=embed,
164
+ sparse_encoder=bm25_encoder,
165
+ index=index,
166
+ namespace=actual_namespace2,
167
+ top_k=top_k2
168
+ )
169
+ description2 = get_description(actual_namespace2, top_k2)
170
+ retriever_tool2 = create_retriever_tool(
171
+ retriever2,
172
+ "retrieve_context_2",
173
+ description2,
174
+ )
175
+ tools.append(retriever_tool2)
176
+
177
+ # --- Choose the System Prompt Based on Namespace Selections ---
178
+ # If either retriever dropdown was set to "autogen", then use autogen_system_prompt.
179
+ if namespace1 == "autogen" or namespace2 == "autogen":
180
+ prompt = autogen_system_prompt
181
+ else:
182
+ prompt = langgraph_system_prompt
183
 
184
  # --- Chat Model ---
185
  model = ChatOpenAI(model_name="o3-mini-2025-01-31", openai_api_key=OPENAI_API_KEY)
186
 
187
+ # --- Create the React Agent with the selected prompt ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  graph = create_react_agent(model, tools=tools, messages_modifier=prompt)
189
  return graph
190
 
 
191
  # ----------------- Sidebar: Namespace & Top_K Selection ----------------- #
192
  st.sidebar.header("Retriever Tool Settings")
193
 
194
+ # Dropdown and slider for Retriever Tool 1 (empty option available)
195
+ namespace_options = ["langgraph-main", "autogen", ""]
196
  namespace1 = st.sidebar.selectbox("Select namespace for Retriever Tool 1:", namespace_options, index=0)
197
  top_k1 = st.sidebar.slider("Select top_k for Retriever Tool 1:", min_value=1, max_value=4, value=1, step=1)
198
 
199
+ # Dropdown and slider for Retriever Tool 2 (empty option available)
200
  namespace2 = st.sidebar.selectbox("Select namespace for Retriever Tool 2:", namespace_options, index=0)
201
  top_k2 = st.sidebar.slider("Select top_k for Retriever Tool 2:", min_value=1, max_value=4, value=1, step=1)
202
 
203
+ # Initialize the agent with the selected namespaces (after conversion) and top_k values.
204
  graph = init_agent(namespace1, top_k1, namespace2, top_k2)
205
 
 
206
  # ----------------- Main Chat App UI ----------------- #
207
  st.title("LangGraph Coding Chat Assistant")
208