Repo_Chat / app.py
seansullivan's picture
Update app.py
bf24cbc verified
import os
import time
import streamlit as st
import nltk
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from pinecone import Pinecone
from pinecone_text.sparse import BM25Encoder
from langchain_community.retrievers import PineconeHybridSearchRetriever
from langchain.tools.retriever import create_retriever_tool
from langgraph.prebuilt import create_react_agent
LANGCHAIN_API_KEY = os.environ['LANGCHAIN_API_KEY']
os.environ['LANGCHAIN_TRACING_V2'] = "true"
os.environ['LANGCHAIN_ENDPOINT'] = "https://api.smith.langchain.com"
# Download the NLTK tokenizer (if not already downloaded)
nltk.download('punkt_tab')
# ----------------- System Prompts ----------------- #
# Autogen system prompt (used if "autogen" is selected in either dropdown)
autogen_system_prompt = """
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.
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.
When using the retriever_tool, formulate your search queries using these key terms to find specific information from the documentation:
- "Getting started" for installation, setup, and configuration instructions.
- "Agents" for creating, managing, and customizing AI agents.
- "Multi-agent workflows" for establishing conversations and collaborations among agents.
- "API Reference" for detailed documentation on classes, methods, and functions.
- "Code execution" for instructions on running code snippets or managing code-based tasks.
- "Extensions" for integrating third-party services or adding custom tools.
- "AutoGen Studio" for guidance on using the no-code interface and prototyping applications.
- "Core API" for understanding the low-level components and event-driven architectures.
- "AgentChat" for best practices in multi-agent interaction and conversation patterns.
- "Tool use" for incorporating external functionalities and custom integrations.
- "Configuration" for customizing the framework’s behavior.
- "Migration" for upgrading between AutoGen versions.
- "Examples" for practical code samples and real-world use cases.
- "FAQ" for common questions, troubleshooting tips, and clarifications.
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.
When responding to user queries:
1. Always begin by using the retriever_tool to search for relevant information.
2. Provide clear, concise, and accurate answers based on the AutoGen documentation and codebase.
3. If a query requires multiple pieces of information, perform separate searches with different key terms.
4. Include code snippets or API usage examples when relevant.
5. Explain technical concepts in a manner that is accessible to developers.
Format your responses as follows:
1. Start with a brief introduction addressing the user's query.
2. Present the main answer or explanation.
3. Include any relevant code snippets or API examples.
4. Offer additional context or related information when applicable.
5. Conclude with suggestions for next steps or related topics the user might explore further.
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.
IMPORTANT: Be concise with your code generation to adhere to AutoGen's framework.
"""
# LangGraph system prompt (for LangGraph’s codebase)
langgraph_system_prompt = """
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.
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.
When using the `retriever_tool`, formulate your search queries with these key terms:
- **Getting started**: for installation, setup, and configuration instructions.
- **Nodes**: for creating, managing, and customizing workflow nodes.
- **Multi-agent workflows**: for establishing interactions and collaborations among agents.
- **API Reference**: for detailed documentation on classes, methods, and functions.
- **Code execution**: for instructions on running code snippets or managing code-based tasks.
- **Extensions**: for integrating third-party services or adding custom tools.
- **LangGraph Studio**: for guidance on using the graphical interface and prototyping applications.
- **Core API**: for understanding low-level components and event-driven architectures.
- **Tool use**: for incorporating external functionalities and custom integrations.
- **Configuration**: for customizing the framework’s behavior.
- **Migration**: for upgrading between LangGraph versions.
- **Examples**: for practical code samples and real-world use cases.
- **FAQ**: for common questions, troubleshooting tips, and clarifications.
*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.
When responding to user queries:
1. **Begin** by using the `retriever_tool` to search for relevant information.
2. **Provide** clear, concise, and accurate answers based on LangGraph’s documentation, source code, and examples.
3. **Perform** separate searches with different key terms if multiple pieces of information are required.
4. **Include** code snippets or API usage examples when relevant.
5. **Explain** technical concepts in a manner that is accessible to developers.
**Response Format:**
- Start with a brief introduction addressing the user's query.
- Present the main answer or explanation.
- Include any relevant code snippets or API examples.
- Offer additional context or related information when applicable.
- Conclude with suggestions for next steps or related topics to explore further.
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.
Always use the `retriever_tool` frequently—even for queries you think you know well—since LangGraph’s resources are continuously updated.
IMPORTANT: Be concise with your code generation to adhere to LangGraph's framework.
"""
def convert_namespace(ns: str) -> str:
"""
Convert the UI namespace option to the actual namespace used.
If the user selects "autogen", return "lmsys" instead.
Otherwise, return the option as-is.
"""
return "lmsys" if ns == "autogen" else ns
def get_description(actual_namespace: str, top_k: int) -> str:
"""
Generate a dynamic description for a retriever tool based on the actual namespace and top_k value.
"""
if actual_namespace == "lmsys":
return f"Search and return information from AutoGen's codebase using namespace '{actual_namespace}' with top_k = {top_k}."
elif actual_namespace == "llm-cli":
return f"Search and return information using the LLM CLI interface with namespace '{actual_namespace}' with top_k = {top_k}."
else:
return f"Search and return information from LangGraph's documentation using namespace '{actual_namespace}' with top_k = {top_k}."
@st.cache_resource
def init_agent(namespace1: str, top_k1: int, namespace2: str, top_k2: int):
"""
Initialize the LangGraph agent with up to two Pinecone retriever tools.
Only add a retriever tool if a non-empty namespace is provided.
Choose the system prompt based on the active namespaces:
- If either dropdown is set to "autogen", use autogen_system_prompt.
- Else if any non-empty namespace is "langgraph-main", use langgraph_system_prompt.
- Else if the only active namespace is "llm-cli", use an empty string as the prompt.
"""
# Retrieve API keys from environment variables
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
PINE_API_KEY = os.environ.get("PINE_API_KEY")
if not OPENAI_API_KEY or not PINE_API_KEY:
raise ValueError("Please set the OPENAI_API_KEY and PINE_API_KEY environment variables.")
# --- Embeddings ---
embed = OpenAIEmbeddings(
model='text-embedding-3-small',
openai_api_key=OPENAI_API_KEY,
dimensions=768
)
# --- Pinecone Setup ---
index_name = 'autogen'
pc = Pinecone(api_key=PINE_API_KEY)
index = pc.Index(index_name)
# Allow a moment for the index to connect
time.sleep(1)
index.describe_index_stats()
# --- BM25 Sparse Encoder ---
bm25_encoder = BM25Encoder().default()
tools = [] # Initialize empty list of tools
# Create first retriever tool if a namespace is selected.
if namespace1:
actual_namespace1 = convert_namespace(namespace1)
retriever1 = PineconeHybridSearchRetriever(
embeddings=embed,
sparse_encoder=bm25_encoder,
index=index,
namespace=actual_namespace1,
top_k=top_k1
)
description1 = get_description(actual_namespace1, top_k1)
retriever_tool1 = create_retriever_tool(
retriever1,
"retrieve_context_1",
description1,
)
tools.append(retriever_tool1)
# Create second retriever tool if a namespace is selected.
if namespace2:
actual_namespace2 = convert_namespace(namespace2)
retriever2 = PineconeHybridSearchRetriever(
embeddings=embed,
sparse_encoder=bm25_encoder,
index=index,
namespace=actual_namespace2,
top_k=top_k2
)
description2 = get_description(actual_namespace2, top_k2)
retriever_tool2 = create_retriever_tool(
retriever2,
"retrieve_context_2",
description2,
)
tools.append(retriever_tool2)
# --- Choose the System Prompt Based on Namespace Selections ---
active_ns = [ns for ns in [namespace1, namespace2] if ns != ""]
if "autogen" in active_ns:
prompt = autogen_system_prompt
elif any(ns == "langgraph-main" for ns in active_ns):
prompt = langgraph_system_prompt
elif active_ns and all(ns == "llm-cli" for ns in active_ns):
prompt = "" # Empty system prompt when only llm-cli is active.
else:
prompt = ""
# --- Chat Model ---
model = ChatOpenAI(model_name="o3-mini-2025-01-31", openai_api_key=OPENAI_API_KEY)
# --- Create the React Agent with the selected prompt ---
graph = create_react_agent(model, tools=tools, messages_modifier=prompt)
return graph
# ----------------- Sidebar: Namespace & Top_K Selection ----------------- #
st.sidebar.header("Retriever Tool Settings")
# Dropdown and slider for Retriever Tool 1 (empty option available)
namespace_options = ["langgraph-main", "autogen", "llm-cli", "smolagents", ""]
namespace1 = st.sidebar.selectbox("Select namespace for Retriever Tool 1:", namespace_options, index=0)
top_k1 = st.sidebar.slider("Select top_k for Retriever Tool 1:", min_value=1, max_value=4, value=1, step=1)
# For Retriever Tool 2, we limit the options (if desired) or leave the same.
namespace_options2 = ["autogen", "smolagents", "llm-cli", ""]
namespace2 = st.sidebar.selectbox("Select namespace for Retriever Tool 2:", namespace_options2, index=0)
top_k2 = st.sidebar.slider("Select top_k for Retriever Tool 2:", min_value=1, max_value=4, value=1, step=1)
# Initialize the agent with the selected namespaces (after conversion) and top_k values.
graph = init_agent(namespace1, top_k1, namespace2, top_k2)
# ----------------- Main Chat App UI ----------------- #
st.title("LangGraph Coding Chat Assistant")
# Initialize conversation history in session state
if "chat_history" not in st.session_state:
st.session_state.chat_history = [] # Each entry is a tuple: (role, message)
def display_conversation():
"""Display the chat conversation."""
for role, message in st.session_state.chat_history:
if role == "user":
st.markdown(f"**You:** {message}")
else:
st.markdown(f"**Assistant:** {message}")
# Display the existing conversation
display_conversation()
# --- Chat Input Form ---
with st.form("chat_form", clear_on_submit=True):
user_input = st.text_area("Enter your message:", height=150)
submitted = st.form_submit_button("Send")
if submitted and user_input:
st.session_state.chat_history.append(("user", user_input))
# No need to force a rerun—Streamlit re-runs automatically on widget interaction.
# --- Generate Assistant Response ---
if st.session_state.chat_history and st.session_state.chat_history[-1][0] == "user":
inputs = {"messages": st.session_state.chat_history}
# Create separate placeholders for each section.
tool_calls_placeholder = st.empty()
tool_output_placeholder = st.empty()
final_answer_placeholder = st.empty()
# Accumulators for each section.
tool_calls_text = ""
tool_output_text = ""
final_answer_text = ""
# Stream the agent's response chunk-by-chunk.
for s in graph.stream(inputs, stream_mode="values"):
# Extract the last message from the current chunk.
message = s["messages"][-1]
if isinstance(message, tuple):
# This is a tool-related message.
# We use a simple heuristic: if the text contains "call_" (case-insensitive), we treat it as a tool call.
role, text = message
if "call_" in text.lower():
tool_calls_text += text + "\n\n"
tool_calls_placeholder.markdown(
f"### Tool Calls\n\n{tool_calls_text}",
unsafe_allow_html=True
)
else:
tool_output_text += text + "\n\n"
tool_output_placeholder.markdown(
f"### Tool Output\n\n{tool_output_text}",
unsafe_allow_html=True
)
else:
# This is the final answer generated by the AI.
text = message.content
final_answer_text += text + "\n\n"
final_answer_placeholder.markdown(
f"### Final Answer\n\n{final_answer_text}",
unsafe_allow_html=True
)
# Once complete, combine all sections into one record for persistence.
combined_response = (
f"**Tool Calls:**\n\n{tool_calls_text}\n\n"
f"**Tool Output:**\n\n{tool_output_text}\n\n"
f"**Final Answer:**\n\n{final_answer_text}"
)
st.session_state.chat_history.append(("assistant", combined_response))