Spaces:
Running
Running
File size: 20,014 Bytes
b5246f1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
"""
Prompt templates optimized for technical documentation Q&A.
This module provides specialized prompt templates for different types of
technical queries, with a focus on embedded systems and AI documentation.
"""
from enum import Enum
from typing import Dict, List, Optional
from dataclasses import dataclass
class QueryType(Enum):
"""Types of technical queries."""
DEFINITION = "definition"
IMPLEMENTATION = "implementation"
COMPARISON = "comparison"
TROUBLESHOOTING = "troubleshooting"
SPECIFICATION = "specification"
CODE_EXAMPLE = "code_example"
HARDWARE_CONSTRAINT = "hardware_constraint"
GENERAL = "general"
@dataclass
class PromptTemplate:
"""Represents a prompt template with its components."""
system_prompt: str
context_format: str
query_format: str
answer_guidelines: str
few_shot_examples: Optional[List[str]] = None
class TechnicalPromptTemplates:
"""
Collection of prompt templates optimized for technical documentation.
Features:
- Domain-specific templates for embedded systems and AI
- Structured output formats
- Citation requirements
- Technical accuracy emphasis
"""
@staticmethod
def get_base_system_prompt() -> str:
"""Get the base system prompt for technical documentation."""
return """You are an expert technical documentation assistant specializing in embedded systems,
RISC-V architecture, RTOS, and embedded AI/ML. Your role is to provide accurate, detailed
technical answers based strictly on the provided context.
Key responsibilities:
1. Answer questions using ONLY information from the provided context
2. Include precise citations using [chunk_X] notation for every claim
3. Maintain technical accuracy and use correct terminology
4. Format code snippets and technical specifications properly
5. Clearly state when information is not available in the context
6. Consider hardware constraints and embedded system limitations when relevant
Write naturally and conversationally. Avoid repetitive phrases and numbered lists unless specifically requested. Never make up information. If the context doesn't contain the answer, say so explicitly."""
@staticmethod
def get_definition_template() -> PromptTemplate:
"""Template for definition/explanation queries."""
return PromptTemplate(
system_prompt=TechnicalPromptTemplates.get_base_system_prompt() + """
For definition queries, focus on:
- Clear, concise technical definitions
- Related concepts and terminology
- Technical context and applications
- Any acronym expansions""",
context_format="""Technical Documentation Context:
{context}""",
query_format="""Define or explain: {query}
Provide a comprehensive technical definition with proper citations.""",
answer_guidelines="""Provide a clear, comprehensive answer that directly addresses the question. Include relevant technical details and cite your sources using [chunk_X] notation. Make your response natural and conversational while maintaining technical accuracy.""",
few_shot_examples=[
"""Q: What is RISC-V?
A: RISC-V is an open-source instruction set architecture (ISA) based on established reduced instruction set computing (RISC) principles [chunk_1]. Unlike proprietary ISAs, RISC-V is freely available under open-source licenses, allowing anyone to implement RISC-V processors without licensing fees [chunk_2]. The architecture supports 32-bit, 64-bit, and 128-bit address spaces, with a modular design that includes base integer instruction sets and optional extensions [chunk_3]. RISC-V stands for "RISC-Five" referring to the fifth generation of RISC architecture developed at UC Berkeley.""",
"""Q: What is FreeRTOS?
A: FreeRTOS is a real-time operating system kernel for embedded devices that provides multitasking capabilities for microcontrollers and small microprocessors [chunk_1]. It implements a preemptive scheduler with priority-based task scheduling, ensuring deterministic real-time behavior [chunk_2]. FreeRTOS includes core features like task management, semaphores, queues, and memory management while maintaining a small footprint typically under 10KB [chunk_3]. The "Free" in FreeRTOS refers to both its open-source license and the fact that it's free of charge for commercial use."""
]
)
@staticmethod
def get_implementation_template() -> PromptTemplate:
"""Template for implementation/how-to queries."""
return PromptTemplate(
system_prompt=TechnicalPromptTemplates.get_base_system_prompt() + """
For implementation queries, focus on:
- Step-by-step instructions
- Required components or dependencies
- Code examples with proper formatting
- Hardware/software requirements
- Common pitfalls or considerations""",
context_format="""Implementation Documentation:
{context}""",
query_format="""Implementation question: {query}
Provide detailed implementation guidance with code examples where available.""",
answer_guidelines="""Structure your answer as:
1. Overview of the implementation approach [chunk_X]
2. Prerequisites and requirements [chunk_Y]
3. Step-by-step implementation:
- Step 1: Description [chunk_Z]
- Step 2: Description [chunk_W]
4. Code example (if available):
```language
// Code here
```
5. Important considerations or warnings""",
few_shot_examples=[
"""Q: How do I configure GPIO pins for output in RISC-V?
A: GPIO configuration for output requires setting the pin direction and initial value through memory-mapped registers [chunk_1]. First, identify the GPIO base address for your specific RISC-V implementation (commonly 0x10060000 for SiFive cores) [chunk_2].
Steps:
1. Set pin direction to output by writing to GPIO_OUTPUT_EN register [chunk_3]
2. Configure initial output value using GPIO_OUTPUT_VAL register [chunk_4]
```c
#define GPIO_BASE 0x10060000
#define GPIO_OUTPUT_EN (GPIO_BASE + 0x08)
#define GPIO_OUTPUT_VAL (GPIO_BASE + 0x0C)
// Configure pin 5 as output
volatile uint32_t *gpio_en = (uint32_t*)GPIO_OUTPUT_EN;
volatile uint32_t *gpio_val = (uint32_t*)GPIO_OUTPUT_VAL;
*gpio_en |= (1 << 5); // Enable output on pin 5
*gpio_val |= (1 << 5); // Set pin 5 high
```
Important: Always check your board's documentation for the correct GPIO base address and pin mapping [chunk_5].""",
"""Q: How to implement a basic timer interrupt in RISC-V?
A: Timer interrupts in RISC-V use the machine timer (mtime) and timer compare (mtimecmp) registers for precise timing control [chunk_1]. The implementation requires configuring the timer hardware, setting up the interrupt handler, and enabling machine timer interrupts [chunk_2].
Prerequisites:
- RISC-V processor with timer support
- Access to machine-level CSRs
- Understanding of memory-mapped timer registers [chunk_3]
Implementation steps:
1. Set up timer compare value in mtimecmp register [chunk_4]
2. Enable machine timer interrupt in mie CSR [chunk_5]
3. Configure interrupt handler in mtvec CSR [chunk_6]
```c
#define MTIME_BASE 0x0200bff8
#define MTIMECMP_BASE 0x02004000
void setup_timer_interrupt(uint64_t interval) {
uint64_t *mtime = (uint64_t*)MTIME_BASE;
uint64_t *mtimecmp = (uint64_t*)MTIMECMP_BASE;
// Set next interrupt time
*mtimecmp = *mtime + interval;
// Enable machine timer interrupt
asm volatile ("csrs mie, %0" : : "r"(0x80));
// Enable global interrupts
asm volatile ("csrs mstatus, %0" : : "r"(0x8));
}
```
Critical considerations: Timer registers are 64-bit and must be accessed atomically on 32-bit systems [chunk_7]."""
]
)
@staticmethod
def get_comparison_template() -> PromptTemplate:
"""Template for comparison queries."""
return PromptTemplate(
system_prompt=TechnicalPromptTemplates.get_base_system_prompt() + """
For comparison queries, focus on:
- Clear distinction between compared items
- Technical specifications and differences
- Use cases for each option
- Performance or resource implications
- Recommendations based on context""",
context_format="""Technical Comparison Context:
{context}""",
query_format="""Compare: {query}
Provide a detailed technical comparison with clear distinctions.""",
answer_guidelines="""Structure your answer as:
1. Overview of items being compared [chunk_X]
2. Key differences:
- Feature A: Item1 vs Item2 [chunk_Y]
- Feature B: Item1 vs Item2 [chunk_Z]
3. Technical specifications comparison
4. Use case recommendations
5. Performance/resource considerations"""
)
@staticmethod
def get_specification_template() -> PromptTemplate:
"""Template for specification/parameter queries."""
return PromptTemplate(
system_prompt=TechnicalPromptTemplates.get_base_system_prompt() + """
For specification queries, focus on:
- Exact technical specifications
- Parameter ranges and limits
- Units and measurements
- Compliance with standards
- Version-specific information""",
context_format="""Technical Specifications:
{context}""",
query_format="""Specification query: {query}
Provide precise technical specifications with all relevant parameters.""",
answer_guidelines="""Structure your answer as:
1. Specification overview [chunk_X]
2. Detailed parameters:
- Parameter 1: value (unit) [chunk_Y]
- Parameter 2: value (unit) [chunk_Z]
3. Operating conditions or constraints
4. Compliance/standards information
5. Version or variant notes"""
)
@staticmethod
def get_code_example_template() -> PromptTemplate:
"""Template for code example queries."""
return PromptTemplate(
system_prompt=TechnicalPromptTemplates.get_base_system_prompt() + """
For code example queries, focus on:
- Complete, runnable code examples
- Proper syntax highlighting
- Clear comments and documentation
- Error handling
- Best practices for embedded systems""",
context_format="""Code Examples and Documentation:
{context}""",
query_format="""Code example request: {query}
Provide working code examples with explanations.""",
answer_guidelines="""Structure your answer as:
1. Purpose and overview [chunk_X]
2. Required includes/imports [chunk_Y]
3. Complete code example:
```c
// Or appropriate language
#include <necessary_headers.h>
// Function or code implementation
```
4. Key points explained [chunk_Z]
5. Common variations or modifications"""
)
@staticmethod
def get_hardware_constraint_template() -> PromptTemplate:
"""Template for hardware constraint queries."""
return PromptTemplate(
system_prompt=TechnicalPromptTemplates.get_base_system_prompt() + """
For hardware constraint queries, focus on:
- Memory requirements (RAM, Flash)
- Processing power needs (MIPS, frequency)
- Power consumption
- I/O requirements
- Real-time constraints
- Temperature/environmental limits""",
context_format="""Hardware Specifications and Constraints:
{context}""",
query_format="""Hardware constraint question: {query}
Analyze feasibility and constraints for embedded deployment.""",
answer_guidelines="""Structure your answer as:
1. Hardware requirements summary [chunk_X]
2. Detailed constraints:
- Memory: RAM/Flash requirements [chunk_Y]
- Processing: CPU/frequency needs [chunk_Z]
- Power: Consumption estimates [chunk_W]
3. Feasibility assessment
4. Optimization suggestions
5. Alternative approaches if constraints are exceeded"""
)
@staticmethod
def get_troubleshooting_template() -> PromptTemplate:
"""Template for troubleshooting queries."""
return PromptTemplate(
system_prompt=TechnicalPromptTemplates.get_base_system_prompt() + """
For troubleshooting queries, focus on:
- Common error causes
- Diagnostic steps
- Solution procedures
- Preventive measures
- Debug techniques for embedded systems""",
context_format="""Troubleshooting Documentation:
{context}""",
query_format="""Troubleshooting issue: {query}
Provide diagnostic steps and solutions.""",
answer_guidelines="""Structure your answer as:
1. Problem identification [chunk_X]
2. Common causes:
- Cause 1: Description [chunk_Y]
- Cause 2: Description [chunk_Z]
3. Diagnostic steps:
- Step 1: Check... [chunk_W]
- Step 2: Verify... [chunk_V]
4. Solutions for each cause
5. Prevention recommendations"""
)
@staticmethod
def get_general_template() -> PromptTemplate:
"""Default template for general queries."""
return PromptTemplate(
system_prompt=TechnicalPromptTemplates.get_base_system_prompt(),
context_format="""Technical Documentation:
{context}""",
query_format="""Question: {query}
Provide a comprehensive technical answer based on the documentation.""",
answer_guidelines="""Provide a clear, comprehensive answer that directly addresses the question. Include relevant technical details and cite your sources using [chunk_X] notation. Write naturally and conversationally while maintaining technical accuracy. Acknowledge any limitations in available information."""
)
@staticmethod
def detect_query_type(query: str) -> QueryType:
"""
Detect the type of query based on keywords and patterns.
Args:
query: User's question
Returns:
Detected QueryType
"""
query_lower = query.lower()
# Definition keywords
if any(keyword in query_lower for keyword in [
"what is", "what are", "define", "definition", "meaning of", "explain what"
]):
return QueryType.DEFINITION
# Implementation keywords
if any(keyword in query_lower for keyword in [
"how to", "how do i", "implement", "setup", "configure", "install"
]):
return QueryType.IMPLEMENTATION
# Comparison keywords
if any(keyword in query_lower for keyword in [
"difference between", "compare", "vs", "versus", "better than", "which is"
]):
return QueryType.COMPARISON
# Specification keywords
if any(keyword in query_lower for keyword in [
"specification", "specs", "parameters", "limits", "range", "maximum", "minimum"
]):
return QueryType.SPECIFICATION
# Code example keywords
if any(keyword in query_lower for keyword in [
"example", "code", "snippet", "sample", "demo", "show me"
]):
return QueryType.CODE_EXAMPLE
# Hardware constraint keywords
if any(keyword in query_lower for keyword in [
"memory", "ram", "flash", "mcu", "constraint", "fit on", "run on", "power consumption"
]):
return QueryType.HARDWARE_CONSTRAINT
# Troubleshooting keywords
if any(keyword in query_lower for keyword in [
"error", "problem", "issue", "debug", "troubleshoot", "fix", "solve", "not working"
]):
return QueryType.TROUBLESHOOTING
return QueryType.GENERAL
@staticmethod
def get_template_for_query(query: str) -> PromptTemplate:
"""
Get the appropriate template based on query type.
Args:
query: User's question
Returns:
Appropriate PromptTemplate
"""
query_type = TechnicalPromptTemplates.detect_query_type(query)
template_map = {
QueryType.DEFINITION: TechnicalPromptTemplates.get_definition_template,
QueryType.IMPLEMENTATION: TechnicalPromptTemplates.get_implementation_template,
QueryType.COMPARISON: TechnicalPromptTemplates.get_comparison_template,
QueryType.SPECIFICATION: TechnicalPromptTemplates.get_specification_template,
QueryType.CODE_EXAMPLE: TechnicalPromptTemplates.get_code_example_template,
QueryType.HARDWARE_CONSTRAINT: TechnicalPromptTemplates.get_hardware_constraint_template,
QueryType.TROUBLESHOOTING: TechnicalPromptTemplates.get_troubleshooting_template,
QueryType.GENERAL: TechnicalPromptTemplates.get_general_template
}
return template_map[query_type]()
@staticmethod
def format_prompt_with_template(
query: str,
context: str,
template: Optional[PromptTemplate] = None,
include_few_shot: bool = True
) -> Dict[str, str]:
"""
Format a complete prompt using the appropriate template.
Args:
query: User's question
context: Retrieved context chunks
template: Optional specific template (auto-detected if None)
include_few_shot: Whether to include few-shot examples
Returns:
Dict with 'system' and 'user' prompts
"""
if template is None:
template = TechnicalPromptTemplates.get_template_for_query(query)
# Format the context
formatted_context = template.context_format.format(context=context)
# Format the query
formatted_query = template.query_format.format(query=query)
# Build user prompt with optional few-shot examples
user_prompt_parts = []
# Add few-shot examples if available and requested
if include_few_shot and template.few_shot_examples:
user_prompt_parts.append("Here are some examples of how to answer similar questions:")
user_prompt_parts.append("\n\n".join(template.few_shot_examples))
user_prompt_parts.append("\nNow answer the following question using the same format:")
user_prompt_parts.extend([
formatted_context,
formatted_query,
template.answer_guidelines
])
user_prompt = "\n\n".join(user_prompt_parts)
return {
"system": template.system_prompt,
"user": user_prompt
}
# Example usage and testing
if __name__ == "__main__":
# Test query type detection
test_queries = [
"What is RISC-V?",
"How do I implement a timer interrupt?",
"What's the difference between FreeRTOS and Zephyr?",
"What are the memory specifications for STM32F4?",
"Show me an example of GPIO configuration",
"Can this model run on an MCU with 256KB RAM?",
"Debug error: undefined reference to main"
]
for query in test_queries:
query_type = TechnicalPromptTemplates.detect_query_type(query)
print(f"Query: '{query}' -> Type: {query_type.value}")
# Example prompt formatting
example_context = "RISC-V is an open instruction set architecture..."
example_query = "What is RISC-V?"
formatted = TechnicalPromptTemplates.format_prompt_with_template(
query=example_query,
context=example_context
)
print("\nFormatted prompt example:")
print("System:", formatted["system"][:100], "...")
print("User:", formatted["user"][:200], "...") |