This module provides an interactive agent that helps users create PyKGML configuration scripts through a conversational interface.
The agent guides users through creating two types of configuration scripts:
- Model Structure Configuration - For designing neural network architectures
- Loss Function Configuration - For customizing loss functions
The agent follows a step-by-step question-answer loop:
- Script Type Selection - Determines if user wants model structure or loss function
- Status Check - Validates required fields and identifies missing information
- Question Generation - Uses RAG to retrieve relevant documentation and generates contextual questions
- Information Extraction - Parses user responses to extract structured values
- Script Update - Updates configuration dictionary with extracted values
- Loop - Returns to status check until configuration is complete
The agent is integrated into server_app.py with a /config_generator endpoint:
from pykgml_config_agent.agent_chain import create_simple_config_chain
# Create chain
config_agent_chain = create_simple_config_chain(llm, retriever)
# Add to FastAPI app
add_routes(app, config_generator_chain, path="/config_generator")The frontend (frontend/frontend_block.py) includes a "Config Generator" mode that:
- Maintains state across conversation turns
- Displays generated questions
- Shows final configuration when complete
- User: "I want to create a model structure"
- Agent: "Let's start! What would you like to name your model class?"
- User: "MyGRUModel"
- Agent: "Great! What will be your input dimension?"
- User: "19"
- [Continues until all fields are filled]
- Agent: "Configuration complete! Here's your script: [shows config]"
ConfigGenerationState- Pydantic model tracking script generation progress
get_model_structure_template()- Empty model structure templateget_loss_function_template()- Empty loss function template
check_completion_status()- Validates configuration completeness
- Uses RAG to retrieve relevant documentation
- Generates contextual questions for missing fields
- Parses natural language responses
- Extracts structured values using LLM
- Validates extracted values
- Updates configuration dictionaries safely
- Orchestrates the question-answer-update loop
create_simple_config_chain()- Main chain for integration
{
"class_name": "MyModel",
"base_class": "TimeSeriesModel",
"init_params": {
"input_dim": 19,
"hidden_dim": 128,
"num_layers": 2,
"output_dim": 3
},
"layers": {
"gru_basic": ("gru", "input_dim", "hidden_dim", "num_layers", "dropout")
},
"forward": {
"out_basic, hidden": "gru_basic(x)",
"output": "fc(out_basic)"
}
}{
"parameters": {
"Ra_idx": 0,
"Rh_idx": 1
},
"variables": {
"Ra_pred": "y_pred[:, :, Ra_idx]",
"Ra_true": "y_true[:, :, Ra_idx]"
},
"loss_formula": {
"loss": "mean((Ra_pred - Ra_true)**2)"
}
}- LangChain
- Pydantic
- FastAPI
- Gradio (for frontend)
- Existing RAG infrastructure (FAISS vector store, retriever, LLM)