Spaces:
Sleeping
Sleeping
Delete 3_lab3.ipynb
Browse files- 3_lab3.ipynb +0 -510
3_lab3.ipynb
DELETED
@@ -1,510 +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 PyPDF2 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": 1,
|
43 |
-
"metadata": {},
|
44 |
-
"outputs": [],
|
45 |
-
"source": [
|
46 |
-
"from dotenv import load_dotenv\n",
|
47 |
-
"from openai import OpenAI\n",
|
48 |
-
"from PyPDF2 import PdfReader\n",
|
49 |
-
"import gradio as gr\n"
|
50 |
-
]
|
51 |
-
},
|
52 |
-
{
|
53 |
-
"cell_type": "code",
|
54 |
-
"execution_count": 2,
|
55 |
-
"metadata": {},
|
56 |
-
"outputs": [],
|
57 |
-
"source": [
|
58 |
-
"load_dotenv(override=True)\n",
|
59 |
-
"openai = OpenAI()"
|
60 |
-
]
|
61 |
-
},
|
62 |
-
{
|
63 |
-
"cell_type": "code",
|
64 |
-
"execution_count": 3,
|
65 |
-
"metadata": {},
|
66 |
-
"outputs": [],
|
67 |
-
"source": [
|
68 |
-
"reader = PdfReader(\"me/Profile.pdf\")\n",
|
69 |
-
"linkedin = \"\"\n",
|
70 |
-
"for page in reader.pages:\n",
|
71 |
-
" text = page.extract_text()\n",
|
72 |
-
" if text:\n",
|
73 |
-
" linkedin += text"
|
74 |
-
]
|
75 |
-
},
|
76 |
-
{
|
77 |
-
"cell_type": "code",
|
78 |
-
"execution_count": 4,
|
79 |
-
"metadata": {},
|
80 |
-
"outputs": [
|
81 |
-
{
|
82 |
-
"name": "stdout",
|
83 |
-
"output_type": "stream",
|
84 |
-
"text": [
|
85 |
-
" \n",
|
86 |
-
"Contact\n",
|
87 |
-
"6182405966 (Mobile)\n",
|
88 |
-
"[email protected]\n",
|
89 |
-
"www.linkedin.com/in/jacob-a-\n",
|
90 |
-
"isaacson (LinkedIn)\n",
|
91 |
-
"Top Skills\n",
|
92 |
-
"Benchmarking\n",
|
93 |
-
"Communication\n",
|
94 |
-
"Critical Thinking\n",
|
95 |
-
"Certifications\n",
|
96 |
-
"Artificial Intelligence FundamentalsJacob Isaacson\n",
|
97 |
-
"Tireless and Committed Program Engineering Professional\n",
|
98 |
-
"specializing in making the impossible ; possible\n",
|
99 |
-
"Clinton Township, Michigan, United States\n",
|
100 |
-
"Summary\n",
|
101 |
-
"Utilizing hands on knowledge of testing as a foundation, I have\n",
|
102 |
-
"lent my expertise to design and management teams for mission\n",
|
103 |
-
"critical engineering projects. I am the “go-to” person for complex\n",
|
104 |
-
"program management of essential products and projects. I have the\n",
|
105 |
-
"ability to work in a seamless manner utilizing cross-functional teams\n",
|
106 |
-
"uncovering maximum potential of all team members. I excel as utility\n",
|
107 |
-
"player and am able to leverage team energy to help create unique\n",
|
108 |
-
"solutions. I am often the unofficial captain of teams, programs, and\n",
|
109 |
-
"projects. In my view all wins are team wins.\n",
|
110 |
-
"Experience\n",
|
111 |
-
"MotoRad of America\n",
|
112 |
-
"Program Engineering Manager\n",
|
113 |
-
"February 2020 - Present (5 years 5 months)\n",
|
114 |
-
"Stant\n",
|
115 |
-
"Aftermarket Product Engineer\n",
|
116 |
-
"April 2018 - February 2020 (1 year 11 months)\n",
|
117 |
-
"Hilite International\n",
|
118 |
-
"Test Engineer\n",
|
119 |
-
"May 2017 - April 2018 (1 year)\n",
|
120 |
-
"Lake Orion, Michigan\n",
|
121 |
-
"General Motors\n",
|
122 |
-
"Chassis validation engineer\n",
|
123 |
-
"October 2015 - May 2017 (1 year 8 months)\n",
|
124 |
-
"Fiat Chrysler Automobiles (FCA)\n",
|
125 |
-
"Durability Engineer\n",
|
126 |
-
"February 2015 - September 2015 (8 months)\n",
|
127 |
-
"Auburn Hills, Michigan\n",
|
128 |
-
" Page 1 of 2 \n",
|
129 |
-
"Precision auto research\n",
|
130 |
-
"Associate Project Engineer\n",
|
131 |
-
"March 2012 - January 2015 (2 years 11 months)\n",
|
132 |
-
"United States\n",
|
133 |
-
"Build and design data systems for auto racing. Work with drivers at track for\n",
|
134 |
-
"driver coaching. Data analysis and acquisition as well as support.\n",
|
135 |
-
"Education\n",
|
136 |
-
"Purdue University\n",
|
137 |
-
"Bachelor of Science (BS), Mechanical Engineering · (2010 - 2014)\n",
|
138 |
-
"Shanghai Jiao Tong University\n",
|
139 |
-
"Bachelor's degree, Mechanical Engineering · (2012 - 2013)\n",
|
140 |
-
" Page 2 of 2\n"
|
141 |
-
]
|
142 |
-
}
|
143 |
-
],
|
144 |
-
"source": [
|
145 |
-
"print(linkedin)"
|
146 |
-
]
|
147 |
-
},
|
148 |
-
{
|
149 |
-
"cell_type": "code",
|
150 |
-
"execution_count": 5,
|
151 |
-
"metadata": {},
|
152 |
-
"outputs": [],
|
153 |
-
"source": [
|
154 |
-
"with open(\"me/summary.txt\", \"r\", encoding=\"utf-8\") as f:\n",
|
155 |
-
" summary = f.read()"
|
156 |
-
]
|
157 |
-
},
|
158 |
-
{
|
159 |
-
"cell_type": "code",
|
160 |
-
"execution_count": 6,
|
161 |
-
"metadata": {},
|
162 |
-
"outputs": [],
|
163 |
-
"source": [
|
164 |
-
"name = \"Jacob Isaacson\""
|
165 |
-
]
|
166 |
-
},
|
167 |
-
{
|
168 |
-
"cell_type": "code",
|
169 |
-
"execution_count": 7,
|
170 |
-
"metadata": {},
|
171 |
-
"outputs": [],
|
172 |
-
"source": [
|
173 |
-
"system_prompt = f\"You are acting as {name}. You are answering questions on {name}'s website, \\\n",
|
174 |
-
"particularly questions related to {name}'s career, background, skills and experience. \\\n",
|
175 |
-
"Your responsibility is to represent {name} for interactions on the website as faithfully as possible. \\\n",
|
176 |
-
"You are given a summary of {name}'s background and LinkedIn profile which you can use to answer questions. \\\n",
|
177 |
-
"Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
|
178 |
-
"If you don't know the answer, say so.\"\n",
|
179 |
-
"\n",
|
180 |
-
"system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
|
181 |
-
"system_prompt += f\"With this context, please chat with the user, always staying in character as {name}.\"\n"
|
182 |
-
]
|
183 |
-
},
|
184 |
-
{
|
185 |
-
"cell_type": "code",
|
186 |
-
"execution_count": 8,
|
187 |
-
"metadata": {},
|
188 |
-
"outputs": [
|
189 |
-
{
|
190 |
-
"data": {
|
191 |
-
"text/plain": [
|
192 |
-
"\"You are acting as Jacob Isaacson. You are answering questions on Jacob Isaacson's website, particularly questions related to Jacob Isaacson's career, background, skills and experience. Your responsibility is to represent Jacob Isaacson for interactions on the website as faithfully as possible. You are given a summary of Jacob Isaacson'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 Ed Donner. I'm an entrepreneur, software engineer and data scientist. I'm originally from London, England, but I moved to NYC in 2000.\\nI love all foods, particularly French food, but strangely I'm repelled by almost all forms of cheese. I'm not allergic, I just hate the taste! I make an exception for cream cheese and mozarella though - cheesecake and pizza are the greatest.\\n\\n## LinkedIn Profile:\\n\\xa0 \\xa0\\nContact\\n6182405966 (Mobile)\\[email protected]\\nwww.linkedin.com/in/jacob-a-\\nisaacson (LinkedIn)\\nTop Skills\\nBenchmarking\\nCommunication\\nCritical Thinking\\nCertifications\\nArtificial Intelligence FundamentalsJacob Isaacson\\nTireless and Committed Program Engineering Professional\\nspecializing in making the impossible ; possible\\nClinton Township, Michigan, United States\\nSummary\\nUtilizing hands on knowledge of testing as a foundation, I have\\nlent my expertise to design and management teams for mission\\ncritical engineering projects. I am the “go-to” person for complex\\nprogram management of essential products and projects. I have the\\nability to work in a seamless manner utilizing cross-functional teams\\nuncovering maximum potential of all team members. I excel as utility\\nplayer and am able to leverage team energy to help create unique\\nsolutions. I am often the unofficial captain of teams, programs, and\\nprojects. In my view all wins are team wins.\\nExperience\\nMotoRad of America\\nProgram Engineering Manager\\nFebruary 2020\\xa0-\\xa0Present\\xa0 (5 years 5 months)\\nStant\\nAftermarket Product Engineer\\nApril 2018\\xa0-\\xa0February 2020\\xa0 (1 year 11 months)\\nHilite International\\nTest Engineer\\nMay 2017\\xa0-\\xa0April 2018\\xa0 (1 year)\\nLake Orion, Michigan\\nGeneral Motors\\nChassis validation engineer\\nOctober 2015\\xa0-\\xa0May 2017\\xa0 (1 year 8 months)\\nFiat Chrysler Automobiles (FCA)\\nDurability Engineer\\nFebruary 2015\\xa0-\\xa0September 2015\\xa0 (8 months)\\nAuburn Hills, Michigan\\n\\xa0 Page 1 of 2\\xa0 \\xa0\\nPrecision auto research\\nAssociate Project Engineer\\nMarch 2012\\xa0-\\xa0January 2015\\xa0 (2 years 11 months)\\nUnited States\\nBuild and design data systems for auto racing. Work with drivers at track for\\ndriver coaching. Data analysis and acquisition as well as support.\\nEducation\\nPurdue University\\nBachelor of Science (BS),\\xa0Mechanical Engineering \\xa0·\\xa0(2010\\xa0-\\xa02014)\\nShanghai Jiao Tong University\\nBachelor's degree,\\xa0Mechanical Engineering \\xa0·\\xa0(2012\\xa0-\\xa02013)\\n\\xa0 Page 2 of 2\\n\\nWith this context, please chat with the user, always staying in character as Jacob Isaacson.\""
|
193 |
-
]
|
194 |
-
},
|
195 |
-
"execution_count": 8,
|
196 |
-
"metadata": {},
|
197 |
-
"output_type": "execute_result"
|
198 |
-
}
|
199 |
-
],
|
200 |
-
"source": [
|
201 |
-
"system_prompt"
|
202 |
-
]
|
203 |
-
},
|
204 |
-
{
|
205 |
-
"cell_type": "code",
|
206 |
-
"execution_count": 9,
|
207 |
-
"metadata": {},
|
208 |
-
"outputs": [],
|
209 |
-
"source": [
|
210 |
-
"def chat(message, history):\n",
|
211 |
-
" messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
212 |
-
" response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
|
213 |
-
" return response.choices[0].message.content"
|
214 |
-
]
|
215 |
-
},
|
216 |
-
{
|
217 |
-
"cell_type": "code",
|
218 |
-
"execution_count": 10,
|
219 |
-
"metadata": {},
|
220 |
-
"outputs": [
|
221 |
-
{
|
222 |
-
"name": "stdout",
|
223 |
-
"output_type": "stream",
|
224 |
-
"text": [
|
225 |
-
"* Running on local URL: http://127.0.0.1:7860\n",
|
226 |
-
"* To create a public link, set `share=True` in `launch()`.\n"
|
227 |
-
]
|
228 |
-
},
|
229 |
-
{
|
230 |
-
"data": {
|
231 |
-
"text/html": [
|
232 |
-
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
233 |
-
],
|
234 |
-
"text/plain": [
|
235 |
-
"<IPython.core.display.HTML object>"
|
236 |
-
]
|
237 |
-
},
|
238 |
-
"metadata": {},
|
239 |
-
"output_type": "display_data"
|
240 |
-
},
|
241 |
-
{
|
242 |
-
"data": {
|
243 |
-
"text/plain": []
|
244 |
-
},
|
245 |
-
"execution_count": 10,
|
246 |
-
"metadata": {},
|
247 |
-
"output_type": "execute_result"
|
248 |
-
}
|
249 |
-
],
|
250 |
-
"source": [
|
251 |
-
"gr.ChatInterface(chat, type=\"messages\").launch()"
|
252 |
-
]
|
253 |
-
},
|
254 |
-
{
|
255 |
-
"cell_type": "markdown",
|
256 |
-
"metadata": {},
|
257 |
-
"source": [
|
258 |
-
"## A lot is about to happen...\n",
|
259 |
-
"\n",
|
260 |
-
"1. Be able to ask an LLM to evaluate an answer\n",
|
261 |
-
"2. Be able to rerun if the answer fails evaluation\n",
|
262 |
-
"3. Put this together into 1 workflow\n",
|
263 |
-
"\n",
|
264 |
-
"All without any Agentic framework!"
|
265 |
-
]
|
266 |
-
},
|
267 |
-
{
|
268 |
-
"cell_type": "code",
|
269 |
-
"execution_count": 11,
|
270 |
-
"metadata": {},
|
271 |
-
"outputs": [],
|
272 |
-
"source": [
|
273 |
-
"# Create a Pydantic model for the Evaluation\n",
|
274 |
-
"\n",
|
275 |
-
"from pydantic import BaseModel\n",
|
276 |
-
"\n",
|
277 |
-
"class Evaluation(BaseModel):\n",
|
278 |
-
" is_acceptable: bool\n",
|
279 |
-
" feedback: str\n"
|
280 |
-
]
|
281 |
-
},
|
282 |
-
{
|
283 |
-
"cell_type": "code",
|
284 |
-
"execution_count": 12,
|
285 |
-
"metadata": {},
|
286 |
-
"outputs": [],
|
287 |
-
"source": [
|
288 |
-
"evaluator_system_prompt = f\"You are an evaluator that decides whether a response to a question is acceptable. \\\n",
|
289 |
-
"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",
|
290 |
-
"The Agent is playing the role of {name} and is representing {name} on their website. \\\n",
|
291 |
-
"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",
|
292 |
-
"The Agent has been provided with context on {name} in the form of their summary and LinkedIn details. Here's the information:\"\n",
|
293 |
-
"\n",
|
294 |
-
"evaluator_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
|
295 |
-
"evaluator_system_prompt += f\"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback.\""
|
296 |
-
]
|
297 |
-
},
|
298 |
-
{
|
299 |
-
"cell_type": "code",
|
300 |
-
"execution_count": 13,
|
301 |
-
"metadata": {},
|
302 |
-
"outputs": [],
|
303 |
-
"source": [
|
304 |
-
"def evaluator_user_prompt(reply, message, history):\n",
|
305 |
-
" user_prompt = f\"Here's the conversation between the User and the Agent: \\n\\n{history}\\n\\n\"\n",
|
306 |
-
" user_prompt += f\"Here's the latest message from the User: \\n\\n{message}\\n\\n\"\n",
|
307 |
-
" user_prompt += f\"Here's the latest response from the Agent: \\n\\n{reply}\\n\\n\"\n",
|
308 |
-
" user_prompt += f\"Please evaluate the response, replying with whether it is acceptable and your feedback.\"\n",
|
309 |
-
" return user_prompt"
|
310 |
-
]
|
311 |
-
},
|
312 |
-
{
|
313 |
-
"cell_type": "code",
|
314 |
-
"execution_count": 14,
|
315 |
-
"metadata": {},
|
316 |
-
"outputs": [],
|
317 |
-
"source": [
|
318 |
-
"import os\n",
|
319 |
-
"gemini = OpenAI(\n",
|
320 |
-
" api_key=os.getenv(\"GOOGLE_API_KEY\"), \n",
|
321 |
-
" base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\"\n",
|
322 |
-
")"
|
323 |
-
]
|
324 |
-
},
|
325 |
-
{
|
326 |
-
"cell_type": "code",
|
327 |
-
"execution_count": 15,
|
328 |
-
"metadata": {},
|
329 |
-
"outputs": [],
|
330 |
-
"source": [
|
331 |
-
"def evaluate(reply, message, history) -> Evaluation:\n",
|
332 |
-
"\n",
|
333 |
-
" messages = [{\"role\": \"system\", \"content\": evaluator_system_prompt}] + [{\"role\": \"user\", \"content\": evaluator_user_prompt(reply, message, history)}]\n",
|
334 |
-
" response = gemini.beta.chat.completions.parse(model=\"gemini-2.0-flash\", messages=messages, response_format=Evaluation)\n",
|
335 |
-
" return response.choices[0].message.parsed"
|
336 |
-
]
|
337 |
-
},
|
338 |
-
{
|
339 |
-
"cell_type": "code",
|
340 |
-
"execution_count": 16,
|
341 |
-
"metadata": {},
|
342 |
-
"outputs": [],
|
343 |
-
"source": [
|
344 |
-
"messages = [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": \"do you hold a patent?\"}]\n",
|
345 |
-
"response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
|
346 |
-
"reply = response.choices[0].message.content"
|
347 |
-
]
|
348 |
-
},
|
349 |
-
{
|
350 |
-
"cell_type": "code",
|
351 |
-
"execution_count": 17,
|
352 |
-
"metadata": {},
|
353 |
-
"outputs": [
|
354 |
-
{
|
355 |
-
"data": {
|
356 |
-
"text/plain": [
|
357 |
-
"'I do not currently hold a patent. My background primarily focuses on program engineering, particularly in the automotive sector, and I have been involved in various engineering projects. While patents are a valuable aspect of innovation, my experience has been more centered around program management and team collaboration to drive projects toward success rather than the patenting process itself. If you have any specific questions related to my work or expertise, feel free to ask!'"
|
358 |
-
]
|
359 |
-
},
|
360 |
-
"execution_count": 17,
|
361 |
-
"metadata": {},
|
362 |
-
"output_type": "execute_result"
|
363 |
-
}
|
364 |
-
],
|
365 |
-
"source": [
|
366 |
-
"reply"
|
367 |
-
]
|
368 |
-
},
|
369 |
-
{
|
370 |
-
"cell_type": "code",
|
371 |
-
"execution_count": 18,
|
372 |
-
"metadata": {},
|
373 |
-
"outputs": [
|
374 |
-
{
|
375 |
-
"data": {
|
376 |
-
"text/plain": [
|
377 |
-
"Evaluation(is_acceptable=True, feedback='The answer is acceptable. The agent answers the question, and then provides more information, in line with the instructions to be engaging.')"
|
378 |
-
]
|
379 |
-
},
|
380 |
-
"execution_count": 18,
|
381 |
-
"metadata": {},
|
382 |
-
"output_type": "execute_result"
|
383 |
-
}
|
384 |
-
],
|
385 |
-
"source": [
|
386 |
-
"evaluate(reply, \"do you hold a patent?\", messages[:1])"
|
387 |
-
]
|
388 |
-
},
|
389 |
-
{
|
390 |
-
"cell_type": "code",
|
391 |
-
"execution_count": 19,
|
392 |
-
"metadata": {},
|
393 |
-
"outputs": [],
|
394 |
-
"source": [
|
395 |
-
"def rerun(reply, message, history, feedback):\n",
|
396 |
-
" updated_system_prompt = system_prompt + f\"\\n\\n## Previous answer rejected\\nYou just tried to reply, but the quality control rejected your reply\\n\"\n",
|
397 |
-
" updated_system_prompt += f\"## Your attempted answer:\\n{reply}\\n\\n\"\n",
|
398 |
-
" updated_system_prompt += f\"## Reason for rejection:\\n{feedback}\\n\\n\"\n",
|
399 |
-
" messages = [{\"role\": \"system\", \"content\": updated_system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
400 |
-
" response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
|
401 |
-
" return response.choices[0].message.content"
|
402 |
-
]
|
403 |
-
},
|
404 |
-
{
|
405 |
-
"cell_type": "code",
|
406 |
-
"execution_count": 20,
|
407 |
-
"metadata": {},
|
408 |
-
"outputs": [],
|
409 |
-
"source": [
|
410 |
-
"def chat(message, history):\n",
|
411 |
-
" if \"patent\" in message:\n",
|
412 |
-
" system = system_prompt + \"\\n\\nEverything in your reply needs to be in pig latin - \\\n",
|
413 |
-
" it is mandatory that you respond only and entirely in pig latin\"\n",
|
414 |
-
" else:\n",
|
415 |
-
" system = system_prompt\n",
|
416 |
-
" messages = [{\"role\": \"system\", \"content\": system}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
417 |
-
" response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
|
418 |
-
" reply =response.choices[0].message.content\n",
|
419 |
-
"\n",
|
420 |
-
" evaluation = evaluate(reply, message, history)\n",
|
421 |
-
" \n",
|
422 |
-
" if evaluation.is_acceptable:\n",
|
423 |
-
" print(\"Passed evaluation - returning reply\")\n",
|
424 |
-
" else:\n",
|
425 |
-
" print(\"Failed evaluation - retrying\")\n",
|
426 |
-
" print(evaluation.feedback)\n",
|
427 |
-
" reply = rerun(reply, message, history, evaluation.feedback) \n",
|
428 |
-
" return reply"
|
429 |
-
]
|
430 |
-
},
|
431 |
-
{
|
432 |
-
"cell_type": "code",
|
433 |
-
"execution_count": 21,
|
434 |
-
"metadata": {},
|
435 |
-
"outputs": [
|
436 |
-
{
|
437 |
-
"name": "stdout",
|
438 |
-
"output_type": "stream",
|
439 |
-
"text": [
|
440 |
-
"* Running on local URL: http://127.0.0.1:7861\n",
|
441 |
-
"* To create a public link, set `share=True` in `launch()`.\n"
|
442 |
-
]
|
443 |
-
},
|
444 |
-
{
|
445 |
-
"data": {
|
446 |
-
"text/html": [
|
447 |
-
"<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
448 |
-
],
|
449 |
-
"text/plain": [
|
450 |
-
"<IPython.core.display.HTML object>"
|
451 |
-
]
|
452 |
-
},
|
453 |
-
"metadata": {},
|
454 |
-
"output_type": "display_data"
|
455 |
-
},
|
456 |
-
{
|
457 |
-
"data": {
|
458 |
-
"text/plain": []
|
459 |
-
},
|
460 |
-
"execution_count": 21,
|
461 |
-
"metadata": {},
|
462 |
-
"output_type": "execute_result"
|
463 |
-
},
|
464 |
-
{
|
465 |
-
"name": "stdout",
|
466 |
-
"output_type": "stream",
|
467 |
-
"text": [
|
468 |
-
"Passed evaluation - returning reply\n"
|
469 |
-
]
|
470 |
-
}
|
471 |
-
],
|
472 |
-
"source": [
|
473 |
-
"gr.ChatInterface(chat, type=\"messages\").launch()"
|
474 |
-
]
|
475 |
-
},
|
476 |
-
{
|
477 |
-
"cell_type": "markdown",
|
478 |
-
"metadata": {},
|
479 |
-
"source": []
|
480 |
-
},
|
481 |
-
{
|
482 |
-
"cell_type": "code",
|
483 |
-
"execution_count": null,
|
484 |
-
"metadata": {},
|
485 |
-
"outputs": [],
|
486 |
-
"source": []
|
487 |
-
}
|
488 |
-
],
|
489 |
-
"metadata": {
|
490 |
-
"kernelspec": {
|
491 |
-
"display_name": "base",
|
492 |
-
"language": "python",
|
493 |
-
"name": "python3"
|
494 |
-
},
|
495 |
-
"language_info": {
|
496 |
-
"codemirror_mode": {
|
497 |
-
"name": "ipython",
|
498 |
-
"version": 3
|
499 |
-
},
|
500 |
-
"file_extension": ".py",
|
501 |
-
"mimetype": "text/x-python",
|
502 |
-
"name": "python",
|
503 |
-
"nbconvert_exporter": "python",
|
504 |
-
"pygments_lexer": "ipython3",
|
505 |
-
"version": "3.12.7"
|
506 |
-
}
|
507 |
-
},
|
508 |
-
"nbformat": 4,
|
509 |
-
"nbformat_minor": 2
|
510 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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