Skip to content

Commit 6fd9f12

Browse files
authored
Update ai-agents.md
1 parent 327780e commit 6fd9f12

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

articles/cosmos-db/ai-agents.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: AI agents
3+
titleSuffix: Azure Cosmos DB
34
description: AI agent key concepts and implementation of AI agent memory system.
45
author: wmwxwa
56
ms.author: wangwilliam
@@ -11,10 +12,10 @@ ms.date: 06/26/2024
1112

1213
# AI agents
1314

14-
## What are AI Agents?
15-
1615
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.
1716

17+
## What are AI Agents?
18+
1819
### Common features of AI agents
1920

2021
- [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
6162
- Sophisticated abilities: Multi-agent systems can handle complex or large-scale problems by conducting thorough decision-making processes and distributing tasks among multiple agents.
6263
- Enhanced memory: Multi-agent systems with memory can overcome large language models' context windows, enabling better understanding and information retention.
6364

64-
## Implementing AI agents
65+
## Implement AI agents
6566

6667
### Reasoning and planning
6768

6869
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:
6970

7071
[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.
7273

7374
[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.
7576

7677
[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.
7879

7980
[Reflection/Self-critique](https://arxiv.org/abs/2303.11366)
80-
> 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.
8182

8283
### Frameworks
8384

@@ -170,7 +171,6 @@ Azure Cosmos DB incorporates advanced features such as change feed, which allows
170171
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.
171172

172173
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
174174

175175
> [!TIP]
176176
> 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
187187
### Prerequisites
188188

189189
- 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.
191191
- Create a vCore cluster in Azure Cosmos DB for MongoDB by following this [QuickStart](mongodb/vcore/quickstart-portal.md).
192192
- An IDE for Development, such as VS Code.
193193
- 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
196196

197197
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).
198198

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.
202202

203203
### Load Travel Documents into Azure Cosmos DB
204204

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 sets up the project to load the documents.
206206

207-
### Setting Up the Environment for Loader
207+
### Set Up the Environment for Loader
208208

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:
210210
```python
211211
python -m venv venv
212212
```
@@ -223,7 +223,7 @@ Create a file, named **.env** in the **loader** directory, to store the followin
223223
MONGO_CONNECTION_STRING="mongodb+srv:**your connection string from Azure Cosmos DB**"
224224
```
225225

226-
### Loading Documents and Vectors
226+
### Load Documents and Vectors
227227

228228
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.
229229

@@ -276,7 +276,7 @@ Output:
276276
--load vectors ships--
277277
```
278278

279-
### Building Travel AI Agent with Python FastAPI
279+
### Build Travel AI Agent with Python FastAPI
280280

281281
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.
282282

@@ -287,11 +287,11 @@ The API project in the GitHub repository is structured as follows:
287287
- Service – service layer components responsible for primary business logic and interaction with data layer; LangChain Agent and Agent Tools.
288288
- Data – data layer components responsible for interacting with Azure Cosmos DB for MongoDB documents storage and vector search.
289289

290-
### Setting Up the Environment for the API
290+
### Set Up the Environment for the API
291291

292292
Python version 3.11.4 was utilized for the development and testing of the API.
293293

294-
Setup your python virtual environment in the **api** directory.
294+
Set up your python virtual environment in the **api** directory.
295295
```python
296296
python -m venv venv
297297
```
@@ -308,19 +308,19 @@ Create a file, named **.env** in the **api** directory, to store your environmen
308308
MONGO_CONNECTION_STRING="mongodb+srv:**your connection string from Azure Cosmos DB**"
309309
```
310310

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.
312312
```python
313313
python app.py
314314
```
315315

316316
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
317317

318-
### Using a Session for the AI Agent Memory
318+
### Use a Session for the AI Agent Memory
319319
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).
320320

321321
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.
322322

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.":::
324324
Click Try It out for /session/.
325325

326326
```python
@@ -342,7 +342,7 @@ For the AI Agent, we only need to simulate a session. Thus, the stubbed-out meth
342342

343343
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."
344344

345-
:::image type="content" source="media/gen-ai/ai-agent/FastAPI-agent-chat.png" lightbox="media/gen-ai/ai-agent/FastAPI-agent-chat.png" alt-text="Python FastAPI – Agent Chat.":::
345+
:::image type="content" source="media/gen-ai/ai-agent/fastapi-agent-chat.png" lightbox="media/gen-ai/ai-agent/fastapi-agent-chat.png" alt-text="Screenshot of Python FastAPI – Agent Chat.":::
346346
Click Try It out for /agent/agent_chat.
347347

348348
Example parameter
@@ -374,7 +374,7 @@ Calling the 'agent_chat' for the first time creates a new collection named 'hist
374374

375375
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'.
376376

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.":::
378378

379379
#### Service Layer
380380

@@ -522,11 +522,11 @@ def agent_chat(input:str, session_id:str)->str:
522522

523523
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.
524524

525-
### Integrating AI Agent with React JS User Interface
525+
### Integrate AI Agent with React JS User Interface
526526

527527
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.
528528

529-
#### Setting Up the Environment for React JS
529+
#### Set Up the Environment for React JS
530530

531531
Install Node.js and the dependencies before testing out the React interface.
532532

@@ -545,13 +545,13 @@ Now, we have the ability to execute the following command from the **web** direc
545545
```
546546

547547
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.":::
549549

550550
#### Walkthrough of React JS Web Interface
551551

552552
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.
553553

554-
:::image type="content" source="media/gen-ai/ai-agent/main.png" lightbox="media/gen-ai/ai-agent/main.png" alt-text="JS Interface":::
554+
:::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.":::
555555

556556
#### Main
557557

@@ -775,10 +775,10 @@ export default function ChatLayout(messages) {
775775
```
776776

777777
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.
778-
:::image type="content" source="media/gen-ai/ai-agent/chat-screenshot.png" lightbox="media/gen-ai/ai-agent/chat-screenshot.png" alt-text="Chat Screenshot":::
778+
:::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.":::
779779

780780
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/).
781-
:::image type="content" source="media/gen-ai/ai-agent/semantic-caching.png" lightbox="media/gen-ai/ai-agent/semantic-caching.png" alt-text="Semantic Caching":::
781+
:::image type="content" source="media/gen-ai/ai-agent/semantic-caching.png" lightbox="media/gen-ai/ai-agent/semantic-caching.png" alt-text="Screenshot of Semantic Caching.":::
782782

783783
### Next steps
784784

0 commit comments

Comments
 (0)