File size: 7,720 Bytes
5fdb69e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "603cd418-504a-4b4d-b1c3-be04febf3e79",
   "metadata": {},
   "source": [
    "# Article Title Generator\n",
    "\n",
    "Summarization use-case in which the user provides an article, which the LLM will analyze to suggest an SEO-optimized title.\n",
    "\n",
    "**NOTES**:\n",
    "\n",
    "1. This version does NOT support website scrapping. You must copy and paste the required article.\n",
    "2. The following models were configured:\n",
    "    a. OpenAI gpt-4o-mini\n",
    "    b. Llama llama3.2\n",
    "    c. Deepseek deepseek-r1:1.5b\n",
    "   It is possible to configure additional models by adding the new model to the MODELS dictionary and its\n",
    "   initialization to the CLIENTS dictionary. Then, call the model with --> ***answer =\n",
    "   get_answer('NEW_MODEL')***.\n",
    "3. Users are encouraged to assess and rank the suggested titles using any headline analyzer tool online.\n",
    "   Example: https://www.isitwp.com/headline-analyzer/. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e773daa6-d05e-49bf-ad8e-a8ed4882b77e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Confirming Llama is loaded\n",
    "!ollama pull llama3.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "279b0c00-9bb0-4c7f-9c6d-aa0b108274b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# imports\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from IPython.display import Markdown, display\n",
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4730d8d-3e20-4f3c-a4ff-ed2ac0a8aa27",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set environment variables for OpenAi\n",
    "load_dotenv(override=True)\n",
    "api_key = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "# validate API Key\n",
    "if not api_key:\n",
    "    raise ValueError(\"No API key was found! Please check the .env file.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1abbb826-de66-498c-94d8-33369ad01885",
   "metadata": {},
   "outputs": [],
   "source": [
    "# constants\n",
    "MODELS = { 'GPT': 'gpt-4o-mini', \n",
    "          'LLAMA': 'llama3.2', \n",
    "          'DEEPSEEK': 'deepseek-r1:1.5b'\n",
    "         }\n",
    "\n",
    "CLIENTS = { 'GPT': OpenAI(), \n",
    "            'LLAMA': OpenAI(base_url='http://localhost:11434/v1', api_key='ollama'),\n",
    "            'DEEPSEEK': OpenAI(base_url='http://localhost:11434/v1', api_key='ollama') \n",
    "          }"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f490fe4-32d5-41f3-890d-ecf4e5e01dd4",
   "metadata": {},
   "source": [
    "### Copy & paste your article (without a title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddd76319-13ce-480b-baa7-cab6a5c88168",
   "metadata": {},
   "outputs": [],
   "source": [
    "# article - copy & paste your article\n",
    "article = \"\"\"\n",
    "            REPLACE WITH YOUR ARTICLE CONTENT\n",
    "          \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1914afad-dbd8-4c1f-8e68-80b0e5d743a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# system prompt\n",
    "system_prompt = \"\"\"\n",
    "    You are an experienced SEO-focused copywriter. The user will provide an article, and your task is to analyze its content and generate the most effective, keyword-optimized title to maximize SEO performance.Respond in Markdown format.\n",
    "    \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "176cfac7-5e6d-4d4a-a1c4-1b63b60de1f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# user prompt\n",
    "user_prompt = f\"Following the article to be analyzed. Respond in Markdown format./n/n{article}\"\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c45fc7d7-08c9-4e34-b427-b928a219bb94",
   "metadata": {},
   "outputs": [],
   "source": [
    "# message list\n",
    "messages = [\n",
    "            {\"role\": \"system\", \"content\": system_prompt},\n",
    "            {\"role\": \"user\", \"content\": user_prompt}\n",
    "           ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f67b881f-1040-4cf7-82c5-e85f4c0bd252",
   "metadata": {},
   "outputs": [],
   "source": [
    "# call model and get answer\n",
    "def get_answer(model):\n",
    "    # set required client\n",
    "    client = CLIENTS[model]\n",
    "\n",
    "    # call model\n",
    "    response = client.chat.completions.create(\n",
    "        model=MODELS[model],\n",
    "        messages=messages\n",
    "    )\n",
    "    \n",
    "    # return answer\n",
    "    return response.choices[0].message.content\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "947b42ed-5b43-486d-8af3-e5b671c1fd0e",
   "metadata": {},
   "source": [
    "### Get OpenAI Suggested Title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb6f66e3-ab99-4f76-9358-896cb43c1fa1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# get openAi answer\n",
    "answer = get_answer('GPT')\n",
    "\n",
    "# display openAi answer\n",
    "display(Markdown(f\"### {MODELS['GPT']} Answer\\n\\n{answer}\" ))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70073ebf-a00a-416b-854d-642d450cd99b",
   "metadata": {},
   "source": [
    "### Get Llama Suggested Title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "caa190bb-de5f-45cc-b671-5d62688f7b25",
   "metadata": {},
   "outputs": [],
   "source": [
    "# get Llama answer\n",
    "answer = get_answer('LLAMA')\n",
    "\n",
    "# display Llama answer\n",
    "display(Markdown(f\"### {MODELS['LLAMA']} Answer\\n\\n{answer}\" ))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "811edc4f-20e2-482d-ac89-fae9d1b70bed",
   "metadata": {},
   "source": [
    "### Get Deepseek Suggested Title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "082628e4-ff4c-46dd-ae5f-76578eb017ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "# get Deepseek answer\n",
    "answer = get_answer('DEEPSEEK')\n",
    "\n",
    "# display Deepseek answer\n",
    "display(Markdown(f\"### {MODELS['DEEPSEEK']} Answer\\n\\n{answer}\" ))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fc404a6-3a91-4c09-89de-867d3d69b4b2",
   "metadata": {},
   "source": [
    "### Suggested future improvements\n",
    "\n",
    "1. Add website scrapping support to replace copy/pasting of articles.\n",
    "2. Improve the system_prompt to provide specific SEO best practices to adopt during the title generation.\n",
    "3. Rephrase the system_prompt to ensure the model provides a single Title (not a list of suggestions). \n",
    "4. Add the logic that would allow each model to assess the recommendations from the different models and \n",
    "   select the best among these. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf7403ac-d43b-4493-98bb-6fee94950cb0",
   "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
}