Okay, here's an enhanced prompt, broken down with explanations of why each part is included, designed to get a much better response from an AI chatbot (like ChatGPT, Gemini, etc.) when you're asking it to help refactor your Jupyter Notebook. I've included sections for context, specific requests, and desired output format.
Enhanced Prompt:
I have a Jupyter Notebook that currently performs a question-answering evaluation of several Large Language Models (LLMs) using `anythingllm` and `deepeval`. The notebook is functional, but it's messy, lacks clear organization, and is difficult to understand. I need your help to refactor it into a well-structured, readable, and maintainable format.
**Here's a summary of the current workflow (please ask if you need more detail about any step):**
1. **Question Set:** The notebook uses a predefined set of questions (currently hardcoded, but ideally should be loaded from a file - see 'Desired Improvements' below).
2. **LLM Integration (anythingllm):** It iterates through a list of LLMs available through `anythingllm`.
3. **Question Answering:** For each LLM and each question, it uses `anythingllm` to generate an answer.
4. **Evaluation (deepeval):** It then uses `deepeval` to evaluate the quality of the LLM's answer against a ground truth (also currently hardcoded).
5. **Hyperparameter Search:** The goal is to incorporate a hyperparameter search (using a library like `Optuna` or similar - I'm open to suggestions) to optimize parameters within `anythingllm` (e.g., temperature, top_p, max_tokens) for each LLM to improve performance.
6. **Results Storage:** Currently, results are printed to the console. I'd like to store them in a structured format (e.g., a Pandas DataFrame, CSV file, or JSON file).
**Specific Refactoring Requests:**
* **Modularization:** Break the code into well-defined functions. Each function should have a single, clear responsibility. For example:
* `load_questions(filepath)`: Loads questions from a file.
* `get_llm_answer(llm, question, hyperparameters)`: Gets an answer from a specific LLM for a given question and hyperparameters.
* `evaluate_answer(answer, ground_truth)`: Evaluates the answer using `deepeval`.
* `run_llm_evaluation(llm, questions, ground_truths, hyperparameters)`: Runs the evaluation for a single LLM across all questions.
* `perform_hyperparameter_search(llm, questions, ground_truths, hyperparameter_space)`: Performs the hyperparameter search for a given LLM.
* **Explicit Variable Names:** Use descriptive and meaningful variable names. Avoid single-letter variables or abbreviations that aren't immediately obvious. (e.g., `llm_response` instead of `resp`, `ground_truth_answer` instead of `gt`).
* **Clear Comments:** Add comments to explain the purpose of each section of code and any complex logic.
* **Error Handling:** Include basic error handling (e.g., `try...except` blocks) to gracefully handle potential issues like API errors or invalid input.
* **Configuration:** Move any hardcoded values (like API keys, LLM names, file paths) into a configuration section (e.g., a dictionary or a separate configuration file).
* **Hyperparameter Search Integration:** Help me integrate a hyperparameter search library (recommendations welcome!) to optimize the LLM parameters. I'm thinking of optimizing `temperature`, `top_p`, and `max_tokens`, but I'm open to suggestions.
**Desired Improvements (Optional, but helpful):**
* **Question Loading:** Suggest a good file format for storing the questions and ground truth answers (e.g., CSV, JSON, YAML). The function `load_questions()` should handle this.
* **Results Storage:** Recommend a suitable format for storing the evaluation results (Pandas DataFrame, CSV, JSON).
* **Logging:** Suggest incorporating basic logging to track the progress of the evaluation and any errors that occur.
* **Progress Bar:** Consider adding a progress bar (e.g., using `tqdm`) to visualize the progress of the evaluation loop.
**Output Format:**
Please provide the refactored code as a complete Jupyter Notebook (or a series of code blocks that I can easily copy and paste into a notebook). Include explanations of the key changes you've made and why. Focus on readability and maintainability. I'm looking for a solution that is easy to extend and modify in the future.
**Please ask clarifying questions if anything is unclear.**
Why this is better than the original prompt:
- Context: Provides a clear overview of the existing notebook and its purpose.
- Detailed Workflow: Breaks down the current process into numbered steps, making it easier for the AI to understand the logic.
- Specific Requests: Clearly outlines what you want the AI to do (modularization, variable naming, comments, error handling, etc.). The function name suggestions are particularly helpful.
- Desired Improvements: Suggests potential enhancements that go beyond the basic refactoring, showing you're thinking about the bigger picture.
- Output Format: Specifies the desired output (Jupyter Notebook or code blocks) and emphasizes the importance of explanations.
- Clarification Invitation: Encourages the AI to ask questions if anything is unclear, leading to a more accurate and helpful response.
- Library Suggestions: Open to suggestions for hyperparameter search libraries.
- Focus on Maintainability: Explicitly states the goal of creating a maintainable codebase.
How to use this prompt:
- Copy and paste this prompt into your AI chatbot.
- Be prepared to provide your existing Jupyter Notebook (or relevant code snippets) if the AI asks for it. You might need to share it via a service like Pastebin or a GitHub Gist if the chatbot doesn't allow direct file uploads.
- Review the AI's response carefully. It's likely that you'll need to make some adjustments to the code to fit your specific needs.
- Iterate! If the first response isn't perfect, provide feedback to the AI and ask it to refine its solution.
Good luck! Let me know if you'd like me to refine this prompt further. I can also help you with specific parts of the refactoring process if you get stuck.