| import os
|
| import openai
|
| import gradio as gr
|
| from dotenv import load_dotenv
|
|
|
|
|
| load_dotenv()
|
| api_key = os.getenv("OPENAI_API_KEY")
|
|
|
| if not api_key:
|
| raise ValueError("API Key not found! Ensure you have added OPENAI_API_KEY in your .env file.")
|
|
|
|
|
| openai.api_key = api_key
|
|
|
|
|
| def python_tutor_bot(user_input):
|
| if not user_input.strip():
|
| return "Please enter a valid question."
|
|
|
|
|
| response = openai.ChatCompletion.create(
|
| model="gpt-4o-mini",
|
| messages=[
|
| {
|
| "role": "system",
|
| "content": "Create a Python tutor bot that helps beginners learn and troubleshoot Python programming by answering questions, offering explanations, providing code examples, and suggesting improvements.\n\n"
|
| "Explain concepts like you are doing it for an 8th grader.\n\n"
|
| "# Features\n"
|
| "- Accept user queries related to Python programming, especially focusing on basic syntax and beginner-level concepts.\n"
|
| "- Provide simple and clear explanations suitable for novice users.\n"
|
| "- Supply easy-to-understand code examples to illustrate fundamental concepts and suggest improvements.\n"
|
| "- Troubleshoot user-provided code by identifying errors and explaining how to fix them in a simple manner.\n\n"
|
| "# Steps\n"
|
| "1. **Understand the Query**: Carefully read and relate to the user's question or problem.\n"
|
| "2. **Explanation**:\n"
|
| " - Offer a straightforward explanation of the concept related to the user's query.\n"
|
| " - Break down complex ideas into simpler terms that a beginner would understand.\n"
|
| "3. **Code Examples**:\n"
|
| " - Provide examples demonstrating the concept or solution with an emphasis on clarity.\n"
|
| " - If relevant, show both correct and incorrect versions to highlight common beginner mistakes.\n"
|
| "4. **Troubleshooting**:\n"
|
| " - Examine user-provided code for errors or inefficiencies.\n"
|
| " - Offer simple and clear suggestions on how to resolve issues.\n"
|
| "5. **Engagement**:\n"
|
| " - Encourage further questions or clarification requests to promote deeper understanding, maintaining a supportive tone.\n\n"
|
| "# Output Format\n"
|
| "- Responses should be in clear, conversational language easy for beginners to understand.\n"
|
| "- Code examples should be formatted and clearly delineated from explanations (e.g., using indentation or styled text).\n"
|
| "- Conclude with an inviting tone for follow-up questions.\n\n"
|
| },
|
| {
|
| "role": "user",
|
| "content": user_input
|
| }
|
| ],
|
| temperature=0.03,
|
| max_tokens=2000,
|
| top_p=0.1,
|
| frequency_penalty=0.1,
|
| presence_penalty=0.95
|
| )
|
|
|
|
|
| return response["choices"][0]["message"]["content"]
|
|
|
|
|
| chatbot_ui = gr.Interface(
|
| fn=python_tutor_bot,
|
| inputs=gr.Textbox(lines=3, placeholder="Ask me anything about Python..."),
|
| outputs=gr.Textbox(),
|
| title="Python Tutor Bot",
|
| description="A friendly Python tutor bot to help you learn and troubleshoot Python. Ask any question!"
|
|
|
| )
|
|
|
|
|
| if __name__ == "__main__":
|
| chatbot_ui.launch(share=True)
|
|
|