walker11 commited on
Commit
48215a8
·
verified ·
1 Parent(s): ffb47e3

Upload 2 files

Browse files
Files changed (2) hide show
  1. ai_service.py +1 -1
  2. prompts.py +251 -251
ai_service.py CHANGED
@@ -61,7 +61,7 @@ async def generate_response(messages: List[Dict], retries=3, backoff_factor=1.5)
61
  "model": "deepseek-chat", # استبدل باسم النموذج المناسب من DeepSeek API
62
  "messages": messages,
63
  "temperature": 0.7,
64
- "max_tokens": 2000
65
  }
66
 
67
  last_exception = None
 
61
  "model": "deepseek-chat", # استبدل باسم النموذج المناسب من DeepSeek API
62
  "messages": messages,
63
  "temperature": 0.7,
64
+ "max_tokens": 1000
65
  }
66
 
67
  last_exception = None
prompts.py CHANGED
@@ -1,252 +1,252 @@
1
- from typing import List, Dict, Any
2
- from models import StoryLength, StoryType, Character, StoryConfig
3
-
4
-
5
- def get_system_prompt() -> str:
6
- """
7
- الحصول على برومبت النظام الأساسي الذي يحدد سلوك نموذج الذكاء الاصطناعي
8
- """
9
- return """
10
- You are a professional and creative Arabic story writer. Your task is to write original, engaging, and cohesive Arabic stories.
11
-
12
- Adhere to the following standards in all the stories you write:
13
-
14
- 1. Use correct and understandable classical Arabic language, free from grammatical and spelling errors.
15
- 2. Build a coherent and logical story that follows good dramatic structure principles (beginning, rising action, climax, resolution).
16
- 3. Adhere to Arabic and Islamic values and ethics in the story content.
17
- 4. Avoid inappropriate content or anything that violates public taste or religious values.
18
- 5. Provide detailed sensory descriptions of characters, places, and events to make the story vivid and engaging.
19
- 6. Make dialogue realistic and natural, appropriate to the story's characters and environment.
20
- 7. Maintain consistency in character traits and behaviors throughout the story.
21
- 8. Include positive values and useful lessons in an indirect way.
22
- 9. Use diverse narrative techniques: description, dialogue, narration, internal monologue.
23
- 10. Create a clear conflict that drives the story events and maintains reader interest.
24
-
25
- Each time you are asked to write a new paragraph of the story, you must:
26
- - Write a coherent and engaging narrative paragraph of 6-8 lines in Arabic.
27
- - Provide 3 distinctive and interesting options to develop the story's path.
28
-
29
- Important rules for options:
30
- 1. Make options very practical and short (3-5 words only) in Arabic.
31
- 2. ALWAYS start each option with the character's name followed by the action verb.
32
- 3. Use clear format: "[Character name] + verb", for example: "أحمد يتصل بالشرطة" (Ahmed calls the police), "سارة تهرب من المكان" (Sarah escapes from the place).
33
- 4. Make it absolutely clear WHO is performing the action in each option.
34
- 5. Don't explain what will happen after the choice, just mention the direct action.
35
- 6. Ensure each option will lead to a completely different path in the story.
36
- 7. Make options logical and appropriate to the current situation in the story.
37
-
38
- Result of user choice:
39
- 1. Do not summarize the user's chosen option at the beginning of the next paragraph.
40
- 2. Start directly with the reactions and consequences resulting from the user's choice.
41
- 3. Present surprising and unexpected developments resulting from the choice.
42
- 4. Maintain story consistency despite the change in path.
43
-
44
- When the story is complete, choose an engaging and deep title that reflects the essence and content of the story.
45
- """
46
-
47
-
48
- def format_characters_info(characters: List[Character]) -> str:
49
- """
50
- تنسيق معلومات الشخصيات لتضمينها في البرومبت
51
- """
52
- if not characters:
53
- return "لا توجد شخصيات محددة، يمكنك إنشاء شخصيات مناسبة للقصة."
54
-
55
- characters_info = "معلومات الشخصيات:\n"
56
- for i, character in enumerate(characters, 1):
57
- gender_text = "ذكر" if character.gender.value == "ذكر" else "أنثى"
58
- characters_info += f"{i}. الشخصية: {character.name}، الجنس: {gender_text}، الوصف: {character.description}\n"
59
-
60
- return characters_info
61
-
62
-
63
- def get_story_length_instructions(length: StoryLength) -> Dict[str, Any]:
64
- """
65
- الحصول على تعليمات طول القصة وعدد الفقرات
66
- """
67
- length_mapping = {
68
- StoryLength.SHORT: {"paragraphs": 5, "description": "قصة قصيرة تتكون من 5 فقرات"},
69
- StoryLength.MEDIUM: {"paragraphs": 7, "description": "قصة متوسطة الطول تتكون من 7 فقرات"},
70
- StoryLength.LONG: {"paragraphs": 9, "description": "قصة طويلة تتكون من 9 فقرات"}
71
- }
72
-
73
- return length_mapping.get(length, length_mapping[StoryLength.MEDIUM])
74
-
75
-
76
- def get_story_type_description(primary_type: StoryType, secondary_type: StoryType) -> str:
77
- """
78
- الحصول على وصف نوع القصة
79
- """
80
- if secondary_type == StoryType.NONE:
81
- return f"قصة من نوع {primary_type.value}"
82
- else:
83
- return f"قصة تجمع بين نوعي {primary_type.value} و{secondary_type.value}"
84
-
85
-
86
- def create_story_init_prompt(config: StoryConfig) -> str:
87
- """
88
- إنشاء البرومبت الأولي لبدء القصة
89
- """
90
- length_info = get_story_length_instructions(config.length)
91
- story_type = get_story_type_description(config.primary_type, config.secondary_type)
92
- characters_info = format_characters_info(config.characters)
93
-
94
- prompt = f"""
95
- Please write {length_info['description']} of {story_type}.
96
-
97
- {characters_info}
98
-
99
- Required from you:
100
- 1. Write the first paragraph of the story (6-8 lines) in Arabic.
101
- 2. Start the story with a strong and engaging beginning that captivates the reader from the first line.
102
- 3. Present the characters and setting (place and time) clearly and interestingly.
103
- 4. Establish a conflict, problem, or situation that drives the story events.
104
- 5. Present 3 short, exciting, and logical options for actions the protagonist can take.
105
- 6. Make the options very short (3-5 words only) and practical and direct.
106
- 7. ALWAYS include the character's name in each option before the action verb.
107
- 8. Format: "[Character name] + verb", like: "أحمد يتصل بالشرطة", "سارة تهرب من المكان".
108
-
109
- Present the first paragraph and options in the following format:
110
-
111
- الفقرة:
112
- [Write the first paragraph of the story here in Arabic]
113
-
114
- الخيارات:
115
- 1. [Character name + action verb in Arabic, 3-5 words total]
116
- 2. [Character name + different action verb in Arabic, 3-5 words total]
117
- 3. [Character name + another different action verb in Arabic, 3-5 words total]
118
- """
119
-
120
- return prompt
121
-
122
-
123
- def create_continuation_prompt(story_context: str, choice_id: int, choice_text: str, current_paragraph: int, max_paragraphs: int) -> str:
124
- """
125
- إنشاء برومبت لاستكمال القصة بناءً على اختيار المستخدم
126
- """
127
- is_final = current_paragraph >= max_paragraphs - 1
128
-
129
- prompt = f"""
130
- Story context so far:
131
- {story_context}
132
-
133
- The user chose path number {choice_id}: {choice_text}
134
-
135
- Required from you:
136
- 1. Continue writing the story with a new paragraph (6-8 lines) in Arabic that directly follows the choice made by the user.
137
- 2. Do not summarize the choice that the user made; instead, start directly with the events that result from this choice.
138
- 3. Add unexpected and exciting developments to engage the reader.
139
- 4. Maintain consistency in the story's characters and world.
140
- """
141
-
142
- if is_final:
143
- prompt += """
144
- 5. This is the final paragraph of the story, so end the story in a logical and satisfying way that closes all open paths.
145
- 6. Suggest an appropriate and deep title for the complete story.
146
-
147
- Present the final paragraph and title in the following format:
148
-
149
- الفقرة:
150
- [Write the final paragraph of the story here in Arabic]
151
-
152
- العنوان:
153
- [Write the suggested title for the story here in Arabic]
154
- """
155
- else:
156
- prompt += """
157
- 5. Present 3 short, logical, and practical options for continuing the story.
158
- 6. Make the options very short (3-5 words only) in Arabic.
159
- 7. ALWAYS include the character's name in each option before the action verb.
160
- 8. Format: "[Character name] + verb", like: "أحمد يتصل بالشرطة", "سارة تهرب من المكان".
161
- 9. Make it absolutely clear WHO is performing the action in each option.
162
- 10. Ensure each option will lead to a completely different path in the story.
163
-
164
- Present the next paragraph and options in the following format:
165
-
166
- الفقرة:
167
- [Write the next paragraph of the story here in Arabic]
168
-
169
- الخيارات:
170
- 1. [Character name + action verb in Arabic, 3-5 words total]
171
- 2. [Character name + different action verb in Arabic, 3-5 words total]
172
- 3. [Character name + another different action verb in Arabic, 3-5 words total]
173
- """
174
-
175
- return prompt
176
-
177
-
178
- def create_title_prompt(complete_story: str) -> str:
179
- """
180
- Create a prompt to generate an appropriate title for the completed story
181
- """
182
- return f"""
183
- Here is a complete story:
184
-
185
- {complete_story}
186
-
187
- Suggest an appropriate and engaging title for this story that reflects its essence and content.
188
- Provide only the title without any additional explanation and without any story Characters names in Arabic and without any asterisk
189
- """
190
-
191
-
192
- def create_complete_story_prompt(config):
193
- """
194
- Create prompt to generate a complete story without interaction
195
-
196
- Args:
197
- config: Story configuration
198
-
199
- Returns:
200
- str: Prompt used to generate the complete story
201
- """
202
- # Get story length instructions
203
- length_instructions = get_story_length_instructions(config.length)
204
- paragraph_count = length_instructions["paragraphs"]
205
- word_count = length_instructions.get("words", 1500) # Default value if key doesn't exist
206
-
207
- # Create character descriptions
208
- characters_description = ""
209
- for i, character in enumerate(config.characters, 1):
210
- gender_text = "Male" if character.gender.value == "ذكر" else "Female"
211
- characters_description += f"{i}. {character.name}: {gender_text}, {character.description}\n"
212
-
213
- # Determine story types
214
- primary_genre = config.primary_type.value
215
- secondary_genre = config.secondary_type.value if config.secondary_type != StoryType.NONE else "None"
216
-
217
- # Create the prompt
218
- prompt = f"""
219
- I want you to create a complete Arabic story in one go without user interaction.
220
-
221
- Story Information:
222
- - Primary Genre: {primary_genre}
223
- - Secondary Genre: {secondary_genre}
224
- - Required Paragraphs: approximately {paragraph_count} paragraphs
225
- - Average Word Count: approximately {word_count} words
226
-
227
- Characters:
228
- {characters_description}
229
-
230
- Additional Requirements:
231
- 1. Create a complete and coherent story from beginning to end.
232
- 2. Separate paragraphs with an empty line.
233
- 3. VERY IMPORTANT: Each paragraph MUST be 6-8 lines long with rich details and descriptions.
234
- 4. Avoid short paragraphs - ensure each paragraph is substantial (6-8 lines minimum).
235
- 5. Make the story engaging with a developing plot.
236
- 6. Use classical Arabic language with an appropriate balance of colloquial language in dialogues if appropriate.
237
- 7. Consider the characteristics of the requested literary genres.
238
- 8. Provide a clear and satisfying ending to the story.
239
- 9. Do not use asterisk ** markers around any text in the story.
240
- 10. Do not include headings like "Title" or "End" or "Introduction" within the story.
241
- 11. Write the story directly without putting the title at the beginning of the text.
242
- 12. Do not repeat the title at the beginning or end of the story.
243
- 13. Each paragraph should contain detailed descriptions, character development, and events to ensure its length.
244
-
245
- IMPORTANT:
246
- - The entire story MUST be written in Arabic language only.
247
- - Make sure EACH paragraph is substantial (6-8 lines) with rich content and descriptions.
248
-
249
- Now, create the complete story:
250
- """
251
-
252
  return prompt.strip()
 
1
+ from typing import List, Dict, Any
2
+ from models import StoryLength, StoryType, Character, StoryConfig
3
+
4
+
5
+ def get_system_prompt() -> str:
6
+ """
7
+ الحصول على برومبت النظام الأساسي الذي يحدد سلوك نموذج الذكاء الاصطناعي
8
+ """
9
+ return """
10
+ You are a professional and creative Arabic story writer. Your task is to write original, engaging, and cohesive Arabic stories.
11
+
12
+ Adhere to the following standards in all the stories you write:
13
+
14
+ 1. Use correct and understandable classical Arabic language, free from grammatical and spelling errors.
15
+ 2. Build a coherent and logical story that follows good dramatic structure principles (beginning, rising action, climax, resolution).
16
+ 3. Adhere to Arabic and Islamic values and ethics in the story content.
17
+ 4. Avoid inappropriate content or anything that violates public taste or religious values.
18
+ 5. Provide detailed sensory descriptions of characters, places, and events to make the story vivid and engaging.
19
+ 6. Make dialogue realistic and natural, appropriate to the story's characters and environment.
20
+ 7. Maintain consistency in character traits and behaviors throughout the story.
21
+ 8. Include positive values and useful lessons in an indirect way.
22
+ 9. Use diverse narrative techniques: description, dialogue, narration, internal monologue.
23
+ 10. Create a clear conflict that drives the story events and maintains reader interest.
24
+
25
+ Each time you are asked to write a new paragraph of the story, you must:
26
+ - Write a coherent and engaging narrative paragraph of 4-6 lines in Arabic.
27
+ - Provide 3 distinctive and interesting options to develop the story's path.
28
+
29
+ Important rules for options:
30
+ 1. Make options very practical and short (3-5 words only) in Arabic.
31
+ 2. ALWAYS start each option with the character's name followed by the action verb.
32
+ 3. Use clear format: "[Character name] + verb", for example: "أحمد يتصل بالشرطة" (Ahmed calls the police), "سارة تهرب من المكان" (Sarah escapes from the place).
33
+ 4. Make it absolutely clear WHO is performing the action in each option.
34
+ 5. Don't explain what will happen after the choice, just mention the direct action.
35
+ 6. Ensure each option will lead to a completely different path in the story.
36
+ 7. Make options logical and appropriate to the current situation in the story.
37
+
38
+ Result of user choice:
39
+ 1. Do not summarize the user's chosen option at the beginning of the next paragraph.
40
+ 2. Start directly with the reactions and consequences resulting from the user's choice.
41
+ 3. Present surprising and unexpected developments resulting from the choice.
42
+ 4. Maintain story consistency despite the change in path.
43
+
44
+ When the story is complete, choose an engaging and deep title that reflects the essence and content of the story.
45
+ """
46
+
47
+
48
+ def format_characters_info(characters: List[Character]) -> str:
49
+ """
50
+ تنسيق معلومات الشخصيات لتضمينها في البرومبت
51
+ """
52
+ if not characters:
53
+ return "لا توجد شخصيات محددة، يمكنك إنشاء شخصيات مناسبة للقصة."
54
+
55
+ characters_info = "معلومات الشخصيات:\n"
56
+ for i, character in enumerate(characters, 1):
57
+ gender_text = "ذكر" if character.gender.value == "ذكر" else "أنثى"
58
+ characters_info += f"{i}. الشخصية: {character.name}، الجنس: {gender_text}، الوصف: {character.description}\n"
59
+
60
+ return characters_info
61
+
62
+
63
+ def get_story_length_instructions(length: StoryLength) -> Dict[str, Any]:
64
+ """
65
+ الحصول على تعليمات طول القصة وعدد الفقرات
66
+ """
67
+ length_mapping = {
68
+ StoryLength.SHORT: {"paragraphs": 3, "description": "قصة قصيرة تتكون من 3 فقرات"},
69
+ StoryLength.MEDIUM: {"paragraphs": 5, "description": "قصة متوسطة الطول تتكون من 5 فقرات"},
70
+ StoryLength.LONG: {"paragraphs": 7, "description": "قصة طويلة تتكون من 7 فقرات"}
71
+ }
72
+
73
+ return length_mapping.get(length, length_mapping[StoryLength.MEDIUM])
74
+
75
+
76
+ def get_story_type_description(primary_type: StoryType, secondary_type: StoryType) -> str:
77
+ """
78
+ الحصول على وصف نوع القصة
79
+ """
80
+ if secondary_type == StoryType.NONE:
81
+ return f"قصة من نوع {primary_type.value}"
82
+ else:
83
+ return f"قصة تجمع بين نوعي {primary_type.value} و{secondary_type.value}"
84
+
85
+
86
+ def create_story_init_prompt(config: StoryConfig) -> str:
87
+ """
88
+ إنشاء البرومبت الأولي لبدء القصة
89
+ """
90
+ length_info = get_story_length_instructions(config.length)
91
+ story_type = get_story_type_description(config.primary_type, config.secondary_type)
92
+ characters_info = format_characters_info(config.characters)
93
+
94
+ prompt = f"""
95
+ Please write {length_info['description']} of {story_type}.
96
+
97
+ {characters_info}
98
+
99
+ Required from you:
100
+ 1. Write the first paragraph of the story (4-6 lines) in Arabic.
101
+ 2. Start the story with a strong and engaging beginning that captivates the reader from the first line.
102
+ 3. Present the characters and setting (place and time) clearly and interestingly.
103
+ 4. Establish a conflict, problem, or situation that drives the story events.
104
+ 5. Present 3 short, exciting, and logical options for actions the protagonist can take.
105
+ 6. Make the options very short (3-5 words only) and practical and direct.
106
+ 7. ALWAYS include the character's name in each option before the action verb.
107
+ 8. Format: "[Character name] + verb", like: "أحمد يتصل بالشرطة", "سارة تهرب من المكان".
108
+
109
+ Present the first paragraph and options in the following format:
110
+
111
+ الفقرة:
112
+ [Write the first paragraph of the story here in Arabic]
113
+
114
+ الخيارات:
115
+ 1. [Character name + action verb in Arabic, 3-5 words total]
116
+ 2. [Character name + different action verb in Arabic, 3-5 words total]
117
+ 3. [Character name + another different action verb in Arabic, 3-5 words total]
118
+ """
119
+
120
+ return prompt
121
+
122
+
123
+ def create_continuation_prompt(story_context: str, choice_id: int, choice_text: str, current_paragraph: int, max_paragraphs: int) -> str:
124
+ """
125
+ إنشاء برومبت لاستكمال القصة بناءً على اختيار المستخدم
126
+ """
127
+ is_final = current_paragraph >= max_paragraphs - 1
128
+
129
+ prompt = f"""
130
+ Story context so far:
131
+ {story_context}
132
+
133
+ The user chose path number {choice_id}: {choice_text}
134
+
135
+ Required from you:
136
+ 1. Continue writing the story with a new paragraph (4-6 lines) in Arabic that directly follows the choice made by the user.
137
+ 2. Do not summarize the choice that the user made; instead, start directly with the events that result from this choice.
138
+ 3. Add unexpected and exciting developments to engage the reader.
139
+ 4. Maintain consistency in the story's characters and world.
140
+ """
141
+
142
+ if is_final:
143
+ prompt += """
144
+ 5. This is the final paragraph of the story, so end the story in a logical and satisfying way that closes all open paths.
145
+ 6. Suggest an appropriate and deep title for the complete story.
146
+
147
+ Present the final paragraph and title in the following format:
148
+
149
+ الفقرة:
150
+ [Write the final paragraph of the story here in Arabic]
151
+
152
+ العنوان:
153
+ [Write the suggested title for the story here in Arabic]
154
+ """
155
+ else:
156
+ prompt += """
157
+ 5. Present 3 short, logical, and practical options for continuing the story.
158
+ 6. Make the options very short (3-5 words only) in Arabic.
159
+ 7. ALWAYS include the character's name in each option before the action verb.
160
+ 8. Format: "[Character name] + verb", like: "أحمد يتصل بالشرطة", "سارة تهرب من المكان".
161
+ 9. Make it absolutely clear WHO is performing the action in each option.
162
+ 10. Ensure each option will lead to a completely different path in the story.
163
+
164
+ Present the next paragraph and options in the following format:
165
+
166
+ الفقرة:
167
+ [Write the next paragraph of the story here in Arabic]
168
+
169
+ الخيارات:
170
+ 1. [Character name + action verb in Arabic, 3-5 words total]
171
+ 2. [Character name + different action verb in Arabic, 3-5 words total]
172
+ 3. [Character name + another different action verb in Arabic, 3-5 words total]
173
+ """
174
+
175
+ return prompt
176
+
177
+
178
+ def create_title_prompt(complete_story: str) -> str:
179
+ """
180
+ Create a prompt to generate an appropriate title for the completed story
181
+ """
182
+ return f"""
183
+ Here is a complete story:
184
+
185
+ {complete_story}
186
+
187
+ Suggest an appropriate and engaging title for this story that reflects its essence and content.
188
+ Provide only the title without any additional explanation and without any story Characters names in Arabic.
189
+ """
190
+
191
+
192
+ def create_complete_story_prompt(config):
193
+ """
194
+ Create prompt to generate a complete story without interaction
195
+
196
+ Args:
197
+ config: Story configuration
198
+
199
+ Returns:
200
+ str: Prompt used to generate the complete story
201
+ """
202
+ # Get story length instructions
203
+ length_instructions = get_story_length_instructions(config.length)
204
+ paragraph_count = length_instructions["paragraphs"]
205
+ word_count = length_instructions.get("words", 1500) # Default value if key doesn't exist
206
+
207
+ # Create character descriptions
208
+ characters_description = ""
209
+ for i, character in enumerate(config.characters, 1):
210
+ gender_text = "Male" if character.gender.value == "ذكر" else "Female"
211
+ characters_description += f"{i}. {character.name}: {gender_text}, {character.description}\n"
212
+
213
+ # Determine story types
214
+ primary_genre = config.primary_type.value
215
+ secondary_genre = config.secondary_type.value if config.secondary_type != StoryType.NONE else "None"
216
+
217
+ # Create the prompt
218
+ prompt = f"""
219
+ I want you to create a complete Arabic story in one go without user interaction.
220
+
221
+ Story Information:
222
+ - Primary Genre: {primary_genre}
223
+ - Secondary Genre: {secondary_genre}
224
+ - Required Paragraphs: approximately {paragraph_count} paragraphs
225
+ - Average Word Count: approximately {word_count} words
226
+
227
+ Characters:
228
+ {characters_description}
229
+
230
+ Additional Requirements:
231
+ 1. Create a complete and coherent story from beginning to end.
232
+ 2. Separate paragraphs with an empty line.
233
+ 3. VERY IMPORTANT: Each paragraph MUST be 6-8 lines long with rich details and descriptions.
234
+ 4. Avoid short paragraphs - ensure each paragraph is substantial (6-8 lines minimum).
235
+ 5. Make the story engaging with a developing plot.
236
+ 6. Use classical Arabic language with an appropriate balance of colloquial language in dialogues if appropriate.
237
+ 7. Consider the characteristics of the requested literary genres.
238
+ 8. Provide a clear and satisfying ending to the story.
239
+ 9. Do not use asterisk ** markers around any text in the story.
240
+ 10. Do not include headings like "Title" or "End" or "Introduction" within the story.
241
+ 11. Write the story directly without putting the title at the beginning of the text.
242
+ 12. Do not repeat the title at the beginning or end of the story.
243
+ 13. Each paragraph should contain detailed descriptions, character development, and events to ensure its length.
244
+
245
+ IMPORTANT:
246
+ - The entire story MUST be written in Arabic language only.
247
+ - Make sure EACH paragraph is substantial (6-8 lines) with rich content and descriptions.
248
+
249
+ Now, create the complete story:
250
+ """
251
+
252
  return prompt.strip()