Spaces:
Sleeping
Sleeping
Update prompts/main_prompt.py
Browse files70 unmodified lines717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418570 unmodified lines๐น **Conclusion:**- *The new paint mixture has a **stronger red color** than before.*"""---### **๐ Fully Updated `app.py` (Ensuring Proper OpenAI Handling & Math Formatting)**```pythonimport osimport gradio as grfrom dotenv import load_dotenvfrom openai import OpenAIfrom prompts.initial_prompt import INITIAL_PROMPTfrom prompts.main_prompt import MAIN_PROMPT, PROBLEM_SOLUTIONS_PROMPT # Ensure both are imported# Load the API key from the .env file if availableif os.path.exists(".env"):load_dotenv(".env")OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")client = OpenAI(api_key=OPENAI_API_KEY)def gpt_call(history, user_message,model="gpt-4o",max_tokens=512,temperature=0.7,top_p=0.95):"""Calls the OpenAI API to generate a response.- history: [(user_text, assistant_text), ...]- user_message: The latest user message"""# 1) Start with the system message (MAIN_PROMPT) for contextmessages = [{"role": "system", "content": MAIN_PROMPT}]# 2) Append conversation historyfor user_text, assistant_text in history:if user_text:messages.append({"role": "user", "content": user_text})if assistant_text:messages.append({"role": "assistant", "content": assistant_text})# 3) Add the user's new messagemessages.append({"role": "user", "content": user_message})# 4) Call OpenAI APIcompletion = client.chat.completions.create(model=model,messages=messages,max_tokens=max_tokens,temperature=temperature,top_p=top_p)return completion.choices[0].message.contentdef respond(user_message, history):"""Handles user input and gets GPT-generated response.- user_message: The message from the user- history: List of (user, assistant) conversation history"""if not user_message:return "", history# If the user asks for a solution, inject PROBLEM_SOLUTIONS_PROMPTif "solution" in user_message.lower():assistant_reply = gpt_call(history, PROBLEM_SOLUTIONS_PROMPT)else:assistant_reply = gpt_call(history, user_message)# Add conversation turn to historyhistory.append((user_message, assistant_reply))return "", history############################### Gradio Blocks UI##############################with gr.Blocks() as demo:gr.Markdown("## AI-Guided Math PD Chatbot")# Chatbot initialization with the first AI messagechatbot = gr.Chatbot(value=[("", INITIAL_PROMPT)], # Initial system promptheight=500)# Stores the chat historystate_history = gr.State([("", INITIAL_PROMPT)])# User input fielduser_input = gr.Textbox(placeholder="Type your message here...",label="Your Input")# Submit actionuser_input.submit(respond,inputs=[user_input, state_history],outputs=[user_input, chatbot]).then(fn=lambda _, h: h,inputs=[user_input, chatbot],outputs=[state_history])# Run the Gradio appif __name__ == "__main__":demo.launch(server_name="0.0.0.0", server_port=7860, share=True)70 unmodified lines71727370 unmodified lines๐น **Conclusion:**- *The new paint mixture has a **stronger red color** than before.*"""
prompts/main_prompt.py-112