Skip to content

Commit

Permalink
[feature] Add Alibaba Cloud dashscoe agent (#14318)
Browse files Browse the repository at this point in the history
* [feature]add dashscope agent

* [feature] update the DashScopeAgent readme.

---------

Co-authored-by: tianbing.ytb <[email protected]>
  • Loading branch information
ZhaoyangLiu-Leo and robinyeeh authored Jun 25, 2024
1 parent c090f32 commit 5a31c8c
Show file tree
Hide file tree
Showing 13 changed files with 715 additions and 0 deletions.
301 changes: 301 additions & 0 deletions docs/docs/examples/agent/dashscope_agent.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "24103c51",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/docs/examples/agent/dashscope_agent.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"id": "99cea58c-48bc-4af6-8358-df9695659983",
"metadata": {},
"source": [
"# DashScope Agent Tutorial"
]
},
{
"cell_type": "markdown",
"id": "673df1fe-eb6c-46ea-9a73-a96e7ae7942e",
"metadata": {},
"source": [
"In this notebook tutorial, we will show you how to call the agent application built in the ModelStudio Platform.\n",
"You can create an agent application in ModelStudio platform by referring to document: [Create application in ModelStudio.](https://help.aliyun.com/document_detail/2586405.html?spm=a2c4g.2400264.0.0.7c2e1ff58BeD83)\n",
"If you open this Notebook on colab, you will probably need to install LlamaIndex 🦙 first and then install the following integration."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4985c578",
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index-agent-dashscope"
]
},
{
"cell_type": "markdown",
"id": "6fe08eb1-e638-4c00-9103-5c305bfacccf",
"metadata": {},
"source": [
"Note: Please note that you have to get <app_id> and <api_key> first and then initialize an agent.\n",
"For how to get <api key>, please refer to guide: [How to get API-KEY in ModelStudio](https://help.aliyun.com/document_detail/2712195.html?spm=a2c4g.2400264.0.0.7c2e1ff5tp740E).\n",
"\n",
"There are two ways you can set api-key when initializing a dashscope agent."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88cf323ebdb5df7c",
"metadata": {},
"outputs": [],
"source": [
"# Set API key by environment variables\n",
"%env DASHSCOPE_API_KEY=your_api_key\n",
"\n",
"# set API key by parameters\n",
"# agent = DashScopeAgent(api_key=\"your_api_key\", app_id=\"your_app_id\")"
]
},
{
"cell_type": "markdown",
"id": "fbc2cec5-6cc0-4814-92a1-ca0bd237528f",
"metadata": {},
"source": [
"## Simple Chat\n",
"Let's start with a simple chat by using dashscope agent."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "08928f6e-610c-420b-8a7b-7a7042bbd6c6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Great choice! Seattle is a fantastic city known for its stunning natural beauty, vibrant culture, and technology scene. Here are some key points to consider when planning your trip to Seattle:\n",
"\n",
"1. **Best Time to Visit**: The best time to visit Seattle is from June to September when the weather is at its warmest and driest. Keep in mind that Seattle is famous for its rain, so packing a waterproof jacket is always a good idea.\n",
"\n",
"2. **Attractions**: Don't miss out on these iconic Seattle attractions:\n",
" - **Space Needle**: Take an elevator ride up for panoramic views of the city.\n",
" - **Pike Place Market**: Watch the famous fish toss and explore local artisan crafts and fresh produce.\n",
" - **Seattle Center**: Home to the Space Needle, Chihuly Garden and Glass, and several museums.\n",
" - **Museum of Pop Culture (MoPOP)**: A must-visit for music and pop culture enthusiasts.\n",
" - **Pioneer Square**: The historic heart of Seattle with underground tours and charming architecture.\n",
" - **Chihuly Garden and Glass**: A stunning display of glass art by Dale Chihuly.\n",
"\n",
"3. **Transportation**: Seattle has a good public transportation system including buses, light rail, and streetcars. Consider getting an ORCA card for easy access to public transit. Ride-sharing services like Uber and Lyft are also widely available.\n",
"\n",
"4. **Accommodation**: Seattle offers a range of accommodations from budget-friendly hostels to luxury hotels. Popular areas to stay include Downtown, Capitol Hill, and Belltown for their proximity to attractions and nightlife.\n",
"\n",
"5. **Food and Drink**: Seattle is famous for its coffee culture, seafood, and international cuisine. Be sure to try some fresh seafood at Pike Place Market, grab a cup of coffee from the original Starbucks, and explore the diverse food scene in neighborhoods like Capitol Hill and International District.\n",
"\n",
"6. **Outdoor Activities**: Seattle is surrounded by natural beauty. Take a ferry to Bainbridge Island, hike in Mount Rainier National Park, or explore the lush forests of Olympic National Park.\n",
"\n",
"7. **Safety Tips**: Like any big city, be aware of your surroundings, especially in crowded tourist areas. Keep valuables secure and use common sense when traveling at night.\n",
"\n",
"Remember to check the latest COVID-19 related travel restrictions and guidelines before planning your trip. Enjoy your adventure in Seattle!\n"
]
}
],
"source": [
"from llama_index.agent.dashscope import DashScopeAgent\n",
"\n",
"agent = DashScopeAgent(\n",
" app_id=\"your_app_id\", # The id of app that you created\n",
" chat_session=True, # Enable chat session which will auto save and pass chat history to LLM.\n",
" verbose=False, # If need to print more details\n",
")\n",
"\n",
"resp = agent.chat(\"I want to travel to Seattle.\")\n",
"print(resp.response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3baab338b0eeff2d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Absolutely! Seattle is filled with tourist attractions that cater to a wide variety of interests. Here are just a few highlights:\n",
"\n",
"1. **Space Needle**: This 605-foot tower offers breathtaking views of the city, Mount Rainier, Elliott Bay, and the surrounding islands. It's an iconic symbol of Seattle.\n",
"\n",
"2. **Pike Place Market**: One of the oldest continuously operated public farmers' markets in the U.S., famous for its flying fish, local crafts, and vibrant atmosphere.\n",
"\n",
"3. **Chihuly Garden and Glass**: A stunning exhibit showcasing the glass artwork of Dale Chihuly, with indoor galleries and an outdoor garden integrated with his colorful, large-scale sculptures.\n",
"\n",
"4. **Museum of Pop Culture (MoPOP)**: A museum dedicated to contemporary popular culture, featuring exhibits on music, science fiction, and pop culture artifacts.\n",
"\n",
"5. **Seattle Aquarium**: Located on Pier 59, it offers a chance to get up close with marine life from the Puget Sound and beyond.\n",
"\n",
"6. **Seattle Great Wheel**: A giant Ferris wheel on Pier 57, providing a unique perspective of the city skyline and waterfront.\n",
"\n",
"7. **Seattle Art Museum (SAM)**: Houses a diverse collection of global art, including modern and ethnic works, as well as rotating exhibitions.\n",
"\n",
"8. **Woodland Park Zoo**: Known for its naturalistic habitats and conservation efforts, this zoo is home to over 1,000 animals from around the world.\n",
"\n",
"9. **Seattle Waterfront**: A lively area with restaurants, shops, and attractions like the Seattle Aquarium and the Great Wheel, offering picturesque views of Elliott Bay.\n",
"\n",
"10. **International District/Chinatown**: Rich in cultural heritage, this neighborhood offers a variety of Asian cuisines, shops, and historical landmarks.\n",
"\n",
"These are just a few examples; Seattle also boasts numerous parks, gardens, and outdoor spaces like Kerry Park, Discovery Park, and the Hiram M. Chittenden Locks, as well as a thriving arts and cultural scene with theaters, galleries, and live music venues.\n"
]
}
],
"source": [
"resp = agent.chat(\"Are there any tourist attractions there?\")\n",
"print(resp.response)"
]
},
{
"cell_type": "markdown",
"id": "b010c45780fa0621",
"metadata": {},
"source": [
"If you want to start with new conversation, you can reset and then call chat function."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "79649fc0b961fa85",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"To provide a relevant answer, I would need to know which location you are referring to. Please specify the place you are interested in, and I can then share information about tourist attractions there.\n"
]
}
],
"source": [
"# reset agent\n",
"agent.reset()\n",
"\n",
"resp = agent.chat(\"Are there any tourist attractions there?\")\n",
"print(resp.response)"
]
},
{
"cell_type": "markdown",
"id": "707d30b8-6405-4187-a9ed-6146dcc42167",
"metadata": {},
"source": [
"## Streaming Chat\n",
"\n",
"DashScope agent can also return response as a generator for chat, so you can stream every delta step."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d852ece7-e5a1-4368-9d59-c7014e0b5b4d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The\n",
"The area\n",
"The area of\n",
"The area of Seattle, Washington, is\n",
"The area of Seattle, Washington, is approximately 142.5 square\n",
"The area of Seattle, Washington, is approximately 142.5 square miles (369 square kilometers).\n",
"The area of Seattle, Washington, is approximately 142.5 square miles (369 square kilometers). This includes both land and water areas within\n",
"The area of Seattle, Washington, is approximately 142.5 square miles (369 square kilometers). This includes both land and water areas within the city limits.\n"
]
}
],
"source": [
"agent.reset()\n",
"\n",
"response = agent.stream_chat(\"What is the area of Seattle?\")\n",
"for resp in response.chat_stream:\n",
" print(resp.message.content)"
]
},
{
"cell_type": "markdown",
"id": "b907939a40079cbb",
"metadata": {},
"source": [
"## Workspace\n",
"\n",
"Sometimes, you have to pass workspace id when you created an agent application in non-default workspace. \n",
"For how to use workspace, please refer to doc: [How to use different workspaces in ModelStudio.](https://help.aliyun.com/document_detail/2587495.html?spm=a2c4g.2712191.0.i29#c5222ec081sbo)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a042333c597bb0a0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"To find the area of a circle when you know the radius (r), you can use the formula:\n",
"\n",
"\\[ \\text{Area} = \\pi r^2 \\]\n",
"\n",
"Given that the radius \\( r = 9.3 \\) meters, substituting this value into the formula gives:\n",
"\n",
"\\[ \\text{Area} = \\pi (9.3)^2 \\]\n",
"\\[ \\text{Area} = \\pi \\times 86.49 \\]\n",
"\\[ \\text{Area} \\approx 270.17 \\text{ square meters} \\]\n",
"\n",
"Therefore, the area of the circle with a radius of 9.3 meters is approximately 270.17 square meters.\n"
]
}
],
"source": [
"agent = DashScopeAgent(\n",
" app_id=\"your_app_id\", # The id of app that you created\n",
" chat_session=True, # Enable chat session which will auto save and pass chat history to LLM.\n",
" workspace=\"your_workspace_id\", # Workspace id that the agent application belongs to.\n",
" verbose=False, # If need to print more details\n",
")\n",
"\n",
"resp = agent.chat(\"The radius is 9.3 meters, what is the area of the circle?\")\n",
"print(resp.response)"
]
}
],
"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"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,7 @@ plugins:
- ../llama-index-integrations/multi_modal_llms/llama-index-multi-modal-llms-dashscope
- ../llama-index-integrations/agent/llama-index-agent-openai
- ../llama-index-integrations/agent/llama-index-agent-openai-legacy
- ../llama-index-integrations/agent/llama-index-agent-dashscope
- ../llama-index-integrations/storage/kvstore/llama-index-storage-kvstore-postgres
- ../llama-index-integrations/storage/kvstore/llama-index-storage-kvstore-redis
- ../llama-index-integrations/storage/kvstore/llama-index-storage-kvstore-firestore
Expand Down
Loading

0 comments on commit 5a31c8c

Please sign in to comment.