3 changed files with 296 additions and 12 deletions
@ -0,0 +1,199 @@ |
|||||||
|
{ |
||||||
|
"cells": [ |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "0a2cd326-08fd-4f28-b0a3-b343691bda16", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"# imports\n", |
||||||
|
"\n", |
||||||
|
"import os\n", |
||||||
|
"import requests\n", |
||||||
|
"from dotenv import load_dotenv\n", |
||||||
|
"from bs4 import BeautifulSoup\n", |
||||||
|
"from IPython.display import Markdown, display\n", |
||||||
|
"from openai import OpenAI\n" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"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": [ |
||||||
|
"openai = OpenAI()" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"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": "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", |
||||||
|
" response = ask_ai(user_input)\n", |
||||||
|
"# Display output in Markdown format\n", |
||||||
|
" display(Markdown(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 |
||||||
|
} |
Loading…
Reference in new issue