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
+18-19Lines changed: 18 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,5 @@
1
1
---
2
2
title: AI agents
3
-
titleSuffix: Azure Cosmos DB
4
3
description: AI agent key concepts and implementation of AI agent memory system.
5
4
author: wmwxwa
6
5
ms.author: wangwilliam
@@ -192,19 +191,19 @@ Chatbots have been a long-standing concept, but AI agents are advancing beyond b
192
191
- An IDE for Development, such as VS Code.
193
192
- Python 3.11.4 installed on development environment.
194
193
195
-
### Download the Project
194
+
### Download the project
196
195
197
196
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). In this repository, you can find the following folders:
198
197
199
198
-**loader**: This folder contains Python code for loading sample documents and vector embeddings in Azure Cosmos DB.
200
199
-**api**: This folder contains Python FastAPI for Hosting Travel AI Agent.
201
200
-**web**: The folder contains the Web Interface with React JS.
202
201
203
-
### Load Travel Documents into Azure Cosmos DB
202
+
### Load travel documents into Azure Cosmos DB
204
203
205
204
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.
206
205
207
-
### Set Up the Environment for Loader
206
+
### Set up the environment for loader
208
207
209
208
Set up your Python virtual environment in the **loader** directory by running the following:
210
209
```python
@@ -223,7 +222,7 @@ Create a file, named **.env** in the **loader** directory, to store the followin
223
222
MONGO_CONNECTION_STRING="mongodb+srv:**your connection string from Azure Cosmos DB**"
224
223
```
225
224
226
-
### Load Documents and Vectors
225
+
### Load documents and vectors
227
226
228
227
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
228
@@ -276,7 +275,7 @@ Output:
276
275
--load vectors ships--
277
276
```
278
277
279
-
### Build Travel AI Agent with Python FastAPI
278
+
### Build travel AI agent with Python FastAPI
280
279
281
280
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
281
@@ -287,7 +286,7 @@ The API project in the GitHub repository is structured as follows:
287
286
- Service – service layer components responsible for primary business logic and interaction with data layer; LangChain Agent and Agent Tools.
288
287
- Data – data layer components responsible for interacting with Azure Cosmos DB for MongoDB documents storage and vector search.
289
288
290
-
### Set Up the Environment for the API
289
+
### Set up the environment for the API
291
290
292
291
Python version 3.11.4 was utilized for the development and testing of the API.
293
292
@@ -315,7 +314,7 @@ With the environment configured and variables set up, we are ready to initiate t
315
314
316
315
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
316
318
-
### Use a Session for the AI Agent Memory
317
+
### Use a session for the AI agent memory
319
318
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
319
321
320
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.
@@ -338,7 +337,7 @@ For the AI Agent, we only need to simulate a session. Thus, the stubbed-out meth
338
337
return {'session_id':str(uuid.uuid4().hex)}
339
338
```
340
339
341
-
### Start a Conversation with the AI Travel Agent
340
+
### Start a conversation with the AI travel agent
342
341
343
342
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."
344
343
@@ -370,13 +369,13 @@ Output when calling ```data.mongodb.travel.similarity_search()```
370
369
371
370
Calling the 'agent_chat' for the first time creates a new collection named 'history' in Azure Cosmos DB to store the conversation by session. This call enables the agent to access the stored chat message history as needed. Subsequent executions of 'agent_chat' with the same parameters produce varying results as it draws from memory.
372
371
373
-
### Walkthrough of AI Agent
372
+
### Walkthrough of AI agent
374
373
375
374
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
375
377
376
:::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.":::
378
377
379
-
#### Service Layer
378
+
#### Service layer
380
379
381
380
The service layer forms the cornerstone of our core business logic. In this particular scenario, the service layer plays a crucial role as the repository for the LangChain agent code, facilitating the seamless integration of user prompts with Azure Cosmos DB data, conversation memory, and agent functions for our AI Agent.
382
381
@@ -442,7 +441,7 @@ The **init.py** file commences by initiating the loading of environment variable
442
441
443
442
The LLM prompt initially began with the simple statement "You are a helpful and friendly travel assistant for a cruise company." However, through testing, it was determined that more consistent results could be obtained by including the instruction "Answer travel questions to the best of your ability, providing only relevant information. To book a cruise, capturing the person's name is essential." The results are presented in HTML format to enhance the visual appeal within the web interface.
444
443
445
-
#### Agent Tools
444
+
#### Agent tools
446
445
[Tools](#what-are-ai-agents) are interfaces that an agent can use to interact with the world, often done through function calling.
447
446
448
447
When creating an agent, it is essential to furnish it with a set of tools that it can utilize. The ```@tool``` decorator offers the most straightforward approach to defining a custom tool. By default, the decorator uses the function name as the tool name, although this can be replaced by providing a string as the first argument. Moreover, the decorator will utilize the function's docstring as the tool's description, thus requiring the provision of a docstring.
In the **TravelAgentTools.py** file, three specific tools are defined. The first tool, ```vacation_lookup```, conducts a vector search against Azure Cosmos DB, using a ```similarity_search``` to retrieve relevant travel-related material. The second tool, ```itinerary_lookup```, retrieves cruise package details and schedules for a specified cruise ship. Lastly, ```book_cruise``` is responsible for booking a cruise package for a passenger. Specific instructions ("In order to book a cruise I need to know your name.") might be necessary to ensure the capture of the passenger's name and room number for booking the cruise package. This is in spite of including such instructions in the LLM prompt.
496
495
497
-
#### AI Agent
496
+
#### AI agent
498
497
499
498
The fundamental concept underlying agents is to utilize a language model for selecting a sequence of actions to execute.
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
523
525
-
### Integrate AI Agent with React JS User Interface
524
+
### Integrate AI agent with React JS user interface
526
525
527
526
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
527
529
-
#### Set Up the Environment for React JS
528
+
#### Set up the environment for React JS
530
529
531
530
Install Node.js and the dependencies before testing out the React interface.
532
531
@@ -547,7 +546,7 @@ Now, we have the ability to execute the following command from the **web** direc
547
546
Running the previous command launches the React JS web application.
548
547
:::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
548
550
-
#### Walkthrough of React JS Web Interface
549
+
#### Walkthrough of React JS Web interface
551
550
552
551
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.
553
552
@@ -632,7 +631,7 @@ class Main extends Component {
632
631
exportdefaultMain
633
632
```
634
633
635
-
#### Travel Agent
634
+
#### Travel agent
636
635
637
636
The Travel Agent component has a straightforward purpose – capturing user inputs and displaying responses. It plays a key role in managing the integration with the backend AI Agent, primarily by capturing sessions and forwarding user prompts to our FastAPI service. The resulting responses are stored in an array for display, facilitated by the Chat Layout component.
638
637
@@ -744,7 +743,7 @@ export default function TravelAgent() {
744
743
745
744
Click on "Effortlessly plan your voyage" to launch the travel assistant.
746
745
747
-
#### Chat Layout
746
+
#### Chat layout
748
747
749
748
The Chat Layout component, as indicated by its name, oversees the arrangement of the chat. It systematically processes the chat messages and implements the designated formatting specified in the message JSON object.
750
749
@@ -777,7 +776,7 @@ export default function ChatLayout(messages) {
777
776
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
777
:::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
778
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/).
779
+
When your AI agent is ready go to into production, you can use semantic caching to improve query performance by 80% and reduce LLM inference/API call costs. 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
780
:::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.":::
0 commit comments