teoo33 commited on
Commit
f35f433
·
verified ·
1 Parent(s): 9f7cbcd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -52
app.py CHANGED
@@ -7,7 +7,7 @@ import os
7
  # تنظیم API کلاینت با متغیر محیطی
8
  api_key = os.getenv("OPENAI_API_KEY")
9
  if not api_key:
10
- raise ValueError("OPENAI_API_KEY در متغیرهای محیطی تنظیم نشده است. لطفاً آن را تنظیم کنید.")
11
  client = OpenAI(api_key=api_key)
12
 
13
  # متغیرهای سراسری
@@ -19,7 +19,7 @@ faq_output = ""
19
  business_info = None
20
  product_info = None
21
 
22
- # پرامپت مادر
23
  mother_prompt = """
24
  You are the Nova System, an innovative problem-solving approach implemented by a dynamic consortium of virtual experts, each serving a distinct role. Your goal is to assist the user in generating high-quality prompts, a comprehensive knowledge base, and an automatically generated Frequently Asked Questions (FAQ) section for chatbots.
25
 
@@ -27,12 +27,16 @@ You are the Nova System, an innovative problem-solving approach implemented by a
27
  The Nova System process is iterative and cyclical, involving the following key stages:
28
  1. **Receiving and Processing User Information Forms:** Process the information from the Business Information Form and Product/Service Information Form provided by the user.
29
  2. **Assigning Expert Roles:**
30
- - **DCE:** Manage and guide the process.
31
- - **PEE:** Generate initial drafts of chatbot prompt, knowledge base, and FAQ.
32
- - **CAE:** Review and critique outputs, providing improvement suggestions.
33
- 3. **Iterations and Expert Dialogue:** Conduct iterations with DCE instructions, PEE generation, CAE critique, and DCE summary, all in Persian.
 
 
 
 
34
  4. **Iterate the Process:** Continue until high-quality outputs are achieved.
35
- 5. **Present the Final Outputs:** Final prompt in English, knowledge base and FAQ in JSON-like format.
36
 
37
  Conduct all dialogues in Persian, but output the final prompt in English and knowledge base/FAQ in JSON-like format.
38
  """
@@ -58,43 +62,55 @@ def process_excel_files(file1, file2):
58
  def start_process(file1, file2):
59
  global iteration_count, iteration_history, business_info, product_info
60
  iteration_count = 1
61
- iteration_history = "سلام! من سیستم نوا هستم. فرایند شروع شد.\n"
62
 
63
  # پردازش فایل‌ها
64
  business_info, product_info = process_excel_files(file1, file2)
65
-
66
- # تبدیل داده‌ها به فرمت قابل سریال‌سازی
67
  business_info_serializable = convert_to_serializable(business_info)
68
  product_info_serializable = convert_to_serializable(product_info)
69
 
70
- # دستورات اولیه DCE
71
  dce_instructions = f"iteration {iteration_count}: لطفاً بر اساس اطلاعات فرم‌ها، پرامپت اولیه، پایگاه دانش و FAQ رو تولید کنید."
72
  iteration_history += f"**دستورات DCE:** {dce_instructions}\n"
73
 
74
- # تولید اولیه توسط PEE
 
 
 
 
 
 
 
75
  pee_response = client.chat.completions.create(
76
  model="gpt-4o",
77
- messages=[
78
- {"role": "system", "content": mother_prompt},
79
- {"role": "user", "content": f"اطلاعات کسب‌وکار: {json.dumps(business_info_serializable, ensure_ascii=False)}\nاطلاعات محصولات: {json.dumps(product_info_serializable, ensure_ascii=False)}\n{dce_instructions}"}
80
- ]
81
  )
82
  pee_output = pee_response.choices[0].message.content
83
- iteration_history += f"**خروجی PEE:** {pee_output}\n"
84
-
85
- # نقد CAE
 
 
 
 
 
86
  cae_response = client.chat.completions.create(
87
  model="gpt-4o",
88
- messages=[
89
- {"role": "system", "content": mother_prompt},
90
- {"role": "user", "content": f"لطفاً خروجی PEE رو نقد کنید:\n{pee_output}"}
91
- ]
92
  )
93
  cae_output = cae_response.choices[0].message.content
94
- iteration_history += f"**نقد CAE:** {cae_output}\n"
95
-
96
- # جمع‌بندی DCE
97
- iteration_history += f"**جمع‌بندی DCE:** iteration {iteration_count} با موفقیت انجام شد. هدف بعدی: بهبود بر اساس نقد CAE.\n**پایان iteration {iteration_count}**\n"
 
 
 
 
 
 
 
 
98
 
99
  return iteration_history, "", "", ""
100
 
@@ -103,38 +119,51 @@ def continue_iteration():
103
  global iteration_count, iteration_history
104
  iteration_count += 1
105
 
106
- # تبدیل داده‌ها به فرمت قابل سریال‌سازی
107
  business_info_serializable = convert_to_serializable(business_info)
108
  product_info_serializable = convert_to_serializable(product_info)
109
 
110
- # دستورات DCE
111
- dce_instructions = f"iteration {iteration_count}: لطفاً خروجی قبلی رو بر اساس نقد CAE بهبود بدید."
112
  iteration_history += f"**دستورات DCE:** {dce_instructions}\n"
113
 
114
- # تولید مجدد توسط PEE
 
 
 
 
 
 
 
 
115
  pee_response = client.chat.completions.create(
116
  model="gpt-4o",
117
- messages=[
118
- {"role": "system", "content": mother_prompt},
119
- {"role": "user", "content": f"اطلاعات کسب‌وکار: {json.dumps(business_info_serializable, ensure_ascii=False)}\nاطلاعات محصولات: {json.dumps(product_info_serializable, ensure_ascii=False)}\nتاریخچه iteration قبلی:\n{iteration_history}\n{dce_instructions}"}
120
- ]
121
  )
122
  pee_output = pee_response.choices[0].message.content
123
- iteration_history += f"**خروجی PEE:** {pee_output}\n"
124
-
125
- # نقد CAE
 
 
 
 
 
126
  cae_response = client.chat.completions.create(
127
  model="gpt-4o",
128
- messages=[
129
- {"role": "system", "content": mother_prompt},
130
- {"role": "user", "content": f"لطفاً خروجی PEE رو نقد کنید:\n{pee_output}"}
131
- ]
132
  )
133
  cae_output = cae_response.choices[0].message.content
134
- iteration_history += f"**نقد CAE:** {cae_output}\n"
135
-
136
- # جمع‌بندی DCE
137
- iteration_history += f"**جمع‌بندی DCE:** iteration {iteration_count} انجام شد. هدف بعدی: ادامه بهبود یا اتمام.\n**پایان iteration {iteration_count}**\n"
 
 
 
 
 
 
 
138
 
139
  return iteration_history, "", "", ""
140
 
@@ -142,17 +171,24 @@ def continue_iteration():
142
  def end_process():
143
  global iteration_history, prompt_output, knowledge_base_output, faq_output
144
 
145
- # تبدیل داده‌ها به فرمت قابل سریال‌سازی
146
  business_info_serializable = convert_to_serializable(business_info)
147
  product_info_serializable = convert_to_serializable(product_info)
148
 
149
  # تولید خروجی نهایی
 
 
 
 
 
 
 
 
 
 
 
150
  final_response = client.chat.completions.create(
151
  model="gpt-4o",
152
- messages=[
153
- {"role": "system", "content": mother_prompt},
154
- {"role": "user", "content": f"لطفاً بر اساس تاریخچه iteration‌ها، پرامپت نهایی (به انگلیسی)، پایگاه دانش و FAQ (به فرمت JSON) رو تولید کنید:\n{iteration_history}\nاطلاعات کسب‌وکار: {json.dumps(business_info_serializable, ensure_ascii=False)}\nاطلاعات محصولات: {json.dumps(product_info_serializable, ensure_ascii=False)}"}
155
- ]
156
  )
157
  final_output = final_response.choices[0].message.content
158
 
@@ -162,7 +198,7 @@ def end_process():
162
  knowledge_base_output = parts[1].strip() if len(parts) > 1 else "پایگاه دانش تولید نشد"
163
  faq_output = parts[2].strip() if len(parts) > 2 else "FAQ تولید نشد"
164
 
165
- iteration_history += "\n**فرایند پایان یافت.**\n"
166
  return iteration_history, prompt_output, knowledge_base_output, faq_output
167
 
168
  # رابط کاربری Gradio
 
7
  # تنظیم API کلاینت با متغیر محیطی
8
  api_key = os.getenv("OPENAI_API_KEY")
9
  if not api_key:
10
+ raise ValueError("OPENAI_API_KEY در متغیرهای محیطی تنظیم نشده است.")
11
  client = OpenAI(api_key=api_key)
12
 
13
  # متغیرهای سراسری
 
19
  business_info = None
20
  product_info = None
21
 
22
+ # پرامپت مادر به‌عنوان دستورالعمل اصلی
23
  mother_prompt = """
24
  You are the Nova System, an innovative problem-solving approach implemented by a dynamic consortium of virtual experts, each serving a distinct role. Your goal is to assist the user in generating high-quality prompts, a comprehensive knowledge base, and an automatically generated Frequently Asked Questions (FAQ) section for chatbots.
25
 
 
27
  The Nova System process is iterative and cyclical, involving the following key stages:
28
  1. **Receiving and Processing User Information Forms:** Process the information from the Business Information Form and Product/Service Information Form provided by the user.
29
  2. **Assigning Expert Roles:**
30
+ - **Discussion Continuity Expert (DCE):** Manage and guide the process, provide instructions, summarize progress, and define goals for each iteration.
31
+ - **Prompt Engineering Expert (PEE):** Generate initial drafts of chatbot prompt, knowledge base, and FAQ based on user information.
32
+ - **Critical Analysis Expert (CAE):** Review and critique outputs, providing improvement suggestions.
33
+ 3. **Iterations and Expert Dialogue:** Conduct iterations with the following steps in Persian (Farsi):
34
+ - **DCE's Instructions:** Provide instructions for PEE and CAE.
35
+ - **PEE Output:** Generate or refine chatbot prompt, knowledge base, and FAQ.
36
+ - **CAE Analysis:** Critique PEE outputs and suggest improvements.
37
+ - **DCE Summary:** Summarize progress and set goals for the next iteration.
38
  4. **Iterate the Process:** Continue until high-quality outputs are achieved.
39
+ 5. **Present the Final Outputs:** Final prompt in English with sections (Persona, Tone, Guidelines, etc.), knowledge base and FAQ in JSON-like format.
40
 
41
  Conduct all dialogues in Persian, but output the final prompt in English and knowledge base/FAQ in JSON-like format.
42
  """
 
62
  def start_process(file1, file2):
63
  global iteration_count, iteration_history, business_info, product_info
64
  iteration_count = 1
65
+ iteration_history = "سلام! من سیستم نوا هستم، یک سیستم پیشرفته برای تولید پرامپت‌های چت‌بات، پایگاه دانش و FAQ خودکار. فرایند شروع شد.\n"
66
 
67
  # پردازش فایل‌ها
68
  business_info, product_info = process_excel_files(file1, file2)
 
 
69
  business_info_serializable = convert_to_serializable(business_info)
70
  product_info_serializable = convert_to_serializable(product_info)
71
 
72
+ # مرحله ۱: دستورات DCE
73
  dce_instructions = f"iteration {iteration_count}: لطفاً بر اساس اطلاعات فرم‌ها، پرامپت اولیه، پایگاه دانش و FAQ رو تولید کنید."
74
  iteration_history += f"**دستورات DCE:** {dce_instructions}\n"
75
 
76
+ # مرحله ۲: تولید توسط PEE
77
+ pee_prompt = f"""
78
+ {mother_prompt}
79
+ شما Prompt Engineering Expert (PEE) هستید. بر اساس اطلاعات زیر، پرامپت اولیه چت‌بات، پایگاه دانش و FAQ رو به فارسی تولید کنید:
80
+ اطلاعات کسب‌وکار: {json.dumps(business_info_serializable, ensure_ascii=False)}
81
+ اطلاعات محصولات: {json.dumps(product_info_serializable, ensure_ascii=False)}
82
+ {dce_instructions}
83
+ """
84
  pee_response = client.chat.completions.create(
85
  model="gpt-4o",
86
+ messages=[{"role": "system", "content": pee_prompt}]
 
 
 
87
  )
88
  pee_output = pee_response.choices[0].message.content
89
+ iteration_history += f"**خروجی PEE:**\n{pee_output}\n"
90
+
91
+ # مرحله ۳: نقد توسط CAE
92
+ cae_prompt = f"""
93
+ {mother_prompt}
94
+ شما Critical Analysis Expert (CAE) هستید. لطفاً خروجی PEE رو نقد کنید و پیشنهادات بهبود بدید:
95
+ خروجی PEE:\n{pee_output}
96
+ """
97
  cae_response = client.chat.completions.create(
98
  model="gpt-4o",
99
+ messages=[{"role": "system", "content": cae_prompt}]
 
 
 
100
  )
101
  cae_output = cae_response.choices[0].message.content
102
+ iteration_history += f"**نقد CAE:**\n{cae_output}\n"
103
+
104
+ # مرحله ۴: جمع‌بندی DCE
105
+ dce_summary = f"""
106
+ **جمع‌بندی DCE:** iteration {iteration_count} با موفقیت انجام شد.
107
+ **وضعیت فعلی:** پرامپت اولیه، پایگاه دانش و FAQ تولید شدند و نقد شدند.
108
+ **اهداف iteration بعدی:**
109
+ #G-{iteration_count}-1: بهبود پرامپت بر اساس نقد CAE.
110
+ #G-{iteration_count}-2: تکمیل پایگاه دانش و FAQ.
111
+ **پایان iteration {iteration_count}**
112
+ """
113
+ iteration_history += dce_summary
114
 
115
  return iteration_history, "", "", ""
116
 
 
119
  global iteration_count, iteration_history
120
  iteration_count += 1
121
 
 
122
  business_info_serializable = convert_to_serializable(business_info)
123
  product_info_serializable = convert_to_serializable(product_info)
124
 
125
+ # مرحله ۱: دستورات DCE
126
+ dce_instructions = f"iteration {iteration_count}: لطفاً خروجی قبلی رو بر اساس نقد CAE بهبود بدید و پرامپت، پایگاه دانش و FAQ رو بهینه کنید."
127
  iteration_history += f"**دستورات DCE:** {dce_instructions}\n"
128
 
129
+ # مرحله ۲: تولید توسط PEE
130
+ pee_prompt = f"""
131
+ {mother_prompt}
132
+ شما Prompt Engineering Expert (PEE) هستید. بر اساس تاریخچه و نقد قبلی، پرامپت، پایگاه دانش و FAQ رو بهبود بدید:
133
+ اطلاعات کسب‌وکار: {json.dumps(business_info_serializable, ensure_ascii=False)}
134
+ اطلاعات محصولات: {json.dumps(product_info_serializable, ensure_ascii=False)}
135
+ تاریخچه iteration قبلی:\n{iteration_history}
136
+ {dce_instructions}
137
+ """
138
  pee_response = client.chat.completions.create(
139
  model="gpt-4o",
140
+ messages=[{"role": "system", "content": pee_prompt}]
 
 
 
141
  )
142
  pee_output = pee_response.choices[0].message.content
143
+ iteration_history += f"**خروجی PEE:**\n{pee_output}\n"
144
+
145
+ # مرحله ۳: نقد توسط CAE
146
+ cae_prompt = f"""
147
+ {mother_prompt}
148
+ شما Critical Analysis Expert (CAE) هستید. لطفاً خروجی جدید PEE رو نقد کنید و پیشنهادات بهبود بدید:
149
+ خروجی PEE:\n{pee_output}
150
+ """
151
  cae_response = client.chat.completions.create(
152
  model="gpt-4o",
153
+ messages=[{"role": "system", "content": cae_prompt}]
 
 
 
154
  )
155
  cae_output = cae_response.choices[0].message.content
156
+ iteration_history += f"**نقد CAE:**\n{cae_output}\n"
157
+
158
+ # مرحله ۴: جمع‌بندی DCE
159
+ dce_summary = f"""
160
+ **جمع‌بندی DCE:** iteration {iteration_count} انجام شد.
161
+ **وضعیت فعلی:** خروجی‌ها بهبود یافتند.
162
+ **اهداف iteration بعدی:**
163
+ #G-{iteration_count}-1: ادامه بهبود یا اتمام فرایند.
164
+ **پایان iteration {iteration_count}**
165
+ """
166
+ iteration_history += dce_summary
167
 
168
  return iteration_history, "", "", ""
169
 
 
171
  def end_process():
172
  global iteration_history, prompt_output, knowledge_base_output, faq_output
173
 
 
174
  business_info_serializable = convert_to_serializable(business_info)
175
  product_info_serializable = convert_to_serializable(product_info)
176
 
177
  # تولید خروجی نهایی
178
+ final_prompt = f"""
179
+ {mother_prompt}
180
+ فرایند iteration‌ها تمام شده. لطفاً خروجی نهایی رو تولید کنید:
181
+ - پرامپت چت‌بات به انگلیسی با بخش‌های: Persona, Tone, Guidelines, About Us, Responses to Common Questions, Contact Information, Additional Guidelines
182
+ - پایگاه دانش به فرمت JSON-like
183
+ - FAQ به فرمت JSON-like
184
+ اطلاعات کسب‌وکار: {json.dumps(business_info_serializable, ensure_ascii=False)}
185
+ اطلاعات محصولات: {json.dumps(product_info_serializable, ensure_ascii=False)}
186
+ تاریخچه iteration‌ها:\n{iteration_history}
187
+ لطفاً خروجی‌ها رو با --- از هم جدا کنید.
188
+ """
189
  final_response = client.chat.completions.create(
190
  model="gpt-4o",
191
+ messages=[{"role": "system", "content": final_prompt}]
 
 
 
192
  )
193
  final_output = final_response.choices[0].message.content
194
 
 
198
  knowledge_base_output = parts[1].strip() if len(parts) > 1 else "پایگاه دانش تولید نشد"
199
  faq_output = parts[2].strip() if len(parts) > 2 else "FAQ تولید نشد"
200
 
201
+ iteration_history += "\n**فرایند پایان یافت و خروجی نهایی تولید شد.**\n"
202
  return iteration_history, prompt_output, knowledge_base_output, faq_output
203
 
204
  # رابط کاربری Gradio