Spaces:
Sleeping
Sleeping
Update prompts/main_prompt.py
Browse files- prompts/main_prompt.py +0 -112
prompts/main_prompt.py
CHANGED
|
@@ -71,115 +71,3 @@ x = \frac{600}{2} = 300
|
|
| 71 |
🔹 **Conclusion:**
|
| 72 |
- *The new paint mixture has a **stronger red color** than before.*
|
| 73 |
"""
|
| 74 |
-
|
| 75 |
-
---
|
| 76 |
-
|
| 77 |
-
### **🚀 Fully Updated `app.py` (Ensuring Proper OpenAI Handling & Math Formatting)**
|
| 78 |
-
```python
|
| 79 |
-
import os
|
| 80 |
-
import gradio as gr
|
| 81 |
-
from dotenv import load_dotenv
|
| 82 |
-
from openai import OpenAI
|
| 83 |
-
from prompts.initial_prompt import INITIAL_PROMPT
|
| 84 |
-
from prompts.main_prompt import MAIN_PROMPT, PROBLEM_SOLUTIONS_PROMPT # Ensure both are imported
|
| 85 |
-
|
| 86 |
-
# Load the API key from the .env file if available
|
| 87 |
-
if os.path.exists(".env"):
|
| 88 |
-
load_dotenv(".env")
|
| 89 |
-
|
| 90 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 91 |
-
|
| 92 |
-
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
def gpt_call(history, user_message,
|
| 96 |
-
model="gpt-4o",
|
| 97 |
-
max_tokens=512,
|
| 98 |
-
temperature=0.7,
|
| 99 |
-
top_p=0.95):
|
| 100 |
-
"""
|
| 101 |
-
Calls the OpenAI API to generate a response.
|
| 102 |
-
- history: [(user_text, assistant_text), ...]
|
| 103 |
-
- user_message: The latest user message
|
| 104 |
-
"""
|
| 105 |
-
# 1) Start with the system message (MAIN_PROMPT) for context
|
| 106 |
-
messages = [{"role": "system", "content": MAIN_PROMPT}]
|
| 107 |
-
|
| 108 |
-
# 2) Append conversation history
|
| 109 |
-
for user_text, assistant_text in history:
|
| 110 |
-
if user_text:
|
| 111 |
-
messages.append({"role": "user", "content": user_text})
|
| 112 |
-
if assistant_text:
|
| 113 |
-
messages.append({"role": "assistant", "content": assistant_text})
|
| 114 |
-
|
| 115 |
-
# 3) Add the user's new message
|
| 116 |
-
messages.append({"role": "user", "content": user_message})
|
| 117 |
-
|
| 118 |
-
# 4) Call OpenAI API
|
| 119 |
-
completion = client.chat.completions.create(
|
| 120 |
-
model=model,
|
| 121 |
-
messages=messages,
|
| 122 |
-
max_tokens=max_tokens,
|
| 123 |
-
temperature=temperature,
|
| 124 |
-
top_p=top_p
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
return completion.choices[0].message.content
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
def respond(user_message, history):
|
| 131 |
-
"""
|
| 132 |
-
Handles user input and gets GPT-generated response.
|
| 133 |
-
- user_message: The message from the user
|
| 134 |
-
- history: List of (user, assistant) conversation history
|
| 135 |
-
"""
|
| 136 |
-
if not user_message:
|
| 137 |
-
return "", history
|
| 138 |
-
|
| 139 |
-
# If the user asks for a solution, inject PROBLEM_SOLUTIONS_PROMPT
|
| 140 |
-
if "solution" in user_message.lower():
|
| 141 |
-
assistant_reply = gpt_call(history, PROBLEM_SOLUTIONS_PROMPT)
|
| 142 |
-
else:
|
| 143 |
-
assistant_reply = gpt_call(history, user_message)
|
| 144 |
-
|
| 145 |
-
# Add conversation turn to history
|
| 146 |
-
history.append((user_message, assistant_reply))
|
| 147 |
-
|
| 148 |
-
return "", history
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
##############################
|
| 152 |
-
# Gradio Blocks UI
|
| 153 |
-
##############################
|
| 154 |
-
with gr.Blocks() as demo:
|
| 155 |
-
gr.Markdown("## AI-Guided Math PD Chatbot")
|
| 156 |
-
|
| 157 |
-
# Chatbot initialization with the first AI message
|
| 158 |
-
chatbot = gr.Chatbot(
|
| 159 |
-
value=[("", INITIAL_PROMPT)], # Initial system prompt
|
| 160 |
-
height=500
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
# Stores the chat history
|
| 164 |
-
state_history = gr.State([("", INITIAL_PROMPT)])
|
| 165 |
-
|
| 166 |
-
# User input field
|
| 167 |
-
user_input = gr.Textbox(
|
| 168 |
-
placeholder="Type your message here...",
|
| 169 |
-
label="Your Input"
|
| 170 |
-
)
|
| 171 |
-
|
| 172 |
-
# Submit action
|
| 173 |
-
user_input.submit(
|
| 174 |
-
respond,
|
| 175 |
-
inputs=[user_input, state_history],
|
| 176 |
-
outputs=[user_input, chatbot]
|
| 177 |
-
).then(
|
| 178 |
-
fn=lambda _, h: h,
|
| 179 |
-
inputs=[user_input, chatbot],
|
| 180 |
-
outputs=[state_history]
|
| 181 |
-
)
|
| 182 |
-
|
| 183 |
-
# Run the Gradio app
|
| 184 |
-
if __name__ == "__main__":
|
| 185 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 71 |
🔹 **Conclusion:**
|
| 72 |
- *The new paint mixture has a **stronger red color** than before.*
|
| 73 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|