File size: 11,471 Bytes
7e820ed |
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "a42824e4",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "905d1b79",
"metadata": {},
"source": [
"Built an evaluation model to assess the output of the current model\n",
"1. Be able to ask an LLM to evaluate answer\n",
"2. Be able to rerun if the answer fail the evaluation\n",
"3. Be able to incorporate into a workflow"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1b931a48",
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv\n",
"import os\n",
"from pypdf import PdfReader\n",
"import google.generativeai as genai\n",
"import gradio as gr\n",
"from pydantic import BaseModel\n",
"import json\n",
"load_dotenv(override=True)\n",
"genai.configure(api_key=os.getenv(\"GEMINI_API\"))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "220dbf02",
"metadata": {},
"outputs": [],
"source": [
"# Read the PDF and summary \n",
"reader = PdfReader(\"../Week_1/Data_w1/linkedin.pdf\")\n",
"linkedin = \"\"\n",
"for page in reader.pages:\n",
" linkedin += page.extract_text()\n",
"\n",
"with open(\"../Week_1/Data_w1/summary.txt\", \"r\") as f:\n",
" summary = f.read()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6a8c0ccb",
"metadata": {},
"outputs": [],
"source": [
"# Create a system prompt\n",
"initial_system_prompt = f\"You are acting as Ed Donner. You are answering questions on Ed Donner's website, \\\n",
"particularly questions related to Ed Donner's career, background, skills and experience. \\\n",
"Your responsibility is to represent Ed Donner for interactions on the website as faithfully as possible. \\\n",
"You are given a summary of Ed Donner's background and LinkedIn profile which you can use to answer questions. \\\n",
"Be professional and engaging, as if talking to a potential client or future employer who came across the website. \\\n",
"If you don't know the answer, say so.\"\n",
"\n",
"initial_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
"initial_system_prompt += f\"With this context, please chat with the user, always staying in character as Ed Donner.\"\n",
"\n",
"chat_session = None"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "fb1d2679",
"metadata": {},
"outputs": [],
"source": [
"evaluator_system_prompt = f\"You are an evaluator that decides whether a response to a question is acceptable. \\\n",
"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",
"The Agent is playing the role of Ed Donner and is representing Ed Donner on their website. \\\n",
"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",
"The Agent has been provided with context on Ed Donner in the form of their summary and LinkedIn details. Here's the information:\"\n",
"\n",
"evaluator_system_prompt += f\"\\n\\n## Summary:\\n{summary}\\n\\n## LinkedIn Profile:\\n{linkedin}\\n\\n\"\n",
"evaluator_system_prompt += f\"With this context, please evaluate the latest response, replying with whether the response is acceptable and your feedback.\"\n",
"\n",
"def evaluator_user_prompt(reply, message, history):\n",
" user_prompt = f\"Here's the conversation between the User and the Agent: \\n\\n{history}\\n\\n\"\n",
" user_prompt += f\"Here's the latest message from the User: \\n\\n{message}\\n\\n\"\n",
" user_prompt += f\"Here's the latest response from the Agent: \\n\\n{reply}\\n\\n\"\n",
" user_prompt += f\"Please evaluate the response, replying with whether it is acceptable and your feedback.\"\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "25afd8a8",
"metadata": {},
"outputs": [],
"source": [
"class Evaluation(BaseModel):\n",
" is_acceptable: bool\n",
" response: str\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "5d7aceac",
"metadata": {},
"outputs": [],
"source": [
"# Create a model for evaluation\n",
"\n",
"model_evaluator = genai.GenerativeModel(\n",
" 'gemini-2.0-flash-exp',\n",
" system_instruction=evaluator_system_prompt\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "1b33200d",
"metadata": {},
"outputs": [],
"source": [
"def evaluate_response(reply, message, history) -> Evaluation:\n",
" try:\n",
" # Create evaluation prompt\n",
" eval_prompt = evaluator_user_prompt(reply, message, history)\n",
" response = model_evaluator.generate_content(eval_prompt)\n",
"\n",
" # Parse the JSON response\n",
" try:\n",
" eval_data = json.loads(response.text)\n",
" return Evaluation(\n",
" is_acceptable=eval_data.get(\"is_acceptable\", True),\n",
" response=eval_data.get(\"response\", \"No response provided.\")\n",
"\n",
" )\n",
" except json.JSONDecodeError:\n",
" # If JSON parsing fails, try to extract boolean and text\n",
" text = response.text.lower()\n",
" is_acceptable = \"true\" in text or \"acceptable\" in text\n",
" return Evaluation(\n",
" is_acceptable=is_acceptable,\n",
" response=response.text\n",
" )\n",
" except Exception as e:\n",
" # Return default evaluation on error\n",
" return Evaluation(\n",
" is_acceptable=True,\n",
" response=f\"Evaluation failed: {str(e)}\"\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "a2ee32f8",
"metadata": {},
"outputs": [],
"source": [
"# Create the main chat\n",
"def chat(message, history, system_prompt=initial_system_prompt):\n",
" model = genai.GenerativeModel(\n",
" 'gemini-2.0-flash',\n",
" system_instruction=system_prompt\n",
" )\n",
" # Convert Gradio messages format to Gemini format\n",
" gemini_history = []\n",
" for msg in history:\n",
" if msg[\"role\"] == \"user\":\n",
" gemini_history.append({\n",
" \"role\": \"user\",\n",
" \"parts\": [msg[\"content\"]]\n",
" })\n",
" elif msg[\"role\"] == \"assistant\":\n",
" gemini_history.append({\n",
" \"role\": \"model\", # Gemini uses \"model\" instead of \"assistant\"\n",
" \"parts\": [msg[\"content\"]]\n",
" })\n",
" \n",
" # Start chat with history\n",
" chat_session = model.start_chat(history=gemini_history)\n",
" \n",
" # Create an acceptable retries if the message is not acceptable\n",
" for try_count in range(3):\n",
" try:\n",
" # Send the current message\n",
" response = chat_session.send_message(message).text\n",
"\n",
" # Evaluate the response\n",
" evaluation = evaluate_response(response, message, history)\n",
" if evaluation.is_acceptable:\n",
" print(\"Passed evaluation - returning reply\")\n",
" return response\n",
" else:\n",
" print(\"Failed evaluation - retrying\")\n",
" if try_count < 2:\n",
" retry_message = f\"{message}\\n\\nPlease provide a better response. Previous attempt had issues: {evaluation.response}\"\n",
" # Create a new chat to avoid the bad response\n",
" chat_session = model.start_chat(history=gemini_history)\n",
" message = retry_message\n",
" else:\n",
" return f\"{response}\\n\\n*[Note: Response may need improvement - {evaluation.response}]*\"\n",
" except Exception as e:\n",
" if try_count < 2:\n",
" continue\n",
" else:\n",
" return f\"Error: {str(e)} after 3 tries\"\n",
" return \"Failed to generate acceptable response after maximum retries.\"\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ba3b599c",
"metadata": {},
"outputs": [],
"source": [
"# Create interface with additional inputs\n",
"with gr.Blocks() as demo:\n",
" gr.Markdown(\"# Chat with Google Gemini\")\n",
" \n",
" system_prompt = gr.Textbox(\n",
" value=initial_system_prompt,\n",
" label=\"System Prompt\",\n",
" placeholder=\"Enter system instructions for the AI...\",\n",
" lines=2\n",
" )\n",
" \n",
" chat_interface = gr.ChatInterface(\n",
" fn=chat,\n",
" additional_inputs=[system_prompt],\n",
" title=\"\",\n",
" cache_examples=False,\n",
" type='messages'\n",
" \n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce1addde",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7862\n",
"* To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Passed evaluation - returning reply\n",
"Passed evaluation - returning reply\n"
]
}
],
"source": [
"# Launch the interface\n",
"demo.launch()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "9039693e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Closing server running on port: 7862\n"
]
}
],
"source": [
"demo.close()"
]
}
],
"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
}
|