Added my contributions to community-contributions.
Add notebook to integrate Ollama API for email summarization and subject line generation
This commit is contained in:
@@ -0,0 +1,203 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"id": "initial_id",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true,
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-10-20T06:52:25.678220Z",
|
||||||
|
"start_time": "2025-10-20T06:52:25.676346Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"from dotenv import load_dotenv\n",
|
||||||
|
"from ollama import Client"
|
||||||
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-10-20T06:56:35.714971Z",
|
||||||
|
"start_time": "2025-10-20T06:56:35.709631Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"load_dotenv(override=True)\n",
|
||||||
|
"api_key = os.environ.get('OLLAMA_API_KEY')\n",
|
||||||
|
"\n",
|
||||||
|
"if not api_key:\n",
|
||||||
|
" print(\"No API key found\")\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"API key found\")"
|
||||||
|
],
|
||||||
|
"id": "843d8bdd7328b813",
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"API key found\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"execution_count": 14
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-10-20T06:56:39.109637Z",
|
||||||
|
"start_time": "2025-10-20T06:56:39.097743Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"client = Client(\n",
|
||||||
|
" host=\"https://ollama.com\",\n",
|
||||||
|
" headers={'Authorization': 'Bearer ' + os.environ.get('OLLAMA_API_KEY')}\n",
|
||||||
|
")"
|
||||||
|
],
|
||||||
|
"id": "756969af1d557267",
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 15
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-10-20T06:56:41.408064Z",
|
||||||
|
"start_time": "2025-10-20T06:56:41.405115Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"system_prompt = \"summarize the contents of the email and suggest a short subject line for the email\"\n",
|
||||||
|
"user_prompt = \"\"\"\n",
|
||||||
|
" Summarize the contents of the email and suggest a short subject line for the email.\n",
|
||||||
|
" Dear Alice,\n",
|
||||||
|
" I'm writing to you to tell you that I'm very happy to meet you.\n",
|
||||||
|
" I hope you're doing well.\n",
|
||||||
|
" I'm going to tell you a story.\n",
|
||||||
|
"\n",
|
||||||
|
" The story is about a boy who is a young boy. The skin color of the boy is green and he makes a lot of noise.\n",
|
||||||
|
" He is very clever and has a lot of friends.\n",
|
||||||
|
" He is very smart and has a lot of money.\n",
|
||||||
|
" He is very clever and has a lot of friends.\n",
|
||||||
|
" He is very smart and has a lot of money.\n",
|
||||||
|
"\n",
|
||||||
|
" The story ends here.\n",
|
||||||
|
"\n",
|
||||||
|
" Regards,\n",
|
||||||
|
" Bob\n",
|
||||||
|
"\"\"\""
|
||||||
|
],
|
||||||
|
"id": "79bb55b3876dc175",
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 16
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-10-20T06:56:43.415961Z",
|
||||||
|
"start_time": "2025-10-20T06:56:43.413136Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"messages = [\n",
|
||||||
|
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
||||||
|
" {\"role\": \"user\", \"content\": user_prompt}\n",
|
||||||
|
"]"
|
||||||
|
],
|
||||||
|
"id": "399c33459dc453b3",
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 17
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-10-20T06:56:44.772348Z",
|
||||||
|
"start_time": "2025-10-20T06:56:44.769695Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": "response = client.chat('gpt-oss:120b-cloud', messages=messages, stream=True)",
|
||||||
|
"id": "6a11ca4b60b7181f",
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 18
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-10-20T06:56:46.239943Z",
|
||||||
|
"start_time": "2025-10-20T06:56:46.236990Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": "print(response)",
|
||||||
|
"id": "b2b308eeddd525f",
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"<generator object Client._request.<locals>.inner at 0x1652ec500>\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"execution_count": 19
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-10-20T06:56:49.390850Z",
|
||||||
|
"start_time": "2025-10-20T06:56:47.846726Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"for part in response:\n",
|
||||||
|
" print(part['message']['content'], end='', flush=True)"
|
||||||
|
],
|
||||||
|
"id": "9037211f79d7a752",
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"**Summary:** \n",
|
||||||
|
"Bob writes to Alice to express his happiness about meeting her and hopes she is doing well. He then shares a brief, repetitive story about a young green-skinned boy who makes a lot of noise, is clever, has many friends, and is wealthy. The story ends there, and Bob signs off with his regards.\n",
|
||||||
|
"\n",
|
||||||
|
"**Suggested subject line:** \n",
|
||||||
|
"“Happy to Meet You – A Quick Story Inside”"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"execution_count": 20
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 2
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython2",
|
||||||
|
"version": "2.7.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user