├── README.md
└── Learn_LangChain_GradioLLM_FreeAI_Agents.ipynb
/README.md:
--------------------------------------------------------------------------------
1 | # buildLLMapplicationFree
2 | Repo of the code from the Medium article about Valentina Alto's book
3 |
4 | [here is the notebook](https://github.com/fabiomatricardi/buildLLMapplicationFree/blob/main/Learn_LangChain_GradioLLM_FreeAI_Agents.ipynb) used in the article
5 |
--------------------------------------------------------------------------------
/Learn_LangChain_GradioLLM_FreeAI_Agents.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyNm8dLv/xOKkidzDxYhMCqB",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | ""
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "source": [
32 | "# Tests to create FREE AI AGENTS"
33 | ],
34 | "metadata": {
35 | "id": "FFyfgHcnsur2"
36 | }
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": null,
41 | "metadata": {
42 | "colab": {
43 | "base_uri": "https://localhost:8080/"
44 | },
45 | "id": "_mXrrYRj0VW8",
46 | "outputId": "39dcef86-c2a7-4d30-868a-ef8281cc54e6"
47 | },
48 | "outputs": [
49 | {
50 | "output_type": "stream",
51 | "name": "stdout",
52 | "text": [
53 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m401.7/401.7 kB\u001b[0m \u001b[31m4.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
54 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m973.5/973.5 kB\u001b[0m \u001b[31m8.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
55 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.1/2.1 MB\u001b[0m \u001b[31m13.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
56 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m87.8/87.8 kB\u001b[0m \u001b[31m5.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
57 | "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
58 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m27.0/27.0 MB\u001b[0m \u001b[31m28.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
59 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m44.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
60 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m316.3/316.3 kB\u001b[0m \u001b[31m25.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
61 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m310.2/310.2 kB\u001b[0m \u001b[31m20.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
62 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m124.4/124.4 kB\u001b[0m \u001b[31m7.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
63 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.7/2.7 MB\u001b[0m \u001b[31m49.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
64 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m142.5/142.5 kB\u001b[0m \u001b[31m4.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
65 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.3/49.3 kB\u001b[0m \u001b[31m3.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
66 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m5.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
67 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m129.9/129.9 kB\u001b[0m \u001b[31m9.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
68 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m53.0/53.0 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
69 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m4.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
70 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
71 | "\u001b[?25h Building wheel for google-search-results (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
72 | ]
73 | }
74 | ],
75 | "source": [
76 | "%pip install --upgrade --quiet gradio_tools huggingface_hub langchain langchain-community langgraph google-search-results faiss-cpu tiktoken duckduckgo-search"
77 | ]
78 | },
79 | {
80 | "cell_type": "code",
81 | "source": [
82 | "from tqdm.rich import trange, tqdm\n",
83 | "from rich.markdown import Markdown\n",
84 | "import warnings\n",
85 | "warnings.filterwarnings(action='ignore')\n",
86 | "import datetime\n",
87 | "from rich.console import Console\n",
88 | "console = Console(width=90)\n",
89 | "from typing import Any, Dict, Iterator, List, Mapping, Optional\n",
90 | "from langchain.callbacks.manager import CallbackManagerForLLMRun\n",
91 | "from langchain_core.language_models.llms import LLM\n",
92 | "from langchain_core.outputs import GenerationChunk\n",
93 | "from langchain_core.output_parsers import StrOutputParser\n",
94 | "from langchain_core.prompts import ChatPromptTemplate\n",
95 | "from gradio_client import Client\n",
96 | "from gradio_client import Client\n",
97 | "from langchain_core.prompts import PromptTemplate"
98 | ],
99 | "metadata": {
100 | "id": "EdcClnV7GCBe"
101 | },
102 | "execution_count": null,
103 | "outputs": []
104 | },
105 | {
106 | "cell_type": "markdown",
107 | "source": [
108 | "## ReACT examples with Chains - Using Qwen72B\n",
109 | "\n",
110 | "For example, let’s say we want to ask our model for some up-to-date information about the upcoming\n",
111 | "Olympic games. To do so, we are going to build a smart LangChain agent (as described in Chapter 2)\n",
112 | "leveraging SerpAPIWrapperWrapper (to wrap the SerpApi to navigate the web), the AgentType tool\n",
113 | "(to decide which type of agent to use for our goal), and other prompt-related modules (to make it\n",
114 | "easier to “templatize” our instructions). Let’s see how we can do this (I won’t dive deeper into each\n",
115 | "component of the following code since the next chapter will be entirely focused on LangChain and\n",
116 | "its main components):"
117 | ],
118 | "metadata": {
119 | "id": "MlRvjBJmVjsU"
120 | }
121 | },
122 | {
123 | "cell_type": "code",
124 | "source": [
125 | "from langchain_core.language_models.llms import LLM\n",
126 | "class GradioLLMChat(LLM):\n",
127 | " \"\"\"\n",
128 | " Custom LLM class based on the Gradio API call.\n",
129 | " \"\"\"\n",
130 | " from typing import Any, Dict, Iterator, List, Mapping, Optional\n",
131 | " from langchain.callbacks.manager import CallbackManagerForLLMRun\n",
132 | " from langchain_core.language_models.llms import LLM\n",
133 | " from langchain_core.outputs import GenerationChunk\n",
134 | " from langchain_core.output_parsers import StrOutputParser\n",
135 | " from langchain_core.prompts import ChatPromptTemplate\n",
136 | " from gradio_client import Client\n",
137 | " chatbot: Any = None\n",
138 | "\n",
139 | " def __init__(self, *args, **kwargs):\n",
140 | " super().__init__(*args, **kwargs)\n",
141 | " self.chatbot = Client(\"https://qwen-qwen1-5-72b-chat.hf.space/--replicas/061qr/\")\n",
142 | "\n",
143 | " @property\n",
144 | " def _llm_type(self) -> str:\n",
145 | " return \"Gradio API client Qwen1.5-72b_Chat\"\n",
146 | "\n",
147 | " def _call(\n",
148 | " self,\n",
149 | " prompt: str,\n",
150 | " stop: Optional[List[str]] = None,\n",
151 | " run_manager: Optional[CallbackManagerForLLMRun] = None,\n",
152 | " chatbot=None,\n",
153 | " request: float = 0.95, #it's the history touple\n",
154 | " param: str = 'You are a helpful assistant', #it's the system message\n",
155 | " ) -> str:\n",
156 | " \"\"\"\n",
157 | " Make an API call to the Gradio API client Meta_llama3_8b using the specified prompt and return the response.\n",
158 | " \"\"\"\n",
159 | " if chatbot is None:\n",
160 | " chatbot = self.chatbot\n",
161 | "\n",
162 | " # Return the response from the API\n",
163 | " result = chatbot.predict( #.submit for streaming effect / .predict for normal output\n",
164 | " param,\t# str in 'Input' Textbox component\n",
165 | " [],\t# Tuple[str | Dict(file: filepath, alt_text: str | None) | None, str | Dict(file: filepath, alt_text: str | None) | None] in 'Qwen1.5-72B-Chat' Chatbot component\n",
166 | " prompt,\t# str in 'parameter_9' Textbox component\n",
167 | " api_name=\"/model_chat\"\n",
168 | " )\n",
169 | " return result[1][0][1]"
170 | ],
171 | "metadata": {
172 | "id": "yWqG_xF8wCok"
173 | },
174 | "execution_count": null,
175 | "outputs": []
176 | },
177 | {
178 | "cell_type": "markdown",
179 | "source": [
180 | "## GradioLLM + DuckDuckGo AI agent tool"
181 | ],
182 | "metadata": {
183 | "id": "J8OyTTJUeriT"
184 | }
185 | },
186 | {
187 | "cell_type": "code",
188 | "source": [
189 | "import os\n",
190 | "#from https://python.langchain.com/v0.1/docs/integrations/tools/ddg/\n",
191 | "from langchain_community.utilities import DuckDuckGoSearchAPIWrapper\n",
192 | "#wrapper = DuckDuckGoSearchAPIWrapper(region=\"de-de\", time=\"d\", max_results=8)\n",
193 | "from langchain.agents import AgentType, initialize_agent\n",
194 | "from langchain.chat_models import ChatOpenAI\n",
195 | "from langchain.tools import BaseTool, StructuredTool, Tool, tool\n",
196 | "from langchain.schema import HumanMessage\n",
197 | "from gradio_client import Client\n",
198 | "\n",
199 | "model = GradioLLMChat()\n",
200 | "search = DuckDuckGoSearchAPIWrapper(max_results=5)\n",
201 | "tools = [\n",
202 | " Tool.from_function(\n",
203 | " func=search.run,\n",
204 | " name=\"Search\",\n",
205 | " description=\"useful for when you need to answer questions about current events, search online news\"\n",
206 | " )\n",
207 | " ]\n",
208 | "\n",
209 | "agent_executor = initialize_agent(tools, model, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)"
210 | ],
211 | "metadata": {
212 | "colab": {
213 | "base_uri": "https://localhost:8080/"
214 | },
215 | "id": "Ur67dG6TVkk7",
216 | "outputId": "f7c18fd8-377e-4a5a-ea94-575518e2de50"
217 | },
218 | "execution_count": null,
219 | "outputs": [
220 | {
221 | "output_type": "stream",
222 | "name": "stdout",
223 | "text": [
224 | "Loaded as API: https://qwen-qwen1-5-72b-chat.hf.space/--replicas/061qr/ ✔\n"
225 | ]
226 | },
227 | {
228 | "output_type": "stream",
229 | "name": "stderr",
230 | "text": [
231 | "/usr/local/lib/python3.10/dist-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The function `initialize_agent` was deprecated in LangChain 0.1.0 and will be removed in 0.3.0. Use Use new agent constructor methods like create_react_agent, create_json_agent, create_structured_chat_agent, etc. instead.\n",
232 | " warn_deprecated(\n"
233 | ]
234 | }
235 | ]
236 | },
237 | {
238 | "cell_type": "code",
239 | "source": [
240 | "console.print(agent_executor.agent.llm_chain.prompt.template)"
241 | ],
242 | "metadata": {
243 | "colab": {
244 | "base_uri": "https://localhost:8080/",
245 | "height": 345
246 | },
247 | "id": "SRZuqqJwVkh5",
248 | "outputId": "c9488b3b-3dce-4ab9-8afe-61c33c1be0f0"
249 | },
250 | "execution_count": null,
251 | "outputs": [
252 | {
253 | "output_type": "display_data",
254 | "data": {
255 | "text/plain": [
256 | "Answer the following questions as best you can. You have access to the following tools:\n",
257 | "\n",
258 | "\u001b[1;35mSearch\u001b[0m\u001b[1m(\u001b[0mquery: str\u001b[1m)\u001b[0m -> str - useful for when you need to answer questions about current \n",
259 | "events, search online news\n",
260 | "\n",
261 | "Use the following format:\n",
262 | "\n",
263 | "Question: the input question you must answer\n",
264 | "Thought: you should always think about what to do\n",
265 | "Action: the action to take, should be one of \u001b[1m[\u001b[0mSearch\u001b[1m]\u001b[0m\n",
266 | "Action Input: the input to the action\n",
267 | "Observation: the result of the action\n",
268 | "\u001b[33m...\u001b[0m \u001b[1m(\u001b[0mthis Thought/Action/Action Input/Observation can repeat N times\u001b[1m)\u001b[0m\n",
269 | "Thought: I now know the final answer\n",
270 | "Final Answer: the final answer to the original input question\n",
271 | "\n",
272 | "Begin!\n",
273 | "\n",
274 | "Question: \u001b[1m{\u001b[0minput\u001b[1m}\u001b[0m\n",
275 | "Thought:\u001b[1m{\u001b[0magent_scratchpad\u001b[1m}\u001b[0m\n"
276 | ],
277 | "text/html": [
278 | "
Answer the following questions as best you can. You have access to the following tools:\n", 279 | "\n", 280 | "Search(query: str) -> str - useful for when you need to answer questions about current \n", 281 | "events, search online news\n", 282 | "\n", 283 | "Use the following format:\n", 284 | "\n", 285 | "Question: the input question you must answer\n", 286 | "Thought: you should always think about what to do\n", 287 | "Action: the action to take, should be one of [Search]\n", 288 | "Action Input: the input to the action\n", 289 | "Observation: the result of the action\n", 290 | "... (this Thought/Action/Action Input/Observation can repeat N times)\n", 291 | "Thought: I now know the final answer\n", 292 | "Final Answer: the final answer to the original input question\n", 293 | "\n", 294 | "Begin!\n", 295 | "\n", 296 | "Question: {input}\n", 297 | "Thought:{agent_scratchpad}\n", 298 | "\n" 299 | ] 300 | }, 301 | "metadata": {} 302 | } 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "source": [ 308 | "agent_executor('search online the news and tell me the incident on May 2024 when Giorgia Meloni met governor De Luca')" 309 | ], 310 | "metadata": { 311 | "colab": { 312 | "base_uri": "https://localhost:8080/" 313 | }, 314 | "id": "okmzCQudVken", 315 | "outputId": "3ba2a1d2-26b2-49e1-ef3e-596bd2bb5aac" 316 | }, 317 | "execution_count": null, 318 | "outputs": [ 319 | { 320 | "output_type": "stream", 321 | "name": "stdout", 322 | "text": [ 323 | "\n", 324 | "\n", 325 | "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", 326 | "\u001b[32;1m\u001b[1;3mI need to use the search function to find relevant information about this specific event.\n", 327 | "Action: Search\n", 328 | "Action Input: \"May 2024 Giorgia Meloni governor De Luca incident\"\u001b[0m\n", 329 | "Observation: \u001b[36;1m\u001b[1;3mLeggi su Sky TG24 l'articolo De Luca risponde a Meloni dopo incontro Caivano: \"Ha comunicato sua vera identità\" ... 29 mag 2024 - 14:21 ... Dal presidente del Consiglio Giorgia Meloni, ha ... \"Presidente De Luca, la stronza della Meloni!''. La premier stringe la mano al governatore della Campania così, ricordandogli come il … Nel corso di Tagadà, su La7, viene mandato il passaggio delle comunicazioni social di De Luca che a mente fredda contrattacca ad alzo zero contro Meloni. Ecco i rosicon...zi contro Giorgia. Da De ... Caivano (Napoli), 28 maggio 2024 - È ancora lite tra la premier Giorgia Meloni e Vincenzo De Luca, presidente della Regione Campania. L'occasione, questa volta, la offre l'inaugurazione del ... Premier Giorgia Meloni introduced herself as \"that bitch\" to Campania Governor Vincenzo De Luca on Tuesday after he called her a 'stronza' (bitch) when talking to reporters in the Lower House in ...\u001b[0m\n", 330 | "Thought:\u001b[32;1m\u001b[1;3mI now know the final answer\n", 331 | "Final Answer: In May 2024, an incident occurred between Italian Prime Minister Giorgia Meloni and Campania Governor Vincenzo De Luca during an inauguration event in Caivano, Naples. Governor De Luca had previously referred to Meloni as a \"stronza\" (bitch) in a public statement, which led to a tense interaction between the two when they met. During their encounter, Meloni reportedly introduced herself to De Luca as \"that bitch,\" referencing his previous comment. This exchange sparked further controversy and media attention.\u001b[0m\n", 332 | "\n", 333 | "\u001b[1m> Finished chain.\u001b[0m\n" 334 | ] 335 | }, 336 | { 337 | "output_type": "execute_result", 338 | "data": { 339 | "text/plain": [ 340 | "{'input': 'search online the news and tell me the incident on May 2024 when Giorgia Meloni met governor De Luca',\n", 341 | " 'output': 'In May 2024, an incident occurred between Italian Prime Minister Giorgia Meloni and Campania Governor Vincenzo De Luca during an inauguration event in Caivano, Naples. Governor De Luca had previously referred to Meloni as a \"stronza\" (bitch) in a public statement, which led to a tense interaction between the two when they met. During their encounter, Meloni reportedly introduced herself to De Luca as \"that bitch,\" referencing his previous comment. This exchange sparked further controversy and media attention.'}" 342 | ] 343 | }, 344 | "metadata": {}, 345 | "execution_count": 24 346 | } 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "source": [], 352 | "metadata": { 353 | "id": "ks8llg6ajn8F" 354 | }, 355 | "execution_count": null, 356 | "outputs": [] 357 | } 358 | ] 359 | } --------------------------------------------------------------------------------