|
48 | 48 | "- You only need API keys for the providers used in a given notebook.\n", |
49 | 49 | "- Never commit or publicly share your API keys.\n", |
50 | 50 | "- If a cell fails due to missing credentials, verify that your keys were loaded correctly before rerunning the cell.\n", |
| 51 | + "- Some cells may not produce any visible output, so donβt worry if nothing appears below the cell. As long as you see a green check mark, youβre good to continue.\n", |
51 | 52 | "</details>\n" |
52 | 53 | ] |
53 | 54 | }, |
|
69 | 70 | "**1) Set your API key**\n", |
70 | 71 | "If you're using this notebook as a `Google Colab notebook`, follow the instruction below.\n", |
71 | 72 | "\n", |
72 | | - "- Once you have received a unique API key from OpenAI (or another provider), setup the API keys by clicking key icon on the left tab. More info [here](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb)\n", |
| 73 | + "- Once you have received a unique API key from OpenAI (or another provider), setup the API keys by clicking key icon (π) on the left tab. Under the **Name** column make sure to enter `OPENAI_API_KEY`. These names are case-sensitive, and the notebook code looks for API keys using these exact names. More info [here](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb)\n", |
73 | 74 | "\n", |
74 | | - "- Alternatively, you can add a key to your environment. For example add a `.env` file to the root directory and add all the api keys. Or do `export OPENAI_API_KEY='your-key-here'`\n", |
75 | | - "\n", |
76 | | - "If you're using the **Live Coding** option, make sure to input your API when prompted.\n", |
77 | | - "\n", |
78 | | - "This notebook is written to work with all above cases. \n", |
| 75 | + "- Alternatively, you can add a key to your environment directly. For example add a `.env` file to the root directory and add all the api keys. Or do `export OPENAI_API_KEY='your-key-here'`\n", |
79 | 76 | "\n", |
80 | 77 | "**2) If you're using Google Colab install the requirements by running the cell below.**\n" |
81 | 78 | ] |
|
235 | 232 | { |
236 | 233 | "cell_type": "markdown", |
237 | 234 | "id": "7", |
| 235 | + "metadata": {}, |
| 236 | + "source": [ |
| 237 | + "Now if you want to test a different model from OpenAI you can change the model name here and run the cell again:\n", |
| 238 | + "\n", |
| 239 | + "`LLM_MODEL = \"gpt-4.1-nano\"`\n", |
| 240 | + "\n", |
| 241 | + "You can also try another question by updating the `QUESTION` variable.\n", |
| 242 | + "\n", |
| 243 | + "**What if you want to try an Claude model?**\n", |
| 244 | + "\n", |
| 245 | + "The most important line to change are the following. You see we are using the OpenAI client here. It only has access to OpenAI models. To use a Claude model, you have to import the client from Anthropic. You can use the following steps.\n", |
| 246 | + "\n", |
| 247 | + "1. Install `anthropic` package\n", |
| 248 | + "2. Instead of OpenAI client, instantiate Anthropic client\n", |
| 249 | + "3. Change the model name\n", |
| 250 | + "\n", |
| 251 | + "Same concept goes to whichever the Client and Model you want to use. " |
| 252 | + ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "code", |
| 256 | + "execution_count": null, |
| 257 | + "id": "8", |
| 258 | + "metadata": {}, |
| 259 | + "outputs": [], |
| 260 | + "source": [ |
| 261 | + "! uv pip install anthropic\n", |
| 262 | + "\n", |
| 263 | + "from anthropic import Anthropic\n", |
| 264 | + "\n", |
| 265 | + "anthropic_api_key = get_api_key(\"anthropic\")\n", |
| 266 | + "client = Anthropic(api_key=anthropic_api_key)\n", |
| 267 | + "LLM_MODEL = \"claude-3-5-sonnet-20240620\"" |
| 268 | + ] |
| 269 | + }, |
| 270 | + { |
| 271 | + "cell_type": "markdown", |
| 272 | + "id": "9", |
238 | 273 | "metadata": { |
239 | 274 | "id": "huoitiLpLHsz" |
240 | 275 | }, |
|
258 | 293 | }, |
259 | 294 | { |
260 | 295 | "cell_type": "markdown", |
261 | | - "id": "8", |
| 296 | + "id": "10", |
262 | 297 | "metadata": { |
263 | 298 | "id": "p5aQfAAYgTIJ" |
264 | 299 | }, |
|
309 | 344 | { |
310 | 345 | "cell_type": "code", |
311 | 346 | "execution_count": null, |
312 | | - "id": "9", |
| 347 | + "id": "11", |
313 | 348 | "metadata": { |
314 | 349 | "id": "xmx2Qos0gRW4" |
315 | 350 | }, |
|
358 | 393 | }, |
359 | 394 | { |
360 | 395 | "cell_type": "markdown", |
361 | | - "id": "10", |
| 396 | + "id": "12", |
362 | 397 | "metadata": { |
363 | 398 | "id": "mxc05xdYkT6M" |
364 | 399 | }, |
|
377 | 412 | { |
378 | 413 | "cell_type": "code", |
379 | 414 | "execution_count": null, |
380 | | - "id": "11", |
| 415 | + "id": "13", |
381 | 416 | "metadata": { |
382 | 417 | "colab": { |
383 | 418 | "base_uri": "https://localhost:8080/" |
|
403 | 438 | }, |
404 | 439 | { |
405 | 440 | "cell_type": "markdown", |
406 | | - "id": "12", |
| 441 | + "id": "14", |
407 | 442 | "metadata": { |
408 | 443 | "id": "yt-6Y046kkU8" |
409 | 444 | }, |
|
422 | 457 | "\n", |
423 | 458 | "- Here, the retrieved abstracts (chunks) are combined into one text block (context), which is sent to the LLM. The LLM then reads all the information and generates an informative answer.\n", |
424 | 459 | "\n", |
425 | | - "- You can edit the prompt and see how it affects the model output." |
| 460 | + "**Adding a prompt**\n", |
| 461 | + "\n", |
| 462 | + "A prompt is the set of instructions or input you provide to the LLM to guide its behavior and responses. Small changes to the prompt can significantly affect the quality, style, and accuracy of the output. **The prompt is how you tell the LLM what task to perform.**\n", |
| 463 | + "\n", |
| 464 | + "In the example below, weβve included a placeholders `{query}` and `{context_text}`. These placeholder lets you update the prompt with new user questions and contexts for each run while keeping the rest of the instructions unchanged. You can continue updating and populating this field as you experiment.\n", |
| 465 | + "\n", |
| 466 | + "In code block below, we do so with this line:\n", |
| 467 | + "\n", |
| 468 | + "```python\n", |
| 469 | + "PROMPT.format(context_text=context_text, query=query)\n", |
| 470 | + "```\n", |
| 471 | + "\n", |
| 472 | + "*Test if yourself:* Edit the prompt below to see how different instructions affect the modelβs output." |
426 | 473 | ] |
427 | 474 | }, |
428 | 475 | { |
429 | 476 | "cell_type": "code", |
430 | 477 | "execution_count": null, |
431 | | - "id": "13", |
432 | | - "metadata": { |
433 | | - "id": "7v_giOlhkeW_" |
434 | | - }, |
| 478 | + "id": "15", |
| 479 | + "metadata": {}, |
435 | 480 | "outputs": [], |
436 | 481 | "source": [ |
437 | | - "from openai import OpenAI\n", |
438 | | - "from google.colab import userdata\n", |
439 | | - "\n", |
440 | | - "#set openai api key\n", |
441 | | - "openai_api_key = get_api_key(\"openai\")\n", |
442 | | - "\n", |
443 | | - "client = OpenAI(api_key=openai_api_key)\n", |
444 | | - "\n", |
| 482 | + "# ================================================\n", |
445 | 483 | "PROMPT = \"\"\"Answer the question based on the context provided.\n", |
446 | 484 | "\n", |
447 | 485 | "Context:\n", |
|
451 | 489 | "Question:\n", |
452 | 490 | "{query}\n", |
453 | 491 | "\n", |
454 | | - "Answer: \"\"\"\n", |
| 492 | + "Answer: \"\"\"" |
| 493 | + ] |
| 494 | + }, |
| 495 | + { |
| 496 | + "cell_type": "code", |
| 497 | + "execution_count": null, |
| 498 | + "id": "16", |
| 499 | + "metadata": {}, |
| 500 | + "outputs": [], |
| 501 | + "source": [ |
| 502 | + "# set openai api key\n", |
| 503 | + "from openai import OpenAI\n", |
| 504 | + "from google.colab import userdata\n", |
455 | 505 | "\n", |
| 506 | + "openai_api_key = get_api_key(\"openai\")\n", |
| 507 | + "client = OpenAI(api_key=openai_api_key)" |
| 508 | + ] |
| 509 | + }, |
| 510 | + { |
| 511 | + "cell_type": "code", |
| 512 | + "execution_count": null, |
| 513 | + "id": "17", |
| 514 | + "metadata": { |
| 515 | + "id": "7v_giOlhkeW_" |
| 516 | + }, |
| 517 | + "outputs": [], |
| 518 | + "source": [ |
456 | 519 | "# === Use OpenAI LLM to generate answer given retrieved abstracts and query ===\n", |
457 | 520 | "def generate_answer_with_openai(contexts: list[str], query: str, llm_model: str=\"gpt-4\") -> str:\n", |
458 | 521 | " context_text = \"\\n\\n\".join(contexts)\n", |
|
473 | 536 | }, |
474 | 537 | { |
475 | 538 | "cell_type": "markdown", |
476 | | - "id": "14", |
| 539 | + "id": "18", |
| 540 | + "metadata": {}, |
| 541 | + "source": [ |
| 542 | + "> π‘Tip\n", |
| 543 | + "> In the message to the LLM, we added a \"system prompt\"\n", |
| 544 | + "> ```\n", |
| 545 | + "> {\"role\": \"system\", \n", |
| 546 | + "> \"content\": \"You are a helpful scientific assistant.\"}\n", |
| 547 | + ">```\n", |
| 548 | + "\n", |
| 549 | + "> A system prompt is a special set of instructions given to an LLM before the userβs prompt. It defines the modelβs overall behavior, rules, tone, and constraints throughout the conversation.\n", |
| 550 | + "\n", |
| 551 | + "> You can think of it as the modelβs βoperating instructions.β\n" |
| 552 | + ] |
| 553 | + }, |
| 554 | + { |
| 555 | + "cell_type": "markdown", |
| 556 | + "id": "19", |
477 | 557 | "metadata": { |
478 | 558 | "id": "BjrdJvpglnw3" |
479 | 559 | }, |
|
492 | 572 | { |
493 | 573 | "cell_type": "code", |
494 | 574 | "execution_count": null, |
495 | | - "id": "15", |
| 575 | + "id": "20", |
496 | 576 | "metadata": { |
497 | 577 | "id": "XBoJfhz5lznd" |
498 | 578 | }, |
|
533 | 613 | { |
534 | 614 | "cell_type": "code", |
535 | 615 | "execution_count": null, |
536 | | - "id": "16", |
| 616 | + "id": "21", |
537 | 617 | "metadata": { |
538 | 618 | "colab": { |
539 | 619 | "base_uri": "https://localhost:8080/" |
|
552 | 632 | }, |
553 | 633 | { |
554 | 634 | "cell_type": "markdown", |
555 | | - "id": "17", |
| 635 | + "id": "22", |
556 | 636 | "metadata": {}, |
557 | 637 | "source": [ |
558 | 638 | "Now that you have seen what RAG can do, what are your thoughts on the following?\n", |
|
599 | 679 | }, |
600 | 680 | { |
601 | 681 | "cell_type": "markdown", |
602 | | - "id": "18", |
| 682 | + "id": "23", |
603 | 683 | "metadata": { |
604 | 684 | "id": "YzPq2CjyAl7H" |
605 | 685 | }, |
|
0 commit comments