AnonymousSpottedToad/securityPatternsRecovery / codebase /repos /python /stateset_stateset-agents /examples /quick_start.py
| """ | |
| Quick Start Guide for GRPO Agent Framework | |
| This example shows the simplest way to get started with training | |
| a multi-turn conversational agent. | |
| """ | |
| import asyncio | |
| import logging | |
| from stateset_agents import ( | |
| ConversationEnvironment, | |
| HelpfulnessReward, | |
| MultiTurnAgent, | |
| SafetyReward, | |
| train, | |
| ) | |
| from stateset_agents.core.agent import AgentConfig | |
| # Set up logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| QUICKSTART_STUB_RESPONSES = [ | |
| "I'd be happy to help you with that.", | |
| "A good starting point is to focus on the fundamentals and practice often.", | |
| "You can build confidence by trying a small project end to end.", | |
| "That sounds like a solid next step.", | |
| ] | |
| def create_quickstart_config( | |
| system_prompt: str, | |
| *, | |
| temperature: float = 0.8, | |
| max_new_tokens: int = 256, | |
| ) -> AgentConfig: | |
| """Return a portable default config for the quick-start examples. | |
| The quick start should run on a fresh local machine without model downloads, | |
| GPU kernels, or optional inference packages. Users can swap in a real model | |
| by changing ``model_name`` and disabling ``use_stub_model``. | |
| """ | |
| return AgentConfig( | |
| model_name="stub://quickstart", | |
| system_prompt=system_prompt, | |
| temperature=temperature, | |
| max_new_tokens=max_new_tokens, | |
| use_stub_model=True, | |
| stub_responses=QUICKSTART_STUB_RESPONSES, | |
| attn_implementation="eager", | |
| ) | |
| async def basic_example(): | |
| """Most basic example - train a helpful assistant""" | |
| logger.info("GRPO Agent Framework - Quick Start Example") | |
| logger.info("=" * 50) | |
| # Step 1: Create an agent | |
| logger.info("Step 1: Creating agent...") | |
| config = create_quickstart_config( | |
| "You are a helpful AI assistant. Be friendly and informative.", | |
| temperature=0.8, | |
| max_new_tokens=256, | |
| ) | |
| agent = MultiTurnAgent(config) | |
| await agent.initialize() | |
| # Step 2: Create an environment | |
| logger.info("Step 2: Creating environment...") | |
| # Simple conversation scenarios | |
| scenarios = [ | |
| { | |
| "id": "general_help", | |
| "topic": "general_assistance", | |
| "context": "User needs general help", | |
| "user_responses": [ | |
| "Hi there! Can you help me with something?", | |
| "I'm trying to learn about machine learning. Where should I start?", | |
| "That's helpful. What about practical projects?", | |
| "Great suggestions! Thank you for your help.", | |
| ], | |
| }, | |
| { | |
| "id": "creative_help", | |
| "topic": "creative_writing", | |
| "context": "User wants creative writing help", | |
| "user_responses": [ | |
| "I'm working on a short story and need some inspiration.", | |
| "It's about a detective in a futuristic city. Any ideas for plot twists?", | |
| "I like that! How can I make the character more interesting?", | |
| "Perfect! That gives me a lot to work with.", | |
| ], | |
| }, | |
| ] | |
| environment = ConversationEnvironment(scenarios=scenarios, max_turns=4) | |
| # Step 3: Define reward function (optional - can use defaults) | |
| logger.info("Step 3: Setting up rewards...") | |
| from stateset_agents.core.reward import CompositeReward | |
| reward_fn = CompositeReward( | |
| [HelpfulnessReward(weight=0.7), SafetyReward(weight=0.3)] | |
| ) | |
| # Step 4: Train the agent | |
| logger.info("Step 4: Training agent...") | |
| trained_agent = await train( | |
| agent=agent, | |
| environment=environment, | |
| reward_fn=reward_fn, | |
| num_episodes=4, # Keep the quick start genuinely quick | |
| profile="balanced", | |
| training_mode="single_turn", | |
| ) | |
| logger.info("Training completed!") | |
| # Step 5: Test the trained agent | |
| logger.info("\nStep 5: Testing trained agent...") | |
| await test_conversation(trained_agent) | |
| async def test_conversation(agent): | |
| """Test the trained agent with a sample conversation""" | |
| conversation = [{"role": "system", "content": agent.config.system_prompt}] | |
| test_inputs = [ | |
| "Hello! I'm new to programming. Can you help me get started?", | |
| "What programming language would you recommend for a beginner?", | |
| "That sounds good. Where can I find good tutorials?", | |
| "Thank you! This is very helpful.", | |
| ] | |
| logger.info("\n" + "=" * 40) | |
| logger.info("TEST CONVERSATION") | |
| logger.info("=" * 40) | |
| for user_input in test_inputs: | |
| logger.info(f"\nUser: {user_input}") | |
| conversation.append({"role": "user", "content": user_input}) | |
| response = await agent.generate_response(conversation) | |
| logger.info(f"Assistant: {response}") | |
| conversation.append({"role": "assistant", "content": response}) | |
| logger.info("\n" + "=" * 40) | |
| async def auto_training_example(): | |
| """Example using automatic training optimization""" | |
| logger.info("\nAuto Training Example") | |
| logger.info("=" * 30) | |
| from stateset_agents.training.train import AutoTrainer | |
| # Create agent and environment (same as before) | |
| config = create_quickstart_config( | |
| "You are a helpful AI assistant.", | |
| temperature=0.8, | |
| ) | |
| agent = MultiTurnAgent(config) | |
| await agent.initialize() | |
| scenarios = [ | |
| { | |
| "id": "help_request", | |
| "topic": "assistance", | |
| "context": "User needs help", | |
| "user_responses": [ | |
| "I need help with something.", | |
| "Can you explain how to write a good email?", | |
| "What about the subject line?", | |
| "Thanks, that's very helpful!", | |
| ], | |
| } | |
| ] | |
| environment = ConversationEnvironment(scenarios=scenarios, max_turns=6) | |
| # Use AutoTrainer for automatic optimization | |
| auto_trainer = AutoTrainer(auto_adjust=True, early_stopping=True) | |
| logger.info("Starting automatic training...") | |
| trained_agent = await auto_trainer.train( | |
| agent=agent, | |
| environment=environment, | |
| num_episodes=4, # Fast smoke-sized demo | |
| ) | |
| logger.info("Automatic training completed!") | |
| await test_conversation(trained_agent) | |
| async def custom_reward_example(): | |
| """Example with custom reward function""" | |
| logger.info("\nCustom Reward Example") | |
| logger.info("=" * 25) | |
| from stateset_agents.core.reward import RewardFunction, RewardResult | |
| class PolitenesReward(RewardFunction): | |
| """Custom reward for politeness""" | |
| async def compute_reward(self, turns, context=None): | |
| polite_phrases = [ | |
| "please", | |
| "thank you", | |
| "you're welcome", | |
| "excuse me", | |
| "i'm sorry", | |
| "pardon me", | |
| ] | |
| assistant_turns = [t for t in turns if t.role == "assistant"] | |
| if not assistant_turns: | |
| return RewardResult(score=0.0, breakdown={}, metadata={}) | |
| total_score = 0.0 | |
| breakdown = {} | |
| for i, turn in enumerate(assistant_turns): | |
| content_lower = turn.content.lower() | |
| politeness_count = sum( | |
| 1 for phrase in polite_phrases if phrase in content_lower | |
| ) | |
| score = min(politeness_count * 0.3, 1.0) | |
| total_score += score | |
| breakdown[f"turn_{i}_politeness"] = score | |
| avg_score = total_score / len(assistant_turns) | |
| return RewardResult( | |
| score=avg_score, | |
| breakdown=breakdown, | |
| metadata={"polite_phrases_found": sum(breakdown.values())}, | |
| ) | |
| # Create agent with politeness emphasis | |
| config = create_quickstart_config( | |
| "You are a very polite and courteous AI assistant. Always use please, thank you, and other polite expressions.", | |
| temperature=0.7, | |
| ) | |
| agent = MultiTurnAgent(config) | |
| await agent.initialize() | |
| # Simple environment | |
| scenarios = [ | |
| { | |
| "id": "polite_test", | |
| "user_responses": ["Hello!", "Can you help me?", "Thank you!"], | |
| } | |
| ] | |
| environment = ConversationEnvironment(scenarios=scenarios, max_turns=4) | |
| # Use custom reward | |
| from stateset_agents.core.reward import CompositeReward | |
| reward_fn = CompositeReward( | |
| [PolitenesReward(weight=0.6), HelpfulnessReward(weight=0.4)] | |
| ) | |
| logger.info("Training with custom politeness reward...") | |
| trained_agent = await train( | |
| agent=agent, | |
| environment=environment, | |
| reward_fn=reward_fn, | |
| num_episodes=4, | |
| profile="conservative", # More stable for custom rewards | |
| ) | |
| logger.info("Custom reward training completed!") | |
| await test_conversation(trained_agent) | |
| async def main(): | |
| """Run the minimal onboarding example.""" | |
| await basic_example() | |
| logger.info("\nQuick start completed successfully!") | |
| logger.info("\nNext steps:") | |
| logger.info( | |
| "1. Run the auto-training variant inside this file if you want a larger demo" | |
| ) | |
| logger.info("2. Run custom_reward_example() to experiment with custom rewards") | |
| logger.info("3. Swap the stub model config for a real checkpoint when you're ready") | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |
Xet Storage Details
- Size:
- 9.36 kB
- Xet hash:
- a28265ca544bf10bcf39844ef2dca60223249bec58894c69ea0088a12309c75e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.