Skip to content

Commit 55a4750

Browse files
committed
Refactor prompt templates to use a dedicated class and improve readability
1 parent 724e24c commit 55a4750

File tree

5 files changed

+68
-58
lines changed

5 files changed

+68
-58
lines changed

camel_database_agent/database/manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ def session_scope(session_maker: sessionmaker) -> Iterator[Session]:
2828
try:
2929
yield session
3030
session.commit()
31-
except Exception as e:
31+
except Exception:
3232
session.rollback()
33-
logger.error(f"Database error: {e}")
3433
raise
3534
finally:
3635
session.close()

camel_database_agent/database/prompts.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import textwrap
2+
3+
14
class PromptTemplates:
2-
POLISH_SCHEMA_OUTPUT_EXAMPLE = """Please add detailed {{language}} comments to the following DDL script, explaining the business meaning and design intent of each table and field.
5+
POLISH_SCHEMA_OUTPUT_EXAMPLE = textwrap.dedent("""
6+
Please add detailed {{language}} comments to the following DDL script, explaining the business meaning and design intent of each table and field.
37
48
Requirements:
59
- Keep the original DDL script completely unchanged
@@ -32,4 +36,4 @@ class PromptTemplates:
3236
- Provide specific guidance for adding comments
3337
- Specify the expected format and content of comments
3438
- Emphasize professionalism and conciseness
35-
"""
39+
""")

camel_database_agent/database_agent.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@
3535
strip_sql_code_block,
3636
timing,
3737
)
38-
from camel_database_agent.database_prompt import (
39-
DATABASE_SUMMARY_OUTPUT_EXAMPLE,
40-
QUESTION_CONVERT_SQL,
41-
)
38+
from camel_database_agent.database_prompt import PromptTemplates
4239
from camel_database_agent.datagen.pipeline import (
4340
DataQueryInferencePipeline,
4441
)
@@ -314,7 +311,7 @@ def _generate_database_summary(self, query_samples_size: int) -> TokenUsage:
314311
self.database_knowledge_backend.get_query_collection_sample(query_samples_size)
315312
)
316313

317-
prompt = DATABASE_SUMMARY_OUTPUT_EXAMPLE
314+
prompt = PromptTemplates.DATABASE_SUMMARY_OUTPUT_EXAMPLE
318315
prompt = prompt.replace("{{ddl_sql}}", self.ddl_sql)
319316
prompt = prompt.replace("{{language}}", self.language)
320317

@@ -430,7 +427,7 @@ def train_knowledge(
430427
@timing
431428
def question_to_sql(self, question: str, dialect_name: str) -> QuestionMeta:
432429
"""Question to SQL"""
433-
prompt = QUESTION_CONVERT_SQL.replace("{{dialect_name}}", dialect_name)
430+
prompt = PromptTemplates.QUESTION_CONVERT_SQL.replace("{{dialect_name}}", dialect_name)
434431

435432
ddl_records: List[DDLRecord] = self.database_knowledge_backend.query_ddl(question)
436433
prompt = prompt.replace(
Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
1-
DATABASE_SUMMARY_OUTPUT_EXAMPLE = """You are a business database expert. Please generate a {{language}} database summary based on the following table structure, with the aim of helping people understand what information this database can provide from a business perspective.
2-
3-
## Table Schema
4-
{{ddl_sql}}
5-
6-
## Output Example
7-
8-
This database is the core data model of a typical e-commerce system,
9-
including modules for user management, product management, order transactions,
10-
payment processes, and address management.
11-
12-
It achieves a complete business loop through multi-table associations
13-
(such as user-order-product-payment), supporting users throughout
14-
the entire process from registration, browsing products,
15-
placing orders and making payments to receiving goods.
16-
17-
Each table ensures data consistency through foreign key constraints
18-
(such as the strong association between orders and users or addresses)
19-
and includes timestamp fields (`created_at`/`updated_at`) for tracking data changes.
20-
21-
Now, You only need to output a descriptive text in {{language}}.
22-
"""
23-
24-
QUESTION_CONVERT_SQL = """The following is the table structure in the database and some common query SQL statements. Please convert the user's question into an SQL query statement. Note to comply with sqlite syntax. Do not explain, just provide the SQL directly.
25-
26-
Database System: {{dialect_name}}
27-
28-
## Table Schema
29-
```sql
30-
{{table_schema}}
31-
```
32-
33-
## Data Example
34-
```sql
35-
{{sample_data}}
36-
```
37-
## Few-Shot Example
38-
{{qa_pairs}}
39-
40-
## User Question
41-
{{question}}
42-
43-
## Instructions
44-
1. Follow {{dialect_name}} syntax
45-
2. Do not provide explanations, just give the SQL statement directly
46-
"""
1+
import textwrap
2+
3+
4+
class PromptTemplates:
5+
DATABASE_SUMMARY_OUTPUT_EXAMPLE = textwrap.dedent("""
6+
You are a business database expert. Please generate a {{language}} database summary based on the following table structure, with the aim of helping people understand what information this database can provide from a business perspective.
7+
8+
## Table Schema
9+
{{ddl_sql}}
10+
11+
## Output Example
12+
13+
This database is the core data model of a typical e-commerce system,
14+
including modules for user management, product management, order transactions,
15+
payment processes, and address management.
16+
17+
It achieves a complete business loop through multi-table associations
18+
(such as user-order-product-payment), supporting users throughout
19+
the entire process from registration, browsing products,
20+
placing orders and making payments to receiving goods.
21+
22+
Each table ensures data consistency through foreign key constraints
23+
(such as the strong association between orders and users or addresses)
24+
and includes timestamp fields (`created_at`/`updated_at`) for tracking data changes.
25+
26+
Now, You only need to output a descriptive text in {{language}}.
27+
""")
28+
29+
QUESTION_CONVERT_SQL = textwrap.dedent("""
30+
The following is the table structure in the database and some common query SQL statements. Please convert the user's question into an SQL query statement. Note to comply with sqlite syntax. Do not explain, just provide the SQL directly.
31+
32+
Database System: {{dialect_name}}
33+
34+
## Table Schema
35+
```sql
36+
{{table_schema}}
37+
```
38+
39+
## Data Example
40+
```sql
41+
{{sample_data}}
42+
```
43+
## Few-Shot Example
44+
{{qa_pairs}}
45+
46+
## User Question
47+
{{question}}
48+
49+
## Instructions
50+
1. Follow {{dialect_name}} syntax
51+
2. Do not provide explanations, just give the SQL statement directly
52+
""")

camel_database_agent/datagen/prompts.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import textwrap
2+
3+
14
class PromptTemplates:
2-
QUESTION_INFERENCE_PIPELINE = """Please carefully analyze the following database information and conduct an in-depth analysis from a business perspective. What business query questions might users raise? Please fully consider some complex query scenarios, including but not limited to multi-table associations, grouping statistics, etc.
5+
QUESTION_INFERENCE_PIPELINE = textwrap.dedent("""
6+
Please carefully analyze the following database information and conduct an in-depth analysis from a business perspective. What business query questions might users raise? Please fully consider some complex query scenarios, including but not limited to multi-table associations, grouping statistics, etc.
37
48
Database Schema:
59
```
@@ -11,4 +15,4 @@ class PromptTemplates:
1115
{{data_sql}}
1216
```
1317
14-
Now, Please generate {{query_samples_size}} real user query questions along with the corresponding SQL query statements without using placeholders. Please output in JSON format."""
18+
Now, Please generate {{query_samples_size}} real user query questions along with the corresponding SQL query statements without using placeholders. Please output in JSON format.""")

0 commit comments

Comments
 (0)