rasa-pro 3.13.0rc4__py3-none-any.whl → 3.13.1a2__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.

Files changed (59) hide show
  1. rasa/builder/README.md +120 -0
  2. rasa/builder/__init__.py +0 -0
  3. rasa/builder/config.py +69 -0
  4. rasa/builder/create_openai_vector_store.py +228 -0
  5. rasa/builder/exceptions.py +49 -0
  6. rasa/builder/llm-helper-schema.json +69 -0
  7. rasa/builder/llm_context.py +81 -0
  8. rasa/builder/llm_helper_prompt.jinja2 +245 -0
  9. rasa/builder/llm_service.py +327 -0
  10. rasa/builder/logging_utils.py +51 -0
  11. rasa/builder/main.py +61 -0
  12. rasa/builder/models.py +174 -0
  13. rasa/builder/project_generator.py +264 -0
  14. rasa/builder/scrape_rasa_docs.py +97 -0
  15. rasa/builder/service.py +447 -0
  16. rasa/builder/skill_to_bot_prompt.jinja2 +164 -0
  17. rasa/builder/training_service.py +123 -0
  18. rasa/builder/validation_service.py +79 -0
  19. rasa/cli/project_templates/finance/config.yml +17 -0
  20. rasa/cli/project_templates/finance/credentials.yml +33 -0
  21. rasa/cli/project_templates/finance/data/flows/transfer_money.yml +5 -0
  22. rasa/cli/project_templates/finance/data/patterns/pattern_session_start.yml +7 -0
  23. rasa/cli/project_templates/finance/domain.yml +7 -0
  24. rasa/cli/project_templates/finance/endpoints.yml +58 -0
  25. rasa/cli/project_templates/plain/config.yml +17 -0
  26. rasa/cli/project_templates/plain/credentials.yml +33 -0
  27. rasa/cli/project_templates/plain/data/patterns/pattern_session_start.yml +7 -0
  28. rasa/cli/project_templates/plain/domain.yml +5 -0
  29. rasa/cli/project_templates/plain/endpoints.yml +58 -0
  30. rasa/cli/project_templates/telecom/config.yml +17 -0
  31. rasa/cli/project_templates/telecom/credentials.yml +33 -0
  32. rasa/cli/project_templates/telecom/data/flows/upgrade_contract.yml +5 -0
  33. rasa/cli/project_templates/telecom/data/patterns/pattern_session_start.yml +7 -0
  34. rasa/cli/project_templates/telecom/domain.yml +7 -0
  35. rasa/cli/project_templates/telecom/endpoints.yml +58 -0
  36. rasa/cli/scaffold.py +19 -3
  37. rasa/core/actions/action.py +5 -3
  38. rasa/core/channels/studio_chat.py +29 -8
  39. rasa/core/policies/flows/flow_executor.py +8 -1
  40. rasa/model_manager/model_api.py +2 -2
  41. rasa/model_manager/runner_service.py +1 -1
  42. rasa/model_manager/trainer_service.py +12 -9
  43. rasa/model_manager/utils.py +1 -29
  44. rasa/shared/core/domain.py +62 -15
  45. rasa/shared/core/flows/flow_step.py +7 -1
  46. rasa/shared/core/flows/yaml_flows_io.py +16 -8
  47. rasa/shared/core/slots.py +4 -0
  48. rasa/shared/importers/importer.py +6 -0
  49. rasa/shared/importers/static.py +63 -0
  50. rasa/telemetry.py +2 -1
  51. rasa/utils/io.py +27 -9
  52. rasa/utils/log_utils.py +5 -1
  53. rasa/validator.py +7 -3
  54. rasa/version.py +1 -1
  55. {rasa_pro-3.13.0rc4.dist-info → rasa_pro-3.13.1a2.dist-info}/METADATA +3 -3
  56. {rasa_pro-3.13.0rc4.dist-info → rasa_pro-3.13.1a2.dist-info}/RECORD +59 -23
  57. {rasa_pro-3.13.0rc4.dist-info → rasa_pro-3.13.1a2.dist-info}/NOTICE +0 -0
  58. {rasa_pro-3.13.0rc4.dist-info → rasa_pro-3.13.1a2.dist-info}/WHEEL +0 -0
  59. {rasa_pro-3.13.0rc4.dist-info → rasa_pro-3.13.1a2.dist-info}/entry_points.txt +0 -0
rasa/builder/models.py ADDED
@@ -0,0 +1,174 @@
1
+ """Pydantic models for request/response validation."""
2
+
3
+ from typing import Any, Dict, List, Literal, Optional, Union
4
+
5
+ from pydantic import BaseModel, Field, validator
6
+
7
+ from rasa.cli.scaffold import ProjectTemplateName
8
+ from rasa.shared.core.trackers import DialogueStateTracker
9
+
10
+
11
+ class PromptRequest(BaseModel):
12
+ """Request model for prompt-to-bot endpoint."""
13
+
14
+ prompt: str = Field(
15
+ ..., min_length=1, max_length=10000, description="The skill description prompt"
16
+ )
17
+ client_id: Optional[str] = Field(
18
+ None, max_length=255, description="Optional client identifier"
19
+ )
20
+
21
+ @validator("prompt")
22
+ def validate_prompt(cls, v):
23
+ if not v.strip():
24
+ raise ValueError("Prompt cannot be empty or whitespace only")
25
+ return v.strip()
26
+
27
+
28
+ class TemplateRequest(BaseModel):
29
+ """Request model for template-to-bot endpoint."""
30
+
31
+ template_name: ProjectTemplateName = Field(
32
+ ...,
33
+ description=(
34
+ f"The template name to use ({ProjectTemplateName.supported_values()})"
35
+ ),
36
+ )
37
+ client_id: Optional[str] = Field(
38
+ None, max_length=255, description="Optional client identifier"
39
+ )
40
+
41
+ @validator("template_name")
42
+ def validate_template_name(cls, v):
43
+ if v not in ProjectTemplateName:
44
+ raise ValueError(
45
+ f"Template name must be one of {ProjectTemplateName.supported_values()}"
46
+ )
47
+ return v
48
+
49
+
50
+ class ChatMessage(BaseModel):
51
+ """Model for chat messages."""
52
+
53
+ type: str = Field(..., pattern="^(user|assistant)$")
54
+ content: Union[str, List[Dict[str, Any]]] = Field(...)
55
+
56
+
57
+ class LLMBuilderRequest(BaseModel):
58
+ """Request model for LLM builder endpoint."""
59
+
60
+ messages: List[ChatMessage] = Field(..., min_items=1, max_items=50)
61
+
62
+
63
+ class LLMBuilderContext(BaseModel):
64
+ """Context model for LLM builder endpoint."""
65
+
66
+ tracker: Optional[DialogueStateTracker] = Field(None)
67
+ bot_logs: str = Field("")
68
+ chat_bot_files: Dict[str, str] = Field({})
69
+ chat_history: List[ChatMessage] = Field([])
70
+
71
+ class Config:
72
+ """Config for LLMBuilderContext."""
73
+
74
+ arbitrary_types_allowed = True
75
+
76
+
77
+ class BotDataUpdateRequest(BaseModel):
78
+ """Request model for bot data updates."""
79
+
80
+ domain_yml: Optional[str] = Field(None, alias="domain.yml")
81
+ flows_yml: Optional[str] = Field(None, alias="flows.yml")
82
+ config_yml: Optional[str] = Field(None, alias="config.yml")
83
+
84
+ class Config:
85
+ """Config for BotDataUpdateRequest."""
86
+
87
+ allow_population_by_field_name = True
88
+
89
+
90
+ class ContentBlock(BaseModel):
91
+ """Base model for content blocks."""
92
+
93
+ type: str = Field(...)
94
+
95
+
96
+ class TextBlock(ContentBlock):
97
+ """Text content block."""
98
+
99
+ type: Literal["text"] = "text"
100
+ text: str = Field(...)
101
+
102
+
103
+ class CodeBlock(ContentBlock):
104
+ """Code content block."""
105
+
106
+ type: Literal["code"] = "code"
107
+ text: str = Field(...)
108
+ language: Optional[str] = Field(None)
109
+
110
+
111
+ class FileBlock(ContentBlock):
112
+ """File content block."""
113
+
114
+ type: Literal["file"] = "file"
115
+ file: str = Field(...)
116
+ content: str = Field(...)
117
+
118
+
119
+ class LinkBlock(ContentBlock):
120
+ """Link content block."""
121
+
122
+ type: Literal["link"] = "link"
123
+ text: str = Field(..., pattern=r"^https?://")
124
+
125
+
126
+ class LLMHelperResponse(BaseModel):
127
+ """Response model for LLM helper."""
128
+
129
+ content_blocks: List[Union[TextBlock, CodeBlock, FileBlock, LinkBlock]] = Field(...)
130
+
131
+
132
+ class ApiResponse(BaseModel):
133
+ """Standard API response model."""
134
+
135
+ status: str = Field(...)
136
+ message: Optional[str] = Field(None)
137
+ data: Optional[Dict[str, Any]] = Field(None)
138
+
139
+
140
+ class ApiErrorResponse(BaseModel):
141
+ """API error response model."""
142
+
143
+ status: Literal["error"] = "error"
144
+ error: str = Field(...)
145
+ details: Optional[Dict[str, Any]] = Field(None)
146
+
147
+
148
+ class ServerSentEvent(BaseModel):
149
+ """Server-sent event model."""
150
+
151
+ event: str = Field(...)
152
+ data: Dict[str, Any] = Field(...)
153
+
154
+ def format(self) -> str:
155
+ """Format as SSE string."""
156
+ import json
157
+
158
+ return f"event: {self.event}\ndata: {json.dumps(self.data)}\n\n"
159
+
160
+
161
+ class ValidationResult(BaseModel):
162
+ """Result of validation operation."""
163
+
164
+ is_valid: bool = Field(...)
165
+ errors: Optional[List[str]] = Field(None)
166
+ warnings: Optional[List[str]] = Field(None)
167
+
168
+
169
+ class TrainingResult(BaseModel):
170
+ """Result of training operation."""
171
+
172
+ success: bool = Field(...)
173
+ model_path: Optional[str] = Field(None)
174
+ error: Optional[str] = Field(None)
@@ -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
+ )
@@ -0,0 +1,97 @@
1
+ import json
2
+ import os
3
+ from pathlib import Path
4
+ from urllib.parse import urljoin, urlparse
5
+
6
+ import requests
7
+ from bs4 import BeautifulSoup
8
+
9
+ BASE_URL = "https://rasa.com"
10
+ DOCS_ROOT = "https://rasa.com/docs"
11
+ OUTPUT_DIR = "rasa_docs_md"
12
+ MAX_PAGES = 100 # Optional limit for safety
13
+
14
+ visited = set()
15
+ to_visit = [DOCS_ROOT]
16
+
17
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
18
+
19
+
20
+ def is_valid_doc_url(url):
21
+ return url.startswith(DOCS_ROOT) and not any(
22
+ [url.endswith(".pdf"), "#" in url, "mailto:" in url]
23
+ )
24
+
25
+
26
+ def slugify_url(url):
27
+ path = urlparse(url).path.strip("/").replace("/", "_")
28
+ return path if path else "index"
29
+
30
+
31
+ def clean_text(html):
32
+ soup = BeautifulSoup(html, "html.parser")
33
+
34
+ # Remove navs, footers, and code tabs (customize if needed)
35
+ for tag in soup(["nav", "footer", "script", "style", "form", "button"]):
36
+ tag.decompose()
37
+
38
+ main = soup.find("main") or soup.body
39
+ if not main:
40
+ return ""
41
+
42
+ # Replace <code> with backticks
43
+ for code in main.find_all("code"):
44
+ code.string = f"`{code.get_text(strip=True)}`"
45
+
46
+ text = main.get_text(separator="\n", strip=True)
47
+ return text
48
+
49
+
50
+ def save_as_markdown(text) -> str:
51
+ slug = slugify_url(url)
52
+ file_name = f"{slug}.md"
53
+ md_path = Path(OUTPUT_DIR) / file_name
54
+ with open(md_path, "w", encoding="utf-8") as f:
55
+ f.write(text)
56
+
57
+ print(f"✅ Saved: {md_path}")
58
+ return file_name
59
+
60
+
61
+ pages_scraped = 0
62
+ markdown_to_url = {}
63
+
64
+ while to_visit and pages_scraped < MAX_PAGES:
65
+ url = to_visit.pop(0)
66
+ if url in visited:
67
+ continue
68
+
69
+ try:
70
+ print(f"Scraping: {url}")
71
+ response = requests.get(url)
72
+ response.raise_for_status()
73
+
74
+ html = response.text
75
+ text = clean_text(html)
76
+ if len(text) < 200: # skip very short pages
77
+ print("⏭️ Skipped (too short)")
78
+ continue
79
+
80
+ file_name = save_as_markdown(text)
81
+ markdown_to_url[file_name] = url
82
+ pages_scraped += 1
83
+
84
+ soup = BeautifulSoup(html, "html.parser")
85
+ for link_tag in soup.find_all("a", href=True):
86
+ link = urljoin(url, link_tag["href"])
87
+ if is_valid_doc_url(link) and link not in visited:
88
+ to_visit.append(link)
89
+
90
+ visited.add(url)
91
+
92
+ except Exception as e:
93
+ print(f"⚠️ Failed to scrape {url}: {e}")
94
+
95
+
96
+ with open("markdown_to_url.json", "w") as f:
97
+ json.dump(markdown_to_url, f, indent=2)