Skip to content
Closed
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
285 changes: 53 additions & 232 deletions 01_materials/labs/01_1_introduction.ipynb

Large diffs are not rendered by default.

8,008 changes: 31 additions & 7,977 deletions 01_materials/labs/01_2_longer_context.ipynb

Large diffs are not rendered by default.

266 changes: 7 additions & 259 deletions 01_materials/labs/01_3_local_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "b93f5989",
"metadata": {},
"outputs": [],
Expand All @@ -71,29 +71,10 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "1d1a1fa2",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Hello! I'm an AI assistant designed to help with a wide range of tasks and answer questions. How can I assist you today? \n",
"\n",
"(Note: The response strictly follows the instruction—only two concise sentences, no elaboration beyond that.) ✅ \n",
"Final Output: \n",
"Hello! I'm an AI assistant designed to help with a wide range of tasks and answer questions. How can I assist you today? \n",
"--- \n",
"(End of response)"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"from IPython.display import display, Markdown\n",
"\n",
Expand All @@ -110,7 +91,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "33946c5c",
"metadata": {},
"outputs": [],
Expand All @@ -124,253 +105,20 @@
")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ddb99db2",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Sure! Here are two examples of tasks I can perform:\n",
"\n",
"1. **Answering Questions**: For instance, when you ask me, \"What is the capital of France?\", I can provide a clear, accurate response—Paris—in just seconds.\n",
"\n",
"2. **Summarizing Information**: If you share a long article or report, I can condense it into a concise summary that highlights key points and insights.\n",
"\n",
"Regarding your question about classifying images: Currently, I cannot directly classify images. While I am proficient at understanding and generating text, my capabilities are focused on working with textual content. To analyze or classify an image—such as identifying objects, scenes, or patterns—I would need to rely on a visual model or system that is specifically designed for image processing.\n",
"\n",
"So, in summary: \n",
"✅ Tasks I can perform: Answering questions, summarizing information \n",
"❌ Classification of images: Not yet directly supported (though this feature is being actively developed)\n",
"\n",
"Let me know if you'd like to explore how we might work together on text-based tasks or if you have any visual content that you’d like to share!\n",
"\n",
"---\n",
"\n"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display, Markdown\n",
"\n",
"display(Markdown(response.output_text))"
]
},
{
"cell_type": "markdown",
"id": "2551b016",
"metadata": {},
"source": [
"## Using an Image as Context\n",
"\n",
"There are several ways to interact with the client and use an image as context. They are discussed in the OpenAI platform [documentation](https://platform.openai.com/docs/guides/images-vision?format=base64-encoded#analyze-images). LMStudio inherits this behaviour. Keep in mind the model in use would need to support the task of interest. As we saw in the cell above, we could execute the following code but the model would fail at the specific task"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0519bc55",
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"client = OpenAI(base_url=\"http://127.0.0.1:1234/v1\", api_key=\"not-needed\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ddb99db2",
"metadata": {},
"outputs": [],
"source": [
"import base64\n",
"# Function to encode the image\n",
"def encode_image(image_path):\n",
" with open(image_path, \"rb\") as image_file:\n",
" return base64.b64encode(image_file.read()).decode(\"utf-8\")\n",
"# Path to your image\n",
"image_path = \"./img/00_cat.jpg\"\n",
"\n",
"# Getting the Base64 string\n",
"base64_image = encode_image(image_path)\n",
"\n",
"\n",
"response = client.responses.create(\n",
" model=\"local-model\",\n",
" input=[\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": [\n",
" { \"type\": \"input_text\", \"text\": \"what's in this image?\" },\n",
" {\n",
" \"type\": \"input_image\",\n",
" \"image_url\": f\"data:image/jpeg;base64,{base64_image}\",\n",
" },\n",
" ],\n",
" }\n",
" ],\n",
")"
]
},
{
"cell_type": "markdown",
"id": "1f2a1432",
"metadata": {},
"source": [
"Only to exemplify the identification or classification of images, we can consume OpenAI directly to accomplish it. To refresh our memory, we repeat the task we did before for text"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b0cba538",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append('../../05_src/')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ada76fe2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from dotenv import load_dotenv\n",
"load_dotenv('../../05_src/.secrets')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6cb271d4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello! How can I assist you today?\n"
]
}
],
"source": [
"import os\n",
"from openai import OpenAI\n",
"client = OpenAI(base_url='https://k7uffyg03f.execute-api.us-east-1.amazonaws.com/prod/openai/v1', \n",
" api_key='any value',\n",
" default_headers={\"x-api-key\": os.getenv('API_GATEWAY_KEY')})\n",
"\n",
"response = client.responses.create(\n",
" model = 'gpt-4o-mini',\n",
" input = 'Hello world!'\n",
" \n",
")\n",
"\n",
"print(response.output_text)"
]
},
{
"cell_type": "markdown",
"id": "e99cfb8c",
"metadata": {},
"source": [
"Now that the `client` varaible is created, we can use it for a different type of response we are looking for"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "3a2b1216",
"metadata": {},
"outputs": [],
"source": [
"import base64\n",
"# Function to encode the image\n",
"def encode_image(image_path):\n",
" with open(image_path, \"rb\") as image_file:\n",
" return base64.b64encode(image_file.read()).decode(\"utf-8\")\n",
"# Path to your image\n",
"image_path = \"./img/00_cat.jpg\"\n",
"\n",
"# Getting the Base64 string\n",
"base64_image = encode_image(image_path)\n",
"\n",
"\n",
"response = client.responses.create(\n",
" model=\"gpt-4o-mini\",\n",
" input=[\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": [\n",
" { \"type\": \"input_text\", \"text\": \"what's in this image?\" },\n",
" {\n",
" \"type\": \"input_image\",\n",
" \"image_url\": f\"data:image/jpeg;base64,{base64_image}\",\n",
" },\n",
" ],\n",
" }\n",
" ],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "acdcaee4",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"The image features a tabby cat sitting on a ledge, with a background of bare branches and a blue sky. The cat has a distinct striped pattern and piercing green eyes."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display, Markdown\n",
"display(Markdown(response.output_text))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8bd72979",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "deploying-ai-env",
"display_name": "deploying-ai-env (3.12.7)",
"language": "python",
"name": "python3"
},
Expand All @@ -384,7 +132,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.12"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
Loading
Loading