Spaces:
Sleeping
Sleeping
added wikipedia_RAG tool
Browse files- __pycache__/main_agent.cpython-313.pyc +0 -0
- __pycache__/tools.cpython-313.pyc +0 -0
- main_agent.py +2 -2
- requirements.txt +1 -0
- test.ipynb +131 -15
- tools.py +43 -2
- tools_test.ipynb +192 -0
__pycache__/main_agent.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/main_agent.cpython-313.pyc and b/__pycache__/main_agent.cpython-313.pyc differ
|
|
|
__pycache__/tools.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/tools.cpython-313.pyc and b/__pycache__/tools.cpython-313.pyc differ
|
|
|
main_agent.py
CHANGED
|
@@ -4,7 +4,7 @@ from langgraph.graph.message import add_messages
|
|
| 4 |
from langchain_core.messages import AnyMessage, SystemMessage
|
| 5 |
from langchain_openai import AzureChatOpenAI
|
| 6 |
from langgraph.graph import START, StateGraph
|
| 7 |
-
from tools import duckduck_tool
|
| 8 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 9 |
|
| 10 |
import prompts_lib as my_prompts
|
|
@@ -29,7 +29,7 @@ llm = AzureChatOpenAI(
|
|
| 29 |
)
|
| 30 |
|
| 31 |
# bild tools
|
| 32 |
-
tools = [duckduck_tool]
|
| 33 |
chat_w_tools = llm.bind_tools(tools)
|
| 34 |
|
| 35 |
# load system prompt
|
|
|
|
| 4 |
from langchain_core.messages import AnyMessage, SystemMessage
|
| 5 |
from langchain_openai import AzureChatOpenAI
|
| 6 |
from langgraph.graph import START, StateGraph
|
| 7 |
+
from tools import duckduck_tool, wiki_RAG_tool
|
| 8 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 9 |
|
| 10 |
import prompts_lib as my_prompts
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
# bild tools
|
| 32 |
+
tools = [duckduck_tool, wiki_RAG_tool]
|
| 33 |
chat_w_tools = llm.bind_tools(tools)
|
| 34 |
|
| 35 |
# load system prompt
|
requirements.txt
CHANGED
|
@@ -4,6 +4,7 @@ langgraph
|
|
| 4 |
langchain-openai
|
| 5 |
dotenv
|
| 6 |
langchain-community
|
|
|
|
| 7 |
ddgs
|
| 8 |
langfuse
|
| 9 |
wikipedia
|
|
|
|
| 4 |
langchain-openai
|
| 5 |
dotenv
|
| 6 |
langchain-community
|
| 7 |
+
langchain-chroma
|
| 8 |
ddgs
|
| 9 |
langfuse
|
| 10 |
wikipedia
|
test.ipynb
CHANGED
|
@@ -14,7 +14,21 @@
|
|
| 14 |
},
|
| 15 |
{
|
| 16 |
"cell_type": "code",
|
| 17 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"id": "bbd5b78b",
|
| 19 |
"metadata": {},
|
| 20 |
"outputs": [],
|
|
@@ -25,7 +39,7 @@
|
|
| 25 |
},
|
| 26 |
{
|
| 27 |
"cell_type": "code",
|
| 28 |
-
"execution_count":
|
| 29 |
"id": "9d00e0ae",
|
| 30 |
"metadata": {},
|
| 31 |
"outputs": [],
|
|
@@ -44,7 +58,7 @@
|
|
| 44 |
},
|
| 45 |
{
|
| 46 |
"cell_type": "code",
|
| 47 |
-
"execution_count":
|
| 48 |
"id": "e097a098",
|
| 49 |
"metadata": {},
|
| 50 |
"outputs": [],
|
|
@@ -79,7 +93,7 @@
|
|
| 79 |
},
|
| 80 |
{
|
| 81 |
"cell_type": "code",
|
| 82 |
-
"execution_count":
|
| 83 |
"id": "af68a6a5",
|
| 84 |
"metadata": {},
|
| 85 |
"outputs": [],
|
|
@@ -89,7 +103,7 @@
|
|
| 89 |
},
|
| 90 |
{
|
| 91 |
"cell_type": "code",
|
| 92 |
-
"execution_count":
|
| 93 |
"id": "02c87f0c",
|
| 94 |
"metadata": {},
|
| 95 |
"outputs": [
|
|
@@ -99,7 +113,7 @@
|
|
| 99 |
"text": [
|
| 100 |
"Agent received question (first 50 chars): \n",
|
| 101 |
"How many studio albums were published by Mercedes...\n",
|
| 102 |
-
"Agent returning answer: \n"
|
| 103 |
]
|
| 104 |
}
|
| 105 |
],
|
|
@@ -120,6 +134,25 @@
|
|
| 120 |
{
|
| 121 |
"cell_type": "code",
|
| 122 |
"execution_count": null,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
"id": "069db350",
|
| 124 |
"metadata": {},
|
| 125 |
"outputs": [
|
|
@@ -127,19 +160,93 @@
|
|
| 127 |
"name": "stdout",
|
| 128 |
"output_type": "stream",
|
| 129 |
"text": [
|
| 130 |
-
"<class 'langchain_core.messages.human.HumanMessage'>: \n",
|
| 131 |
-
"
|
| 132 |
-
"
|
| 133 |
-
"
|
| 134 |
-
"<class 'langchain_core.messages.
|
| 135 |
-
"
|
| 136 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
]
|
| 138 |
}
|
| 139 |
],
|
| 140 |
"source": [
|
| 141 |
"for m in agent.responses[-1].get('messages'):\n",
|
| 142 |
-
"
|
| 143 |
]
|
| 144 |
},
|
| 145 |
{
|
|
@@ -182,7 +289,16 @@
|
|
| 182 |
"id": "d57e9368",
|
| 183 |
"metadata": {},
|
| 184 |
"outputs": [],
|
| 185 |
-
"source": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
}
|
| 187 |
],
|
| 188 |
"metadata": {
|
|
|
|
| 14 |
},
|
| 15 |
{
|
| 16 |
"cell_type": "code",
|
| 17 |
+
"execution_count": 30,
|
| 18 |
+
"id": "fc82f9e4",
|
| 19 |
+
"metadata": {},
|
| 20 |
+
"outputs": [],
|
| 21 |
+
"source": [
|
| 22 |
+
"import textwrap\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"def print_(message: str):\n",
|
| 25 |
+
"\n",
|
| 26 |
+
" print(textwrap.fill(message, 75))"
|
| 27 |
+
]
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"cell_type": "code",
|
| 31 |
+
"execution_count": 24,
|
| 32 |
"id": "bbd5b78b",
|
| 33 |
"metadata": {},
|
| 34 |
"outputs": [],
|
|
|
|
| 39 |
},
|
| 40 |
{
|
| 41 |
"cell_type": "code",
|
| 42 |
+
"execution_count": 25,
|
| 43 |
"id": "9d00e0ae",
|
| 44 |
"metadata": {},
|
| 45 |
"outputs": [],
|
|
|
|
| 58 |
},
|
| 59 |
{
|
| 60 |
"cell_type": "code",
|
| 61 |
+
"execution_count": 26,
|
| 62 |
"id": "e097a098",
|
| 63 |
"metadata": {},
|
| 64 |
"outputs": [],
|
|
|
|
| 93 |
},
|
| 94 |
{
|
| 95 |
"cell_type": "code",
|
| 96 |
+
"execution_count": 27,
|
| 97 |
"id": "af68a6a5",
|
| 98 |
"metadata": {},
|
| 99 |
"outputs": [],
|
|
|
|
| 103 |
},
|
| 104 |
{
|
| 105 |
"cell_type": "code",
|
| 106 |
+
"execution_count": 28,
|
| 107 |
"id": "02c87f0c",
|
| 108 |
"metadata": {},
|
| 109 |
"outputs": [
|
|
|
|
| 113 |
"text": [
|
| 114 |
"Agent received question (first 50 chars): \n",
|
| 115 |
"How many studio albums were published by Mercedes...\n",
|
| 116 |
+
"Agent returning answer: Three\n"
|
| 117 |
]
|
| 118 |
}
|
| 119 |
],
|
|
|
|
| 134 |
{
|
| 135 |
"cell_type": "code",
|
| 136 |
"execution_count": null,
|
| 137 |
+
"id": "0bc839a3",
|
| 138 |
+
"metadata": {},
|
| 139 |
+
"outputs": [
|
| 140 |
+
{
|
| 141 |
+
"data": {
|
| 142 |
+
"text/plain": [
|
| 143 |
+
"ToolMessage(content='In the early 1970s, Sosa released two concept albums in collaboration with composer Ariel Ramírez and lyricist Félix Luna: Cantata Sudamericana and Mujeres Argentinas (Argentine Women). She also recorded a tribute to Chilean musician Violeta Parra in 1971, including what was to become one of Sosa\\'s signature songs, Gracias a la vida. She further popularized of songs written by Milton Nascimento of Brazil and Pablo Milanés and Silvio Rodríguez both from Cuba. Throughout the decade, she released albums such as Hasta la Victoria in 1972 and Traigo un Pueblo en mi Voz in 1973. They featured songs like \"Cuando tenga la tierra\", written by Ariel Petrocelli and Daniel Toro, which tackles political and social issues like wealth and land inequality. During the 1970s she was a part of two films by the director Leopoldo Torre Nilsson: El Santo de la Espada in 1970 and Güemes, la tierra en armas in 1971, in which she portrayed Juana Azurduy de Padilla, the guerrilla military leader who fought for\\nThe double album was a commercial success, being certified platinum by the CAPIF selling more than 200,000 copies in Argentina, Cantora 1 was also certified platinum selling 40,000 copies while Cantora 2 was certified gold selling 20,000 copies. The album also peaked at numbers 22 and 8 at the Top Latin Albums and Latin Pop Albums charts in United States, respectively, being Sosa\\'s only appearances on both charts.\\nAt documentary film titled Mercedes Sosa, Cantora un viaje íntimo was released on 2009, it was directed by Rodrigo Vila and features the recording process of the album as well as testimonies from the different guest artists that appeared on the project.\\nHaydée Mercedes Sosa (9 July 1935 – 4 October 2009) was an Argentine singer who was popular throughout Latin America and many countries outside the region. With her roots in Argentine folk music, Sosa became one of the preeminent exponents of El nuevo cancionero. She gave voice to songs written by many Latin American songwriters. Her music made people hail her as the \"voice of the voiceless ones\". She was often called \"the conscience of Latin America\".\\nCantora, un Viaje Íntimo (English: Cantora, An Intimate Journey) is a double album by Argentine singer Mercedes Sosa, released on 2009 through Sony Music Argentina. The album features Cantora 1 and Cantora 2, the project is Sosa\\'s final album before her death on October 4, 2009.\\nAt the 10th Annual Latin Grammy Awards, Cantora 1 was nominated for Album of the Year and won Best Folk Album and Best Recording Package, the latter award went to Alejandro Ros, the art director of the album. Additionally, Sosa won two out of five nominations for the albums at the Gardel Awards 2010, the double album was nominated for Album of the Year and Production of the Year and won Best DVD while both Cantora 1 and Cantora 2 were nominated for Best Female Folk Album, with the former winning the category.\\npublic. Sosa and her first husband, Manuel Oscar Matus, with whom she had one son, were key players in the mid-60s nueva canción movement (which was called nuevo cancionero in Argentina). Her second record was Canciones con Fundamento, a collection of Argentine folk songs.\\n== Background ==\\nThe albums were produced by Popi Spatocco, frequent collaborator of Sosa, engineered by Jorge \"Portugués\" Da Silva and recorded from May 2008 to June 2009 at Estudios Panda and Estudios Ion, both in Buenos Aires, Argentina, the first session took place at Estudios Panda while the rest of the album was recorded at Estudios Ion, the first songs to be recorded were \"Agua, Fuego, Tierra y Viento\" with Argentine singer Soledad Pastorutti and \"Misionera\" with Brazilian accordionist Luiz Carlos Borges, the recording of the album was marked by interruptions due to the delicate health condition of Sosa, she would eventually die on October 16, 2009 at age 74, a couple of months following the release of the albums, about Cantora, Spatocco said that \"it was like receiving a diploma of honor, she was dedicated to whatever happened because she knew that I had her back\".\\nSosa \"spent the late 1960s building her audience in Europe and among the cosmopolitan middle class in Buenos Aires, becoming in the process a much bigger star\" than her contemporaries. In 1967, Sosa toured the United States and Europe with great success. In later years, she performed and recorded extensively, broadening her repertoire to include material from throughout Latin America.\\nSosa was born on 9 July 1935, in San Miguel de Tucumán, in the northwestern Argentine province of Tucumán, of mestizo ancestry. She was of French, Spanish and Diaguita descent. Her nickname \"la negra\", which is a common nickname in Argentina for people with darker complexion, is a reference to her indigenous heritage. Her parents, a day laborer and a washerwoman, were Peronists, although they never registered in the party, and she started her career as a singer for the Peronist Party in Tucuman under the name Gladys Osorio. In 1950, at age fifteen, she won a singing competition organized by a local radio station and was given a contract to perform for two months. She recorded her first album, La Voz de la Zafra, in 1959. A performance at the 1965 Cosquín National Folklore Festival—where she was introduced and brought to the stage while sitting in the audience by fellow folk singer Jorge Cafrune— brought her to the attention of the Argentine public. Sosa and her first husband, Manuel', name='wikipedia_search_RAG', id='1b470c1c-7195-418f-85a2-543f6b84e3f4', tool_call_id='call_IDMJMtIXcwRBnCzoPPukPTpK')"
|
| 144 |
+
]
|
| 145 |
+
},
|
| 146 |
+
"execution_count": 33,
|
| 147 |
+
"metadata": {},
|
| 148 |
+
"output_type": "execute_result"
|
| 149 |
+
}
|
| 150 |
+
],
|
| 151 |
+
"source": []
|
| 152 |
+
},
|
| 153 |
+
{
|
| 154 |
+
"cell_type": "code",
|
| 155 |
+
"execution_count": 31,
|
| 156 |
"id": "069db350",
|
| 157 |
"metadata": {},
|
| 158 |
"outputs": [
|
|
|
|
| 160 |
"name": "stdout",
|
| 161 |
"output_type": "stream",
|
| 162 |
"text": [
|
| 163 |
+
"<class 'langchain_core.messages.human.HumanMessage'>: How many studio\n",
|
| 164 |
+
"albums were published by Mercedes Sousa between 2000 and 2009 included? you\n",
|
| 165 |
+
"can use the latest 2022 version of the english wikipedia\n",
|
| 166 |
+
"<class 'langchain_core.messages.ai.AIMessage'>:\n",
|
| 167 |
+
"<class 'langchain_core.messages.tool.ToolMessage'>: In the early 1970s,\n",
|
| 168 |
+
"Sosa released two concept albums in collaboration with composer Ariel\n",
|
| 169 |
+
"Ramírez and lyricist Félix Luna: Cantata Sudamericana and Mujeres\n",
|
| 170 |
+
"Argentinas (Argentine Women). She also recorded a tribute to Chilean\n",
|
| 171 |
+
"musician Violeta Parra in 1971, including what was to become one of Sosa's\n",
|
| 172 |
+
"signature songs, Gracias a la vida. She further popularized of songs\n",
|
| 173 |
+
"written by Milton Nascimento of Brazil and Pablo Milanés and Silvio\n",
|
| 174 |
+
"Rodríguez both from Cuba. Throughout the decade, she released albums such\n",
|
| 175 |
+
"as Hasta la Victoria in 1972 and Traigo un Pueblo en mi Voz in 1973. They\n",
|
| 176 |
+
"featured songs like \"Cuando tenga la tierra\", written by Ariel Petrocelli\n",
|
| 177 |
+
"and Daniel Toro, which tackles political and social issues like wealth and\n",
|
| 178 |
+
"land inequality. During the 1970s she was a part of two films by the\n",
|
| 179 |
+
"director Leopoldo Torre Nilsson: El Santo de la Espada in 1970 and Güemes,\n",
|
| 180 |
+
"la tierra en armas in 1971, in which she portrayed Juana Azurduy de\n",
|
| 181 |
+
"Padilla, the guerrilla military leader who fought for The double album was\n",
|
| 182 |
+
"a commercial success, being certified platinum by the CAPIF selling more\n",
|
| 183 |
+
"than 200,000 copies in Argentina, Cantora 1 was also certified platinum\n",
|
| 184 |
+
"selling 40,000 copies while Cantora 2 was certified gold selling 20,000\n",
|
| 185 |
+
"copies. The album also peaked at numbers 22 and 8 at the Top Latin Albums\n",
|
| 186 |
+
"and Latin Pop Albums charts in United States, respectively, being Sosa's\n",
|
| 187 |
+
"only appearances on both charts. At documentary film titled Mercedes Sosa,\n",
|
| 188 |
+
"Cantora un viaje íntimo was released on 2009, it was directed by Rodrigo\n",
|
| 189 |
+
"Vila and features the recording process of the album as well as testimonies\n",
|
| 190 |
+
"from the different guest artists that appeared on the project. Haydée\n",
|
| 191 |
+
"Mercedes Sosa (9 July 1935 – 4 October 2009) was an Argentine singer who\n",
|
| 192 |
+
"was popular throughout Latin America and many countries outside the region.\n",
|
| 193 |
+
"With her roots in Argentine folk music, Sosa became one of the preeminent\n",
|
| 194 |
+
"exponents of El nuevo cancionero. She gave voice to songs written by many\n",
|
| 195 |
+
"Latin American songwriters. Her music made people hail her as the \"voice of\n",
|
| 196 |
+
"the voiceless ones\". She was often called \"the conscience of Latin\n",
|
| 197 |
+
"America\". Cantora, un Viaje Íntimo (English: Cantora, An Intimate Journey)\n",
|
| 198 |
+
"is a double album by Argentine singer Mercedes Sosa, released on 2009\n",
|
| 199 |
+
"through Sony Music Argentina. The album features Cantora 1 and Cantora 2,\n",
|
| 200 |
+
"the project is Sosa's final album before her death on October 4, 2009. At\n",
|
| 201 |
+
"the 10th Annual Latin Grammy Awards, Cantora 1 was nominated for Album of\n",
|
| 202 |
+
"the Year and won Best Folk Album and Best Recording Package, the latter\n",
|
| 203 |
+
"award went to Alejandro Ros, the art director of the album. Additionally,\n",
|
| 204 |
+
"Sosa won two out of five nominations for the albums at the Gardel Awards\n",
|
| 205 |
+
"2010, the double album was nominated for Album of the Year and Production\n",
|
| 206 |
+
"of the Year and won Best DVD while both Cantora 1 and Cantora 2 were\n",
|
| 207 |
+
"nominated for Best Female Folk Album, with the former winning the category.\n",
|
| 208 |
+
"public. Sosa and her first husband, Manuel Oscar Matus, with whom she had\n",
|
| 209 |
+
"one son, were key players in the mid-60s nueva canción movement (which was\n",
|
| 210 |
+
"called nuevo cancionero in Argentina). Her second record was Canciones con\n",
|
| 211 |
+
"Fundamento, a collection of Argentine folk songs. == Background == The\n",
|
| 212 |
+
"albums were produced by Popi Spatocco, frequent collaborator of Sosa,\n",
|
| 213 |
+
"engineered by Jorge \"Portugués\" Da Silva and recorded from May 2008 to June\n",
|
| 214 |
+
"2009 at Estudios Panda and Estudios Ion, both in Buenos Aires, Argentina,\n",
|
| 215 |
+
"the first session took place at Estudios Panda while the rest of the album\n",
|
| 216 |
+
"was recorded at Estudios Ion, the first songs to be recorded were \"Agua,\n",
|
| 217 |
+
"Fuego, Tierra y Viento\" with Argentine singer Soledad Pastorutti and\n",
|
| 218 |
+
"\"Misionera\" with Brazilian accordionist Luiz Carlos Borges, the recording\n",
|
| 219 |
+
"of the album was marked by interruptions due to the delicate health\n",
|
| 220 |
+
"condition of Sosa, she would eventually die on October 16, 2009 at age 74,\n",
|
| 221 |
+
"a couple of months following the release of the albums, about Cantora,\n",
|
| 222 |
+
"Spatocco said that \"it was like receiving a diploma of honor, she was\n",
|
| 223 |
+
"dedicated to whatever happened because she knew that I had her back\". Sosa\n",
|
| 224 |
+
"\"spent the late 1960s building her audience in Europe and among the\n",
|
| 225 |
+
"cosmopolitan middle class in Buenos Aires, becoming in the process a much\n",
|
| 226 |
+
"bigger star\" than her contemporaries. In 1967, Sosa toured the United\n",
|
| 227 |
+
"States and Europe with great success. In later years, she performed and\n",
|
| 228 |
+
"recorded extensively, broadening her repertoire to include material from\n",
|
| 229 |
+
"throughout Latin America. Sosa was born on 9 July 1935, in San Miguel de\n",
|
| 230 |
+
"Tucumán, in the northwestern Argentine province of Tucumán, of mestizo\n",
|
| 231 |
+
"ancestry. She was of French, Spanish and Diaguita descent. Her nickname \"la\n",
|
| 232 |
+
"negra\", which is a common nickname in Argentina for people with darker\n",
|
| 233 |
+
"complexion, is a reference to her indigenous heritage. Her parents, a day\n",
|
| 234 |
+
"laborer and a washerwoman, were Peronists, although they never registered\n",
|
| 235 |
+
"in the party, and she started her career as a singer for the Peronist Party\n",
|
| 236 |
+
"in Tucuman under the name Gladys Osorio. In 1950, at age fifteen, she won a\n",
|
| 237 |
+
"singing competition organized by a local radio station and was given a\n",
|
| 238 |
+
"contract to perform for two months. She recorded her first album, La Voz de\n",
|
| 239 |
+
"la Zafra, in 1959. A performance at the 1965 Cosquín National Folklore\n",
|
| 240 |
+
"Festival—where she was introduced and brought to the stage while sitting in\n",
|
| 241 |
+
"the audience by fellow folk singer Jorge Cafrune— brought her to the\n",
|
| 242 |
+
"attention of the Argentine public. Sosa and her first husband, Manuel\n",
|
| 243 |
+
"<class 'langchain_core.messages.ai.AIMessage'>: Three\n"
|
| 244 |
]
|
| 245 |
}
|
| 246 |
],
|
| 247 |
"source": [
|
| 248 |
"for m in agent.responses[-1].get('messages'):\n",
|
| 249 |
+
" print_(f'{type(m)}: {m.content}')"
|
| 250 |
]
|
| 251 |
},
|
| 252 |
{
|
|
|
|
| 289 |
"id": "d57e9368",
|
| 290 |
"metadata": {},
|
| 291 |
"outputs": [],
|
| 292 |
+
"source": [
|
| 293 |
+
"user_input=\"Hi there!, My name is subitha, I am a software engineer. Can you tell me about the latest research on quantum computing?\"\n",
|
| 294 |
+
"\n",
|
| 295 |
+
"events=graph.stream(\n",
|
| 296 |
+
" {\"messages\": [(\"user\", user_input)]},stream_mode=\"values\"\n",
|
| 297 |
+
")\n",
|
| 298 |
+
"\n",
|
| 299 |
+
"for event in events:\n",
|
| 300 |
+
" event[\"messages\"][-1].pretty_print()"
|
| 301 |
+
]
|
| 302 |
}
|
| 303 |
],
|
| 304 |
"metadata": {
|
tools.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
|
| 2 |
from langchain.tools import Tool
|
| 3 |
from langchain_community.utilities import WikipediaAPIWrapper
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
duckduck_tool = Tool(
|
| 6 |
name="duckduckgo_search",
|
|
@@ -9,5 +13,42 @@ duckduck_tool = Tool(
|
|
| 9 |
)
|
| 10 |
|
| 11 |
|
| 12 |
-
wikipedia = WikipediaAPIWrapper(top_k_results=1,doc_content_chars_max=
|
| 13 |
-
wikipedia_tool = WikipediaQueryRun(api_wrapper=wikipedia)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
|
| 2 |
from langchain.tools import Tool
|
| 3 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 4 |
+
from langchain_community.document_loaders import WikipediaLoader
|
| 5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain_chroma import Chroma
|
| 7 |
+
from langchain_openai import AzureOpenAIEmbeddings
|
| 8 |
|
| 9 |
duckduck_tool = Tool(
|
| 10 |
name="duckduckgo_search",
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
|
| 16 |
+
wikipedia = WikipediaAPIWrapper(top_k_results=1,doc_content_chars_max=3000)
|
| 17 |
+
wikipedia_tool = WikipediaQueryRun(api_wrapper=wikipedia)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
embeddings = AzureOpenAIEmbeddings(
|
| 24 |
+
model="text-embedding-3-large",
|
| 25 |
+
# dimensions: Optional[int] = None, # Can specify dimensions with new text-embedding-3 models
|
| 26 |
+
# azure_endpoint="https://<your-endpoint>.openai.azure.com/", If not provided, will read env variable AZURE_OPENAI_ENDPOINT
|
| 27 |
+
# api_key=... # Can provide an API key directly. If missing read env variable AZURE_OPENAI_API_KEY
|
| 28 |
+
# openai_api_version=..., # If not provided, will read env variable AZURE_OPENAI_API_VERSION
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
def wiki_RAG(query: str):
|
| 32 |
+
"""####"""
|
| 33 |
+
|
| 34 |
+
loader = WikipediaLoader(query=query, load_max_docs=2)
|
| 35 |
+
docs = loader.load()
|
| 36 |
+
|
| 37 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=50)
|
| 38 |
+
doc_splits = text_splitter.split_documents(docs)
|
| 39 |
+
|
| 40 |
+
vectorstore = Chroma.from_documents(
|
| 41 |
+
documents=doc_splits,
|
| 42 |
+
collection_name="wiki",
|
| 43 |
+
embedding=embeddings,
|
| 44 |
+
)
|
| 45 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 8})
|
| 46 |
+
results = retriever.invoke(query)
|
| 47 |
+
# return results
|
| 48 |
+
return "\n".join([doc.page_content for doc in results])
|
| 49 |
+
|
| 50 |
+
wiki_RAG_tool = Tool(
|
| 51 |
+
name="wikipedia_search_RAG",
|
| 52 |
+
func=wiki_RAG,
|
| 53 |
+
description="Searches information in wikipedia."
|
| 54 |
+
)
|
tools_test.ipynb
CHANGED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "ccd01f7c",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"%load_ext autoreload\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"%autoreload 2"
|
| 13 |
+
]
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"cell_type": "code",
|
| 17 |
+
"execution_count": 17,
|
| 18 |
+
"id": "c14296ff",
|
| 19 |
+
"metadata": {},
|
| 20 |
+
"outputs": [],
|
| 21 |
+
"source": [
|
| 22 |
+
"import textwrap\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"def print_(message: str):\n",
|
| 25 |
+
"\n",
|
| 26 |
+
" print(textwrap.fill(message, 75))"
|
| 27 |
+
]
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"cell_type": "code",
|
| 31 |
+
"execution_count": null,
|
| 32 |
+
"id": "4ec297d7",
|
| 33 |
+
"metadata": {},
|
| 34 |
+
"outputs": [],
|
| 35 |
+
"source": [
|
| 36 |
+
"import tools\n"
|
| 37 |
+
]
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"cell_type": "code",
|
| 41 |
+
"execution_count": 9,
|
| 42 |
+
"id": "7cf442f8",
|
| 43 |
+
"metadata": {},
|
| 44 |
+
"outputs": [
|
| 45 |
+
{
|
| 46 |
+
"data": {
|
| 47 |
+
"text/plain": [
|
| 48 |
+
"WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper(wiki_client=<module 'wikipedia' from 'c:\\\\Users\\\\andrea.giorni\\\\Desktop\\\\code\\\\Hugging face Agent course\\\\Final_Assignment_Template\\\\.venv\\\\Lib\\\\site-packages\\\\wikipedia\\\\__init__.py'>, top_k_results=1, lang='en', load_all_available_meta=False, doc_content_chars_max=300))"
|
| 49 |
+
]
|
| 50 |
+
},
|
| 51 |
+
"execution_count": 9,
|
| 52 |
+
"metadata": {},
|
| 53 |
+
"output_type": "execute_result"
|
| 54 |
+
}
|
| 55 |
+
],
|
| 56 |
+
"source": [
|
| 57 |
+
"tools.wikipedia_tool"
|
| 58 |
+
]
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"cell_type": "code",
|
| 62 |
+
"execution_count": 4,
|
| 63 |
+
"id": "3f538870",
|
| 64 |
+
"metadata": {},
|
| 65 |
+
"outputs": [],
|
| 66 |
+
"source": [
|
| 67 |
+
"out = tools.wikipedia_tool.invoke(\"who is germany chancellor?\")"
|
| 68 |
+
]
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"cell_type": "code",
|
| 72 |
+
"execution_count": 5,
|
| 73 |
+
"id": "ef465273",
|
| 74 |
+
"metadata": {},
|
| 75 |
+
"outputs": [
|
| 76 |
+
{
|
| 77 |
+
"name": "stdout",
|
| 78 |
+
"output_type": "stream",
|
| 79 |
+
"text": [
|
| 80 |
+
"Page: Chancellor of Germany\n",
|
| 81 |
+
"Summary: The chancellor of Germany, officially the federal chancellor of the Federal Republic of Germany, is the head of the federal government of Germany. The chancellor is the chief executive of the Federal Cabinet and heads the executive branch. The chancellor is elect\n"
|
| 82 |
+
]
|
| 83 |
+
}
|
| 84 |
+
],
|
| 85 |
+
"source": [
|
| 86 |
+
"print(out)"
|
| 87 |
+
]
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"cell_type": "code",
|
| 91 |
+
"execution_count": 10,
|
| 92 |
+
"id": "a9768b29",
|
| 93 |
+
"metadata": {},
|
| 94 |
+
"outputs": [],
|
| 95 |
+
"source": [
|
| 96 |
+
"res = tools.wiki_RAG('who is germany chancellor?')"
|
| 97 |
+
]
|
| 98 |
+
},
|
| 99 |
+
{
|
| 100 |
+
"cell_type": "code",
|
| 101 |
+
"execution_count": 22,
|
| 102 |
+
"id": "d140be5f",
|
| 103 |
+
"metadata": {},
|
| 104 |
+
"outputs": [],
|
| 105 |
+
"source": [
|
| 106 |
+
"res2 = tools.wiki_RAG('the list of german chancellors')"
|
| 107 |
+
]
|
| 108 |
+
},
|
| 109 |
+
{
|
| 110 |
+
"cell_type": "code",
|
| 111 |
+
"execution_count": 7,
|
| 112 |
+
"id": "7f334a2f",
|
| 113 |
+
"metadata": {},
|
| 114 |
+
"outputs": [
|
| 115 |
+
{
|
| 116 |
+
"name": "stdout",
|
| 117 |
+
"output_type": "stream",
|
| 118 |
+
"text": [
|
| 119 |
+
"The chancellor of Germany, officially the federal chancellor of the Federal Republic of Germany, is the head of the federal government of Germany. The chancellor is the chief executive of the Federal Cabinet and heads the executive branch. The chancellor is elected by the Bundestag on the proposal of the federal president and without debate (Article 63 of the German Constitution). During a state of defence declared by the Bundestag the chancellor also assumes the position of commander-in-chief of the Bundeswehr.\n",
|
| 120 |
+
"Ten people (nine men and one woman) have served as chancellor of the Federal Republic of Germany, the first being Konrad Adenauer from 1949 to 1963. (Another 26 men had served as \"Reich chancellors\" of the previous German Empire from 1871 to 1945.) The current officeholder is Friedrich Merz of the Christian Democratic Union, sworn in on 6 May 2025.\n",
|
| 121 |
+
"\n",
|
| 122 |
+
"\n",
|
| 123 |
+
"== History of the office (pre-1949) ==\n",
|
| 124 |
+
"chancellor of the Federal Republic of Germany, is the head of the federal government of Germany.\n",
|
| 125 |
+
"The chancellor of Germany, officially the federal chancellor of the Federal Republic of Germany, is\n",
|
| 126 |
+
"The chancellor of Germany is the political leader of Germany and the head of the federal government.\n",
|
| 127 |
+
"The chancellor of Germany is the political leader of Germany and the head of the federal government. The office holder is responsible for selecting all other members of the government and chairing cabinet meetings.\n",
|
| 128 |
+
"The office was created in the North German Confederation in 1867, when Otto von Bismarck became the first chancellor. With the unification of Germany and establishment of the German Empire in 1871, the Confederation evolved into a German nation-state and its leader became known as the chancellor of Germany. Originally, the chancellor was only responsible to the emperor. This changed with the constitutional reform in 1918, when the Parliament was given the right to dismiss the chancellor. Under the 1919 Weimar Constitution the chancellors were appointed by the directly elected president, but were responsible to Parliament.\n"
|
| 129 |
+
]
|
| 130 |
+
}
|
| 131 |
+
],
|
| 132 |
+
"source": [
|
| 133 |
+
"print(res)"
|
| 134 |
+
]
|
| 135 |
+
},
|
| 136 |
+
{
|
| 137 |
+
"cell_type": "code",
|
| 138 |
+
"execution_count": 25,
|
| 139 |
+
"id": "a6933d2d",
|
| 140 |
+
"metadata": {},
|
| 141 |
+
"outputs": [
|
| 142 |
+
{
|
| 143 |
+
"name": "stdout",
|
| 144 |
+
"output_type": "stream",
|
| 145 |
+
"text": [
|
| 146 |
+
"== Federal Republic of Germany (1949–present) == In 1949, two separate\n",
|
| 147 |
+
"German states were established: the Federal Republic of Germany (known as\n",
|
| 148 |
+
"West Germany) and the German Democratic Republic (known as East Germany).\n",
|
| 149 |
+
"The list below gives the chancellors of West Germany; the government of\n",
|
| 150 |
+
"East Germany was headed by the chairman of the Council of Ministers. In\n",
|
| 151 |
+
"1990, East Germany was dissolved as it merged with West Germany; Germany\n",
|
| 152 |
+
"was reunified. It retained the name of the Federal Republic of Germany.\n",
|
| 153 |
+
"Political parties: CDU (6) SPD (4) == Timeline == == See also ==\n",
|
| 154 |
+
"Leadership of East Germany List of chancellors of Germany by time in offi\n"
|
| 155 |
+
]
|
| 156 |
+
}
|
| 157 |
+
],
|
| 158 |
+
"source": [
|
| 159 |
+
"print_(res2[2].page_content)"
|
| 160 |
+
]
|
| 161 |
+
},
|
| 162 |
+
{
|
| 163 |
+
"cell_type": "code",
|
| 164 |
+
"execution_count": null,
|
| 165 |
+
"id": "08e04f44",
|
| 166 |
+
"metadata": {},
|
| 167 |
+
"outputs": [],
|
| 168 |
+
"source": []
|
| 169 |
+
}
|
| 170 |
+
],
|
| 171 |
+
"metadata": {
|
| 172 |
+
"kernelspec": {
|
| 173 |
+
"display_name": ".venv (3.13.6)",
|
| 174 |
+
"language": "python",
|
| 175 |
+
"name": "python3"
|
| 176 |
+
},
|
| 177 |
+
"language_info": {
|
| 178 |
+
"codemirror_mode": {
|
| 179 |
+
"name": "ipython",
|
| 180 |
+
"version": 3
|
| 181 |
+
},
|
| 182 |
+
"file_extension": ".py",
|
| 183 |
+
"mimetype": "text/x-python",
|
| 184 |
+
"name": "python",
|
| 185 |
+
"nbconvert_exporter": "python",
|
| 186 |
+
"pygments_lexer": "ipython3",
|
| 187 |
+
"version": "3.13.6"
|
| 188 |
+
}
|
| 189 |
+
},
|
| 190 |
+
"nbformat": 4,
|
| 191 |
+
"nbformat_minor": 5
|
| 192 |
+
}
|