mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-09-06 08:16:00 +00:00
add quickstart files
This commit is contained in:
parent
12a769ea09
commit
52ffcdcc2c
2 changed files with 289 additions and 0 deletions
262
cookbook/step_agent/examples.py
Normal file
262
cookbook/step_agent/examples.py
Normal file
|
|
@ -0,0 +1,262 @@
|
|||
"""
|
||||
Step Experience Service Usage Examples
|
||||
|
||||
This file demonstrates how to use the step-level experience extraction and context generation service.
|
||||
"""
|
||||
|
||||
import json
|
||||
import requests
|
||||
from typing import List, Dict, Any
|
||||
|
||||
from experiencemaker.enumeration.role import Role
|
||||
from experiencemaker.schema.trajectory import Trajectory, Message, ToolCall
|
||||
|
||||
# Service configuration
|
||||
SERVICE_URL = "http://localhost:8001"
|
||||
WORKSPACE_ID = "test_workspace"
|
||||
|
||||
|
||||
def create_sample_trajectory(query: str, steps: List[Message], done: bool = True) -> Trajectory:
|
||||
"""Create a sample trajectory for testing"""
|
||||
trajectory = Trajectory(
|
||||
query=query,
|
||||
steps=steps,
|
||||
done=done,
|
||||
current_step=len(steps) - 1,
|
||||
metadata={
|
||||
"domain": "coding",
|
||||
"task_type": "problem_solving"
|
||||
}
|
||||
)
|
||||
return trajectory
|
||||
|
||||
|
||||
def example_1_extract_step_experiences():
|
||||
"""Example 1: Extract step-level experiences from trajectories"""
|
||||
print("=== Example 1: Extract Step-Level Experiences ===")
|
||||
|
||||
# Create sample successful trajectory
|
||||
successful_trajectory = create_sample_trajectory(
|
||||
query="How to implement a binary search algorithm?",
|
||||
steps=[
|
||||
Message(
|
||||
role=Role.USER,
|
||||
content="How to implement a binary search algorithm?"
|
||||
),
|
||||
Message(
|
||||
role=Role.ASSISTANT,
|
||||
content="I'll help you implement a binary search algorithm. Let me start by explaining the concept.",
|
||||
reasoning_content="Need to first explain the concept before implementation"
|
||||
),
|
||||
Message(
|
||||
role=Role.ASSISTANT,
|
||||
content="Here's the implementation:\n\ndef binary_search(arr, target):\n left, right = 0, len(arr) - 1\n while left <= right:\n mid = (left + right) // 2\n if arr[mid] == target:\n return mid\n elif arr[mid] < target:\n left = mid + 1\n else:\n right = mid - 1\n return -1",
|
||||
tool_calls=[
|
||||
ToolCall(
|
||||
index=0,
|
||||
id="call_1",
|
||||
name="code_execution",
|
||||
arguments='{"code": "def binary_search(arr, target): ..."}',
|
||||
result="Code executed successfully"
|
||||
)
|
||||
]
|
||||
),
|
||||
Message(
|
||||
role=Role.TOOL,
|
||||
content="Code executed successfully. Binary search implementation is correct."
|
||||
)
|
||||
],
|
||||
done=True
|
||||
)
|
||||
|
||||
# Create sample failed trajectory
|
||||
failed_trajectory = create_sample_trajectory(
|
||||
query="How to implement a binary search algorithm?",
|
||||
steps=[
|
||||
Message(
|
||||
role=Role.USER,
|
||||
content="How to implement a binary search algorithm?"
|
||||
),
|
||||
Message(
|
||||
role=Role.ASSISTANT,
|
||||
content="Here's a binary search:\n\ndef binary_search(arr, target):\n for i in range(len(arr)):\n if arr[i] == target:\n return i\n return -1",
|
||||
reasoning_content="Implementing binary search as linear search by mistake"
|
||||
),
|
||||
Message(
|
||||
role=Role.TOOL,
|
||||
content="Error: This is actually a linear search, not binary search. Binary search requires sorted array and divide-and-conquer approach."
|
||||
)
|
||||
],
|
||||
done=False
|
||||
)
|
||||
|
||||
# Extract experiences using summarizer
|
||||
summarizer_request = {
|
||||
"trajectories": [successful_trajectory.model_dump(), failed_trajectory.model_dump()],
|
||||
"workspace_id": WORKSPACE_ID
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(f"{SERVICE_URL}/summarizer", json=summarizer_request)
|
||||
response.raise_for_status()
|
||||
|
||||
result = response.json()
|
||||
print(f"✅ Extracted {len(result['experiences'])} step-level experiences")
|
||||
|
||||
for i, experience in enumerate(result['experiences']):
|
||||
print(f"\nExperience {i + 1}:")
|
||||
print(f" Condition: {experience['experience_desc']}")
|
||||
print(f" Content: {experience['experience_content'][:100]}...")
|
||||
print(f" Role: {experience['experience_role']}")
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"❌ Error extracting experiences: {e}")
|
||||
|
||||
|
||||
def example_2_generate_step_context():
|
||||
"""Example 2: Generate context from step-level experiences"""
|
||||
print("\n=== Example 2: Generate Step-Level Context ===")
|
||||
|
||||
# Create a new trajectory that needs context
|
||||
current_trajectory = create_sample_trajectory(
|
||||
query="How to implement a quick sort algorithm?",
|
||||
steps=[
|
||||
Message(
|
||||
role=Role.USER,
|
||||
content="How to implement a quick sort algorithm?"
|
||||
),
|
||||
Message(
|
||||
role=Role.ASSISTANT,
|
||||
content="I need to implement a quick sort algorithm. Let me think about the approach.",
|
||||
reasoning_content="Quick sort is a divide-and-conquer algorithm"
|
||||
)
|
||||
],
|
||||
done=False
|
||||
)
|
||||
|
||||
# Generate context using context generator
|
||||
context_request = {
|
||||
"trajectory": current_trajectory.model_dump(),
|
||||
"workspace_id": WORKSPACE_ID
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(f"{SERVICE_URL}/context_generator", json=context_request)
|
||||
response.raise_for_status()
|
||||
|
||||
result = response.json()
|
||||
context_message = result['context_msg']
|
||||
|
||||
print("✅ Generated step-level context:")
|
||||
print(f"Content: {context_message['content']}")
|
||||
|
||||
if 'metadata' in context_message:
|
||||
print(f"Metadata: {context_message['metadata']}")
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"❌ Error generating context: {e}")
|
||||
|
||||
|
||||
def example_3_full_agent_execution():
|
||||
"""Example 3: Full agent execution with step-level experience"""
|
||||
print("\n=== Example 3: Full Agent Execution with Step Experience ===")
|
||||
|
||||
# Execute agent with step-level context
|
||||
agent_request = {
|
||||
"query": "Implement a merge sort algorithm with proper error handling",
|
||||
"workspace_id": WORKSPACE_ID
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(f"{SERVICE_URL}/agent_wrapper", json=agent_request)
|
||||
response.raise_for_status()
|
||||
|
||||
result = response.json()
|
||||
trajectory = result['trajectory']
|
||||
|
||||
print("✅ Agent execution completed:")
|
||||
print(f"Query: {trajectory['query']}")
|
||||
print(f"Steps: {len(trajectory['steps'])}")
|
||||
print(f"Done: {trajectory['done']}")
|
||||
print(f"Answer: {trajectory.get('answer', 'No answer')[:200]}...")
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"❌ Error in agent execution: {e}")
|
||||
|
||||
|
||||
def example_4_custom_configuration():
|
||||
"""Example 4: Custom configuration for different use cases"""
|
||||
print("\n=== Example 4: Custom Configuration Examples ===")
|
||||
|
||||
# Configuration for research-intensive tasks
|
||||
research_config = {
|
||||
"context_generator": {
|
||||
"backend": "step",
|
||||
"enable_llm_rerank": True,
|
||||
"enable_context_rewrite": True,
|
||||
"enable_score_filter": True,
|
||||
"vector_retrieve_top_k": 20,
|
||||
"final_top_k": 8,
|
||||
"min_score_threshold": 0.2
|
||||
},
|
||||
"summarizer": {
|
||||
"backend": "step",
|
||||
"enable_step_segmentation": True,
|
||||
"enable_similarity_search": True,
|
||||
"enable_experience_validation": True,
|
||||
"max_retries": 5
|
||||
}
|
||||
}
|
||||
|
||||
# Configuration for quick tasks
|
||||
quick_config = {
|
||||
"context_generator": {
|
||||
"backend": "step",
|
||||
"enable_llm_rerank": False,
|
||||
"enable_context_rewrite": False,
|
||||
"enable_score_filter": False,
|
||||
"vector_retrieve_top_k": 5,
|
||||
"final_top_k": 2,
|
||||
"min_score_threshold": 0.5
|
||||
},
|
||||
"summarizer": {
|
||||
"backend": "step",
|
||||
"enable_step_segmentation": False,
|
||||
"enable_similarity_search": False,
|
||||
"enable_experience_validation": False,
|
||||
"max_retries": 1
|
||||
}
|
||||
}
|
||||
|
||||
print("📋 Research-intensive configuration:")
|
||||
print(json.dumps(research_config, indent=2))
|
||||
|
||||
print("\n📋 Quick task configuration:")
|
||||
print(json.dumps(quick_config, indent=2))
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all examples"""
|
||||
print("🚀 Step Experience Service Usage Examples")
|
||||
print("=" * 50)
|
||||
|
||||
# Wait for service to be ready
|
||||
try:
|
||||
response = requests.get(f"{SERVICE_URL}/docs")
|
||||
print("✅ Service is running and ready")
|
||||
except requests.exceptions.RequestException:
|
||||
print("❌ Service is not running. Please start the service first:")
|
||||
print(" run.sh")
|
||||
return
|
||||
|
||||
# Run examples
|
||||
example_1_extract_step_experiences()
|
||||
example_2_generate_step_context()
|
||||
example_3_full_agent_execution()
|
||||
example_4_custom_configuration()
|
||||
|
||||
print("\n🎉 All examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
27
cookbook/step_agent/run.sh
Normal file
27
cookbook/step_agent/run.sh
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Step Experience Service Startup Script
|
||||
# This script launches the experiencemaker service with step-level experience extraction and context generation
|
||||
|
||||
# StepSummarizer Parameters
|
||||
# enable_step_segmentation: Segment trajectories into meaningful step sequences
|
||||
# enable_similarity_search: Compare similar sequences between success/failure
|
||||
# enable_experience_validation: Validate experience quality before storage
|
||||
|
||||
# StepContextGenerator Parameters
|
||||
# enable_llm_rerank: Rerank retrieved experiences by relevance using LLM
|
||||
# enable_context_rewrite: Rewrite context to be more task-specific
|
||||
# enable_score_filter: Filter experiences by quality scores
|
||||
|
||||
echo "Starting Step Experience Service..."
|
||||
|
||||
python -m experiencemaker.em_service \
|
||||
--port=8001 \
|
||||
--llm='{"backend": "openai_compatible", "model_name": "qwen-max-2025-01-25", "temperature": 0.6}' \
|
||||
--embedding_model='{"backend": "openai_compatible", "model_name": "text-embedding-v4", "dimensions": 1024}' \
|
||||
--vector_store='{"backend": "local_file", "store_dir": "./step_experiences/"}' \
|
||||
--agent_wrapper='{"backend": "simple"}' \
|
||||
--context_generator='{"backend": "step", "enable_llm_rerank": true, "enable_context_rewrite": true, "enable_score_filter": false, "vector_retrieve_top_k": 15, "final_top_k": 5, "min_score_threshold": 0.3}' \
|
||||
--summarizer='{"backend": "step", "enable_step_segmentation": false, "enable_similarity_search": false, "enable_experience_validation": true, "max_retries": 3}'
|
||||
|
||||
echo "Step Experience Service started on port 8001"
|
||||
Loading…
Add table
Reference in a new issue