Files
LLM_Engineering_OLD/week1/community-contributions/solisoma/end_of_week_exercise.ipynb

489 lines
18 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"id": "6df489a5",
"metadata": {},
"outputs": [],
"source": [
"# week1 -> day1\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from IPython.display import Markdown, display, update_display\n",
"from openai import OpenAI\n",
"\n",
"#week2 -> day2\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8e7fbf42",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"api_key:str = os.getenv('OPENAI_API_KEY')"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "b9266d13",
"metadata": {},
"outputs": [],
"source": [
"class SolveTechnicalQuestions:\n",
" _system_prompt = \"\"\"\n",
" You are a snarkyassistant that analyzes the contents of a website, \n",
" and provides a short, snarky, humorous summary, ignoring text that might be navigation related.\n",
" Respond in markdown. Do not wrap the markdown in a code block - respond just with the markdown.\n",
" \"\"\"\n",
"\n",
" def __init__(self, model: str = \"gpt-4o-mini\") -> None:\n",
" self.openai_client = OpenAI()\n",
" self._MODEL = model\n",
"\n",
" def get_user_technical_question_prompt(self, question:str):\n",
" prompt = f\"\"\"\n",
" Answer this technical questio comprehensively:\n",
" Provide:\n",
" 1. A clear, accurate answer\n",
" 2. Code examples if relevant\n",
" 3. Best practices and recommendations\n",
" 4. Potential pitfalls or considerations\n",
" 5. Additional resources or references if helpful\n",
"\n",
" Format your response in a structured, easy-to-read manner.\n",
"\n",
" Question {question}\n",
" \"\"\"\n",
"\n",
" return prompt\n",
" \n",
" def set_system_prompt(self, system_prompt: str) -> None:\n",
" self._system_prompt = system_prompt\n",
" \n",
" def set_endpoint(self, endpoint: str, api_key: str = \"ollama\") -> None:\n",
" self.openai_client = OpenAI(base_url=endpoint, api_key=api_key)\n",
"\n",
" def set_model(self, model: str) -> None:\n",
" self._MODEL = model\n",
"\n",
" def start(self, stream=False):\n",
" try:\n",
" while True:\n",
" question = input(\">>> \")\n",
" \n",
" if question.strip().lower() in ['quit', 'exit', 'q']:\n",
" print(\"Goodbye!\")\n",
" break\n",
" \n",
" if not question.strip():\n",
" print(\"Please enter a question.\")\n",
" continue\n",
" \n",
" message = self.get_user_technical_question_prompt(question.strip())\n",
" \n",
" response = self.openai_client.chat.completions.create(\n",
" model=self._MODEL, \n",
" messages=[\n",
" {\"role\": \"system\", \"content\": self._system_prompt},\n",
" {\"role\": \"user\", \"content\": message},\n",
" ],\n",
" stream=stream\n",
" )\n",
"\n",
" if stream:\n",
" full_response = \"\"\n",
" display_handle = display(Markdown(full_response), display_id=True)\n",
" for chunk in response:\n",
" if chunk.choices[0].delta.content:\n",
" full_response += chunk.choices[0].delta.content\n",
" update_display(Markdown(full_response), display_id=display_handle.display_id)\n",
" full_response += \"\\n\"\n",
" update_display(Markdown(full_response), display_id=display_handle.display_id)\n",
" else:\n",
" full_response = response.choices[0].message.content\n",
" display(Markdown(full_response))\n",
" \n",
" except KeyboardInterrupt:\n",
" print(\"\\nGoodbye!\")\n",
" except Exception as e:\n",
" print(f\"Error: {e}\")\n",
"\n",
" def start_with_gradio(self, question:str, stream=False):\n",
" if not question.strip():\n",
" return \"Please enter a question.\"\n",
" \n",
" message = self.get_user_technical_question_prompt(question.strip())\n",
" \n",
" response = self.openai_client.chat.completions.create(\n",
" model=self._MODEL, \n",
" messages=[\n",
" {\"role\": \"system\", \"content\": self._system_prompt},\n",
" {\"role\": \"user\", \"content\": message},\n",
" ],\n",
" stream=stream\n",
" )\n",
"\n",
" if stream:\n",
" full_response = \"\"\n",
" for chunk in response:\n",
" if chunk.choices[0].delta.content:\n",
" full_response += chunk.choices[0].delta.content\n",
" yield full_response\n",
" full_response += \"\\n\"\n",
" yield full_response\n",
" else:\n",
" yield response.choices[0].message.content\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "0bddb2e5",
"metadata": {},
"outputs": [],
"source": [
"TECHNICAL_SYSTEM_PROMPT = \"\"\"\n",
"You are an expert technical assistant with deep knowledge in:\n",
"\n",
"PROGRAMMING & DEVELOPMENT:\n",
"- Python, JavaScript, Java, C++, Go, Rust, TypeScript\n",
"- Web development (React, Vue, Angular, Node.js)\n",
"- Mobile development (iOS, Android, Flutter)\n",
"- DevOps (Docker, Kubernetes, CI/CD, AWS, Azure, GCP)\n",
"- Database systems (SQL, NoSQL, PostgreSQL, MongoDB)\n",
"- Software architecture patterns and best practices\n",
"\n",
"SYSTEMS & INFRASTRUCTURE:\n",
"- Operating systems (Linux, Windows, macOS)\n",
"- Networking protocols and security\n",
"- Cloud computing and distributed systems\n",
"- Monitoring, logging, and observability\n",
"- Performance optimization and scaling\n",
"\n",
"AI & MACHINE LEARNING:\n",
"- Machine learning algorithms and frameworks\n",
"- Deep learning (TensorFlow, PyTorch)\n",
"- Natural language processing\n",
"- Computer vision and image processing\n",
"- MLOps and model deployment\n",
"\n",
"RESPONSE GUIDELINES:\n",
"1. Provide accurate, up-to-date technical information\n",
"2. Include code examples when relevant\n",
"3. Explain complex concepts clearly\n",
"4. Suggest best practices and alternatives\n",
"5. Warn about potential pitfalls or security issues\n",
"6. Reference official documentation when appropriate\n",
"\n",
"Always prioritize accuracy and practical applicability in your technical responses.\n",
"\"\"\"\n",
"\n",
"Chat = SolveTechnicalQuestions()\n",
"Chat.set_system_prompt(TECHNICAL_SYSTEM_PROMPT)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8675757",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"## Understanding the `for` Loop\n",
"\n",
"### 1. Clear and Accurate Answer\n",
"\n",
"A `for` loop is a control flow statement used in programming to iterate over a sequence (like a list, tuple, string, or range) or perform a task a specific number of times. It allows you to execute a block of code repeatedly, which is crucial for automating repetitive tasks.\n",
"\n",
"### 2. Code Examples\n",
"\n",
"#### Python Example\n",
"\n",
"```python\n",
"# Iterate over a list\n",
"numbers = [1, 2, 3, 4, 5]\n",
"for number in numbers:\n",
" print(number)\n",
"```\n",
"\n",
"#### JavaScript Example\n",
"\n",
"```javascript\n",
"// Iterate over an array\n",
"const numbers = [1, 2, 3, 4, 5];\n",
"for (let number of numbers) {\n",
" console.log(number);\n",
"}\n",
"```\n",
"\n",
"#### Java Example\n",
"\n",
"```java\n",
"// Iterate over an array\n",
"int[] numbers = {1, 2, 3, 4, 5};\n",
"for (int number : numbers) {\n",
" System.out.println(number);\n",
"}\n",
"```\n",
"\n",
"### 3. Best Practices and Recommendations\n",
"\n",
"- **Use Descriptive Variable Names:** This improves code readability. Avoid vague names like `i` or `j`, unless they are commonly used as loop counters.\n",
" \n",
"- **Limit Loop Complexity:** Ensure that the logic inside the loop is straightforward. If the loop gets complicated, consider refactoring or extracting the logic into a separate function.\n",
"\n",
"- **Control Iteration with Care:** If you're iterating through large datasets, be mindful of performance impacts and consider alternatives (like list comprehensions in Python).\n",
"\n",
"### 4. Potential Pitfalls or Considerations\n",
"\n",
"- **Off-by-One Errors:** These are common when dealing with loop boundaries. Always double-check loop conditions to ensure you dont miss elements or go out of range.\n",
"\n",
"- **Infinite Loops:** Ensure that your loop has a condition that eventually becomes false, or it could result in an infinite loop, causing your program to hang.\n",
"\n",
"- **Modifying the Loop Variable:** Changing the loop variable within the loops body can lead to unexpected behaviors, especially in languages like Python.\n",
"\n",
"### 5. Additional Resources or References\n",
"\n",
"- [Python for Loop Documentation](https://docs.python.org/3/reference/compound_stmts.html#for)\n",
"- [JavaScript for Loop Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)\n",
"- [Java for Loop Documentation](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/ch04.html#for)\n",
"\n",
"These resources provide in-depth explanations and examples for different programming languages, and can be useful for further learning about `for` loops and loops in general.\n"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Goodbye!\n"
]
}
],
"source": [
"# Set stream to true to allow streaming of the response\n",
"# It Mimics REPL\n",
"# After running look up to see a terminal where you put in your question\n",
"Chat.start(stream=True)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "7a086b95",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"### What is an `if` Statement?\n",
"\n",
"An `if` statement is a fundamental control flow statement in programming that allows you to execute a block of code based on a specified condition. If the condition evaluates to `true`, the block of code will execute; otherwise, it will be skipped.\n",
"\n",
"#### 1. A Clear, Accurate Answer\n",
"\n",
"In programming, the `if` statement checks a condition. If the condition is `true`, the code inside the `if` block is executed. If the condition is `false`, the block is ignored. \n",
"\n",
"Heres the basic syntax in Python and JavaScript as examples:\n",
"\n",
"**Python Syntax:**\n",
"```python\n",
"if condition:\n",
" # Code to execute if condition is true\n",
"```\n",
"\n",
"**JavaScript Syntax:**\n",
"```javascript\n",
"if (condition) {\n",
" // Code to execute if condition is true\n",
"}\n",
"```\n",
"\n",
"#### 2. Code Examples\n",
"\n",
"**Python Example:**\n",
"```python\n",
"temperature = 30\n",
"\n",
"if temperature > 25:\n",
" print(\"It's a hot day!\")\n",
"```\n",
"\n",
"**JavaScript Example:**\n",
"```javascript\n",
"let temperature = 30;\n",
"\n",
"if (temperature > 25) {\n",
" console.log(\"It's a hot day!\");\n",
"}\n",
"```\n",
"\n",
"In both examples, if the `temperature` variable is greater than 25, the corresponding message will be printed to the console.\n",
"\n",
"#### 3. Best Practices and Recommendations\n",
"\n",
"- **Use Clear Conditions**: Ensure that the condition being evaluated is clear and understandable. \n",
"- **Avoid Complex Conditions**: If conditions become too complex, consider breaking them down into multiple `if` statements or using logical operators for clarity.\n",
"- **Indentation**: Properly indent your code blocks. This improves readability and maintainability.\n",
"- **Use `elif`/`else if` for Multiple Conditions**: When evaluating multiple conditions, use `elif` (Python) or `else if` (JavaScript) to make the logic cleaner.\n",
" \n",
" **Example with `elif`:**\n",
" ```python\n",
" score = 85\n",
"\n",
" if score >= 90:\n",
" print(\"Grade: A\")\n",
" elif score >= 80:\n",
" print(\"Grade: B\")\n",
" else:\n",
" print(\"Grade: C\")\n",
" ```\n",
"\n",
"#### 4. Potential Pitfalls or Considerations\n",
"\n",
"- **Boolean Context**: Ensure that the condition evaluates to a boolean (`true` or `false`). Improper conditions could result in unexpected behavior.\n",
"- **Missing `else` or `elif`**: If not handled correctly, cases that fall outside the specified conditions may go unnoticed. Consider using an `else` statement to capture any situations not defined in prior conditions.\n",
"- **Short-Circuit Evaluation**: In languages like Python and JavaScript, using logical operators (`and`, `or`) as conditions can lead to short-circuit evaluation, which might affect the execution of your code. Be cautious about using these in conditions.\n",
"\n",
"#### 5. Additional Resources or References\n",
"\n",
"- **Python Documentation on `if` Statements**: [Python If Statement](https://docs.python.org/3/tutorial/controlflow.html#if-statements)\n",
"- **JavaScript Documentation on Conditional Statements**: [MDN Web Docs - Conditionals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling#conditional_statements)\n",
"\n",
"Understanding how `if` statements work is crucial for implementing decision-making logic in your programs, enabling dynamic behavior based on varying conditions."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Goodbye!\n"
]
}
],
"source": [
"# Set stream to false to get a single response\n",
"Chat.start(stream=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "95daf1f1",
"metadata": {},
"outputs": [],
"source": [
"# Ignore if you don't want to use ollama\n",
"# Here shows the ability to switch from one endpoint to another\n",
"Chat.set_endpoint(\"http://localhost:11434/v1\")\n",
"Chat.set_model(\"llama3.2\")\n",
"\n",
"Chat.start(stream=True)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "c7d66ef7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7861\n",
"* To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gr_output = gr.Markdown(label=\"Response\")\n",
"stream_input = gr.Checkbox(label='Stream', value=False)\n",
"question_input = gr.Textbox(label=\"Question\", info=\"Ask it any technical question\", lines=1)\n",
"\n",
"interface = gr.Interface(\n",
" fn=Chat.start_with_gradio, \n",
" title=\"ChatGPT\", \n",
" inputs=[question_input, stream_input], \n",
" outputs=[gr_output], \n",
" flagging_mode=\"never\"\n",
")\n",
"\n",
"interface.launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ed776b93",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}