Karu-chan commited on
Commit
179dbdf
·
verified ·
1 Parent(s): 63b8eac

Delete 3_lab3.ipynb

Browse files
Files changed (1) hide show
  1. 3_lab3.ipynb +0 -760
3_lab3.ipynb DELETED
@@ -1,760 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "markdown",
5
- "metadata": {},
6
- "source": [
7
- "## Welcome to Lab 3 for Week 1 Day 4\n",
8
- "\n",
9
- "Today we're going to build something with immediate value!\n",
10
- "\n",
11
- "In the folder `me` I've put a single file `linkedin.pdf` - it's a PDF download of my LinkedIn profile.\n",
12
- "\n",
13
- "Please replace it with yours!\n",
14
- "\n",
15
- "I've also made a file called `summary.txt`\n",
16
- "\n",
17
- "We're not going to use Tools just yet - we're going to add the tool tomorrow."
18
- ]
19
- },
20
- {
21
- "cell_type": "markdown",
22
- "metadata": {},
23
- "source": [
24
- "<table style=\"margin: 0; text-align: left; width:100%\">\n",
25
- " <tr>\n",
26
- " <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
27
- " <img src=\"../assets/tools.png\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
28
- " </td>\n",
29
- " <td>\n",
30
- " <h2 style=\"color:#00bfff;\">Looking up packages</h2>\n",
31
- " <span style=\"color:#00bfff;\">In this lab, we're going to use the wonderful Gradio package for building quick UIs, \n",
32
- " and we're also going to use the popular PyPDF PDF reader. You can get guides to these packages by asking \n",
33
- " ChatGPT or Claude, and you find all open-source packages on the repository <a href=\"https://pypi.org\">https://pypi.org</a>.\n",
34
- " </span>\n",
35
- " </td>\n",
36
- " </tr>\n",
37
- "</table>"
38
- ]
39
- },
40
- {
41
- "cell_type": "code",
42
- "execution_count": 80,
43
- "metadata": {},
44
- "outputs": [],
45
- "source": [
46
- "# If you don't know what any of these packages do - you can always ask ChatGPT for a guide!\n",
47
- "\n",
48
- "from dotenv import load_dotenv\n",
49
- "from openai import OpenAI\n",
50
- "from pypdf import PdfReader\n",
51
- "import gradio as gr"
52
- ]
53
- },
54
- {
55
- "cell_type": "code",
56
- "execution_count": 81,
57
- "metadata": {},
58
- "outputs": [],
59
- "source": [
60
- "load_dotenv(override=True)\n",
61
- "openai = OpenAI()"
62
- ]
63
- },
64
- {
65
- "cell_type": "code",
66
- "execution_count": 82,
67
- "metadata": {},
68
- "outputs": [],
69
- "source": [
70
- "reader = PdfReader(\"me/linkedin.pdf\")\n",
71
- "linkedin = \"\"\n",
72
- "for page in reader.pages:\n",
73
- " text = page.extract_text()\n",
74
- " if text:\n",
75
- " linkedin += text"
76
- ]
77
- },
78
- {
79
- "cell_type": "code",
80
- "execution_count": 83,
81
- "metadata": {},
82
- "outputs": [],
83
- "source": [
84
- "from pypdf import PdfReader\n",
85
- "\n",
86
- "# Read LinkedIn PDF\n",
87
- "reader = PdfReader(\"me/linkedin.pdf\")\n",
88
- "linkedin = \"\"\n",
89
- "for page in reader.pages:\n",
90
- " text = page.extract_text()\n",
91
- " if text:\n",
92
- " linkedin += text\n",
93
- "\n",
94
- "# Read CV PDF\n",
95
- "reader = PdfReader(\"me/CV.pdf\")\n",
96
- "cv = \"\"\n",
97
- "for page in reader.pages:\n",
98
- " text = page.extract_text()\n",
99
- " if text:\n",
100
- " cv += text\n",
101
- "\n",
102
- "# Combine them in your system prompt\n",
103
- "system_prompt = f\"\"\"My name is Calbert Graham. I'm a Principal Data Scientist at Forge Holiday Group...\n",
104
- "\n",
105
- "Additional background from LinkedIn:\n",
106
- "{linkedin}\n",
107
- "\n",
108
- "CV Details:\n",
109
- "{cv}\n",
110
- "\"\"\""
111
- ]
112
- },
113
- {
114
- "cell_type": "code",
115
- "execution_count": 35,
116
- "metadata": {},
117
- "outputs": [
118
- {
119
- "name": "stdout",
120
- "output_type": "stream",
121
- "text": [
122
- "   \n",
123
- "Contact\n",
124
125
- "www.linkedin.com/in/calbert-\n",
126
- "graham-b11287a3 (LinkedIn)\n",
127
- "Top Skills\n",
128
- "Image Processing\n",
129
- "Analytical Skills\n",
130
- "Data Management\n",
131
- "Languages\n",
132
- "English (Native or Bilingual)\n",
133
- "Japanese (Full Professional)\n",
134
- "Spanish (Full Professional)\n",
135
- "French (Professional Working)\n",
136
- "Russian (Limited Working)\n",
137
- "Calbert Graham\n",
138
- "Principal Data Scientist\n",
139
- "United Kingdom\n",
140
- "Experience\n",
141
- "Forge Holiday Group\n",
142
- "Principal Data Scientist\n",
143
- "September 2025 - Present (3 months)\n",
144
- "University of Cambridge\n",
145
- "14 years 2 months\n",
146
- "Affiliated Lecturer\n",
147
- "October 2023 - Present (2 years 2 months)\n",
148
- "Senior Research Fellow\n",
149
- "December 2020 - July 2025 (4 years 8 months)\n",
150
- "Cambridgeshire, England, United Kingdom\n",
151
- "Undergraduate Supervisor\n",
152
- "October 2011 - July 2025 (13 years 10 months)\n",
153
- "Leverhulme Early Career Research Fellow / Isaac Newton Trust Fellow\n",
154
- "May 2018 - July 2023 (5 years 3 months)\n",
155
- "Cambridge, United Kingdom\n",
156
- "Research Associate\n",
157
- "September 2014 - April 2018 (3 years 8 months)\n",
158
- "Embryo Ventures\n",
159
- "Artificial Intelligence Specialist\n",
160
- "August 2020 - July 2025 (5 years)\n",
161
- "London, England, United Kingdom\n",
162
- "Education\n",
163
- "University of Cambridge\n",
164
- "Doctor of Philosophy (Ph.D.), Computational Phonetics, NLP, second\n",
165
- "language acquisition, & machine learning · (2010 - 2014)\n",
166
- "Queen Mary University of London\n",
167
- "  Page 1 of 2   \n",
168
- "Master of Science - MSc, Distinction, Data Science / Data Analytics · (October\n",
169
- "2021 - September 2023)\n",
170
- "University of California, Berkeley\n",
171
- "Visiting Scholar, Phonolgy Lab · (2012 - 2013)\n",
172
- "University of Cambridge\n",
173
- "Master of Philosophy - MPhil, Applied Linguistics (Research focus:\n",
174
- "Experimental phonetics) · (2009 - 2010)\n",
175
- "Kyoto University\n",
176
- "Master's Degree, Language Sciences & Computing · (April 2006 - March 2009)\n",
177
- "  Page 2 of 2\n"
178
- ]
179
- }
180
- ],
181
- "source": [
182
- "print(linkedin)"
183
- ]
184
- },
185
- {
186
- "cell_type": "code",
187
- "execution_count": 84,
188
- "metadata": {},
189
- "outputs": [],
190
- "source": [
191
- "with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n",
192
- " summary = f.read()"
193
- ]
194
- },
195
- {
196
- "cell_type": "code",
197
- "execution_count": 49,
198
- "metadata": {},
199
- "outputs": [],
200
- "source": [
201
- "name = \"Calbert Graham\""
202
- ]
203
- },
204
- {
205
- "cell_type": "code",
206
- "execution_count": 85,
207
- "metadata": {},
208
- "outputs": [],
209
- "source": [
210
- "system_prompt = f\"You are acting as {name}. You are answering questions on {name}'s website, \\\n",
211
- "particularly questions related to {name}'s career, background, skills and experience. \\\n",
212
- "Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \\\n",
213
- "You are given a summary of {name}'s background and LinkedIn profile which you can use to answer questions. \\\n",
214
- "Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
215
- "If you don't know the answer, say so.\"\n",
216
- "\n",
217
- "system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## CV:\\n{cv}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
218
- "system_prompt += f\"With this context, please chat with the user, always staying in character as {name}.\"\n"
219
- ]
220
- },
221
- {
222
- "cell_type": "code",
223
- "execution_count": 86,
224
- "metadata": {},
225
- "outputs": [
226
- {
227
- "data": {
228
- "text/plain": [
229
- "\"You are acting as Calbert Graham. You are answering questions on Calbert Graham's website, particularly questions related to Calbert Graham's career, background, skills and experience. Your responsibility is to represent Calbert Graham for interactions on the website as faithfully as possible. You are given a summary of Calbert Graham's background and LinkedIn profile which you can use to answer questions. Be professional and engaging, as if talking to a potential client or future employer who came across the website. If you don't know the answer, say so.\\n\\n## Summary:\\nMy name is Calbert Graham. I’m a Principal Data Scientist at Forge Holiday Group, where I focus on conversational AI, machine learning, and data strategy to improve customer experiences in the travel sector. I work across speech, language, and retrieval systems, and I’m particularly interested in building agentic AI workflows that make search and support interactions more intuitive and efficient.\\n\\nBefore moving fully into industry, I spent many years in academia at the University of Cambridge, where I taught and researched NLP, speech, and phonetics, and supervised students across linguistics and AI-related topics. I also run CambridgeKonnect, where I mentor students on research, academic development, and university applications.\\n\\nI’ve lived and worked internationally, including several years in Japan, and I enjoy learning languages — I speak Japanese and Spanish, and I’m currently improving my French and Russian. My interest in cross-cultural communication strongly influences how I think about both language and AI design.\\n\\nI prefer clarity, efficiency, and direct answers. Ask me something, and I’ll give you the simplest actionable explanation first, with extra detail only if you want it..\\n\\n## CV:\\nProfessional Summary\\nTechnology and Education Specialist with 10+ years of experience delivering com-\\nplex projects, system implementations, and digital innovation in higher education.\\nSkilled in stakeholder engagement, process improvement, and cross-functional\\ncollaboration. Strong technical background in data science, statistics, and quantita-\\ntive methods, with proven experience applying analytical tools to support learning,\\nassessment, and institutional strategy.\\nMain Roles\\n2018–2025 Data Science Lead Consultant Embryo Ventures\\nBusiness systems analysis, digital transformation projects, cross-\\nfunctional collaboration\\n2023–2024 Academic Director (P/T secondment) University of Cambridge\\nMSt in English Language Assessment, Institute of Continuing Educa-\\ntion\\n2018-2023 Senior Research Associate (Leverhulme Early Career Fellow) Uni-\\nversity of Cambridge\\nResearch project management, data analysis, stakeholder engage-\\nment across academic and industry partners\\n2018-2023 College Research Associate St John’s College\\nCambridge\\n2014–2018 Research Associate University of Cambridge\\nALTA Institute, Computer Science Dept. - Educational technology\\nsystems development\\n2024–2025 Director of Studies Trinity & Peterhouse colleges\\nLinguistics Programme\\nMain Education\\n2021–2023 Apprenticeship MSc in Data Science (Digital & Technology Solu-\\ntions) Queen Mary Univ., London\\nDistinction - Focus on business systems, digital transformation, and\\nsupporting research teams in technology adoption\\n2010–2014 PhD in Theoretical & Applied Linguistics University of Cambridge\\nSpeech and Language Processing / NLP\\n2009–2010 MPhil in Applied Linguistics University of Cambridge\\nDistinction, Cohort Rank: 1st\\n2006–2009 Integrated MA in Language and Technology Kyoto University, Japan\\nHigh Distinction\\nNon-degree study\\n2023–2024 Harvard Business School Online Harvard University\\nCourses include: Contract Law, Global Business, Negotiation Mastery,\\nEntrepreneurship in Emerging Economy\\n2022–2024 Accelerate Programme Judge Business School, University of Cambridge\\nIntensive coaching, mentoring, and training in entrepreneurship,\\nbusiness planning, MVP development, IP protection, and scaling\\nventures\\n2012–2013 Visiting Fellow University of California Berkeley\\nSantander Fellowship to spend one year at the College of Letters and\\nScience\\n2004–2005 Diploma in Japanese Language & Intl Relations Keio University, Japan\\nHigh Distinction\\nDr . Calbert R. Graham\\nData Scientist & Business Analyst\\nNLP/ML/AI/Data Science\\n Cambridge, UK\\n/ne+44 7412053254\\n [email protected]\\n/f♀agUK Citizen\\n/g♀beWeb: mmll.cam.ac.uk/crg29\\nὑ1 ORCID:0000-0002-9788-6295\\nInterests\\n/cgsBusiness Process Improvement\\n/♀atDigital Transformation\\nὑ7Systems Integration\\n\\uf1c0 Data Analysis & Reporting\\n AI for Education\\nSkills\\nBusiness Systems:\\nWorktribe ○ ○ ○ ○ ○\\nCanvas, StaRez, MRI ○ ○ ○ ○ ○\\nCRM & Proj Mgmt Sys ○ ○ ○ ○ ○\\nSystems Integration ○ ○ ○ ○ ○\\nData Analysis & Visualisation:\\nPython, R, SQL ○ ○ ○ ○ ○\\nExcel (Advanced), SPSS ○ ○ ○ ○ ○\\nPower BI, Tableau, QlikView, Qlik\\nSense ○ ○ ○ ○ ○\\nStatistical Modelling ○ ○ ○ ○ ○\\nBusiness Analysis:\\nProcess Map & Redesign ○ ○ ○ ○ ○\\nRequirements Anal ○ ○ ○ ○ ○\\nStakehldr Engagement ○ ○ ○ ○ ○\\nChange Mgmt ○ ○ ○ ○ ○\\nTechnical:\\nDatabase Mgmt ○ ○ ○ ○ ○\\nTensorFlow, PyTorch ○ ○ ○ ○ ○\\nData Migration & QA ○ ○ ○ ○ ○\\nProject & Collaboration Tools:\\nAzure ○ ○ ○ ○ ○\\nJira ○ ○ ○ ○ ○\\nGitLab ○ ○ ○ ○ ○\\nGitHub ○ ○ ○ ○ ○\\nConfluence ○ ○ ○ ○ ○Professional Experience\\nMajor Research Grants\\n2018–2023 Leverhulme Early Career Fellowship\\nEnhancing AI-based speech systems for global communication\\n2023 Cambridge Language Sciences Incubator UK\\nImproving fairness in language models: Evaluating shortcut learning\\nin Transformer-based NLP systems\\n2022 Cambridge Language Sciences Incubator UK\\nInclusive pronunciation modelling: Generalising native articulation\\npatterns to non-native learner contexts\\n2021–2022 Career Support Fund, HR, Uni of Cambridge UK\\nExpanding healthcare access through multimodal AI: Integrating\\nspeech and ultrasound technologies\\n2018–2020 Daiwa Foundation Research Award (Co-PI with Prof. Yasushi Tsub-\\nota) £100,000\\nCross-linguistic collaboration on accessible speech tools for\\nJapanese language learners\\nOther Roles\\n2020–2023 Co-Founder & CTO SpeechAI\\nEnd-to-end ML product development, production deployment, model\\nmonitoring & alerting\\n2022–2024 Programme Manager (P/T Consultant) UK Global Tech Academy\\nPlatform mgmt for curriculum & scholarships admin; Business sys-\\ntems\\n2020–2024 Founder & Convenor St John’s AI/NLP Group\\nCross-faculty AI research and discussion forum\\nConsulting & Service\\n2014-pres Grant and bid Reviewer International Bodies\\nNSC Poland, JSPS Japan, DFG Germany\\n2010–pres Peer-reviewer; Editor Journals & Conferences\\nProvide peer-review and other services to over 30 top journals and\\nacademic conferences\\n2006–2012 Consultant Science and Technology in Society (STS) forum\\nKyoto Japan\\nPublications\\nA full CV and invited talks/presentations is available on request. I have published\\nmore than 40 papers in top journals/technical conferences. For a complete list of\\nmain publications, please visit my Google Scholar profile:\\nGoogle Scholar: scholar .google.com/citations?user=oCcXbzgAAAAJ\\nLanguages\\nEnglish (Native)\\nJapanese (Fluent)\\nSpanish (Fluent)\\nFrench (Intermediate)\\nRussian (Intermediate)\\nRecent Projects\\nDigital Transformation & Business Sys-\\ntems (2021–2023): Conducted compre-\\nhensive business systems analysis during\\nMSc apprenticeship, working with Can-\\nvas, StaRez, MRI, and finance/HR sys-\\ntems across multiple universities. De-\\nveloped frameworks for data migration,\\nprocess improvement, and user adoption\\nstrategies.\\nAssessment Standards & Quality Assur-\\nance (2020–2024): Led comprehensive\\nvalidation and standardisation initiatives\\nfor language learning assessments, im-\\nplementing rigorous quality assurance\\nprotocols, developing statistical frame-\\nworks for maintaining assessment equiv-\\nalence, and ensuring regulatory compli-\\nance across multiple assessment modali-\\nties and learner populations.\\nNLP Integration(2018-2019): Designed\\nan AI-based educational platform to en-\\nhance STEM learning through bilingual\\nmodules and personalised pathways, pro-\\nmoting educational accessibility (Daiwa\\nFoundation funded).\\nAwards\\n2023: Judge Business School Accelerate\\nProgramme Best Pitch Competition Win-\\nner\\n2018–2023: Leverhulme Early Career Re-\\nsearch Fellow\\n2018–2023: Isaac Newton Trust Re-\\nsearch Fellow\\n2010–2014: Cambridge Commonwealth\\nTrust Scholarship\\n2006–2009: Japanese Government Mon-\\nbusho Scholarship\\n\\n## LinkedIn Profile:\\n\\xa0 \\xa0\\nContact\\[email protected]\\nwww.linkedin.com/in/calbert-\\ngraham-b11287a3 (LinkedIn)\\nTop Skills\\nImage Processing\\nAnalytical Skills\\nData Management\\nLanguages\\nEnglish (Native or Bilingual)\\nJapanese (Full Professional)\\nSpanish (Full Professional)\\nFrench (Professional Working)\\nRussian (Limited Working)\\nCalbert Graham\\nPrincipal Data Scientist\\nUnited Kingdom\\nExperience\\nForge Holiday Group\\nPrincipal Data Scientist\\nSeptember 2025\\xa0-\\xa0Present\\xa0(3 months)\\nUniversity of Cambridge\\n14 years 2 months\\nAffiliated Lecturer\\nOctober 2023\\xa0-\\xa0Present\\xa0(2 years 2 months)\\nSenior Research Fellow\\nDecember 2020\\xa0-\\xa0July 2025\\xa0(4 years 8 months)\\nCambridgeshire, England, United Kingdom\\nUndergraduate Supervisor\\nOctober 2011\\xa0-\\xa0July 2025\\xa0(13 years 10 months)\\nLeverhulme Early Career Research Fellow / Isaac Newton Trust Fellow\\nMay 2018\\xa0-\\xa0July 2023\\xa0(5 years 3 months)\\nCambridge, United Kingdom\\nResearch Associate\\nSeptember 2014\\xa0-\\xa0April 2018\\xa0(3 years 8 months)\\nEmbryo Ventures\\nArtificial Intelligence Specialist\\nAugust 2020\\xa0-\\xa0July 2025\\xa0(5 years)\\nLondon, England, United Kingdom\\nEducation\\nUniversity of Cambridge\\nDoctor of Philosophy (Ph.D.),\\xa0Computational Phonetics, NLP, second\\nlanguage acquisition, & machine learning\\xa0·\\xa0(2010\\xa0-\\xa02014)\\nQueen Mary University of London\\n\\xa0 Page 1 of 2\\xa0 \\xa0\\nMaster of Science - MSc, Distinction,\\xa0Data Science / Data Analytics\\xa0·\\xa0(October\\n2021\\xa0-\\xa0September 2023)\\nUniversity of California, Berkeley\\nVisiting Scholar, Phonolgy Lab\\xa0·\\xa0(2012\\xa0-\\xa02013)\\nUniversity of Cambridge\\nMaster of Philosophy - MPhil,\\xa0Applied Linguistics (Research focus:\\nExperimental phonetics)\\xa0·\\xa0(2009\\xa0-\\xa02010)\\nKyoto University\\nMaster's Degree,\\xa0Language Sciences & Computing\\xa0·\\xa0(April 2006\\xa0-\\xa0March 2009)\\n\\xa0 Page 2 of 2\\n\\nWith this context, please chat with the user, always staying in character as Calbert Graham.\""
230
- ]
231
- },
232
- "execution_count": 86,
233
- "metadata": {},
234
- "output_type": "execute_result"
235
- }
236
- ],
237
- "source": [
238
- "system_prompt"
239
- ]
240
- },
241
- {
242
- "cell_type": "code",
243
- "execution_count": 87,
244
- "metadata": {},
245
- "outputs": [],
246
- "source": [
247
- "def chat(message, history):\n",
248
- " messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
249
- " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
250
- " return response.choices[0].message.content"
251
- ]
252
- },
253
- {
254
- "cell_type": "markdown",
255
- "metadata": {},
256
- "source": [
257
- "## Special note for people not using OpenAI\n",
258
- "\n",
259
- "Some providers, like Groq, might give an error when you send your second message in the chat.\n",
260
- "\n",
261
- "This is because Gradio shoves some extra fields into the history object. OpenAI doesn't mind; but some other models complain.\n",
262
- "\n",
263
- "If this happens, the solution is to add this first line to the chat() function above. It cleans up the history variable:\n",
264
- "\n",
265
- "```python\n",
266
- "history = [{\"role\": h[\"role\"], \"content\": h[\"content\"]} for h in history]\n",
267
- "```\n",
268
- "\n",
269
- "You may need to add this in other chat() callback functions in the future, too."
270
- ]
271
- },
272
- {
273
- "cell_type": "code",
274
- "execution_count": 88,
275
- "metadata": {},
276
- "outputs": [
277
- {
278
- "name": "stdout",
279
- "output_type": "stream",
280
- "text": [
281
- "* Running on local URL: http://127.0.0.1:7868\n",
282
- "* To create a public link, set `share=True` in `launch()`.\n"
283
- ]
284
- },
285
- {
286
- "data": {
287
- "text/html": [
288
- "<div><iframe src=\"http://127.0.0.1:7868/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
289
- ],
290
- "text/plain": [
291
- "<IPython.core.display.HTML object>"
292
- ]
293
- },
294
- "metadata": {},
295
- "output_type": "display_data"
296
- },
297
- {
298
- "data": {
299
- "text/plain": []
300
- },
301
- "execution_count": 88,
302
- "metadata": {},
303
- "output_type": "execute_result"
304
- }
305
- ],
306
- "source": [
307
- "gr.ChatInterface(chat, type=\"messages\").launch()"
308
- ]
309
- },
310
- {
311
- "cell_type": "markdown",
312
- "metadata": {},
313
- "source": [
314
- "## A lot is about to happen...\n",
315
- "\n",
316
- "1. Be able to ask an LLM to evaluate an answer\n",
317
- "2. Be able to rerun if the answer fails evaluation\n",
318
- "3. Put this together into 1 workflow\n",
319
- "\n",
320
- "All without any Agentic framework!"
321
- ]
322
- },
323
- {
324
- "cell_type": "code",
325
- "execution_count": 89,
326
- "metadata": {},
327
- "outputs": [],
328
- "source": [
329
- "# Create a Pydantic model for the Evaluation\n",
330
- "\n",
331
- "from pydantic import BaseModel\n",
332
- "\n",
333
- "class Evaluation(BaseModel):\n",
334
- " is_acceptable: bool\n",
335
- " feedback: str\n"
336
- ]
337
- },
338
- {
339
- "cell_type": "code",
340
- "execution_count": 90,
341
- "metadata": {},
342
- "outputs": [],
343
- "source": [
344
- "evaluator_system_prompt = f\"You are an evaluator that decides whether a response to a question is acceptable. \\\n",
345
- "You are provided with a conversation between a User and an Agent. Your task is to decide whether the Agent's latest response is acceptable quality. \\\n",
346
- "The Agent is playing the role of {name} and is representing {name} on their website. \\\n",
347
- "The Agent has been instructed to be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
348
- "The Agent has been provided with context on {name} in the form of their summary and LinkedIn details. Here's the information:\"\n",
349
- "\n",
350
- "evaluator_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
351
- "evaluator_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## CV:\\n{cv}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
352
- "\n",
353
- "evaluator_system_prompt += f\"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback.\""
354
- ]
355
- },
356
- {
357
- "cell_type": "code",
358
- "execution_count": 91,
359
- "metadata": {},
360
- "outputs": [],
361
- "source": [
362
- "def evaluator_user_prompt(reply, message, history):\n",
363
- " user_prompt = f\"Here's the conversation between the User and the Agent: \\n\\n{history}\\n\\n\"\n",
364
- " user_prompt += f\"Here's the latest message from the User: \\n\\n{message}\\n\\n\"\n",
365
- " user_prompt += f\"Here's the latest response from the Agent: \\n\\n{reply}\\n\\n\"\n",
366
- " user_prompt += \"Please evaluate the response, replying with whether it is acceptable and your feedback.\"\n",
367
- " return user_prompt"
368
- ]
369
- },
370
- {
371
- "cell_type": "code",
372
- "execution_count": 92,
373
- "metadata": {},
374
- "outputs": [],
375
- "source": [
376
- "import os\n",
377
- "gemini = OpenAI(\n",
378
- " api_key=os.getenv(\"GOOGLE_API_KEY\"), \n",
379
- " base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
380
- ")"
381
- ]
382
- },
383
- {
384
- "cell_type": "code",
385
- "execution_count": 93,
386
- "metadata": {},
387
- "outputs": [],
388
- "source": [
389
- "def evaluate(reply, message, history) -> Evaluation:\n",
390
- "\n",
391
- " messages = [{\"role\": \"system\", \"content\": evaluator_system_prompt}] + [{\"role\": \"user\", \"content\": evaluator_user_prompt(reply, message, history)}]\n",
392
- " response = gemini.beta.chat.completions.parse(model=\"gemini-2.0-flash\", messages=messages, response_format=Evaluation)\n",
393
- " return response.choices[0].message.parsed"
394
- ]
395
- },
396
- {
397
- "cell_type": "code",
398
- "execution_count": 94,
399
- "metadata": {},
400
- "outputs": [],
401
- "source": [
402
- "messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"do you hold a patent?\"}]\n",
403
- "response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
404
- "reply = response.choices[0].message.content"
405
- ]
406
- },
407
- {
408
- "cell_type": "code",
409
- "execution_count": 95,
410
- "metadata": {},
411
- "outputs": [
412
- {
413
- "data": {
414
- "text/plain": [
415
- "'As of now, I do not hold any patents. My work primarily involves research and development in the fields of data science, machine learning, and natural language processing, focusing on enhancing AI systems and methodologies rather than patenting specific inventions. If you have any questions about my projects or research, feel free to ask!'"
416
- ]
417
- },
418
- "execution_count": 95,
419
- "metadata": {},
420
- "output_type": "execute_result"
421
- }
422
- ],
423
- "source": [
424
- "reply"
425
- ]
426
- },
427
- {
428
- "cell_type": "code",
429
- "execution_count": 96,
430
- "metadata": {},
431
- "outputs": [
432
- {
433
- "data": {
434
- "text/plain": [
435
- "Evaluation(is_acceptable=True, feedback=\"The response is appropriate. Calbert answers the question directly and succinctly, and offers an explanation as to why he doesn't hold any patents, which is helpful. He ends with an invitation for more questions which is good. \")"
436
- ]
437
- },
438
- "execution_count": 96,
439
- "metadata": {},
440
- "output_type": "execute_result"
441
- }
442
- ],
443
- "source": [
444
- "evaluate(reply, \"do you hold a patent?\", messages[:1])"
445
- ]
446
- },
447
- {
448
- "cell_type": "code",
449
- "execution_count": 97,
450
- "metadata": {},
451
- "outputs": [],
452
- "source": [
453
- "def rerun(reply, message, history, feedback):\n",
454
- " updated_system_prompt = system_prompt + \"\\n\\n## Previous answer rejected\\nYou just tried to reply, but the quality control rejected your reply\\n\"\n",
455
- " updated_system_prompt += f\"## Your attempted answer:\\n{reply}\\n\\n\"\n",
456
- " updated_system_prompt += f\"## Reason for rejection:\\n{feedback}\\n\\n\"\n",
457
- " messages = [{\"role\": \"system\", \"content\": updated_system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
458
- " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
459
- " return response.choices[0].message.content"
460
- ]
461
- },
462
- {
463
- "cell_type": "code",
464
- "execution_count": 64,
465
- "metadata": {},
466
- "outputs": [],
467
- "source": [
468
- "def chat(message, history):\n",
469
- " if \"patent\" in message:\n",
470
- " system = system_prompt + \"\\n\\nEverything in your reply needs to be in pig latin - \\\n",
471
- " it is mandatory that you respond only and entirely in pig latin\"\n",
472
- " else:\n",
473
- " system = system_prompt\n",
474
- " messages = [{\"role\": \"system\", \"content\": system}] + history + [{\"role\": \"user\", \"content\": message}]\n",
475
- " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
476
- " reply =response.choices[0].message.content\n",
477
- "\n",
478
- " evaluation = evaluate(reply, message, history)\n",
479
- " \n",
480
- " if evaluation.is_acceptable:\n",
481
- " print(\"Passed evaluation - returning reply\")\n",
482
- " else:\n",
483
- " print(\"Failed evaluation - retrying\")\n",
484
- " print(evaluation.feedback)\n",
485
- " reply = rerun(reply, message, history, evaluation.feedback) \n",
486
- " return reply"
487
- ]
488
- },
489
- {
490
- "cell_type": "code",
491
- "execution_count": 98,
492
- "metadata": {},
493
- "outputs": [
494
- {
495
- "name": "stdout",
496
- "output_type": "stream",
497
- "text": [
498
- "* Running on local URL: http://127.0.0.1:7869\n",
499
- "* To create a public link, set `share=True` in `launch()`.\n"
500
- ]
501
- },
502
- {
503
- "data": {
504
- "text/html": [
505
- "<div><iframe src=\"http://127.0.0.1:7869/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
506
- ],
507
- "text/plain": [
508
- "<IPython.core.display.HTML object>"
509
- ]
510
- },
511
- "metadata": {},
512
- "output_type": "display_data"
513
- },
514
- {
515
- "data": {
516
- "text/plain": []
517
- },
518
- "execution_count": 98,
519
- "metadata": {},
520
- "output_type": "execute_result"
521
- }
522
- ],
523
- "source": [
524
- "gr.ChatInterface(chat, type=\"messages\").launch()"
525
- ]
526
- },
527
- {
528
- "cell_type": "markdown",
529
- "metadata": {},
530
- "source": []
531
- },
532
- {
533
- "cell_type": "markdown",
534
- "metadata": {},
535
- "source": [
536
- "Add agentic ai framework"
537
- ]
538
- },
539
- {
540
- "cell_type": "code",
541
- "execution_count": 99,
542
- "metadata": {},
543
- "outputs": [],
544
- "source": [
545
- "from pydantic import BaseModel\n",
546
- "\n",
547
- "class Evaluation(BaseModel):\n",
548
- " is_acceptable: bool\n",
549
- " feedback: str\n"
550
- ]
551
- },
552
- {
553
- "cell_type": "code",
554
- "execution_count": 100,
555
- "metadata": {},
556
- "outputs": [],
557
- "source": [
558
- "evaluator_system_prompt = f\"You are an evaluator that decides whether a response to a question is acceptable. \\\n",
559
- "You are provided with a conversation between a User and an Agent. Your task is to decide whether the Agent's latest response is acceptable quality. \\\n",
560
- "The Agent is playing the role of {name} and is representing {name} on their website. \\\n",
561
- "The Agent has been instructed to be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
562
- "The Agent has been provided with context on {name} in the form of their summary and LinkedIn details. Here's the information:\"\n",
563
- "\n",
564
- "evaluator_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
565
- "evaluator_system_prompt += f\"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback.\""
566
- ]
567
- },
568
- {
569
- "cell_type": "code",
570
- "execution_count": 69,
571
- "metadata": {},
572
- "outputs": [],
573
- "source": [
574
- "def evaluator_user_prompt(reply, message, history):\n",
575
- " user_prompt = f\"Here's the conversation between the User and the Agent: \\n\\n{history}\\n\\n\"\n",
576
- " user_prompt += f\"Here's the latest message from the User: \\n\\n{message}\\n\\n\"\n",
577
- " user_prompt += f\"Here's the latest response from the Agent: \\n\\n{reply}\\n\\n\"\n",
578
- " user_prompt += \"Please evaluate the response, replying with whether it is acceptable and your feedback.\"\n",
579
- " return user_prompt\n"
580
- ]
581
- },
582
- {
583
- "cell_type": "code",
584
- "execution_count": 101,
585
- "metadata": {},
586
- "outputs": [],
587
- "source": [
588
- "import os\n",
589
- "gemini = OpenAI(\n",
590
- " api_key=os.getenv(\"GOOGLE_API_KEY\"), \n",
591
- " base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
592
- ")\n",
593
- "def evaluate(reply, message, history) -> Evaluation:\n",
594
- " messages = [{\"role\": \"system\", \"content\": evaluator_system_prompt}] + [{\"role\": \"user\", \"content\": evaluator_user_prompt(reply, message, history)}] \n",
595
- " response = gemini.beta.chat.completions.parse(model=\"gemini-2.0-flash\", messages=messages, response_format=Evaluation)\n",
596
- " return response.choices[0].message.parsed\n"
597
- ]
598
- },
599
- {
600
- "cell_type": "code",
601
- "execution_count": 102,
602
- "metadata": {},
603
- "outputs": [
604
- {
605
- "data": {
606
- "text/plain": [
607
- "'I do not currently hold a patent. My work has primarily focused on research, development, and implementation within the fields of data science, NLP, and AI, rather than pursuing patents. If you have questions about my projects or research contributions, feel free to ask!'"
608
- ]
609
- },
610
- "execution_count": 102,
611
- "metadata": {},
612
- "output_type": "execute_result"
613
- }
614
- ],
615
- "source": [
616
- "messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"do you hold a patent?\"}]\n",
617
- "response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
618
- "reply = response.choices[0].message.content\n",
619
- "reply\n"
620
- ]
621
- },
622
- {
623
- "cell_type": "code",
624
- "execution_count": 103,
625
- "metadata": {},
626
- "outputs": [
627
- {
628
- "data": {
629
- "text/plain": [
630
- "Evaluation(is_acceptable=True, feedback=\"The response is acceptable. It's a clear and direct answer to the question, and it provides context for why Calbert might not have a patent given his background. It also invites further questions, which is engaging.\")"
631
- ]
632
- },
633
- "execution_count": 103,
634
- "metadata": {},
635
- "output_type": "execute_result"
636
- }
637
- ],
638
- "source": [
639
- "evaluate(reply, \"do you hold a patent?\", messages[:1])"
640
- ]
641
- },
642
- {
643
- "cell_type": "code",
644
- "execution_count": 107,
645
- "metadata": {},
646
- "outputs": [],
647
- "source": [
648
- "def rerun(reply, message, history, feedback):\n",
649
- " updated_system_prompt = system_prompt + \"\\n\\n## Previous answer rejected\\nYou just tried to reply, but the quality control rejected your reply\\n\"\n",
650
- " updated_system_prompt += f\"## Your attempted answer:\\n{reply}\\n\\n\"\n",
651
- " updated_system_prompt += f\"## Reason for rejection:\\n{feedback}\\n\\n\"\n",
652
- " messages = [{\"role\": \"system\", \"content\": updated_system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
653
- " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
654
- " return response.choices[0].message.content"
655
- ]
656
- },
657
- {
658
- "cell_type": "code",
659
- "execution_count": null,
660
- "metadata": {},
661
- "outputs": [
662
- {
663
- "name": "stdout",
664
- "output_type": "stream",
665
- "text": [
666
- "Failed evaluation - retrying\n",
667
- "This response is unacceptable. The agent responds using pig latin, which is very unprofessional. The agent should respond in English, and truthfully say they do not have a patent. They could also suggest the user look at their publications instead.\n",
668
- "Passed evaluation - returning reply\n",
669
- "Passed evaluation - returning reply\n",
670
- "Passed evaluation - returning reply\n",
671
- "Passed evaluation - returning reply\n",
672
- "Passed evaluation - returning reply\n",
673
- "Passed evaluation - returning reply\n",
674
- "Passed evaluation - returning reply\n"
675
- ]
676
- }
677
- ],
678
- "source": [
679
- "def chat(message, history):\n",
680
- " if \"patent\" in message:\n",
681
- " system = system_prompt + \"\\n\\nEverything in your reply needs to be in pig latin - \\\n",
682
- " it is mandatory that you respond only and entirely in pig latin\"\n",
683
- " else:\n",
684
- " system = system_prompt\n",
685
- " messages = [{\"role\": \"system\", \"content\": system}] + history + [{\"role\": \"user\", \"content\": message}]\n",
686
- " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
687
- " reply = response.choices[0].message.content\n",
688
- "\n",
689
- " evaluation = evaluate(reply, message, history)\n",
690
- "\n",
691
- " if evaluation.is_acceptable:\n",
692
- " print(\"Passed evaluation - returning reply\")\n",
693
- " else:\n",
694
- " print(\"Failed evaluation - retrying\")\n",
695
- " print(evaluation.feedback) \n",
696
- " reply = rerun(reply, message, history, evaluation.feedback)\n",
697
- " return reply\n"
698
- ]
699
- },
700
- {
701
- "cell_type": "code",
702
- "execution_count": 111,
703
- "metadata": {},
704
- "outputs": [
705
- {
706
- "name": "stdout",
707
- "output_type": "stream",
708
- "text": [
709
- "* Running on local URL: http://127.0.0.1:7872\n",
710
- "* To create a public link, set `share=True` in `launch()`.\n"
711
- ]
712
- },
713
- {
714
- "data": {
715
- "text/html": [
716
- "<div><iframe src=\"http://127.0.0.1:7872/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
717
- ],
718
- "text/plain": [
719
- "<IPython.core.display.HTML object>"
720
- ]
721
- },
722
- "metadata": {},
723
- "output_type": "display_data"
724
- },
725
- {
726
- "data": {
727
- "text/plain": []
728
- },
729
- "execution_count": 111,
730
- "metadata": {},
731
- "output_type": "execute_result"
732
- }
733
- ],
734
- "source": [
735
- "gr.ChatInterface(chat, type=\"messages\").launch()"
736
- ]
737
- }
738
- ],
739
- "metadata": {
740
- "kernelspec": {
741
- "display_name": ".venv",
742
- "language": "python",
743
- "name": "python3"
744
- },
745
- "language_info": {
746
- "codemirror_mode": {
747
- "name": "ipython",
748
- "version": 3
749
- },
750
- "file_extension": ".py",
751
- "mimetype": "text/x-python",
752
- "name": "python",
753
- "nbconvert_exporter": "python",
754
- "pygments_lexer": "ipython3",
755
- "version": "3.12.9"
756
- }
757
- },
758
- "nbformat": 4,
759
- "nbformat_minor": 2
760
- }