rasa-pro 3.13.0a1.dev6__py3-none-any.whl → 3.13.0a1.dev7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of rasa-pro might be problematic. Click here for more details.
- rasa/builder/README.md +120 -0
- rasa/builder/config.py +69 -0
- rasa/builder/create_openai_vector_store.py +204 -45
- rasa/builder/exceptions.py +49 -0
- rasa/builder/llm_service.py +327 -0
- rasa/builder/logging_utils.py +51 -0
- rasa/builder/main.py +61 -0
- rasa/builder/models.py +174 -0
- rasa/builder/project_generator.py +264 -0
- rasa/builder/service.py +447 -0
- rasa/builder/skill_to_bot_prompt.jinja2 +6 -1
- rasa/builder/training_service.py +123 -0
- rasa/builder/validation_service.py +79 -0
- rasa/cli/project_templates/finance/config.yml +17 -0
- rasa/cli/project_templates/finance/credentials.yml +33 -0
- rasa/cli/project_templates/finance/data/flows/transfer_money.yml +5 -0
- rasa/cli/project_templates/finance/data/patterns/pattern_session_start.yml +7 -0
- rasa/cli/project_templates/finance/domain.yml +7 -0
- rasa/cli/project_templates/finance/endpoints.yml +58 -0
- rasa/cli/project_templates/plain/config.yml +17 -0
- rasa/cli/project_templates/plain/credentials.yml +33 -0
- rasa/cli/project_templates/plain/data/patterns/pattern_session_start.yml +7 -0
- rasa/cli/project_templates/plain/domain.yml +5 -0
- rasa/cli/project_templates/plain/endpoints.yml +58 -0
- rasa/cli/project_templates/telecom/config.yml +17 -0
- rasa/cli/project_templates/telecom/credentials.yml +33 -0
- rasa/cli/project_templates/telecom/data/flows/upgrade_contract.yml +5 -0
- rasa/cli/project_templates/telecom/data/patterns/pattern_session_start.yml +7 -0
- rasa/cli/project_templates/telecom/domain.yml +7 -0
- rasa/cli/project_templates/telecom/endpoints.yml +58 -0
- rasa/cli/scaffold.py +19 -3
- rasa/core/actions/action.py +5 -3
- rasa/model_manager/model_api.py +1 -1
- rasa/model_manager/runner_service.py +1 -1
- rasa/model_manager/trainer_service.py +1 -1
- rasa/model_manager/utils.py +1 -29
- rasa/shared/core/domain.py +62 -15
- rasa/shared/core/flows/yaml_flows_io.py +16 -8
- rasa/telemetry.py +2 -1
- rasa/utils/io.py +27 -9
- rasa/version.py +1 -1
- {rasa_pro-3.13.0a1.dev6.dist-info → rasa_pro-3.13.0a1.dev7.dist-info}/METADATA +1 -1
- {rasa_pro-3.13.0a1.dev6.dist-info → rasa_pro-3.13.0a1.dev7.dist-info}/RECORD +46 -19
- rasa/builder/prompt_to_bot.py +0 -650
- {rasa_pro-3.13.0a1.dev6.dist-info → rasa_pro-3.13.0a1.dev7.dist-info}/NOTICE +0 -0
- {rasa_pro-3.13.0a1.dev6.dist-info → rasa_pro-3.13.0a1.dev7.dist-info}/WHEEL +0 -0
- {rasa_pro-3.13.0a1.dev6.dist-info → rasa_pro-3.13.0a1.dev7.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"""Service for generating Rasa projects from prompts."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import shutil
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from textwrap import dedent
|
|
8
|
+
from typing import Any, Dict, List, Optional
|
|
9
|
+
|
|
10
|
+
import structlog
|
|
11
|
+
|
|
12
|
+
from rasa.builder import config
|
|
13
|
+
from rasa.builder.exceptions import ProjectGenerationError, ValidationError
|
|
14
|
+
from rasa.builder.llm_service import get_skill_generation_messages, llm_service
|
|
15
|
+
from rasa.builder.validation_service import validate_project
|
|
16
|
+
from rasa.cli.scaffold import ProjectTemplateName, create_initial_project
|
|
17
|
+
from rasa.shared.core.flows import yaml_flows_io
|
|
18
|
+
from rasa.shared.importers.importer import TrainingDataImporter
|
|
19
|
+
from rasa.shared.utils.yaml import dump_obj_as_yaml_to_string
|
|
20
|
+
from rasa.utils.io import subpath
|
|
21
|
+
|
|
22
|
+
structlogger = structlog.get_logger()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ProjectGenerator:
|
|
26
|
+
"""Service for generating Rasa projects from skill descriptions."""
|
|
27
|
+
|
|
28
|
+
def __init__(self, project_folder: str):
|
|
29
|
+
"""Initialize the project generator with a folder for file persistence.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
project_folder: Path to the folder where project files will be stored
|
|
33
|
+
"""
|
|
34
|
+
self.project_folder = Path(project_folder)
|
|
35
|
+
self.project_folder.mkdir(parents=True, exist_ok=True)
|
|
36
|
+
|
|
37
|
+
def init_from_template(self, template: ProjectTemplateName):
|
|
38
|
+
"""Create the initial project files."""
|
|
39
|
+
self.cleanup()
|
|
40
|
+
create_initial_project(self.project_folder.as_posix(), template)
|
|
41
|
+
|
|
42
|
+
async def generate_project_with_retries(
|
|
43
|
+
self,
|
|
44
|
+
skill_description: str,
|
|
45
|
+
template: ProjectTemplateName,
|
|
46
|
+
max_retries: Optional[int] = None,
|
|
47
|
+
) -> Dict[str, str]:
|
|
48
|
+
"""Generate a Rasa project with retry logic for validation failures.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
skill_description: Natural language description of the skill
|
|
52
|
+
rasa_config: Rasa configuration dictionary
|
|
53
|
+
template: Project template to use for the initial project
|
|
54
|
+
max_retries: Maximum number of retry attempts
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
Dictionary of generated file contents (filename -> content)
|
|
58
|
+
|
|
59
|
+
Raises:
|
|
60
|
+
ProjectGenerationError: If generation fails after all retries
|
|
61
|
+
"""
|
|
62
|
+
if max_retries is None:
|
|
63
|
+
max_retries = config.MAX_RETRIES
|
|
64
|
+
|
|
65
|
+
self.init_from_template(template)
|
|
66
|
+
|
|
67
|
+
project_data = self._get_bot_data_for_llm()
|
|
68
|
+
|
|
69
|
+
initial_messages = get_skill_generation_messages(
|
|
70
|
+
skill_description, project_data
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
async def _generate_with_retry(
|
|
74
|
+
messages: List[Dict[str, Any]], attempts_left: int
|
|
75
|
+
):
|
|
76
|
+
try:
|
|
77
|
+
# Generate project data using LLM
|
|
78
|
+
project_data = await llm_service.generate_rasa_project(messages)
|
|
79
|
+
|
|
80
|
+
# Update stored bot data
|
|
81
|
+
self._update_bot_files_from_llm_response(project_data)
|
|
82
|
+
|
|
83
|
+
bot_files = self.get_bot_files()
|
|
84
|
+
structlogger.info(
|
|
85
|
+
"project_generator.generated_project",
|
|
86
|
+
attempts_left=attempts_left,
|
|
87
|
+
files=list(bot_files.keys()),
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
# Validate the generated project
|
|
91
|
+
await self._validate_generated_project()
|
|
92
|
+
|
|
93
|
+
structlogger.info(
|
|
94
|
+
"project_generator.validation_success", attempts_left=attempts_left
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
return bot_files
|
|
98
|
+
|
|
99
|
+
except ValidationError as e:
|
|
100
|
+
structlogger.error(
|
|
101
|
+
"project_generator.validation_error",
|
|
102
|
+
error=str(e),
|
|
103
|
+
attempts_left=attempts_left,
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
if attempts_left <= 0:
|
|
107
|
+
raise ProjectGenerationError(
|
|
108
|
+
f"Failed to generate valid Rasa project: {e}", max_retries
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
# Create error feedback for next attempt
|
|
112
|
+
error_feedback_messages = messages + [
|
|
113
|
+
{
|
|
114
|
+
"role": "assistant",
|
|
115
|
+
"content": json.dumps(project_data),
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"role": "user",
|
|
119
|
+
"content": dedent(f"""
|
|
120
|
+
Previous attempt failed validation with error: {e}
|
|
121
|
+
|
|
122
|
+
Please fix the issues and generate a valid Rasa project.
|
|
123
|
+
Pay special attention to:
|
|
124
|
+
- Proper YAML syntax
|
|
125
|
+
- Required fields in domain and flows
|
|
126
|
+
- Consistent naming between flows and domain
|
|
127
|
+
- Valid slot types and mappings
|
|
128
|
+
""").strip(),
|
|
129
|
+
},
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
return await _generate_with_retry(
|
|
133
|
+
error_feedback_messages, attempts_left - 1
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
except Exception as e:
|
|
137
|
+
structlogger.error(
|
|
138
|
+
"project_generator.generation_error",
|
|
139
|
+
error=str(e),
|
|
140
|
+
attempts_left=attempts_left,
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
if attempts_left <= 0:
|
|
144
|
+
raise ProjectGenerationError(
|
|
145
|
+
f"Failed to generate Rasa project: {e}", max_retries
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
# For non-validation errors, retry with original messages
|
|
149
|
+
return await _generate_with_retry(initial_messages, attempts_left - 1)
|
|
150
|
+
|
|
151
|
+
return await _generate_with_retry(initial_messages, max_retries)
|
|
152
|
+
|
|
153
|
+
async def _validate_generated_project(self):
|
|
154
|
+
"""Validate the generated project using the validation service."""
|
|
155
|
+
importer = self._create_importer()
|
|
156
|
+
validation_error = await validate_project(importer)
|
|
157
|
+
|
|
158
|
+
if validation_error:
|
|
159
|
+
raise ValidationError(validation_error)
|
|
160
|
+
|
|
161
|
+
def _create_importer(self) -> TrainingDataImporter:
|
|
162
|
+
"""Create a training data importer from the current bot files."""
|
|
163
|
+
try:
|
|
164
|
+
return TrainingDataImporter.load_from_config(
|
|
165
|
+
config_path=self.project_folder / "config.yml",
|
|
166
|
+
domain_path=self.project_folder / "domain.yml",
|
|
167
|
+
training_data_paths=[
|
|
168
|
+
self.project_folder / "data",
|
|
169
|
+
],
|
|
170
|
+
args={},
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
except Exception as e:
|
|
174
|
+
raise ValidationError(f"Failed to create importer: {e}")
|
|
175
|
+
|
|
176
|
+
def get_bot_files(self) -> Dict[str, str]:
|
|
177
|
+
"""Get the current bot files by reading from disk."""
|
|
178
|
+
bot_files = {}
|
|
179
|
+
|
|
180
|
+
for file in self.project_folder.glob("**/*"):
|
|
181
|
+
# Skip directories
|
|
182
|
+
if not file.is_file():
|
|
183
|
+
continue
|
|
184
|
+
|
|
185
|
+
relative_path = file.relative_to(self.project_folder)
|
|
186
|
+
|
|
187
|
+
# Skip hidden files and directories (any path component starting with '.')
|
|
188
|
+
if any(part.startswith(".") for part in relative_path.parts):
|
|
189
|
+
continue
|
|
190
|
+
|
|
191
|
+
# exclude the project_folder / models folder
|
|
192
|
+
if relative_path.parts[0] == "models":
|
|
193
|
+
continue
|
|
194
|
+
|
|
195
|
+
# Read file content and store with relative path as key
|
|
196
|
+
bot_files[relative_path.as_posix()] = file.read_text(encoding="utf-8")
|
|
197
|
+
|
|
198
|
+
return bot_files
|
|
199
|
+
|
|
200
|
+
def _get_bot_data_for_llm(self) -> Dict[str, Any]:
|
|
201
|
+
"""Get the current bot data for the LLM."""
|
|
202
|
+
file_importer = self._create_importer()
|
|
203
|
+
|
|
204
|
+
# only include data created by the user (or the builder llm)
|
|
205
|
+
# avoid including to many defaults that are not customized
|
|
206
|
+
domain = file_importer.get_user_domain()
|
|
207
|
+
flows = file_importer.get_user_flows()
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
"domain": domain.as_dict(should_clean_json=True),
|
|
211
|
+
"flows": yaml_flows_io.get_flows_as_json(flows, should_clean_json=True),
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
def _path_for_flow(self, flow_id: str) -> str:
|
|
215
|
+
"""Get the path for a flow."""
|
|
216
|
+
if flow_id.startswith("pattern_"):
|
|
217
|
+
return f"data/patterns/{flow_id}.yml"
|
|
218
|
+
else:
|
|
219
|
+
return f"data/flows/{flow_id}.yml"
|
|
220
|
+
|
|
221
|
+
def _update_bot_files_from_llm_response(self, project_data: Dict[str, Any]):
|
|
222
|
+
"""Update the bot files with generated data by writing to disk."""
|
|
223
|
+
files = {"domain.yml": dump_obj_as_yaml_to_string(project_data["domain"])}
|
|
224
|
+
# split up flows into one file per flow in the /flows folder
|
|
225
|
+
for flow_id, flow_data in project_data["flows"].get("flows", {}).items():
|
|
226
|
+
flow_file_path = self._path_for_flow(flow_id)
|
|
227
|
+
single_flow_file_data = {"flows": {flow_id: flow_data}}
|
|
228
|
+
files[flow_file_path] = dump_obj_as_yaml_to_string(single_flow_file_data)
|
|
229
|
+
|
|
230
|
+
# removes any other flows that the LLM didn't generate
|
|
231
|
+
self._cleanup_flows()
|
|
232
|
+
self.update_bot_files(files)
|
|
233
|
+
|
|
234
|
+
def _cleanup_flows(self):
|
|
235
|
+
"""Cleanup the flows folder."""
|
|
236
|
+
flows_folder = self.project_folder / "data" / "flows"
|
|
237
|
+
if flows_folder.exists():
|
|
238
|
+
shutil.rmtree(flows_folder)
|
|
239
|
+
flows_folder.mkdir(parents=True, exist_ok=True)
|
|
240
|
+
|
|
241
|
+
def update_bot_files(self, files: Dict[str, str]):
|
|
242
|
+
"""Update bot files with new content by writing to disk."""
|
|
243
|
+
for filename, content in files.items():
|
|
244
|
+
file_path = Path(subpath(self.project_folder, filename))
|
|
245
|
+
file_path.parent.mkdir(parents=True, exist_ok=True)
|
|
246
|
+
file_path.write_text(content, encoding="utf-8")
|
|
247
|
+
|
|
248
|
+
def cleanup(self):
|
|
249
|
+
"""Cleanup the project folder."""
|
|
250
|
+
# remove all the files and folders in the project folder resulting
|
|
251
|
+
# in an empty folder
|
|
252
|
+
for filename in os.listdir(self.project_folder):
|
|
253
|
+
file_path = os.path.join(self.project_folder, filename)
|
|
254
|
+
try:
|
|
255
|
+
if os.path.isfile(file_path) or os.path.islink(file_path):
|
|
256
|
+
os.unlink(file_path)
|
|
257
|
+
elif os.path.isdir(file_path):
|
|
258
|
+
shutil.rmtree(file_path)
|
|
259
|
+
except Exception as e:
|
|
260
|
+
structlogger.error(
|
|
261
|
+
"project_generator.cleanup_error",
|
|
262
|
+
error=str(e),
|
|
263
|
+
file_path=file_path,
|
|
264
|
+
)
|