120 lines
2.7 KiB
Plaintext
120 lines
2.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "834bf7f1",
|
|
"metadata": {},
|
|
"source": [
|
|
"Task: build a tool that takes a technical question and responds with an explanation"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ac41ae00",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# imports \n",
|
|
"\n",
|
|
"from openai import OpenAI"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c9727896",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "8e2ed70e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"MODEL_LLAMA = 'llama3.2'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ae31ec03",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# here is the question; type over this to ask something new\n",
|
|
"\n",
|
|
"question = \"\"\"\n",
|
|
"Please explain what this code does and why:\n",
|
|
"yield from {book.get(\"author\") for book in books if book.get(\"author\")}\n",
|
|
"\"\"\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "918bc133",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"system_prompt = \"\"\"\n",
|
|
"You are an expert software engineer.\n",
|
|
"You are given a technical question and you need to explain what the code does and why.\n",
|
|
"\"\"\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c9bbdcb8",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Get Llama 3.2 to answer\n",
|
|
"from IPython.display import Markdown, update_display\n",
|
|
"\n",
|
|
"\n",
|
|
"stream = openai.chat.completions.create(\n",
|
|
" model=MODEL_LLAMA,\n",
|
|
" messages=[\n",
|
|
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
|
" {\"role\": \"user\", \"content\": question}\n",
|
|
" ],\n",
|
|
" stream=True\n",
|
|
")\n",
|
|
"response = \"\"\n",
|
|
"display_handle = display(Markdown(\"\"), display_id=True)\n",
|
|
"for chunk in stream:\n",
|
|
" response += chunk.choices[0].delta.content or ''\n",
|
|
" update_display(Markdown(response), display_id=display_handle.display_id)\n"
|
|
]
|
|
}
|
|
],
|
|
"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.10"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|