File size: 8,069 Bytes
640609e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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

# Download the NLTK tokenizer if not already downloaded
nltk.download('punkt_tab')


@st.cache_resource
def init_agent():
    # 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'
    namespace_name = 'langgraph-main'
    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()

    # --- Create a Hybrid Retriever ---
    retriever = PineconeHybridSearchRetriever(
        embeddings=embed,
        sparse_encoder=bm25_encoder,
        index=index,
        namespace=namespace_name,
        top_k=4
    )

    # --- Chat Model ---
    model = ChatOpenAI(model_name="o3-mini-2025-01-31", openai_api_key=OPENAI_API_KEY)

    # --- Create the Retriever Tool ---
    retriever_tool = create_retriever_tool(
        retriever,
        "retrieve_context",
        "Search and return information form Autogen's codebase and documentation",
    )
    tools = [retriever_tool]

    # --- System Prompt ---
    prompt = """
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.

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 code examples, API references, and implementation details to ensure that your responses reflect the latest details from LangGraph. Use it extensively to fetch relevant coding resources when necessary.

When using the `retriever_tool`, formulate your search queries with these key terms:
- **Graph coding**: for guidance on building and structuring LangGraph graphs.
- **Nodes implementation**: for creating, managing, and customizing workflow nodes in code.
- **Multi-agent graph workflows**: for coding interactions and collaborations among agents.
- **API Code Examples**: for detailed usage of classes, methods, and functions with code snippets.
- **Graph Execution**: for instructions on running LangGraph applications and troubleshooting code execution issues.
- **Extensions and Integrations**: for integrating third-party services or custom tools in your code.
- **LangGraph Studio Coding**: for coding best practices while using the graphical interface and prototyping.
- **Core API Code**: for understanding and coding low-level components and event-driven architectures.
- **Tool Integration in Code**: for incorporating external functionalities into your LangGraph graphs.
- **Configuration via Code**: for customizing the framework’s behavior programmatically.
- **Code Migration**: for instructions on upgrading LangGraph versions in your codebase.
- **Practical Coding Examples**: for real-world code samples and demonstrations.

*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.

When responding to user queries:
1. **Focus on coding**: Prioritize providing code examples, step-by-step coding instructions, and debugging tips related to building LangGraph graphs.
2. **Begin** by understanding the specific coding challenge or feature the user wants to implement.
3. **Search** for relevant coding examples or API details using the `retriever_tool` if necessary.
4. **Provide** clear, concise, and accurate code snippets, including explanations for each part of the code.
5. **Explain** technical concepts in a way that is accessible to developers who are implementing LangGraph graphs.
6. **Suggest** best practices, testing strategies, and debugging techniques for the user’s code.

**Response Format:**
- Start with a brief introduction that acknowledges the user’s coding challenge or request.
- Present the main coding solution or explanation with well-commented code snippets.
- Include any relevant code samples or API usage examples directly in your response.
- Offer additional context, tips, or advanced techniques related to coding LangGraph graphs.
- Conclude with recommendations for next steps, additional topics, or further code refinement tips.

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.

Always use the `retriever_tool` frequently—even for queries you are confident about—since LangGraph’s coding resources are continuously updated.

Now, please help the user with their coding query for LangGraph:
    """

    # --- Create the React Agent ---
    graph = create_react_agent(model, tools=tools, messages_modifier=prompt)
    return graph


# Initialize the agent (cached for the session)
graph = init_agent()

##########################################
# Streamlit 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)

# Function to display the conversation
def display_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_input("Enter your message:")
    submitted = st.form_submit_button("Send")
    if submitted and user_input:
        st.session_state.chat_history.append(("user", user_input))
        st.experimental_rerun()

# --- 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}

    # Placeholder for streaming response updates
    response_placeholder = st.empty()
    assistant_message = ""

    # Stream the agent's response in real time
    for s in graph.stream(inputs, stream_mode="values"):
        # Extract the last message from the messages list
        message = s["messages"][-1]
        if isinstance(message, tuple):
            # If the message is a tuple like ("assistant", text)
            role, text = message
        else:
            # Otherwise, assume it has a 'content' attribute.
            text = message.content
        assistant_message += text
        response_placeholder.markdown(f"**Assistant:** {assistant_message}")

    # Append the full response to the chat history once complete
    st.session_state.chat_history.append(("assistant", assistant_message))
    st.experimental_rerun()