From the uDemy course on LLM engineering.
https://www.udemy.com/course/llm-engineering-master-ai-and-large-language-models
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
226 lines
6.5 KiB
226 lines
6.5 KiB
{ |
|
"cells": [ |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "0a2cd326-08fd-4f28-b0a3-b343691bda16", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"import os\n", |
|
"import requests\n", |
|
"from dotenv import load_dotenv\n", |
|
"from bs4 import BeautifulSoup\n", |
|
"from IPython.display import Markdown, display\n", |
|
"import openai \n", |
|
"import ollama " |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "0a5f3e89-6a79-4fb2-be72-ed67d340a38c", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# Load environment variables in a file called .env\n", |
|
"\n", |
|
"load_dotenv(override=True)\n", |
|
"api_key = os.getenv('OPENAI_API_KEY')\n", |
|
"\n", |
|
"# Check the key\n", |
|
"\n", |
|
"if not api_key:\n", |
|
" print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n", |
|
"elif not api_key.startswith(\"sk-proj-\"):\n", |
|
" print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n", |
|
"elif api_key.strip() != api_key:\n", |
|
" print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n", |
|
"else:\n", |
|
" print(\"API key found and looks good so far!\")\n" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "b42f2583-7f15-435b-8ab6-315ae9f316cf", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# Initialize OpenAI\n", |
|
"openai_client = openai.OpenAI(api_key=api_key)" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "3ee959e1-22ef-42dd-9c98-edda119729e8", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"def ask_ai(prompt):\n", |
|
" \"\"\" Function to send a prompt to OpenAI and return the response \"\"\"\n", |
|
" try:\n", |
|
" response = openai.chat.completions.create(\n", |
|
" model=\"gpt-4o-mini\",\n", |
|
" messages=[\n", |
|
" {\"role\": \"system\", \"content\": \"You are an advanced AI assistant specialized in software development. You generate complete, optimized, and well-documented code for any requested approach, ensuring best practices, efficiency, and scalability. You provide explanations alongside the code, highlighting important concepts and potential improvements.\"},\n", |
|
" {\"role\": \"user\", \"content\": prompt}\n", |
|
" ]\n", |
|
" )\n", |
|
" return response.choices[0].message.content\n", |
|
" except Exception as e:\n", |
|
" return f\"Error: {e}\"" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "421c4ebe-7017-4ac8-b4d1-7837e1a68223", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# Function to ask Ollama\n", |
|
"def ask_ollama(prompt):\n", |
|
" \"\"\" send a prompt to ollama and return the response \"\"\"\n", |
|
" try:\n", |
|
" response = ollama.chat(\n", |
|
" model=\"llama3.2\",\n", |
|
" messages=[\n", |
|
" {\"role\": \"system\", \"content\": \"You are an advanced AI assistant specialized in software development. You generate complete, optimized, and well-documented code for any requested approach, ensuring best practices, efficiency, and scalability. You provide explanations alongside the code, highlighting important concepts and potential improvements.\"},\n", |
|
" {\"role\": \"user\", \"content\": prompt}\n", |
|
" ]\n", |
|
" )\n", |
|
" return response['message']['content']\n", |
|
" except Exception as e:\n", |
|
" return f\"Ollama Error: {e}\" " |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "bae8d4aa-7a29-4087-b6af-2e90cb0d9b0d", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# Run the AI assistant in a loop\n", |
|
"print(\"AI Coding Assistant: Type 'exit' to stop\")\n", |
|
"while True:\n", |
|
" user_input = input(\"\\nYou: \")\n", |
|
" \n", |
|
" if user_input.lower() == \"exit\":\n", |
|
" print(\"Goodbye!\")\n", |
|
" break\n", |
|
"\n", |
|
" print(\"\\n **OpenAI Response:**\")\n", |
|
" openai_response = ask_ai(user_input)\n", |
|
" display(Markdown(openai_response))\n", |
|
"\n", |
|
" print(\"\\n **Ollama Response:**\")\n", |
|
" ollama_response = ask_ollama(user_input)\n", |
|
" display(Markdown(ollama_response))" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "f5fa23de-670e-4dcb-a237-5b7398ae638d", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "74e73c7c-8488-49b6-b7ec-1a9e68348a45", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "0b949382-4f23-4f12-bd59-5231f68725e7", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "09a21202-01a9-418a-8177-3a7f8dd8f643", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "a44c7b69-d361-425e-b9e6-3edbea9f6949", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "7867fb13-ac3e-43c9-aeb1-414d3d5f330b", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "50fa0835-842f-49ca-9c91-cd3fd52e765e", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "44ca77da-cd34-4bd2-912a-71fb548ada86", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "deb595bf-cf2a-4798-88df-1b4fe06cb0f7", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"id": "77a0e0fe-5e65-41d6-a3ee-b1ef96b44394", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [] |
|
} |
|
], |
|
"metadata": { |
|
"kernelspec": { |
|
"display_name": "Python 3 (ipykernel)", |
|
"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.11.11" |
|
} |
|
}, |
|
"nbformat": 4, |
|
"nbformat_minor": 5 |
|
}
|
|
|