shukdevdatta123 commited on
Commit
2185c1f
·
verified ·
1 Parent(s): c021f41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -9
app.py CHANGED
@@ -35,7 +35,9 @@ SYNTHESIS_SYSTEM_PROMPT = """You are a deep thinking AI, you may use extremely l
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
  SOCRATIC_SYSTEM_PROMPT = """You are DeepHermes, a philosophical guide using the Socratic method.
41
  For the philosophical question "{question}", generate 5 probing Socratic questions that would help someone explore this topic more deeply.
@@ -51,6 +53,10 @@ def call_deephermes(api_key: str, prompt: str, system_prompt: str, temperature:
51
  api_key=api_key
52
  )
53
 
 
 
 
 
54
  completion = client.chat.completions.create(
55
  extra_headers={
56
  "HTTP-Referer": "https://philosophical-debate-moderator.app",
@@ -61,7 +67,8 @@ def call_deephermes(api_key: str, prompt: str, system_prompt: str, temperature:
61
  {"role": "system", "content": system_prompt},
62
  {"role": "user", "content": prompt}
63
  ],
64
- temperature=temperature
 
65
  )
66
  return completion.choices[0].message.content
67
  except Exception as e:
@@ -72,10 +79,32 @@ def extract_thinking(response: str) -> Tuple[str, str]:
72
  thinking = ""
73
  cleaned_response = response
74
 
75
- thinking_match = re.search(r'<thinking>(.*?)</thinking>', response, re.DOTALL)
76
- if thinking_match:
77
- thinking = thinking_match.group(1).strip()
78
- cleaned_response = re.sub(r'<thinking>.*?</thinking>', '', response, flags=re.DOTALL).strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  return thinking, cleaned_response
81
 
@@ -94,7 +123,22 @@ def philosopher_perspective(api_key: str, question: str, philosopher: str) -> st
94
  def synthesize_perspectives(api_key: str, question: str, perspectives: str) -> Dict[str, str]:
95
  """Synthesize the various perspectives into a balanced conclusion."""
96
  system_prompt = SYNTHESIS_SYSTEM_PROMPT.format(question=question)
97
- prompt = f"Here are various philosophical perspectives on the question '{question}':\n\n{perspectives}\n\nSynthesize these perspectives into a balanced conclusion."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  response = call_deephermes(api_key, prompt, system_prompt)
99
  thinking, conclusion = extract_thinking(response)
100
  return {"thinking": thinking, "conclusion": conclusion}
@@ -157,7 +201,15 @@ def app_main():
157
 
158
  with gr.TabItem("Synthesis & Deep Thinking"):
159
  synthesis_btn = gr.Button("Synthesize Perspectives")
160
- with gr.Accordion("Deep Thinking Process", open=False):
 
 
 
 
 
 
 
 
161
  thinking_output = gr.Markdown(label="AI's Chain of Thought")
162
  conclusion_output = gr.Markdown(label="Synthesized Conclusion")
163
 
@@ -185,7 +237,7 @@ def app_main():
185
 
186
  synthesis_btn.click(
187
  fn=run_synthesis,
188
- inputs=[api_key, question, num_perspectives],
189
  outputs=[thinking_output, conclusion_output]
190
  )
191
 
 
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
  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
  {"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
  thinking = ""
80
  cleaned_response = response
81
 
82
+ # Look for different possible tag formats the model might use
83
+ thinking_patterns = [
84
+ r'<thinking>(.*?)</thinking>',
85
+ r'<Thinking>(.*?)</Thinking>',
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
  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
 
202
  with gr.TabItem("Synthesis & Deep Thinking"):
203
  synthesis_btn = gr.Button("Synthesize Perspectives")
204
+ with gr.Row():
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
 
238
  synthesis_btn.click(
239
  fn=run_synthesis,
240
+ inputs=[api_key, question, num_perspectives, temperature_slider],
241
  outputs=[thinking_output, conclusion_output]
242
  )
243