langchain agents
This commit is contained in:
201
agents/conversation_agents.ipynb
Normal file
201
agents/conversation_agents.ipynb
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "cc23a178-8b8e-4fa4-aa6c-4ad61c6cf11b",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Conversation Agents"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "de022b79-4eb6-46d8-bc0b-0c8acdb51722",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain.agents import tool\n",
|
||||||
|
"from langchain.agents import load_tools\n",
|
||||||
|
"from langchain.agents import initialize_agent\n",
|
||||||
|
"from langchain.agents import AgentType\n",
|
||||||
|
"from langchain.chat_models import ChatOpenAI\n",
|
||||||
|
"from langchain.memory import ConversationBufferMemory"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "4d53be2b-0081-406f-8cf4-9ab7c39deefc",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"memory = ConversationBufferMemory(memory_key=\"chat_history\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "ca455b6f-8e51-4095-83b6-023a91ebe181",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"llm=ChatOpenAI(temperature=0)\n",
|
||||||
|
"tools = load_tools([\"llm-math\"], llm=llm)\n",
|
||||||
|
"agent_chain = initialize_agent(tools,\n",
|
||||||
|
" llm,\n",
|
||||||
|
" agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,\n",
|
||||||
|
" verbose=True,\n",
|
||||||
|
" memory=memory)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "1233e7a0-a460-42d1-b999-f3e0d16d22d5",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||||||
|
"\u001b[32;1m\u001b[1;3mThought: Do I need to use a tool? No\n",
|
||||||
|
"AI: Thai cuisine is known for its bold flavors and aromatic ingredients. Here are a few popular Thai food recipes that you might enjoy:\n",
|
||||||
|
"\n",
|
||||||
|
"1. Pad Thai: This stir-fried noodle dish is a classic Thai favorite. It typically includes rice noodles, shrimp or chicken, eggs, tofu, bean sprouts, and a flavorful sauce made from tamarind paste, fish sauce, and lime juice.\n",
|
||||||
|
"\n",
|
||||||
|
"2. Green Curry: Green curry is a spicy and fragrant dish made with a paste of green chilies, lemongrass, galangal, and other herbs and spices. It is usually cooked with coconut milk, vegetables, and your choice of meat or tofu.\n",
|
||||||
|
"\n",
|
||||||
|
"3. Tom Yum Soup: Tom Yum is a hot and sour soup that is often made with shrimp, mushrooms, lemongrass, lime juice, and chili peppers. It has a tangy and refreshing flavor that is both spicy and sour.\n",
|
||||||
|
"\n",
|
||||||
|
"4. Massaman Curry: Massaman curry is a rich and flavorful curry that is influenced by Indian and Malay cuisines. It is typically made with beef or chicken, potatoes, onions, and a fragrant blend of spices such as cinnamon, cardamom, and cloves.\n",
|
||||||
|
"\n",
|
||||||
|
"5. Mango Sticky Rice: For a sweet ending to your Thai meal, try mango sticky rice. It is a popular Thai dessert made with sticky rice, fresh mango slices, and a sweet coconut sauce.\n",
|
||||||
|
"\n",
|
||||||
|
"These are just a few examples, but there are many more delicious Thai dishes to explore. Enjoy cooking and experimenting with Thai cuisine!\u001b[0m\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"'Thai cuisine is known for its bold flavors and aromatic ingredients. Here are a few popular Thai food recipes that you might enjoy:\\n\\n1. Pad Thai: This stir-fried noodle dish is a classic Thai favorite. It typically includes rice noodles, shrimp or chicken, eggs, tofu, bean sprouts, and a flavorful sauce made from tamarind paste, fish sauce, and lime juice.\\n\\n2. Green Curry: Green curry is a spicy and fragrant dish made with a paste of green chilies, lemongrass, galangal, and other herbs and spices. It is usually cooked with coconut milk, vegetables, and your choice of meat or tofu.\\n\\n3. Tom Yum Soup: Tom Yum is a hot and sour soup that is often made with shrimp, mushrooms, lemongrass, lime juice, and chili peppers. It has a tangy and refreshing flavor that is both spicy and sour.\\n\\n4. Massaman Curry: Massaman curry is a rich and flavorful curry that is influenced by Indian and Malay cuisines. It is typically made with beef or chicken, potatoes, onions, and a fragrant blend of spices such as cinnamon, cardamom, and cloves.\\n\\n5. Mango Sticky Rice: For a sweet ending to your Thai meal, try mango sticky rice. It is a popular Thai dessert made with sticky rice, fresh mango slices, and a sweet coconut sauce.\\n\\nThese are just a few examples, but there are many more delicious Thai dishes to explore. Enjoy cooking and experimenting with Thai cuisine!'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"agent_chain.run(input=\"What are some good thai food recipes?\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "278b1a4f-deaf-48db-b343-490e3d5ba935",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||||||
|
"\u001b[32;1m\u001b[1;3mThought: Do I need to use a tool? No\n",
|
||||||
|
"AI: Among the dishes mentioned, the spiciest one is typically the Green Curry. Green curry paste is made with green chilies, which gives it a spicy kick. However, the level of spiciness can vary depending on personal preferences and the amount of chili used in the recipe. If you prefer milder flavors, you can adjust the amount of chili or use a milder curry paste.\u001b[0m\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"'Among the dishes mentioned, the spiciest one is typically the Green Curry. Green curry paste is made with green chilies, which gives it a spicy kick. However, the level of spiciness can vary depending on personal preferences and the amount of chili used in the recipe. If you prefer milder flavors, you can adjust the amount of chili or use a milder curry paste.'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"agent_chain.run(\"Which one of those dishes is the spiciest?\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "d7bb64ea-04b3-4c65-bf82-660710ed7cb3",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
||||||
|
"\u001b[32;1m\u001b[1;3mThought: Do I need to use a tool? No\n",
|
||||||
|
"AI: To make Green Curry, here's a grocery shopping list:\n",
|
||||||
|
"\n",
|
||||||
|
"- Green curry paste\n",
|
||||||
|
"- Coconut milk\n",
|
||||||
|
"- Chicken, beef, shrimp, or tofu (depending on your preference)\n",
|
||||||
|
"- Vegetables such as bell peppers, eggplant, and bamboo shoots\n",
|
||||||
|
"- Thai basil leaves\n",
|
||||||
|
"- Fish sauce\n",
|
||||||
|
"- Palm sugar or brown sugar\n",
|
||||||
|
"- Kaffir lime leaves (optional)\n",
|
||||||
|
"- Thai bird's eye chilies (optional, for extra spiciness)\n",
|
||||||
|
"- Jasmine rice\n",
|
||||||
|
"\n",
|
||||||
|
"These ingredients should be available at most grocery stores or Asian markets. Enjoy cooking your Green Curry!\u001b[0m\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"\"To make Green Curry, here's a grocery shopping list:\\n\\n- Green curry paste\\n- Coconut milk\\n- Chicken, beef, shrimp, or tofu (depending on your preference)\\n- Vegetables such as bell peppers, eggplant, and bamboo shoots\\n- Thai basil leaves\\n- Fish sauce\\n- Palm sugar or brown sugar\\n- Kaffir lime leaves (optional)\\n- Thai bird's eye chilies (optional, for extra spiciness)\\n- Jasmine rice\\n\\nThese ingredients should be available at most grocery stores or Asian markets. Enjoy cooking your Green Curry!\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"agent_chain.run(\"Give a grocery shopping list to make this dish\")"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
||||||
@@ -71,13 +71,44 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 5,
|
||||||
"id": "5b4b284b-6812-447f-958b-45ac6fb7e1da",
|
"id": "5b4b284b-6812-447f-958b-45ac6fb7e1da",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"memory.save_context({},{})"
|
"memory.save_context({\"input\": \"Hello\"},{\"output\": \"Hi!\"})\n",
|
||||||
|
"memory.save_context({\"input\": \"How was your skii trip?\"},\n",
|
||||||
|
" {\"output\": f\"{skii_trip}\"})"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "6bb955b7-4197-4553-a1c3-7ff0e7e4e497",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"{'history': 'System: The human greets the AI and asks about its ski trip. The AI enthusiastically shares about its recent ski trip to Colorado, describing the breathtaking mountains and cozy ski lodge. The AI also mentions feeling nervous at first but receiving helpful instruction from ski instructors. The AI started with easier slopes to get familiar with the snow.'}"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"memory.load_memory_variables({})"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "52bbafdf-b562-41e3-ae04-09aebb1ca0c6",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|||||||
Reference in New Issue
Block a user