Spaces:
Sleeping
Sleeping
File size: 8,945 Bytes
cae1f36 5b3862c cae1f36 cd43f6b cae1f36 |
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
import streamlit as st
from typing import TypedDict, Optional
from langgraph.graph import StateGraph, END
from together import Together
import os
import re
# Initialize Together client
client = Together(api_key="d03eaa038011423d1b8828a3ff2956511b2ca34b6cc88ffcbc4ad6314db31319")
def llm_invoke(prompt: str) -> str:
"""Call Together LLM with consistent config"""
try:
response = client.completions.create(
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
prompt=prompt,
max_tokens=3000,
temperature=0.2,
stop=["</s>"]
)
return response.choices[0].text.strip()
except Exception as e:
st.error(f"LLM API Error: {str(e)}")
return "Error in LLM response"
# State Definition
class AgentState(TypedDict):
domain: str
goal: str
follow_up_questions: Optional[str]
follow_up_answers: Optional[str]
agents_needed: Optional[str]
code_generator: Optional[str]
explanation: Optional[str]
# Agent Functions
def collect_domain_goal(state: AgentState) -> AgentState:
"""Simply pass through the domain and goal"""
return state
def needs_follow_up(goal: str) -> bool:
"""Check if extra context is needed for files/data"""
prompt = f"""
Does this system goal require clarifying questions about input data, file structure, or formats?
Goal: {goal}
Respond only 'yes' or 'no'.
"""
response = llm_invoke(prompt).strip().lower()
return response == "yes"
def ask_follow_up_questions(state: AgentState) -> AgentState:
"""Generate follow-up questions if needed"""
if needs_follow_up(state["goal"]):
prompt = f"""
Based on the domain: {state['domain']} and goal: {state['goal']},
list 2β3 critical follow-up questions about:
- Data/file formats (CSV, JSON, etc.)
- Column names or structure
- Required reference files (terms, conditions, etc.)
- Any APIs or databases used
Return in bullet-point format.
"""
state["follow_up_questions"] = llm_invoke(prompt)
else:
state["follow_up_questions"] = "No questions needed"
return state
def identify_agents(state: AgentState) -> AgentState:
"""Identify the agents needed for the system"""
prompt = f"""
Domain: {state['domain']}
Goal: {state['goal']}
Context: {state.get('follow_up_answers', 'No additional info')}
List the agents needed in an agentic system. For each:
- Name
- Purpose
- What it processes or validates
Return as a clean markdown list.
"""
state["agents_needed"] = llm_invoke(prompt)
return state
def generate_code(state: AgentState) -> AgentState:
"""Generate the complete LangGraph application code"""
prompt = f"""
You are an expert Python engineer. Based on:
- Domain: {state['domain']}
- Goal: {state['goal']}
- Follow-up Answers: {state.get('follow_up_answers', 'None')}
- Agents Needed: {state.get('agents_needed', 'None')}
Generate a full working LangGraph app using this structure:
1. Imports: streamlit, langgraph, together, re, os, csv if needed
2. State: TypedDict
3. One clean agent function per node
4. LangGraph construction with node/edge logic
5. Together LLM call helper
6. Streamlit UI to collect user input and run graph
7. Output final result cleanly
Match code style and flow of our health advisory example. Avoid AgentState subclasses. Ensure the system is executable.
"""
state["code_generator"] = llm_invoke(prompt)
return state
def explain_code(state: AgentState) -> AgentState:
"""Generate explanation for the generated code"""
if state.get("code_generator"):
prompt = f"Explain this code simply and clearly:\n\n{state['code_generator']}"
state["explanation"] = llm_invoke(prompt)
else:
state["explanation"] = "No code to explain"
return state
# Graph Construction
def create_graph():
"""Create and return the compiled graph"""
graph = StateGraph(AgentState)
# Add nodes
graph.add_node("collect_domain_goal", collect_domain_goal)
graph.add_node("ask_follow_up_questions", ask_follow_up_questions)
graph.add_node("identify_agents", identify_agents)
graph.add_node("generate_code", generate_code)
graph.add_node("explain_code", explain_code)
# Set entry point
graph.set_entry_point("collect_domain_goal")
# Add edges
graph.add_edge("collect_domain_goal", "ask_follow_up_questions")
graph.add_edge("ask_follow_up_questions", "identify_agents")
graph.add_edge("identify_agents", "generate_code")
graph.add_edge("generate_code", "explain_code")
graph.add_edge("explain_code", END)
return graph.compile()
# Initialize the app
app = create_graph()
# Streamlit Interface
def main():
st.set_page_config(page_title="Agentic Code Generator", layout="centered")
st.title("π€ Auto-Build LangGraph App")
st.markdown("Enter your **application domain** and a **clear goal** (what you want the app to do).")
# Input fields
domain = st.text_input("Domain (e.g. finance, warranty claims, medical):")
goal = st.text_area("System Goal (be detailed, include what data is needed):", height=150)
# Initialize session state
if "state" not in st.session_state:
st.session_state.state = {
"domain": "",
"goal": "",
"follow_up_questions": None,
"follow_up_answers": None,
"agents_needed": None,
"code_generator": None,
"explanation": None
}
if "step" not in st.session_state:
st.session_state.step = "initial"
# Generate button
if st.button("π Generate Agentic App") and domain and goal:
st.session_state.state.update({"domain": domain, "goal": goal})
st.session_state.step = "processing"
with st.spinner("Thinking like an architect..."):
try:
result = app.invoke(st.session_state.state)
st.session_state.state.update(result)
# Check if follow-up questions are needed
if (st.session_state.state.get("follow_up_questions") and
st.session_state.state["follow_up_questions"] != "No questions needed"):
st.session_state.step = "follow_up"
else:
st.session_state.step = "complete"
except Exception as e:
st.error(f"Error processing request: {str(e)}")
st.session_state.step = "error"
# Handle follow-up questions
if st.session_state.step == "follow_up":
st.subheader("π Follow-Up Questions")
if st.session_state.state.get("follow_up_questions"):
st.markdown(st.session_state.state["follow_up_questions"])
answers = st.text_area("Your answers to the above:", height=150, key="follow_up_answers")
if st.button("Continue with those answers"):
st.session_state.state["follow_up_answers"] = answers
st.session_state.step = "processing_final"
with st.spinner("Building your LangGraph app..."):
try:
# Re-run the graph with the follow-up answers
final_result = app.invoke(st.session_state.state)
st.session_state.state.update(final_result)
st.session_state.step = "complete"
except Exception as e:
st.error(f"Error in final processing: {str(e)}")
st.session_state.step = "error"
# Display results
if st.session_state.step == "complete" and st.session_state.state.get("code_generator"):
st.subheader("π― Identified Agents")
if st.session_state.state.get("agents_needed"):
st.markdown(st.session_state.state["agents_needed"])
st.subheader("β
Generated Code")
st.code(st.session_state.state["code_generator"], language="python")
st.subheader("π§ Code Explanation")
if st.session_state.state.get("explanation"):
st.markdown(st.session_state.state["explanation"])
# Download button
if st.session_state.state.get("code_generator"):
st.download_button(
label="π₯ Download Generated Code",
data=st.session_state.state["code_generator"],
file_name="generated_langgraph_app.py",
mime="text/python"
)
# Reset button
if st.button("π Reset"):
st.session_state.clear()
st.experimental_rerun()
if __name__ == "__main__":
main()
|