You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/ai-agents.md
+30-30Lines changed: 30 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
---
2
2
title: AI agents
3
+
titleSuffix: Azure Cosmos DB
3
4
description: AI agent key concepts and implementation of AI agent memory system.
4
5
author: wmwxwa
5
6
ms.author: wangwilliam
@@ -11,10 +12,10 @@ ms.date: 06/26/2024
11
12
12
13
# AI agents
13
14
14
-
## What are AI Agents?
15
-
16
15
AI agents are designed to perform specific tasks, answer questions, and automate processes for users. These agents vary widely in complexity, ranging from simple chatbots, to copilots, to advanced AI assistants in the form of digital or robotic systems that can execute complex workflows autonomously.
17
16
17
+
## What are AI Agents?
18
+
18
19
### Common features of AI agents
19
20
20
21
-[Planning](#reasoning-and-planning). AI agents can plan and sequence actions to achieve specific goals. The integration of large language models (LLMs) has revolutionized their planning capabilities.
@@ -61,23 +62,23 @@ A multi-agent system provides the following advantages over a copilot or a singl
61
62
- Sophisticated abilities: Multi-agent systems can handle complex or large-scale problems by conducting thorough decision-making processes and distributing tasks among multiple agents.
62
63
- Enhanced memory: Multi-agent systems with memory can overcome large language models' context windows, enabling better understanding and information retention.
63
64
64
-
## Implementing AI agents
65
+
## Implement AI agents
65
66
66
67
### Reasoning and planning
67
68
68
69
Complex reasoning and planning are the hallmark of advanced autonomous agents. Popular autonomous agent frameworks incorporate one or more of the following methodologies for reasoning and planning:
69
70
70
71
[Self-ask](https://arxiv.org/abs/2210.03350)
71
-
> Improves on chain of thought by having the model explicitly asking itself (and answering) follow-up questions before answering the initial question.
72
+
Improves on chain of thought by having the model explicitly asking itself (and answering) follow-up questions before answering the initial question.
72
73
73
74
[Reason and Act (ReAct)](https://arxiv.org/abs/2210.03629)
74
-
> Use LLMs to generate both reasoning traces and task-specific actions in an interleaved manner. Reasoning traces help the model induce, track, and update action plans as well as handle exceptions, while actions allow it to interface with external sources, such as knowledge bases or environments, to gather additional information.
75
+
Use LLMs to generate both reasoning traces and task-specific actions in an interleaved manner. Reasoning traces help the model induce, track, and update action plans as well as handle exceptions, while actions allow it to interface with external sources, such as knowledge bases or environments, to gather additional information.
75
76
76
77
[Plan and Solve](https://arxiv.org/abs/2305.04091)
77
-
> Devise a plan to divide the entire task into smaller subtasks, and then carry out the subtasks according to the plan. This mitigates the calculation errors, missing-step errors, and semantic misunderstanding errors that are often present in zero-shot chain-of-thought (CoT) prompting.
78
+
Devise a plan to divide the entire task into smaller subtasks, and then carry out the subtasks according to the plan. This mitigates the calculation errors, missing-step errors, and semantic misunderstanding errors that are often present in zero-shot chain-of-thought (CoT) prompting.
> Reflexion agents verbally reflect on task feedback signals, then maintain their own reflective text in an episodic memory buffer to induce better decision-making in subsequent trials.
81
+
Reflexion agents verbally reflect on task feedback signals, then maintain their own reflective text in an episodic memory buffer to induce better decision-making in subsequent trials.
81
82
82
83
### Frameworks
83
84
@@ -170,7 +171,6 @@ Azure Cosmos DB incorporates advanced features such as change feed, which allows
170
171
Additionally, the built-in support for multi-master writes enables high availability and resilience, ensuring continuous operation of AI agents even in the face of regional failures.
171
172
172
173
The five available [consistency levels](consistency-levels.md) (from strong to eventual) can also cater to various distributed workloads depending on the scenario requirements.
173
-
Implementation example
174
174
175
175
> [!TIP]
176
176
> You may choose from two Azure Cosmos DB APIs to build your AI agent memory system: Azure Cosmos DB for NoSQL, and vCore-based Azure Cosmos DB for MongoDB. The former provides 99.999% availability and [three vector search algorithms](nosql/vector-search.md): IVF, HNSW, and the state-of-the-art DiskANN. The latter provides 99.995% availability and [two vector search algorithms](mongodb/vcore/vector-search.md): IVF and HNSW.
@@ -187,7 +187,7 @@ Chatbots have been a long-standing concept, but AI agents are advancing beyond b
187
187
### Prerequisites
188
188
189
189
- If you don't have an Azure subscription, you may [try Azure Cosmos DB free](try-free.md) for 30 days without creating an Azure account; no credit card is required, and no commitment follows when the trial period ends.
190
-
-Setup account for OpenAI API or Azure OpenAI Service.
190
+
-Set up account for OpenAI API or Azure OpenAI Service.
191
191
- Create a vCore cluster in Azure Cosmos DB for MongoDB by following this [QuickStart](mongodb/vcore/quickstart-portal.md).
192
192
- An IDE for Development, such as VS Code.
193
193
- Python 3.11.4 installed on development environment.
@@ -196,17 +196,17 @@ Chatbots have been a long-standing concept, but AI agents are advancing beyond b
196
196
197
197
All of the code and sample datasets are available on [GitHub](https://github.com/jonathanscholtes/Travel-AI-Agent-React-FastAPI-and-Cosmos-DB-Vector-Store).
198
198
199
-
- loader: Python code for loading sample documents and vector embeddings in Azure Cosmos DB
200
-
- api: Python FastAPI for Hosting Travel AI Agent
201
-
- web: Web Interface with React JS
199
+
-**loader**: This folder contains Python code for loading sample documents and vector embeddings in Azure Cosmos DB.
200
+
-**api**: This folder contains Python FastAPI for Hosting Travel AI Agent.
201
+
-**web**: The folder contains the Web Interface with React JS.
202
202
203
203
### Load Travel Documents into Azure Cosmos DB
204
204
205
-
The GitHub repository contains a Python project located in the **loader** directory intended for loading the sample travel documents into Azure Cosmos DB. This section sets-up the project to load the documents.
205
+
The GitHub repository contains a Python project located in the **loader** directory intended for loading the sample travel documents into Azure Cosmos DB. This section setsup the project to load the documents.
206
206
207
-
### Setting Up the Environment for Loader
207
+
### Set Up the Environment for Loader
208
208
209
-
Setup your Python virtual environment in the **loader** directory by running the following:
209
+
Set up your Python virtual environment in the **loader** directory by running the following:
210
210
```python
211
211
python -m venv venv
212
212
```
@@ -223,7 +223,7 @@ Create a file, named **.env** in the **loader** directory, to store the followin
223
223
MONGO_CONNECTION_STRING="mongodb+srv:**your connection string from Azure Cosmos DB**"
224
224
```
225
225
226
-
### Loading Documents and Vectors
226
+
### Load Documents and Vectors
227
227
228
228
The Python file **main.py** serves as the central entry point for loading data into Azure Cosmos DB. This code processes the sample travel data from the GitHub repository, including information about ships and destinations. Additionally, it generates travel itinerary packages for each ship and destination, allowing travelers to book them using the AI agent. The CosmosDBLoader is responsible for creating collections, vector embeddings, and indexes in the Azure Cosmos DB instance.
229
229
@@ -276,7 +276,7 @@ Output:
276
276
--load vectors ships--
277
277
```
278
278
279
-
### Building Travel AI Agent with Python FastAPI
279
+
### Build Travel AI Agent with Python FastAPI
280
280
281
281
The AI travel agent is hosted in a backend API using Python FastAPI, facilitating integration with the frontend user interface. The API project processes agent requests by [grounding](https://techcommunity.microsoft.com/t5/fasttrack-for-azure/grounding-llms/ba-p/3843857) the LLM prompts against the data layer, specifically the vectors and documents in Azure Cosmos DB. Furthermore, the agent makes use of various tools, particularly the Python functions provided at the API service layer. This article focuses on the code necessary for AI agents within the API code.
282
282
@@ -287,11 +287,11 @@ The API project in the GitHub repository is structured as follows:
287
287
- Service – service layer components responsible for primary business logic and interaction with data layer; LangChain Agent and Agent Tools.
288
288
- Data – data layer components responsible for interacting with Azure Cosmos DB for MongoDB documents storage and vector search.
289
289
290
-
### Setting Up the Environment for the API
290
+
### Set Up the Environment for the API
291
291
292
292
Python version 3.11.4 was utilized for the development and testing of the API.
293
293
294
-
Setup your python virtual environment in the **api** directory.
294
+
Set up your python virtual environment in the **api** directory.
295
295
```python
296
296
python -m venv venv
297
297
```
@@ -308,19 +308,19 @@ Create a file, named **.env** in the **api** directory, to store your environmen
308
308
MONGO_CONNECTION_STRING="mongodb+srv:**your connection string from Azure Cosmos DB**"
309
309
```
310
310
311
-
With the environment configured and variables set up, we are ready to initiate the FastAPI server. Run the following command from the api directory to initiate the server.
311
+
With the environment configured and variables set up, we are ready to initiate the FastAPI server. Run the following command from the **api** directory to initiate the server.
312
312
```python
313
313
python app.py
314
314
```
315
315
316
316
The FastAPI server launches on the localhost loopback 127.0.0.1 port 8000 by default. You can access the Swagger documents using the following localhost address: http://127.0.0.1:8000/docs
317
317
318
-
### Using a Session for the AI Agent Memory
318
+
### Use a Session for the AI Agent Memory
319
319
It is imperative for the Travel Agent to have the capability to reference previously provided information within the ongoing conversation. This ability is commonly known as "memory" in the context of LLMs, which should not be confused with the concept of computer memory (like volatile, non-volatile, and persistent memory).
320
320
321
321
To achieve this objective, we use the chat message history, which is securely stored in our Azure Cosmos DB instance. Each chat session will have its history stored using a session ID to ensure that only messages from the current conversation session are accessible. This necessity is the reason behind the existence of a 'Get Session' method in our API. It is a placeholder method for managing web sessions in order to illustrate the use of chat message history.
322
322
323
-
:::image type="content" source="media/gen-ai/ai-agent/FastAPI-get-session.png" lightbox="media/gen-ai/ai-agent/FastAPI-get-session.png" alt-text="Python FastAPI – Get Session.":::
323
+
:::image type="content" source="media/gen-ai/ai-agent/fastapi-get-session.png" lightbox="media/gen-ai/ai-agent/fastapi-get-session.png" alt-text="Screenshot of Python FastAPI – Get Session.":::
324
324
Click Try It out for /session/.
325
325
326
326
```python
@@ -342,7 +342,7 @@ For the AI Agent, we only need to simulate a session. Thus, the stubbed-out meth
342
342
343
343
Let us utilize the obtained session ID from the previous step to initiate a new dialogue with our AI agent to validate its functionality. We shall conduct our test by submitting the following phrase: "I want to take a relaxing vacation."
@@ -374,7 +374,7 @@ Calling the 'agent_chat' for the first time creates a new collection named 'hist
374
374
375
375
When integrating the AI Agent into the API, the web search components are responsible for initiating all requests. This is followed by the search service, and finally the data components. In our specific case, we utilize MongoDB data search, which connects to Azure Cosmos DB. The layers facilitate the exchange of Model components, with the AI Agent and AI Agent Tool code residing in the service layer. This approach was implemented to enable the seamless interchangeability of data sources and to extend the capabilities of the AI Agent with additional, more intricate functionalities or 'tools'.
376
376
377
-
:::image type="content" source="media/gen-ai/ai-agent/Travel-AI-Agent-FastAPI-layers.png" lightbox="media/gen-ai/ai-agent/Travel-AI-Agent-FastAPI-layers.png" alt-text="Travel AI Agent FastAPI layers.":::
377
+
:::image type="content" source="media/gen-ai/ai-agent/travel-ai-agent-fastapi-layers.png" lightbox="media/gen-ai/ai-agent/travel-ai-agent-fastapi-layers.png" alt-text="Screenshot of Travel AI Agent FastAPI layers.":::
The **TravelAgent.py** file is straightforward, as ```agent_with_chat_history```, and its dependencies (tools, prompt, and LLM) are initialized and configured in the **init.py** file. In this file, the agent is called using the input received from the user, along with the session ID for conversation memory. Afterwards, ```PromptResponse``` (model/prompt) is returned with the agent's output and response time.
524
524
525
-
### Integrating AI Agent with React JS User Interface
525
+
### Integrate AI Agent with React JS User Interface
526
526
527
527
With the successful loading of the data and accessibility of our AI Agent through our API, we can now complete the solution by establishing a web user interface using React JS for our travel website. By harnessing the capabilities of React JS, we can illustrate the seamless integration of our AI agent into a travel site, enhancing the user experience with a conversational travel assistant for inquiries and bookings.
528
528
529
-
#### Setting Up the Environment for React JS
529
+
#### Set Up the Environment for React JS
530
530
531
531
Install Node.js and the dependencies before testing out the React interface.
532
532
@@ -545,13 +545,13 @@ Now, we have the ability to execute the following command from the **web** direc
545
545
```
546
546
547
547
Running the previous command launches the React JS web application.
548
-
:::image type="content" source="media/gen-ai/ai-agent/React-JS-Travel-Web-Application.png" lightbox="media/gen-ai/ai-agent/React-JS-Travel-Web-Application.png" alt-text="React JS Travel Web Application.":::
548
+
:::image type="content" source="media/gen-ai/ai-agent/react-js-travel-web-application.png" lightbox="media/gen-ai/ai-agent/react-js-travel-web-application.png" alt-text="Screenshot of React JS Travel Web Application.":::
549
549
550
550
#### Walkthrough of React JS Web Interface
551
551
552
552
The web project of the GitHub repository is a straightforward application to facilitate user interaction with our AI agent. The primary components required to converse with the agent are ```TravelAgent.js``` and ```ChatLayout.js```. The **Main.js** file serves as the central module or user landing page.
:::image type="content" source="media/gen-ai/ai-agent/main.png" lightbox="media/gen-ai/ai-agent/main.png" alt-text="Screenshot of JS Interface.":::
555
555
556
556
#### Main
557
557
@@ -775,10 +775,10 @@ export default function ChatLayout(messages) {
775
775
```
776
776
777
777
User prompts are on the right side and colored blue, while the Travel AI Agent responses are on the left side and colored green. As you can see in the image below, the HTML formatted responses are accounted for in the conversation.
:::image type="content" source="media/gen-ai/ai-agent/chat-screenshot.png" lightbox="media/gen-ai/ai-agent/chat-screenshot.png" alt-text="Screenshot of Chat.":::
779
779
780
780
When your AI agent is ready go to into production, you can improve query performance by 80% and reduce costs by using semantic caching. See this blog post for how to implement [semantic caching](https://stochasticcoder.com/2024/03/22/improve-llm-performance-using-semantic-cache-with-cosmos-db/).
0 commit comments