Skip to content
Open
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
239 changes: 239 additions & 0 deletions 1_foundations/community_contributions/lab1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "8fb49a33",
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ddae906c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "16c1d494",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OPENAI_API_KEY exist and begines with: sk-proj-\n"
]
}
],
"source": [
"import os\n",
"openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n",
" \n",
"if openai_api_key:\n",
" print(\"OPENAI_API_KEY exist and begines with:\",openai_api_key[:8])\n",
"else:\n",
" print(\"OPENAI_API_KEY does not exist do troubleshootiong\")\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "cfab5010",
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e9170acd",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "fcc2fa91",
"metadata": {},
"outputs": [],
"source": [
"message =[{\"role\":\"user\",\"content\":\"what is 2+2?\"}]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6f277088",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 + 2 equals 4.\n"
]
}
],
"source": [
"response = openai.chat.completions.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=message,\n",
" temperature=0.7\n",
")\n",
"\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "3fab8283",
"metadata": {},
"outputs": [],
"source": [
"question =\"pleas propose a hard question to assess someones knowledge on bhagath singh. respond only with the question.\"\n",
"message =[{\"role\":\"user\",\"content\":question}]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "3efca0f9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"What year was Bhagat Singh sentenced to death and executed for his role in the Indian independence movement?\n"
]
}
],
"source": [
"response = openai.chat.completions.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=message,\n",
" \n",
")\n",
"\n",
"question = response.choices[0].message.content\n",
"print(question)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "daf88f41",
"metadata": {},
"outputs": [],
"source": [
"messages = [{\"role\":\"user\",\"content\":question}]\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "5381b7ad",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bhagat Singh was sentenced to death in 1931 and executed on March 23, 1931.\n"
]
}
],
"source": [
"response = openai.chat.completions.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=messages,\n",
")\n",
"answer = response.choices[0].message.content\n",
"print(answer)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "df164fa3",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"# Bhagat Singh was sentenced to death in 1931 and executed on March 23, 1931."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import Markdown, display\n",
"display(Markdown(f\"# {answer}\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef04aad7",
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}