Update app.py
Browse files
app.py
CHANGED
@@ -31,13 +31,10 @@ Respond to the philosophical question as {philosopher} would, using their philos
|
|
31 |
Your response should be scholarly but accessible, around 300 words, and clearly represent {philosopher}'s likely stance on this modern question."""
|
32 |
|
33 |
SYNTHESIS_SYSTEM_PROMPT = """You are a deep thinking AI, you may use extremely long chains of thought to deeply consider the problem and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. You should enclose your thoughts and internal monologue inside <thinking></thinking> tags, and then provide your solution or response to the problem.
|
34 |
-
|
35 |
Given the various philosophical perspectives on the question "{question}", synthesize them into a balanced conclusion.
|
36 |
Consider the strengths and weaknesses of each perspective.
|
37 |
Identify common themes and points of disagreement.
|
38 |
-
Conclude with a nuanced view that acknowledges the complexity of the question.
|
39 |
-
|
40 |
-
IMPORTANT: Place your detailed reasoning process inside <thinking></thinking> tags before providing your final conclusion."""
|
41 |
|
42 |
SOCRATIC_SYSTEM_PROMPT = """You are DeepHermes, a philosophical guide using the Socratic method.
|
43 |
For the philosophical question "{question}", generate 5 probing Socratic questions that would help someone explore this topic more deeply.
|
@@ -53,10 +50,6 @@ def call_deephermes(api_key: str, prompt: str, system_prompt: str, temperature:
|
|
53 |
api_key=api_key
|
54 |
)
|
55 |
|
56 |
-
# For synthesis requests, use a lower temperature for more consistent thinking patterns
|
57 |
-
if "You should enclose your thoughts and internal monologue inside <thinking>" in system_prompt:
|
58 |
-
temperature = 0.5 # Lower temperature for more consistent formatting
|
59 |
-
|
60 |
completion = client.chat.completions.create(
|
61 |
extra_headers={
|
62 |
"HTTP-Referer": "https://philosophical-debate-moderator.app",
|
@@ -67,8 +60,7 @@ def call_deephermes(api_key: str, prompt: str, system_prompt: str, temperature:
|
|
67 |
{"role": "system", "content": system_prompt},
|
68 |
{"role": "user", "content": prompt}
|
69 |
],
|
70 |
-
temperature=temperature
|
71 |
-
max_tokens=4000 # Ensure we get lengthy thinking processes
|
72 |
)
|
73 |
return completion.choices[0].message.content
|
74 |
except Exception as e:
|
@@ -79,32 +71,10 @@ def extract_thinking(response: str) -> Tuple[str, str]:
|
|
79 |
thinking = ""
|
80 |
cleaned_response = response
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
r'<
|
86 |
-
r'<THINKING>(.*?)</THINKING>',
|
87 |
-
r'<thoughts>(.*?)</thoughts>',
|
88 |
-
r'<chain-of-thought>(.*?)</chain-of-thought>',
|
89 |
-
r'<reasoning>(.*?)</reasoning>'
|
90 |
-
]
|
91 |
-
|
92 |
-
for pattern in thinking_patterns:
|
93 |
-
thinking_match = re.search(pattern, response, re.DOTALL | re.IGNORECASE)
|
94 |
-
if thinking_match:
|
95 |
-
thinking = thinking_match.group(1).strip()
|
96 |
-
cleaned_response = re.sub(pattern, '', response, flags=re.DOTALL | re.IGNORECASE).strip()
|
97 |
-
break
|
98 |
-
|
99 |
-
# If no explicit tags, look for other indicators of thinking sections
|
100 |
-
if not thinking:
|
101 |
-
# Look for "Let me think..." or similar phrases that might indicate thinking
|
102 |
-
implicit_thinking_match = re.search(r'(Let me think.*?)(?:\n\n|$)', response, re.DOTALL | re.IGNORECASE)
|
103 |
-
if implicit_thinking_match:
|
104 |
-
thinking = implicit_thinking_match.group(1).strip()
|
105 |
-
# Only remove from cleaned response if we're confident this is thinking
|
106 |
-
if len(thinking.split()) > 20: # Only if substantial thinking
|
107 |
-
cleaned_response = response.replace(thinking, "").strip()
|
108 |
|
109 |
return thinking, cleaned_response
|
110 |
|
@@ -123,22 +93,7 @@ def philosopher_perspective(api_key: str, question: str, philosopher: str) -> st
|
|
123 |
def synthesize_perspectives(api_key: str, question: str, perspectives: str) -> Dict[str, str]:
|
124 |
"""Synthesize the various perspectives into a balanced conclusion."""
|
125 |
system_prompt = SYNTHESIS_SYSTEM_PROMPT.format(question=question)
|
126 |
-
|
127 |
-
# Create a more explicit prompt to encourage showing thinking
|
128 |
-
prompt = f"""Here are various philosophical perspectives on the question '{question}':\n\n{perspectives}\n\n
|
129 |
-
Please synthesize these perspectives into a balanced conclusion.
|
130 |
-
|
131 |
-
IMPORTANT: First, show your detailed reasoning process inside <thinking></thinking> tags.
|
132 |
-
Then provide your final synthesized conclusion.
|
133 |
-
|
134 |
-
Example format:
|
135 |
-
<thinking>
|
136 |
-
[Your detailed analysis and reasoning here...]
|
137 |
-
</thinking>
|
138 |
-
|
139 |
-
[Your final synthesized conclusion here...]
|
140 |
-
"""
|
141 |
-
|
142 |
response = call_deephermes(api_key, prompt, system_prompt)
|
143 |
thinking, conclusion = extract_thinking(response)
|
144 |
return {"thinking": thinking, "conclusion": conclusion}
|
@@ -201,15 +156,7 @@ def app_main():
|
|
201 |
|
202 |
with gr.TabItem("Synthesis & Deep Thinking"):
|
203 |
synthesis_btn = gr.Button("Synthesize Perspectives")
|
204 |
-
with gr.
|
205 |
-
temperature_slider = gr.Slider(
|
206 |
-
minimum=0.1,
|
207 |
-
maximum=1.0,
|
208 |
-
value=0.5,
|
209 |
-
step=0.1,
|
210 |
-
label="Temperature (lower values may produce more consistent thinking formats)"
|
211 |
-
)
|
212 |
-
with gr.Accordion("Deep Thinking Process", open=True):
|
213 |
thinking_output = gr.Markdown(label="AI's Chain of Thought")
|
214 |
conclusion_output = gr.Markdown(label="Synthesized Conclusion")
|
215 |
|
@@ -237,7 +184,7 @@ def app_main():
|
|
237 |
|
238 |
synthesis_btn.click(
|
239 |
fn=run_synthesis,
|
240 |
-
inputs=[api_key, question, num_perspectives
|
241 |
outputs=[thinking_output, conclusion_output]
|
242 |
)
|
243 |
|
|
|
31 |
Your response should be scholarly but accessible, around 300 words, and clearly represent {philosopher}'s likely stance on this modern question."""
|
32 |
|
33 |
SYNTHESIS_SYSTEM_PROMPT = """You are a deep thinking AI, you may use extremely long chains of thought to deeply consider the problem and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. You should enclose your thoughts and internal monologue inside <thinking></thinking> tags, and then provide your solution or response to the problem.
|
|
|
34 |
Given the various philosophical perspectives on the question "{question}", synthesize them into a balanced conclusion.
|
35 |
Consider the strengths and weaknesses of each perspective.
|
36 |
Identify common themes and points of disagreement.
|
37 |
+
Conclude with a nuanced view that acknowledges the complexity of the question."""
|
|
|
|
|
38 |
|
39 |
SOCRATIC_SYSTEM_PROMPT = """You are DeepHermes, a philosophical guide using the Socratic method.
|
40 |
For the philosophical question "{question}", generate 5 probing Socratic questions that would help someone explore this topic more deeply.
|
|
|
50 |
api_key=api_key
|
51 |
)
|
52 |
|
|
|
|
|
|
|
|
|
53 |
completion = client.chat.completions.create(
|
54 |
extra_headers={
|
55 |
"HTTP-Referer": "https://philosophical-debate-moderator.app",
|
|
|
60 |
{"role": "system", "content": system_prompt},
|
61 |
{"role": "user", "content": prompt}
|
62 |
],
|
63 |
+
temperature=temperature
|
|
|
64 |
)
|
65 |
return completion.choices[0].message.content
|
66 |
except Exception as e:
|
|
|
71 |
thinking = ""
|
72 |
cleaned_response = response
|
73 |
|
74 |
+
thinking_match = re.search(r'<thinking>(.*?)</thinking>', response, re.DOTALL)
|
75 |
+
if thinking_match:
|
76 |
+
thinking = thinking_match.group(1).strip()
|
77 |
+
cleaned_response = re.sub(r'<thinking>.*?</thinking>', '', response, flags=re.DOTALL).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
return thinking, cleaned_response
|
80 |
|
|
|
93 |
def synthesize_perspectives(api_key: str, question: str, perspectives: str) -> Dict[str, str]:
|
94 |
"""Synthesize the various perspectives into a balanced conclusion."""
|
95 |
system_prompt = SYNTHESIS_SYSTEM_PROMPT.format(question=question)
|
96 |
+
prompt = f"Here are various philosophical perspectives on the question '{question}':\n\n{perspectives}\n\nSynthesize these perspectives into a balanced conclusion."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
response = call_deephermes(api_key, prompt, system_prompt)
|
98 |
thinking, conclusion = extract_thinking(response)
|
99 |
return {"thinking": thinking, "conclusion": conclusion}
|
|
|
156 |
|
157 |
with gr.TabItem("Synthesis & Deep Thinking"):
|
158 |
synthesis_btn = gr.Button("Synthesize Perspectives")
|
159 |
+
with gr.Accordion("Deep Thinking Process", open=False, visible=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
thinking_output = gr.Markdown(label="AI's Chain of Thought")
|
161 |
conclusion_output = gr.Markdown(label="Synthesized Conclusion")
|
162 |
|
|
|
184 |
|
185 |
synthesis_btn.click(
|
186 |
fn=run_synthesis,
|
187 |
+
inputs=[api_key, question, num_perspectives],
|
188 |
outputs=[thinking_output, conclusion_output]
|
189 |
)
|
190 |
|