From cad75b0acffc7722debc7abb1b59fadcb97d8699 Mon Sep 17 00:00:00 2001 From: Robert Haase Date: Tue, 31 Dec 2024 10:57:44 +0100 Subject: [PATCH] added simplifying agentic workflows example --- .../simplifying_agentic_workflows.ipynb | 186 ++++++++++++++++++ docs/_toc.yml | 1 + 2 files changed, 187 insertions(+) create mode 100644 docs/66_arxiv_agent/simplifying_agentic_workflows.ipynb diff --git a/docs/66_arxiv_agent/simplifying_agentic_workflows.ipynb b/docs/66_arxiv_agent/simplifying_agentic_workflows.ipynb new file mode 100644 index 0000000..a0f13b1 --- /dev/null +++ b/docs/66_arxiv_agent/simplifying_agentic_workflows.ipynb @@ -0,0 +1,186 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "937e36c6-c1e8-4014-a428-cc8921efae7b", + "metadata": {}, + "source": [ + "# Simplifing agentic workflows\n", + "Agentic workflows suffer from non-deterministic model outputs and error propagation. If just a single LLM prompt fails in a long agentic workflow, the overall result may be compromised. As checking intermediate results is not always easy, it might make sense to trade flexibility of agentic workflow with determinism by exchanging parts of the agentic workflow with classical text processing approaches. This notebook demonstrates that the entire agentic workflow for writing a scientific review, shown before, can also be implemented with a single prompt." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "47e60fe0-d906-4d0a-9714-ac3b1c4ac52c", + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import display, Markdown\n", + "from arxiv_utilities import prompt_scadsai_llm, get_arxiv_metadata" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "8aba4bfa-9d9e-410d-9063-6dd29cb9d802", + "metadata": {}, + "outputs": [], + "source": [ + "model = \"meta-llama/Llama-3.3-70B-Instruct\"\n", + "prompt = prompt_scadsai_llm\n", + "verbose = True" + ] + }, + { + "cell_type": "markdown", + "id": "fa5078c2-737a-45fd-8992-f51d29a7beb7", + "metadata": {}, + "source": [ + "## Accumulating paper contents\n", + "Here we use a for-loop to collect paper contents in a string. As mentioned before, we are just collecting paper abstracts due to technical limitations of state-of-the-art LLMs. Token limits prevent us from collecting entire papers in one long string." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "d511b8fe-240d-4c4f-aeec-d61ee304ecc4", + "metadata": {}, + "outputs": [], + "source": [ + "def read_arxiv_paper(arxiv_url:str)->str:\n", + " \"\"\"Read the abstract of an arxiv-paper and return most important contents in markdown format.\n", + "\n", + " Args:\n", + " arxiv_url: url of the Arxiv paper\n", + " \"\"\"\n", + " if verbose:\n", + " print(f\"read_arxiv_paper({arxiv_url})\")\n", + " arxiv_id = arxiv_url.split(\"/\")[-1]\n", + " metadata = get_arxiv_metadata(arxiv_id)\n", + " title = metadata[\"title\"]\n", + " summary = metadata[\"summary\"]\n", + " authors = \", \".join(metadata[\"authors\"])\n", + " \n", + " return f\"\"\"## {title}\n", + "By {authors}\n", + "\n", + "{summary}\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "48233b8e-269e-4fab-9b47-16be4a1dd664", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "read_arxiv_paper(https://arxiv.org/abs/2211.11501)\n", + "read_arxiv_paper(https://arxiv.org/abs/2308.16458)\n" + ] + } + ], + "source": [ + "paper_urls = [\"https://arxiv.org/abs/2211.11501\",\n", + "\"https://arxiv.org/abs/2308.16458\",\n", + "\"https://arxiv.org/abs/2411.07781\",\n", + "\"https://arxiv.org/abs/2408.13204\",\n", + "\"https://arxiv.org/abs/2406.15877\"]\n", + "\n", + "paper_contents = \"\"\n", + "for url in paper_urls:\n", + " paper_contents += read_arxiv_paper(url) + \"\\n\"" + ] + }, + { + "cell_type": "markdown", + "id": "8c314815-1f52-4802-9314-eded64f2b1a8", + "metadata": {}, + "source": [ + "## Prompting\n", + "Here we combine the paper contents with detailed instructions for writing a manuscript reviewing those papers." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "944bbd32-af7e-4045-9f19-9337ba1df415", + "metadata": {}, + "outputs": [], + "source": [ + "result = prompt(f\"\"\"You are a great scientific writer. Your task is to write a scientific review manuscript about some papers which are summarized below.\n", + "\n", + "# Content\n", + "\n", + "Write about the following summarized papers:\n", + "\n", + "{paper_contents}\n", + "\n", + "# Formatting\n", + "Your goal is to write a manuscript that follows these criteria:\n", + "* a scientific text with a short and descriptive title,\n", + "* a scientific text with markdown sub-sections (# title, ## headlines, ...) avoiding bullet points,\n", + "* structured in sub-sections by content, e.g. introduction, recent developments, methods, results, discussion, future work, ...\n", + "* text using high-quality scientific language,\n", + "* proper citations using markdown links to original paper urls (do not make up references!),\n", + "* a clear abstract at the beginning of the text, and conclusions at the end\n", + "\n", + "# Your task\n", + "Write a scientific review manuscript about the content summarized above following the mentioned formatting guidelines.\n", + "\"\"\", model=model)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e530f509-f3da-472b-8970-f4fe56160a8b", + "metadata": {}, + "outputs": [], + "source": [ + "display(Markdown(result))" + ] + }, + { + "cell_type": "markdown", + "id": "ee09773a-1fc2-4088-96b0-6e5f49a1224d", + "metadata": {}, + "source": [ + "## Exercise\n", + "Use a [relexion](https://arxiv.org/abs/2303.11366) approach to give feedback to the LLM about the text it just wrote and ask it to improve the manuscript." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f4583c79-0381-4bf5-ad33-a341f1111d06", + "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 +} diff --git a/docs/_toc.yml b/docs/_toc.yml index 61a803f..c83fb0b 100644 --- a/docs/_toc.yml +++ b/docs/_toc.yml @@ -119,6 +119,7 @@ parts: - file: 66_arxiv_agent/arxiv_powerpoint_karaoke.ipynb - file: 35_agents/smolagents.ipynb - file: 66_arxiv_agent/multiagent_write_review.ipynb + - file: 66_arxiv_agent/simplifying_agentic_workflows.ipynb - file: 70_fine_tuning/readme.md sections: