Skip to content

Code Generator using OpenAI and Ollama #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 226 additions & 0 deletions week1/community-contributions/Day 1_Code_generation_llm.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
{
"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
}
91 changes: 81 additions & 10 deletions week1/community-contributions/day-1-Stock-data-analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "4e2a9393-7767-488e-a8bf-27c12dca35bd",
"metadata": {},
"outputs": [],
Expand All @@ -35,10 +35,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "7b87cadb-d513-4303-baee-a37b6f938e4d",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"API key found and looks good so far!\n"
]
}
],
"source": [
"# Load environment variables in a file called .env\n",
"\n",
Expand All @@ -59,7 +67,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "019974d9-f3ad-4a8a-b5f9-0a3719aea2d3",
"metadata": {},
"outputs": [],
Expand All @@ -69,7 +77,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "51d42a08-188e-4c56-9578-47cd549bd1d8",
"metadata": {},
"outputs": [],
Expand All @@ -84,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "682eff74-55c4-4d4b-b267-703edbc293c7",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -121,7 +129,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "70b8d7e7-51e7-4392-9b85-9ac9f67a907c",
"metadata": {},
"outputs": [],
Expand All @@ -145,7 +153,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "de514421-4cc8-4881-85b4-97f03e94c589",
"metadata": {},
"outputs": [],
Expand All @@ -165,10 +173,73 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "41acc36f-484a-4257-a240-cf27520e7396",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/markdown": [
"## Stock Performance Analysis for GOOG\n",
"\n",
"The following is an analysis based on the provided data from the weekly performance of Google (GOOG) stock over a year. Here’s a breakdown of the key trends and observations:\n",
"\n",
"### Key Data Metrics\n",
"\n",
"- **Date Range**: The last recorded week was on March 21, 2025.\n",
"- **Opening Price**: The price at which the stock opened at the start of the period was generally high, around $163.32.\n",
"- **Closing Price**: The closing price also displayed variability with a notable peak reaching as high as $206.25 at some points.\n",
"- **High and Low Prices**: Price variation indicates several peaks and troughs, with highs occasionally exceeding $200, while lows dipped to around $148.\n",
"\n",
"### Summary of Trends\n",
"\n",
"1. **Overall Upward Trend**:\n",
" - Starting from around $150 on March 25, 2024, the stock reached approximately $166 by March 21, 2025. This suggests a gradual increase in the stock price over this period despite fluctuations.\n",
" \n",
"2. **Price Volatility**:\n",
" - The stock experienced several fluctuations where the high price during the week often exceeded $200, signaling periods of strong market interest.\n",
" - The lows reflected corrections; for instance, the lowest weekly closing was around $148, suggesting some weeks where buying pressure was lower.\n",
"\n",
"3. **Volume Trends**:\n",
" - There was a considerable trading volume in many weeks, especially during price fluctuations. The weekly volumes often surpassed tens of millions, showing active trading.\n",
"\n",
"4. **Dividends**:\n",
" - There were multiple dividends noted (e.g., $0.20 per share weeks), reflecting a strategy to share profits with shareholders.\n",
"\n",
"### Returns Calculation\n",
"\n",
"To calculate the total returns based on closing prices:\n",
"\n",
"- **Starting Price (Closing Price on March 25, 2024)**: $152.26\n",
"- **Ending Price (Closing Price on March 21, 2025)**: $166.25\n",
"\n",
"#### Calculation:\n",
"\n",
"\\[\n",
"\\text{Total Return} = \\left(\\frac{\\text{Ending Price} - \\text{Starting Price}}{\\text{Starting Price}}\\right) \\times 100\n",
"\\]\n",
"\n",
"Substituting the values in:\n",
"\n",
"\\[\n",
"\\text{Total Return} = \\left(\\frac{166.25 - 152.26}{152.26}\\right) \\times 100 \\approx \\left(\\frac{13.99}{152.26}\\right) \\times 100 \\approx 9.17\\%\n",
"\\]\n",
"\n",
"### Conclusion\n",
"\n",
"- **Total Returns**: An investor in GOOG could have expected approximately **9.17%** total returns over the analyzed period.\n",
"- The continued interest in Google stock, indicated by consistent trading volumes and dividends, along with the upward trajectory, suggests that it remains a strong candidate for investors looking to hold positions in tech stocks.\n",
"\n",
"This summary reflects the trends and performance of Google's stock based on the provided data, offering insights into trading behaviors and potential investment opportunities."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display_analysis(\"GOOG\")"
]
Expand Down
Loading