mirror of
https://github.com/run-llama/mongo-genai-showcase.git
synced 2026-07-01 21:54:00 -04:00
INTPYTHON-442 Add autoformatting to repo
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
name: "Tests"
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- "main"
|
||||
push:
|
||||
branches:
|
||||
- "main"
|
||||
|
||||
jobs:
|
||||
pre-commit:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-python@v5
|
||||
- uses: pre-commit/action@v3.0.1
|
||||
@@ -1,3 +1,2 @@
|
||||
.ipynb_checkpoints
|
||||
**/.DS_Store
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v5.0.0
|
||||
hooks:
|
||||
- id: check-added-large-files
|
||||
- id: check-case-conflict
|
||||
- id: check-toml
|
||||
- id: check-yaml
|
||||
- id: debug-statements
|
||||
- id: end-of-file-fixer
|
||||
- id: forbid-new-submodules
|
||||
- id: trailing-whitespace
|
||||
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
# Ruff version.
|
||||
rev: v0.8.2
|
||||
hooks:
|
||||
# - id: ruff
|
||||
# args: ["--fix", "--show-fixes"]
|
||||
- id: ruff-format
|
||||
exclude: notebooks/techniques/evaluating_information_retrival_techniques_mongondb_langchain.ipynb
|
||||
@@ -75,26 +75,26 @@ An agent is an artificial computational entity with an awareness of its environm
|
||||
|
||||
| Title | Stack | Colab Link | Article Link |
|
||||
|--------------------------------|----------------------|------------------------------------------------|--------------------------------------------------|
|
||||
| Agentic Factory Safety Assistant | LangGraph, Open AI, MongoDB, LangChain |[](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/agentic_rag_factory_safety_assistant_with_langgraph_langchain_mongodb.ipynb) | |
|
||||
| AI Research Assistant | FireWorks AI, MongoDB, LangChain |[](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/agent_fireworks_ai_langchain_mongodb.ipynb) | [](https://www.mongodb.com/developer/products/atlas/agent-fireworksai-mongodb-langchain/) |
|
||||
| Agentic Factory Safety Assistant | LangGraph, Open AI, MongoDB, LangChain |[](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/agentic_rag_factory_safety_assistant_with_langgraph_langchain_mongodb.ipynb) | |
|
||||
| AI Research Assistant | FireWorks AI, MongoDB, LangChain |[](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/agent_fireworks_ai_langchain_mongodb.ipynb) | [](https://www.mongodb.com/developer/products/atlas/agent-fireworksai-mongodb-langchain/) |
|
||||
AI Investment Researcher | MongoDB, CrewAI and LangChain | [](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/crewai-mdb-agg.ipynb) | [](https://www.mongodb.com/developer/products/mongodb/augment-llm-capabilities-with-mdb-aggregation/)
|
||||
| Agentic RAG: Recommmendation System | Claude 3.5, LlamaIndex, MongoDB | [](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/how_to_build_ai_agent_claude_3_5_sonnet_llamaindex_mongodb.ipynb) | [](https://www.mongodb.com/developer/products/atlas/claude_3_5_sonnet_rag/)|
|
||||
| Agentic HR Chatbot | Claude 3.5, LangGraph, MongoDB | [](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/hr_agentic_chatbot_with_langgraph_claude.ipynb) | Coming Soon|
|
||||
|AWS Bedrock Agent | Claude 3, AWS Bedrock, Python, MongoDB | [](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/mongodb_with_aws_bedrock_agent.ipynb) | [](https://www.mongodb.com/developer/products/atlas/mdb-aws-bedrock-agent-start/)
|
||||
|Asset Manager Assistant | LangGraph, OpenAI, Anthropic, MongoDB | [](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/asset_management_analyst_assistant_agentic_chatbot_langgraph_mongodb.ipynb) |
|
||||
| Implementing Working Memory with Tavily and MongoDB | Python, Tavily, MongoDB | [](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/implementing_working_memory_with_tavily_and_mongodb.ipynb) |
|
||||
|Asset Manager Assistant | LangGraph, OpenAI, Anthropic, MongoDB | [](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/asset_management_analyst_assistant_agentic_chatbot_langgraph_mongodb.ipynb) |
|
||||
| Implementing Working Memory with Tavily and MongoDB | Python, Tavily, MongoDB | [](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/implementing_working_memory_with_tavily_and_mongodb.ipynb) |
|
||||
|AI Food Assistant | Semantic Kernel, C#, OpenAI, MongoDB | [GitHub Repo](https://github.com/mongodb-developer/foodagent-dotnet) |[](https://www.mongodb.com/developer/languages/csharp/ai-agents-dotnet-with-semantic-kernel/) | |
|
||||
|
||||
|
||||
## ML
|
||||
This folder will contain all traditional machine learning tutorials. They include important explanations, step-by-step instructions, and everything a reader needs in order to be successful following the tutorial from beginning to end.
|
||||
This folder will contain all traditional machine learning tutorials. They include important explanations, step-by-step instructions, and everything a reader needs in order to be successful following the tutorial from beginning to end.
|
||||
|
||||
| Title | Colab Link |
|
||||
|-------|------------|
|
||||
| Written in the Stars: Predict Your Future With Tensorflow and MongoDB Charts | [](https://github.com/mongodb-developer/GenAI-Showcase/blob/main/notebooks/ml/tensorflow_mongodbcharts_horoscopes.ipynb) |
|
||||
|
||||
## MongoDB Specific
|
||||
These MongoDB specific tutorials are meant to showcase a specific MongoDB platform integrated with artificial intelligence or machine learning. These step-by-step tutorials will allow the reader to truly understand not only the platform, but also the AI use-case.
|
||||
These MongoDB specific tutorials are meant to showcase a specific MongoDB platform integrated with artificial intelligence or machine learning. These step-by-step tutorials will allow the reader to truly understand not only the platform, but also the AI use-case.
|
||||
|
||||
| Title | Colab Link |
|
||||
|-------|------------|
|
||||
|
||||
@@ -15,7 +15,7 @@ Before starting this tutorial, you'll need to have the following:
|
||||
- Maven 3.9.6+ configured for your project.
|
||||
|
||||
## Application Configuration
|
||||
Configure your Spring application to set up the vector store and other necessary beans.
|
||||
Configure your Spring application to set up the vector store and other necessary beans.
|
||||
|
||||
In our application properties, we are going to configure our MongoDB database, as well as everything we need for semantically searching our data. We'll also add in information such as our OpenAI embedding model and api key.
|
||||
|
||||
|
||||
+1
-1
@@ -13,4 +13,4 @@ public interface LyricSearchRepository {
|
||||
Optional<Boolean> deleteDocuments(List<String> ids);
|
||||
|
||||
List<Document> semanticSearchByLyrics(SearchRequest searchRequest);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -33,4 +33,4 @@ public class LyricSearchRepositoryImpl implements LyricSearchRepository {
|
||||
public List<Document> semanticSearchByLyrics(SearchRequest searchRequest) {
|
||||
return vectorStore.similaritySearch(searchRequest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,4 +5,4 @@ spring.ai.openai.embedding.options.model=text-embedding-ada-002
|
||||
spring.data.mongodb.uri=
|
||||
spring.data.mongodb.database=lyrics
|
||||
spring.ai.vectorstore.mongodb.indexName=vector_index
|
||||
spring.ai.vectorstore.mongodb.collection-name=vector_store
|
||||
spring.ai.vectorstore.mongodb.collection-name=vector_store
|
||||
|
||||
@@ -198,4 +198,4 @@
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
limitations under the License.
|
||||
|
||||
@@ -85,6 +85,6 @@ To run this project, you'll need:
|
||||
|
||||
## Technologies Used
|
||||
- **Spring Boot**: Java-based framework for building REST APIs.
|
||||
- **MongoDB Atlas**: An integrated suite of data services (including Atlas Vector Search) centered around a cloud database designed to accelerate and simplify how you build with data. Build faster and build smarter with a developer data platform that helps solve your data challenges. Click [here](https://www.mongodb.com/products/platform/atlas-database) to learn more.
|
||||
- **MongoDB Atlas**: An integrated suite of data services (including Atlas Vector Search) centered around a cloud database designed to accelerate and simplify how you build with data. Build faster and build smarter with a developer data platform that helps solve your data challenges. Click [here](https://www.mongodb.com/products/platform/atlas-database) to learn more.
|
||||
- **OpenAI**: Generates embeddings for semantic searches.
|
||||
- **Terraform**: Automates infrastructure management for MongoDB Atlas.
|
||||
|
||||
@@ -53,7 +53,7 @@ resource "mongodbatlas_database_user" "db_user" {
|
||||
}
|
||||
|
||||
resource "mongodbatlas_search_index" "vector_search" {
|
||||
name = "search-index"
|
||||
name = "search-index"
|
||||
project_id = mongodbatlas_project.rag_project.id
|
||||
cluster_name = mongodbatlas_advanced_cluster.rag_cluster.name
|
||||
type = "vectorSearch"
|
||||
@@ -67,4 +67,4 @@ resource "mongodbatlas_search_index" "vector_search" {
|
||||
"similarity": "cosine"
|
||||
}]
|
||||
EOF
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,5 +38,5 @@ variable "db_password" {
|
||||
variable "ip_address" {
|
||||
description = "IP address to whitelist"
|
||||
type = string
|
||||
default = "196.000.0.000"
|
||||
default = "196.000.0.000"
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -65,7 +65,13 @@
|
||||
"# a vector database instance\n",
|
||||
"from autogen.retrieve_utils import TEXT_FORMATS\n",
|
||||
"\n",
|
||||
"config_list = [{\"model\": \"gpt-3.5-turbo-0125\", \"api_key\": os.environ[\"OPENAI_API_KEY\"], \"api_type\": \"openai\"}]\n",
|
||||
"config_list = [\n",
|
||||
" {\n",
|
||||
" \"model\": \"gpt-3.5-turbo-0125\",\n",
|
||||
" \"api_key\": os.environ[\"OPENAI_API_KEY\"],\n",
|
||||
" \"api_type\": \"openai\",\n",
|
||||
" }\n",
|
||||
"]\n",
|
||||
"assert len(config_list) > 0\n",
|
||||
"print(\"models to use: \", [config_list[i][\"model\"] for i in range(len(config_list))])"
|
||||
]
|
||||
@@ -141,7 +147,9 @@
|
||||
" \"vector_db\": \"mongodb\", # MongoDB Atlas database\n",
|
||||
" \"collection_name\": \"demo_collection\",\n",
|
||||
" \"db_config\": {\n",
|
||||
" \"connection_string\": os.environ[\"MONGODB_URI\"], # MongoDB Atlas connection string\n",
|
||||
" \"connection_string\": os.environ[\n",
|
||||
" \"MONGODB_URI\"\n",
|
||||
" ], # MongoDB Atlas connection string\n",
|
||||
" \"database_name\": \"test_db\", # MongoDB Atlas database\n",
|
||||
" \"index_name\": \"vector_index\",\n",
|
||||
" \"wait_until_index_ready\": 120.0, # Setting to wait 120 seconds or until index is constructed before querying\n",
|
||||
@@ -547,7 +555,9 @@
|
||||
"# The conversation continues until the termination condition is met, in RetrieveChat, the termination condition when no human-in-loop is no code block detected.\n",
|
||||
"# With human-in-loop, the conversation will continue until the user says \"exit\".\n",
|
||||
"code_problem = \"How can I use FLAML to perform a classification task and use spark to do parallel training. Train 30 seconds and force cancel jobs if time limit is reached.\"\n",
|
||||
"chat_result = ragproxyagent.initiate_chat(assistant, message=ragproxyagent.message_generator, problem=code_problem)"
|
||||
"chat_result = ragproxyagent.initiate_chat(\n",
|
||||
" assistant, message=ragproxyagent.message_generator, problem=code_problem\n",
|
||||
")"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
+3771
-3707
File diff suppressed because one or more lines are too long
+3208
-3147
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
+1322
-1283
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -1,415 +1,421 @@
|
||||
{
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"name": "python3",
|
||||
"display_name": "Python 3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"[](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/mongodb_with_aws_bedrock_agent.ipynb)"
|
||||
]
|
||||
},
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"[](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/agents/mongodb_with_aws_bedrock_agent.ipynb)"
|
||||
]
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "CmKeBSvBWIcS"
|
||||
},
|
||||
"source": [
|
||||
"# MongoDB with Bedrock agent quick tutorial\n",
|
||||
"MongoDB Atlas and Amazon Bedrock have joined forces to streamline the development of generative AI applications through their seamless integration. MongoDB Atlas, a robust cloud-based database service, now offers native support for Amazon Bedrock, AWS's managed service for generative AI. This integration leverages Atlas's vector search capabilities, enabling the effective utilization of enterprise data to augment the foundational models provided by Bedrock, such as Anthropic's Claude and Amazon's Titan. The combination ensures that the generative AI models have access to the most relevant and up-to-date data, significantly improving the accuracy and reliability of AI-driven applications with [MongoDB](https://www.mongodb.com/developer/products/atlas/rag-workflow-with-atlas-amazon-bedrock/).\n",
|
||||
"\n",
|
||||
"This integration simplifies the workflow for developers aiming to implement retrieval-augmented generation (RAG). RAG helps mitigate the issue of hallucinations in AI models by allowing them to fetch and utilize specific data from a predefined knowledge base, in this case, MongoDB Atlas Developers can easily set up this workflow by creating a vector search index in Atlas, which stores the vector embeddings and metadata of the text data. This setup not only enhances the performance and reliability of AI applications but also ensures data privacy and security through features like AWS PrivateLink.\n",
|
||||
"\n",
|
||||
"This notebook demonstrates how to interact with a predefined agent using [AWS Bedrock](https://aws.amazon.com/bedrock/) in a Google Colab environment. It utilizes the `boto3` library to communicate with the AWS Bedrock service and allows you to input prompts and receive responses directly within the notebook.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Key Features:\n",
|
||||
"1. **Secure Handling of AWS Credentials**: The `getpass` module is used to securely enter your AWS Access Key and Secret Key.\n",
|
||||
"2. **Session Management**: Each session is assigned a random session ID to maintain continuity in conversations.\n",
|
||||
"3. **Agent Invocation**: The notebook sends user prompts to a predefined agent and streams the responses back to the user.\n",
|
||||
"\n",
|
||||
"### Requirements:\n",
|
||||
"- AWS Access Key and Secret Key with appropriate permissions.\n",
|
||||
"- Boto3 and Requests libraries for interacting with AWS services and fetching data from URLs.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Setting up MongoDB Atlas\n",
|
||||
"\n",
|
||||
"1. Follow the [getting started with Atlas](https://www.mongodb.com/docs/atlas/getting-started/) guide and setup your cluster with `0.0.0.0/0` allowed connection for this notebook.\n",
|
||||
"2. Predefined an Atlas Vector Index on database `bedrock` collection `agenda`, this collection will host the data for the AWS summit agenda and will serve as a context store for the agent:\n",
|
||||
"**Index name**: `vector_index`\n",
|
||||
"```json\n",
|
||||
"{\n",
|
||||
" \"fields\": [\n",
|
||||
" {\n",
|
||||
" \"type\": \"vector\",\n",
|
||||
" \"path\": \"embedding\",\n",
|
||||
" \"numDimensions\": 1024,\n",
|
||||
" \"similarity\": \"cosine\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"type\" : \"filter\",\n",
|
||||
" \"path\" : \"metadata\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"type\" : \"filter\",\n",
|
||||
" \"path\" : \"text\"\n",
|
||||
" },\n",
|
||||
" ]\n",
|
||||
"}\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Setup AWS Bedrock\n",
|
||||
"\n",
|
||||
"**We will use US-EAST-1 AWS region for this notebook**\n",
|
||||
"\n",
|
||||
"Follow our official tutorial to enable a bedrock knowledge base against the created database and collection in MongoDB Atlas. This [guide](https://www.mongodb.com/docs/atlas/atlas-vector-search/ai-integrations/amazon-bedrock/) highlight a detailed step of action to build the knowledge base and agent.\n",
|
||||
"\n",
|
||||
"For this notebook, we will perform the following tasks according to the guide:\n",
|
||||
"\n",
|
||||
"1. Go to the bedrock console and enable\n",
|
||||
"- Amazon Titan Text Embedding model (`amazon.titan-embed-text-v2:0`)\n",
|
||||
"- Claude 3 Sonnet Model (The LLM(\n",
|
||||
"\n",
|
||||
"2. Upload the following source data about the AWS summit agenda to your S3 bucket:\n",
|
||||
"- https://s3.amazonaws.com/bedrocklogs.pavel/ocr_db.aws_events.json\n",
|
||||
"- https://s3.amazonaws.com/bedrocklogs.pavel/ocr_db.aws_sessions.json\n",
|
||||
"\n",
|
||||
"This will be our source data listing the events happening in the summit.\n",
|
||||
"\n",
|
||||
"3. Go to Secrets Manager on the AWS console and create credentials to our atlas cluster via \"Other type of secret\":\n",
|
||||
"- key : username , value : `<ATLAS_USERNAME>`\n",
|
||||
"- key : password , value : `<ATLAS_PASSWORD>`\n",
|
||||
"\n",
|
||||
"4. Follow the setup of the knowledge base wizard to connect Bedrock models with Atlas :\n",
|
||||
"- Click \"Create Knowledge Base\" and input:\n",
|
||||
"\n",
|
||||
"|input|value|\n",
|
||||
"|---|---|\n",
|
||||
"|Name| `<NAME>` |\n",
|
||||
"|Chose| Create and use a new service role|\n",
|
||||
"|Data source name| `<NAME>`|\n",
|
||||
"|S3 URI| Browse for the S3 bucket hosting the 2 uploaded source files|\n",
|
||||
"|Embedding Model| Titan Text Embeddings v2|\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"- let's choose MongoDB Atlas in the \"Vector Database\" choose the \"Choose a vector store you have created\" section:\n",
|
||||
"\n",
|
||||
"|input|value|\n",
|
||||
"|---|---|\n",
|
||||
"|Select your vector store| **MongoDB Atlas** |\n",
|
||||
"|Hostname| Your atlas srv hostname `eg. cluster0.abcd.mongodb.net`|\n",
|
||||
"|Database name| `bedrock`|\n",
|
||||
"|Collection name| `agenda`|\n",
|
||||
"|Credentials secret ARN| Copy the created credentials from the \"Secrets manager\"|\n",
|
||||
"|Vector search index name|`vector_index`|\n",
|
||||
"|Vector embedding field path| `embedding`|\n",
|
||||
"|Text field path| `text`|\n",
|
||||
"|Metadata field path| `metadata` |\n",
|
||||
"5. Click Next, review the details and \"Create Knowledge Base\".\n",
|
||||
"\n",
|
||||
"6. Once the knowledge base is marked with \"Status : Ready\", go to `Data source` section, choose the one datasource we have and click the \"Sync\" button on its right upper corner. This operation should load the data to Atlas if everything was setup correctly.\n",
|
||||
"\n",
|
||||
"## Setting up an agenda agent\n",
|
||||
"\n",
|
||||
"We can now set up our agent, who will work with a set of instructions and our knowledge base.\n",
|
||||
"\n",
|
||||
"1. Go to the \"Agents\" tab in the bedrock UI.\n",
|
||||
"2. Click \"Create Agent\" and give it a meaningful name (e.g. agenda_assistant)\n",
|
||||
"3. Input the following data in the agent builder:\n",
|
||||
"\n",
|
||||
"|input|value|\n",
|
||||
"|---|---|\n",
|
||||
"|Agent Name| agenda_assistant |\n",
|
||||
"|Agent resource role| Create and use a new service role |\n",
|
||||
"|Select model| Anthropic - Claude 3 Sonnet |\n",
|
||||
"|Instructions for the Agent| **You are a friendly AI chatbot that helps users find and build agenda Items for AWS Summit Tel Aviv. elaborate as much as possible on the response.** |\n",
|
||||
"|Agent Name| agenda_assistant |\n",
|
||||
"|Knowledge bases| **Choose your Knowledge Base** |\n",
|
||||
"|Aliases| Create a new Alias|\n",
|
||||
"\n",
|
||||
"And now, we have a functioning agent that can be tested via the console.\n",
|
||||
"Let's move to the notebook.\n",
|
||||
"\n",
|
||||
"**Take note of the Agent ID and create an Agent Alias ID for the notebook**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "NmjfN1HavIqF"
|
||||
},
|
||||
"source": [
|
||||
"## Interacting with the agent\n",
|
||||
"\n",
|
||||
"To interact with the agent, we need to install the AWS python SDK:\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "6L8lkSTzvig1",
|
||||
"outputId": "0300d850-872d-47e0-aae1-caa5396f3db3"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# MongoDB with Bedrock agent quick tutorial\n",
|
||||
"MongoDB Atlas and Amazon Bedrock have joined forces to streamline the development of generative AI applications through their seamless integration. MongoDB Atlas, a robust cloud-based database service, now offers native support for Amazon Bedrock, AWS's managed service for generative AI. This integration leverages Atlas's vector search capabilities, enabling the effective utilization of enterprise data to augment the foundational models provided by Bedrock, such as Anthropic's Claude and Amazon's Titan. The combination ensures that the generative AI models have access to the most relevant and up-to-date data, significantly improving the accuracy and reliability of AI-driven applications with [MongoDB](https://www.mongodb.com/developer/products/atlas/rag-workflow-with-atlas-amazon-bedrock/).\n",
|
||||
"\n",
|
||||
"This integration simplifies the workflow for developers aiming to implement retrieval-augmented generation (RAG). RAG helps mitigate the issue of hallucinations in AI models by allowing them to fetch and utilize specific data from a predefined knowledge base, in this case, MongoDB Atlas Developers can easily set up this workflow by creating a vector search index in Atlas, which stores the vector embeddings and metadata of the text data. This setup not only enhances the performance and reliability of AI applications but also ensures data privacy and security through features like AWS PrivateLink.\n",
|
||||
"\n",
|
||||
"This notebook demonstrates how to interact with a predefined agent using [AWS Bedrock](https://aws.amazon.com/bedrock/) in a Google Colab environment. It utilizes the `boto3` library to communicate with the AWS Bedrock service and allows you to input prompts and receive responses directly within the notebook.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Key Features:\n",
|
||||
"1. **Secure Handling of AWS Credentials**: The `getpass` module is used to securely enter your AWS Access Key and Secret Key.\n",
|
||||
"2. **Session Management**: Each session is assigned a random session ID to maintain continuity in conversations.\n",
|
||||
"3. **Agent Invocation**: The notebook sends user prompts to a predefined agent and streams the responses back to the user.\n",
|
||||
"\n",
|
||||
"### Requirements:\n",
|
||||
"- AWS Access Key and Secret Key with appropriate permissions.\n",
|
||||
"- Boto3 and Requests libraries for interacting with AWS services and fetching data from URLs.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Setting up MongoDB Atlas\n",
|
||||
"\n",
|
||||
"1. Follow the [getting started with Atlas](https://www.mongodb.com/docs/atlas/getting-started/) guide and setup your cluster with `0.0.0.0/0` allowed connection for this notebook.\n",
|
||||
"2. Predefined an Atlas Vector Index on database `bedrock` collection `agenda`, this collection will host the data for the AWS summit agenda and will serve as a context store for the agent:\n",
|
||||
"**Index name**: `vector_index`\n",
|
||||
"```json\n",
|
||||
"{\n",
|
||||
" \"fields\": [\n",
|
||||
" {\n",
|
||||
" \"type\": \"vector\",\n",
|
||||
" \"path\": \"embedding\",\n",
|
||||
" \"numDimensions\": 1024,\n",
|
||||
" \"similarity\": \"cosine\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"type\" : \"filter\",\n",
|
||||
" \"path\" : \"metadata\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"type\" : \"filter\",\n",
|
||||
" \"path\" : \"text\"\n",
|
||||
" },\n",
|
||||
" ]\n",
|
||||
"}\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Setup AWS Bedrock\n",
|
||||
"\n",
|
||||
"**We will use US-EAST-1 AWS region for this notebook**\n",
|
||||
"\n",
|
||||
"Follow our official tutorial to enable a bedrock knowledge base against the created database and collection in MongoDB Atlas. This [guide](https://www.mongodb.com/docs/atlas/atlas-vector-search/ai-integrations/amazon-bedrock/) highlight a detailed step of action to build the knowledge base and agent.\n",
|
||||
"\n",
|
||||
"For this notebook, we will perform the following tasks according to the guide:\n",
|
||||
"\n",
|
||||
"1. Go to the bedrock console and enable\n",
|
||||
"- Amazon Titan Text Embedding model (`amazon.titan-embed-text-v2:0`)\n",
|
||||
"- Claude 3 Sonnet Model (The LLM(\n",
|
||||
"\n",
|
||||
"2. Upload the following source data about the AWS summit agenda to your S3 bucket:\n",
|
||||
"- https://s3.amazonaws.com/bedrocklogs.pavel/ocr_db.aws_events.json\n",
|
||||
"- https://s3.amazonaws.com/bedrocklogs.pavel/ocr_db.aws_sessions.json\n",
|
||||
"\n",
|
||||
"This will be our source data listing the events happening in the summit.\n",
|
||||
"\n",
|
||||
"3. Go to Secrets Manager on the AWS console and create credentials to our atlas cluster via \"Other type of secret\":\n",
|
||||
"- key : username , value : `<ATLAS_USERNAME>`\n",
|
||||
"- key : password , value : `<ATLAS_PASSWORD>`\n",
|
||||
"\n",
|
||||
"4. Follow the setup of the knowledge base wizard to connect Bedrock models with Atlas :\n",
|
||||
"- Click \"Create Knowledge Base\" and input:\n",
|
||||
"\n",
|
||||
"|input|value|\n",
|
||||
"|---|---|\n",
|
||||
"|Name| `<NAME>` |\n",
|
||||
"|Chose| Create and use a new service role|\n",
|
||||
"|Data source name| `<NAME>`|\n",
|
||||
"|S3 URI| Browse for the S3 bucket hosting the 2 uploaded source files|\n",
|
||||
"|Embedding Model| Titan Text Embeddings v2|\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"- let's choose MongoDB Atlas in the \"Vector Database\" choose the \"Choose a vector store you have created\" section:\n",
|
||||
"\n",
|
||||
"|input|value|\n",
|
||||
"|---|---|\n",
|
||||
"|Select your vector store| **MongoDB Atlas** |\n",
|
||||
"|Hostname| Your atlas srv hostname `eg. cluster0.abcd.mongodb.net`|\n",
|
||||
"|Database name| `bedrock`|\n",
|
||||
"|Collection name| `agenda`|\n",
|
||||
"|Credentials secret ARN| Copy the created credentials from the \"Secrets manager\"|\n",
|
||||
"|Vector search index name|`vector_index`|\n",
|
||||
"|Vector embedding field path| `embedding`|\n",
|
||||
"|Text field path| `text`|\n",
|
||||
"|Metadata field path| `metadata` |\n",
|
||||
"5. Click Next, review the details and \"Create Knowledge Base\".\n",
|
||||
"\n",
|
||||
"6. Once the knowledge base is marked with \"Status : Ready\", go to `Data source` section, choose the one datasource we have and click the \"Sync\" button on its right upper corner. This operation should load the data to Atlas if everything was setup correctly.\n",
|
||||
"\n",
|
||||
"## Setting up an agenda agent\n",
|
||||
"\n",
|
||||
"We can now set up our agent, who will work with a set of instructions and our knowledge base.\n",
|
||||
"\n",
|
||||
"1. Go to the \"Agents\" tab in the bedrock UI.\n",
|
||||
"2. Click \"Create Agent\" and give it a meaningful name (e.g. agenda_assistant)\n",
|
||||
"3. Input the following data in the agent builder:\n",
|
||||
"\n",
|
||||
"|input|value|\n",
|
||||
"|---|---|\n",
|
||||
"|Agent Name| agenda_assistant |\n",
|
||||
"|Agent resource role| Create and use a new service role |\n",
|
||||
"|Select model| Anthropic - Claude 3 Sonnet |\n",
|
||||
"|Instructions for the Agent| **You are a friendly AI chatbot that helps users find and build agenda Items for AWS Summit Tel Aviv. elaborate as much as possible on the response.** |\n",
|
||||
"|Agent Name| agenda_assistant |\n",
|
||||
"|Knowledge bases| **Choose your Knowledge Base** |\n",
|
||||
"|Aliases| Create a new Alias|\n",
|
||||
"\n",
|
||||
"And now, we have a functioning agent that can be tested via the console.\n",
|
||||
"Let's move to the notebook.\n",
|
||||
"\n",
|
||||
"**Take note of the Agent ID and create an Agent Alias ID for the notebook**"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "CmKeBSvBWIcS"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Interacting with the agent\n",
|
||||
"\n",
|
||||
"To interact with the agent, we need to install the AWS python SDK:\n"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "NmjfN1HavIqF"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"!pip install boto3"
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "6L8lkSTzvig1",
|
||||
"outputId": "0300d850-872d-47e0-aae1-caa5396f3db3"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"name": "stdout",
|
||||
"text": [
|
||||
"Collecting boto3\n",
|
||||
" Downloading boto3-1.34.129-py3-none-any.whl (139 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m139.2/139.2 kB\u001b[0m \u001b[31m1.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hCollecting botocore<1.35.0,>=1.34.129 (from boto3)\n",
|
||||
" Downloading botocore-1.34.129-py3-none-any.whl (12.3 MB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.3/12.3 MB\u001b[0m \u001b[31m38.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hCollecting jmespath<2.0.0,>=0.7.1 (from boto3)\n",
|
||||
" Downloading jmespath-1.0.1-py3-none-any.whl (20 kB)\n",
|
||||
"Collecting s3transfer<0.11.0,>=0.10.0 (from boto3)\n",
|
||||
" Downloading s3transfer-0.10.1-py3-none-any.whl (82 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m82.2/82.2 kB\u001b[0m \u001b[31m6.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: python-dateutil<3.0.0,>=2.1 in /usr/local/lib/python3.10/dist-packages (from botocore<1.35.0,>=1.34.129->boto3) (2.8.2)\n",
|
||||
"Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in /usr/local/lib/python3.10/dist-packages (from botocore<1.35.0,>=1.34.129->boto3) (2.0.7)\n",
|
||||
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.35.0,>=1.34.129->boto3) (1.16.0)\n",
|
||||
"Installing collected packages: jmespath, botocore, s3transfer, boto3\n",
|
||||
"Successfully installed boto3-1.34.129 botocore-1.34.129 jmespath-1.0.1 s3transfer-0.10.1\n"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"Let's place the credentials for our AWS account.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "vt-G0dpYvq78"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"import boto3\n",
|
||||
"from botocore.exceptions import ClientError\n",
|
||||
"import os\n",
|
||||
"import random\n",
|
||||
"import getpass\n",
|
||||
"\n",
|
||||
"# Get AWS credentials from user\n",
|
||||
"aws_access_key = getpass.getpass(\"Enter your AWS Access Key: \")\n",
|
||||
"aws_secret_key = getpass.getpass(\"Enter your AWS Secret Key: \")\n"
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "tKzzqSX4v3tp",
|
||||
"outputId": "86ed2e5c-28bb-4b69-99b5-919f8cfdfc49"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your AWS Access Key: ··········\n",
|
||||
"Enter your AWS Secret Key: ··········\n"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"Now, we need to initialise the boto3 client and get the agent ID and alias ID input.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "sjT3QKaVwnI6"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"bedrock_agent_runtime = boto3.client('bedrock-agent-runtime',\n",
|
||||
" aws_access_key_id=aws_access_key,\n",
|
||||
" aws_secret_access_key=aws_secret_key,\n",
|
||||
" region_name=\"us-east-1\")\n",
|
||||
"\n",
|
||||
"# Define agent IDs (replace these with your actual agent IDs)\n",
|
||||
"agent_id = getpass.getpass(\"Enter your agent ID\")\n",
|
||||
"agent_alias_id = getpass.getpass(\"Enter your agent Alias ID\")"
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "cJt6aaxpw1e4",
|
||||
"outputId": "7ed4315a-0352-46d7-ffe8-af66ba7c5a4b"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your agent ID··········\n",
|
||||
"Enter your agent Alias ID··········\n"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"Let's build the helper function to interact with the agent.\n"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "srUoCSPwxIIz"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"def randomise_session_id():\n",
|
||||
" \"\"\"\n",
|
||||
" Generate a random session ID.\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" str: A random session ID.\n",
|
||||
" \"\"\"\n",
|
||||
" return str(random.randint(1000, 9999))\n",
|
||||
"\n",
|
||||
"def data_stream_generator(response):\n",
|
||||
" \"\"\"\n",
|
||||
" Generator to yield data chunks from the response.\n",
|
||||
"\n",
|
||||
" Args:\n",
|
||||
" response (dict): The response dictionary.\n",
|
||||
"\n",
|
||||
" Yields:\n",
|
||||
" str: The next chunk of data.\n",
|
||||
" \"\"\"\n",
|
||||
" for event in response[\"completion\"]:\n",
|
||||
" chunk = event.get(\"chunk\", {})\n",
|
||||
" if \"bytes\" in chunk:\n",
|
||||
" yield chunk[\"bytes\"].decode()\n",
|
||||
"\n",
|
||||
"def invoke_agent(bedrock_agent_runtime, agent_id, agent_alias_id, session_id, prompt):\n",
|
||||
" \"\"\"\n",
|
||||
" Sends a prompt for the agent to process and respond to, streaming the response data.\n",
|
||||
"\n",
|
||||
" Args:\n",
|
||||
" bedrock_agent_runtime (boto3 client): The runtime client to invoke the agent.\n",
|
||||
" agent_id (str): The unique identifier of the agent to use.\n",
|
||||
" agent_alias_id (str): The alias of the agent to use.\n",
|
||||
" session_id (str): The unique identifier of the session. Use the same value across requests to continue the same conversation.\n",
|
||||
" prompt (str): The prompt that you want the agent to complete.\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" str: The response from the agent.\n",
|
||||
" \"\"\"\n",
|
||||
" try:\n",
|
||||
" response = bedrock_agent_runtime.invoke_agent(\n",
|
||||
" agentId=agent_id,\n",
|
||||
" agentAliasId=agent_alias_id,\n",
|
||||
" sessionId=session_id,\n",
|
||||
" inputText=prompt,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # Use the data stream generator to stream the response\n",
|
||||
" ret_response = ''.join(data_stream_generator(response))\n",
|
||||
"\n",
|
||||
" return ret_response\n",
|
||||
"\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"Error invoking agent: {e}\""
|
||||
],
|
||||
"metadata": {
|
||||
"id": "-p1eClRQxL8x"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"We can now interact with the agent using the application code."
|
||||
],
|
||||
"metadata": {
|
||||
"id": "Pu9vtHsPxUsm"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Initialize chat history and session ID\n",
|
||||
"session_id = randomise_session_id()\n",
|
||||
"\n",
|
||||
"while True:\n",
|
||||
" prompt = input(\"Enter your prompt (or type 'exit' to quit): \")\n",
|
||||
"\n",
|
||||
" if prompt.lower() == 'exit':\n",
|
||||
" break\n",
|
||||
"\n",
|
||||
" response = invoke_agent(bedrock_agent_runtime, agent_id, agent_alias_id, session_id, prompt)\n",
|
||||
"\n",
|
||||
" print(\"Agent Response:\")\n",
|
||||
" print(response)"
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "sXs-omN5xYsk",
|
||||
"outputId": "d3f07de7-1b9c-4e16-a787-5cd47d64de83"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your prompt (or type 'exit' to quit): What agenda items are present in the AWS summit\n",
|
||||
"Agent Response:\n",
|
||||
"The AWS Summit agenda items include sessions on digital transformation, generative AI, multi-cloud management, machine learning, vector databases, and OpenSearch services. Other agenda items cover topics like scaling AI within organizations, application resilience with AWS, Amazon Q for GenAI, and leveraging LLM-based AI agents.\n",
|
||||
"Enter your prompt (or type 'exit' to quit): exit\n"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"Here you go! You have a powerful bedrock agent with MongoDB Atlas.\n",
|
||||
"\n",
|
||||
"Conclusions\n",
|
||||
"The integration of MongoDB Atlas with Amazon Bedrock represents a significant advancement in the development and deployment of generative AI applications. By leveraging Atlas's vector search capabilities and the powerful foundational models available through Bedrock, developers can create applications that are both highly accurate and deeply informed by enterprise data. This seamless integration facilitates the retrieval-augmented generation (RAG) workflow, enabling AI models to access and utilize the most relevant data, thereby reducing the likelihood of hallucinations and improving overall performance.\n",
|
||||
"\n",
|
||||
"The benefits of this integration extend beyond just technical enhancements. It also simplifies the generative AI stack, allowing companies to rapidly deploy scalable AI solutions with enhanced privacy and security features, such as those provided by AWS PrivateLink. This makes it an ideal solution for enterprises with stringent data security requirements. Overall, the combination of MongoDB Atlas and Amazon Bedrock provides a robust, efficient, and secure platform for building next-generation AI applications .\n"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "4-SdBf5ox0KF"
|
||||
}
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Collecting boto3\n",
|
||||
" Downloading boto3-1.34.129-py3-none-any.whl (139 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m139.2/139.2 kB\u001b[0m \u001b[31m1.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hCollecting botocore<1.35.0,>=1.34.129 (from boto3)\n",
|
||||
" Downloading botocore-1.34.129-py3-none-any.whl (12.3 MB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.3/12.3 MB\u001b[0m \u001b[31m38.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hCollecting jmespath<2.0.0,>=0.7.1 (from boto3)\n",
|
||||
" Downloading jmespath-1.0.1-py3-none-any.whl (20 kB)\n",
|
||||
"Collecting s3transfer<0.11.0,>=0.10.0 (from boto3)\n",
|
||||
" Downloading s3transfer-0.10.1-py3-none-any.whl (82 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m82.2/82.2 kB\u001b[0m \u001b[31m6.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: python-dateutil<3.0.0,>=2.1 in /usr/local/lib/python3.10/dist-packages (from botocore<1.35.0,>=1.34.129->boto3) (2.8.2)\n",
|
||||
"Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in /usr/local/lib/python3.10/dist-packages (from botocore<1.35.0,>=1.34.129->boto3) (2.0.7)\n",
|
||||
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.35.0,>=1.34.129->boto3) (1.16.0)\n",
|
||||
"Installing collected packages: jmespath, botocore, s3transfer, boto3\n",
|
||||
"Successfully installed boto3-1.34.129 botocore-1.34.129 jmespath-1.0.1 s3transfer-0.10.1\n"
|
||||
]
|
||||
}
|
||||
]
|
||||
],
|
||||
"source": [
|
||||
"!pip install boto3"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "vt-G0dpYvq78"
|
||||
},
|
||||
"source": [
|
||||
"Let's place the credentials for our AWS account.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "tKzzqSX4v3tp",
|
||||
"outputId": "86ed2e5c-28bb-4b69-99b5-919f8cfdfc49"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your AWS Access Key: ··········\n",
|
||||
"Enter your AWS Secret Key: ··········\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import boto3\n",
|
||||
"from botocore.exceptions import ClientError\n",
|
||||
"import os\n",
|
||||
"import random\n",
|
||||
"import getpass\n",
|
||||
"\n",
|
||||
"# Get AWS credentials from user\n",
|
||||
"aws_access_key = getpass.getpass(\"Enter your AWS Access Key: \")\n",
|
||||
"aws_secret_key = getpass.getpass(\"Enter your AWS Secret Key: \")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "sjT3QKaVwnI6"
|
||||
},
|
||||
"source": [
|
||||
"Now, we need to initialise the boto3 client and get the agent ID and alias ID input.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "cJt6aaxpw1e4",
|
||||
"outputId": "7ed4315a-0352-46d7-ffe8-af66ba7c5a4b"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your agent ID··········\n",
|
||||
"Enter your agent Alias ID··········\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"bedrock_agent_runtime = boto3.client(\n",
|
||||
" \"bedrock-agent-runtime\",\n",
|
||||
" aws_access_key_id=aws_access_key,\n",
|
||||
" aws_secret_access_key=aws_secret_key,\n",
|
||||
" region_name=\"us-east-1\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Define agent IDs (replace these with your actual agent IDs)\n",
|
||||
"agent_id = getpass.getpass(\"Enter your agent ID\")\n",
|
||||
"agent_alias_id = getpass.getpass(\"Enter your agent Alias ID\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "srUoCSPwxIIz"
|
||||
},
|
||||
"source": [
|
||||
"Let's build the helper function to interact with the agent.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "-p1eClRQxL8x"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def randomise_session_id():\n",
|
||||
" \"\"\"\n",
|
||||
" Generate a random session ID.\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" str: A random session ID.\n",
|
||||
" \"\"\"\n",
|
||||
" return str(random.randint(1000, 9999))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def data_stream_generator(response):\n",
|
||||
" \"\"\"\n",
|
||||
" Generator to yield data chunks from the response.\n",
|
||||
"\n",
|
||||
" Args:\n",
|
||||
" response (dict): The response dictionary.\n",
|
||||
"\n",
|
||||
" Yields:\n",
|
||||
" str: The next chunk of data.\n",
|
||||
" \"\"\"\n",
|
||||
" for event in response[\"completion\"]:\n",
|
||||
" chunk = event.get(\"chunk\", {})\n",
|
||||
" if \"bytes\" in chunk:\n",
|
||||
" yield chunk[\"bytes\"].decode()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def invoke_agent(bedrock_agent_runtime, agent_id, agent_alias_id, session_id, prompt):\n",
|
||||
" \"\"\"\n",
|
||||
" Sends a prompt for the agent to process and respond to, streaming the response data.\n",
|
||||
"\n",
|
||||
" Args:\n",
|
||||
" bedrock_agent_runtime (boto3 client): The runtime client to invoke the agent.\n",
|
||||
" agent_id (str): The unique identifier of the agent to use.\n",
|
||||
" agent_alias_id (str): The alias of the agent to use.\n",
|
||||
" session_id (str): The unique identifier of the session. Use the same value across requests to continue the same conversation.\n",
|
||||
" prompt (str): The prompt that you want the agent to complete.\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" str: The response from the agent.\n",
|
||||
" \"\"\"\n",
|
||||
" try:\n",
|
||||
" response = bedrock_agent_runtime.invoke_agent(\n",
|
||||
" agentId=agent_id,\n",
|
||||
" agentAliasId=agent_alias_id,\n",
|
||||
" sessionId=session_id,\n",
|
||||
" inputText=prompt,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # Use the data stream generator to stream the response\n",
|
||||
" ret_response = \"\".join(data_stream_generator(response))\n",
|
||||
"\n",
|
||||
" return ret_response\n",
|
||||
"\n",
|
||||
" except Exception as e:\n",
|
||||
" return f\"Error invoking agent: {e}\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "Pu9vtHsPxUsm"
|
||||
},
|
||||
"source": [
|
||||
"We can now interact with the agent using the application code."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "sXs-omN5xYsk",
|
||||
"outputId": "d3f07de7-1b9c-4e16-a787-5cd47d64de83"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your prompt (or type 'exit' to quit): What agenda items are present in the AWS summit\n",
|
||||
"Agent Response:\n",
|
||||
"The AWS Summit agenda items include sessions on digital transformation, generative AI, multi-cloud management, machine learning, vector databases, and OpenSearch services. Other agenda items cover topics like scaling AI within organizations, application resilience with AWS, Amazon Q for GenAI, and leveraging LLM-based AI agents.\n",
|
||||
"Enter your prompt (or type 'exit' to quit): exit\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Initialize chat history and session ID\n",
|
||||
"session_id = randomise_session_id()\n",
|
||||
"\n",
|
||||
"while True:\n",
|
||||
" prompt = input(\"Enter your prompt (or type 'exit' to quit): \")\n",
|
||||
"\n",
|
||||
" if prompt.lower() == \"exit\":\n",
|
||||
" break\n",
|
||||
"\n",
|
||||
" response = invoke_agent(\n",
|
||||
" bedrock_agent_runtime, agent_id, agent_alias_id, session_id, prompt\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" print(\"Agent Response:\")\n",
|
||||
" print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "4-SdBf5ox0KF"
|
||||
},
|
||||
"source": [
|
||||
"Here you go! You have a powerful bedrock agent with MongoDB Atlas.\n",
|
||||
"\n",
|
||||
"Conclusions\n",
|
||||
"The integration of MongoDB Atlas with Amazon Bedrock represents a significant advancement in the development and deployment of generative AI applications. By leveraging Atlas's vector search capabilities and the powerful foundational models available through Bedrock, developers can create applications that are both highly accurate and deeply informed by enterprise data. This seamless integration facilitates the retrieval-augmented generation (RAG) workflow, enabling AI models to access and utilize the most relevant data, thereby reducing the likelihood of hallucinations and improving overall performance.\n",
|
||||
"\n",
|
||||
"The benefits of this integration extend beyond just technical enhancements. It also simplifies the generative AI stack, allowing companies to rapidly deploy scalable AI solutions with enhanced privacy and security features, such as those provided by AWS PrivateLink. This makes it an ideal solution for enterprises with stringent data security requirements. Overall, the combination of MongoDB Atlas and Amazon Bedrock provides a robust, efficient, and secure platform for building next-generation AI applications .\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
|
||||
@@ -23,10 +23,8 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"```python\n",
|
||||
"import pymongo\n",
|
||||
@@ -76,12 +74,10 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"```python\n",
|
||||
"import pymongo\n",
|
||||
"\n",
|
||||
"# connect to your Atlas cluster\n",
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -1,443 +1,455 @@
|
||||
{
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"name": "python3",
|
||||
"display_name": "Python 3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "QFdG4eYf3h0L"
|
||||
},
|
||||
"source": [
|
||||
"[](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/rag/Haystack_MongoDB_Atlas_RAG.ipynb)\n"
|
||||
]
|
||||
},
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "QFdG4eYf3h0L"
|
||||
},
|
||||
"source": [
|
||||
"[](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/rag/Haystack_MongoDB_Atlas_RAG.ipynb)\n"
|
||||
]
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "rrobdhRcNb5I"
|
||||
},
|
||||
"source": [
|
||||
"# Haystack and MongoDB Atlas RAG notebook\n",
|
||||
"\n",
|
||||
"Install dependencies:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "76dK0ehtNY2L",
|
||||
"outputId": "8268021e-8ba1-48e3-e3df-c16625285d31"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# Haystack and MongoDB Atlas RAG notebook\n",
|
||||
"\n",
|
||||
"Install dependencies:"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "rrobdhRcNb5I"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "76dK0ehtNY2L",
|
||||
"outputId": "8268021e-8ba1-48e3-e3df-c16625285d31"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"name": "stdout",
|
||||
"text": [
|
||||
"Collecting haystack-ai\n",
|
||||
" Downloading haystack_ai-2.1.2-py3-none-any.whl (319 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m319.5/319.5 kB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hCollecting mongodb-atlas-haystack\n",
|
||||
" Downloading mongodb_atlas_haystack-0.3.0-py3-none-any.whl (13 kB)\n",
|
||||
"Collecting tiktoken\n",
|
||||
" Downloading tiktoken-0.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m29.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hCollecting boilerpy3 (from haystack-ai)\n",
|
||||
" Downloading boilerpy3-1.0.7-py3-none-any.whl (22 kB)\n",
|
||||
"Collecting haystack-bm25 (from haystack-ai)\n",
|
||||
" Downloading haystack_bm25-1.0.2-py2.py3-none-any.whl (8.8 kB)\n",
|
||||
"Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (3.1.4)\n",
|
||||
"Collecting lazy-imports (from haystack-ai)\n",
|
||||
" Downloading lazy_imports-0.3.1-py3-none-any.whl (12 kB)\n",
|
||||
"Requirement already satisfied: more-itertools in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (10.1.0)\n",
|
||||
"Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (3.3)\n",
|
||||
"Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (1.25.2)\n",
|
||||
"Collecting openai>=1.1.0 (from haystack-ai)\n",
|
||||
" Downloading openai-1.30.5-py3-none-any.whl (320 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m320.7/320.7 kB\u001b[0m \u001b[31m15.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (2.0.3)\n",
|
||||
"Collecting posthog (from haystack-ai)\n",
|
||||
" Downloading posthog-3.5.0-py2.py3-none-any.whl (41 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m41.3/41.3 kB\u001b[0m \u001b[31m4.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: python-dateutil in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (2.8.2)\n",
|
||||
"Requirement already satisfied: pyyaml in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (6.0.1)\n",
|
||||
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (2.31.0)\n",
|
||||
"Requirement already satisfied: tenacity in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (8.3.0)\n",
|
||||
"Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (4.66.4)\n",
|
||||
"Requirement already satisfied: typing-extensions>=4.7 in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (4.11.0)\n",
|
||||
"Collecting pymongo[srv] (from mongodb-atlas-haystack)\n",
|
||||
" Downloading pymongo-4.7.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (670 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m670.0/670.0 kB\u001b[0m \u001b[31m12.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from tiktoken) (2024.5.15)\n",
|
||||
"Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai>=1.1.0->haystack-ai) (3.7.1)\n",
|
||||
"Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai>=1.1.0->haystack-ai) (1.7.0)\n",
|
||||
"Collecting httpx<1,>=0.23.0 (from openai>=1.1.0->haystack-ai)\n",
|
||||
" Downloading httpx-0.27.0-py3-none-any.whl (75 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m7.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from openai>=1.1.0->haystack-ai) (2.7.1)\n",
|
||||
"Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai>=1.1.0->haystack-ai) (1.3.1)\n",
|
||||
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->haystack-ai) (3.3.2)\n",
|
||||
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->haystack-ai) (3.7)\n",
|
||||
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->haystack-ai) (2.0.7)\n",
|
||||
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->haystack-ai) (2024.2.2)\n",
|
||||
"Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->haystack-ai) (2.1.5)\n",
|
||||
"Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->haystack-ai) (2023.4)\n",
|
||||
"Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas->haystack-ai) (2024.1)\n",
|
||||
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil->haystack-ai) (1.16.0)\n",
|
||||
"Collecting monotonic>=1.5 (from posthog->haystack-ai)\n",
|
||||
" Downloading monotonic-1.6-py2.py3-none-any.whl (8.2 kB)\n",
|
||||
"Collecting backoff>=1.10.0 (from posthog->haystack-ai)\n",
|
||||
" Downloading backoff-2.2.1-py3-none-any.whl (15 kB)\n",
|
||||
"Collecting dnspython<3.0.0,>=1.16.0 (from pymongo[srv]->mongodb-atlas-haystack)\n",
|
||||
" Downloading dnspython-2.6.1-py3-none-any.whl (307 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m307.7/307.7 kB\u001b[0m \u001b[31m20.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai>=1.1.0->haystack-ai) (1.2.1)\n",
|
||||
"Collecting httpcore==1.* (from httpx<1,>=0.23.0->openai>=1.1.0->haystack-ai)\n",
|
||||
" Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hCollecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1,>=0.23.0->openai>=1.1.0->haystack-ai)\n",
|
||||
" Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m4.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai>=1.1.0->haystack-ai) (0.7.0)\n",
|
||||
"Requirement already satisfied: pydantic-core==2.18.2 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai>=1.1.0->haystack-ai) (2.18.2)\n",
|
||||
"Installing collected packages: monotonic, lazy-imports, haystack-bm25, h11, dnspython, boilerpy3, backoff, tiktoken, pymongo, posthog, httpcore, httpx, openai, haystack-ai, mongodb-atlas-haystack\n",
|
||||
"Successfully installed backoff-2.2.1 boilerpy3-1.0.7 dnspython-2.6.1 h11-0.14.0 haystack-ai-2.1.2 haystack-bm25-1.0.2 httpcore-1.0.5 httpx-0.27.0 lazy-imports-0.3.1 mongodb-atlas-haystack-0.3.0 monotonic-1.6 openai-1.30.5 posthog-3.5.0 pymongo-4.7.2 tiktoken-0.7.0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"pip install haystack-ai mongodb-atlas-haystack tiktoken"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"\n",
|
||||
"## Setup MongoDB Atlas connection and Open AI\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"* Set the MongoDB connection string. Follow the steps [here](https://www.mongodb.com/docs/manual/reference/connection-string/) to get the connection string from the Atlas UI.\n",
|
||||
"\n",
|
||||
"* Set the OpenAI API key. Steps to obtain an API key as [here](https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "aeg_wcIiPYnY"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"import getpass\n",
|
||||
"import os\n"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "MZokdDxIPb9p"
|
||||
},
|
||||
"execution_count": 2,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"os.environ['MONGO_CONNECTION_STRING']=getpass.getpass(\"Enter your MongoDB connection string:\")"
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "57gYJTBVPfBX",
|
||||
"outputId": "9a3841f2-7b7d-4250-bed2-6b139415b5d5"
|
||||
},
|
||||
"execution_count": 3,
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your MongoDB connection string:··········\n"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"os.environ['OPENAI_API_KEY']=getpass.getpass(\"Enter your Open AI Key:\")"
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "J8Gd-SMuRSH-",
|
||||
"outputId": "bd1135cd-c508-4e4a-d57b-a0d15a82d126"
|
||||
},
|
||||
"execution_count": 4,
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your Open AI Key:··········\n"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [],
|
||||
"metadata": {
|
||||
"id": "HnG43-yPRIl3"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Create vector search index on collection\n",
|
||||
"\n",
|
||||
"Follow this [tutorial](https://www.mongodb.com/docs/atlas/atlas-vector-search/create-index/) to create a vector index on database: `haystack_test` collection `test_collection`.\n",
|
||||
"\n",
|
||||
"Verify that the index name is `vector_index` and the syntax specify:\n",
|
||||
"```\n",
|
||||
"{\n",
|
||||
" \"fields\": [\n",
|
||||
" {\n",
|
||||
" \"type\": \"vector\",\n",
|
||||
" \"path\": \"embedding\",\n",
|
||||
" \"numDimensions\": 1536,\n",
|
||||
" \"similarity\": \"cosine\"\n",
|
||||
" }\n",
|
||||
" ]\n",
|
||||
"}\n",
|
||||
"```"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "Fv1pPHqXQFa-"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"### Setup vector store to load documents:"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "cOMyplbvOMDk"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"from haystack import Pipeline, Document\n",
|
||||
"from haystack.document_stores.types import DuplicatePolicy\n",
|
||||
"from haystack.components.writers import DocumentWriter\n",
|
||||
"from haystack.components.generators import OpenAIGenerator\n",
|
||||
"from haystack.components.builders.prompt_builder import PromptBuilder\n",
|
||||
"from haystack.components.embedders import OpenAITextEmbedder, OpenAIDocumentEmbedder\n",
|
||||
"from haystack_integrations.document_stores.mongodb_atlas import MongoDBAtlasDocumentStore\n",
|
||||
"from haystack_integrations.components.retrievers.mongodb_atlas import MongoDBAtlasEmbeddingRetriever\n",
|
||||
"\n",
|
||||
"# Create some example documents\n",
|
||||
"documents = [\n",
|
||||
" Document(content=\"My name is Jean and I live in Paris.\"),\n",
|
||||
" Document(content=\"My name is Mark and I live in Berlin.\"),\n",
|
||||
" Document(content=\"My name is Giorgio and I live in Rome.\"),\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"document_store = MongoDBAtlasDocumentStore(\n",
|
||||
" database_name=\"haystack_test\",\n",
|
||||
" collection_name=\"test_collection\",\n",
|
||||
" vector_search_index=\"vector_index\",\n",
|
||||
")"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "-y9waymAOOgs"
|
||||
},
|
||||
"execution_count": 5,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"Build the writer pipeline to load documnets"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "3MMitwR3P0uj"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Setting up a document writer to handle the insertion of documents into the MongoDB collection.\n",
|
||||
"doc_writer = DocumentWriter(document_store=document_store, policy=DuplicatePolicy.SKIP)\n",
|
||||
"\n",
|
||||
"# Initializing a document embedder to convert text content into vectorized form.\n",
|
||||
"doc_embedder = OpenAIDocumentEmbedder()\n",
|
||||
"\n",
|
||||
"# Creating a pipeline for indexing documents. The pipeline includes embedding and writing documents.\n",
|
||||
"indexing_pipe = Pipeline()\n",
|
||||
"indexing_pipe.add_component(instance=doc_embedder, name=\"doc_embedder\")\n",
|
||||
"indexing_pipe.add_component(instance=doc_writer, name=\"doc_writer\")\n",
|
||||
"\n",
|
||||
"# Connecting the components of the pipeline for document flow.\n",
|
||||
"indexing_pipe.connect(\"doc_embedder.documents\", \"doc_writer.documents\")\n",
|
||||
"\n",
|
||||
"# Running the pipeline with the list of documents to index them in MongoDB.\n",
|
||||
"indexing_pipe.run({\"doc_embedder\": {\"documents\": documents}})\n"
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "dYEo2ZkMQptv",
|
||||
"outputId": "e6a86d38-7891-4cc8-e8ed-e15e39fc86e8"
|
||||
},
|
||||
"execution_count": 6,
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"name": "stderr",
|
||||
"text": [
|
||||
"Calculating embeddings: 100%|██████████| 1/1 [00:00<00:00, 4.16it/s]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"output_type": "execute_result",
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'doc_embedder': {'meta': {'model': 'text-embedding-ada-002',\n",
|
||||
" 'usage': {'prompt_tokens': 32, 'total_tokens': 32}}},\n",
|
||||
" 'doc_writer': {'documents_written': 0}}"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"execution_count": 6
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Build a RAG Pipeline\n",
|
||||
"\n",
|
||||
"Lets create a pipeline that will Retrieve Augment and Generate a response for user questions"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "TA_ELb_URY0D"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Template for generating prompts for a movie recommendation engine.\n",
|
||||
"prompt_template = \"\"\"\n",
|
||||
" You are an assistant allowed to use the following context documents.\\nDocuments:\n",
|
||||
" {% for doc in documents %}\n",
|
||||
" {{ doc.content }}\n",
|
||||
" {% endfor %}\n",
|
||||
"\n",
|
||||
" \\Query: {{query}}\n",
|
||||
" \\nAnswer:\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"# Setting up a retrieval-augmented generation (RAG) pipeline for generating responses.\n",
|
||||
"rag_pipeline = Pipeline()\n",
|
||||
"rag_pipeline.add_component(\"text_embedder\", OpenAITextEmbedder())\n",
|
||||
"\n",
|
||||
"# Adding a component for retrieving related documents from MongoDB based on the query embedding.\n",
|
||||
"rag_pipeline.add_component(instance=MongoDBAtlasEmbeddingRetriever(document_store=document_store,top_k=15), name=\"retriever\")\n",
|
||||
"\n",
|
||||
"# Building prompts based on retrieved documents to be used for generating responses.\n",
|
||||
"rag_pipeline.add_component(instance=PromptBuilder(template=prompt_template), name=\"prompt_builder\")\n",
|
||||
"\n",
|
||||
"# Adding a language model generator to produce the final text output.\n",
|
||||
"rag_pipeline.add_component(instance=OpenAIGenerator(), name=\"llm\")\n",
|
||||
"\n",
|
||||
"# Connecting the components of the RAG pipeline to ensure proper data flow.\n",
|
||||
"rag_pipeline.connect(\"text_embedder.embedding\", \"retriever.query_embedding\")\n",
|
||||
"rag_pipeline.connect(\"retriever\", \"prompt_builder.documents\")\n",
|
||||
"rag_pipeline.connect(\"prompt_builder\", \"llm\")"
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "RaNFayobRkU9",
|
||||
"outputId": "0152319b-631d-4d45-ec03-7a8c1a2561ea"
|
||||
},
|
||||
"execution_count": 9,
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "execute_result",
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<haystack.core.pipeline.pipeline.Pipeline object at 0x7fc98d95bdf0>\n",
|
||||
"🚅 Components\n",
|
||||
" - text_embedder: OpenAITextEmbedder\n",
|
||||
" - retriever: MongoDBAtlasEmbeddingRetriever\n",
|
||||
" - prompt_builder: PromptBuilder\n",
|
||||
" - llm: OpenAIGenerator\n",
|
||||
"🛤️ Connections\n",
|
||||
" - text_embedder.embedding -> retriever.query_embedding (List[float])\n",
|
||||
" - retriever.documents -> prompt_builder.documents (List[Document])\n",
|
||||
" - prompt_builder.prompt -> llm.prompt (str)"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"execution_count": 9
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"Lets test the pipeline"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "n7mEU3ydRz8k"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"query = \"Where does mark live?\"\n",
|
||||
"result = rag_pipeline.run(\n",
|
||||
" {\n",
|
||||
" \"text_embedder\": {\"text\": query},\n",
|
||||
" \"prompt_builder\": {\"query\": query},\n",
|
||||
" });\n",
|
||||
"print(result['llm']['replies'][0])"
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "GHV5rRhaR3SX",
|
||||
"outputId": "dfb23085-ef80-4c99-a1c2-dd3f107ef313"
|
||||
},
|
||||
"execution_count": 12,
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"name": "stdout",
|
||||
"text": [
|
||||
"Mark lives in Berlin.\n"
|
||||
]
|
||||
}
|
||||
]
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Collecting haystack-ai\n",
|
||||
" Downloading haystack_ai-2.1.2-py3-none-any.whl (319 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m319.5/319.5 kB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hCollecting mongodb-atlas-haystack\n",
|
||||
" Downloading mongodb_atlas_haystack-0.3.0-py3-none-any.whl (13 kB)\n",
|
||||
"Collecting tiktoken\n",
|
||||
" Downloading tiktoken-0.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m29.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hCollecting boilerpy3 (from haystack-ai)\n",
|
||||
" Downloading boilerpy3-1.0.7-py3-none-any.whl (22 kB)\n",
|
||||
"Collecting haystack-bm25 (from haystack-ai)\n",
|
||||
" Downloading haystack_bm25-1.0.2-py2.py3-none-any.whl (8.8 kB)\n",
|
||||
"Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (3.1.4)\n",
|
||||
"Collecting lazy-imports (from haystack-ai)\n",
|
||||
" Downloading lazy_imports-0.3.1-py3-none-any.whl (12 kB)\n",
|
||||
"Requirement already satisfied: more-itertools in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (10.1.0)\n",
|
||||
"Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (3.3)\n",
|
||||
"Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (1.25.2)\n",
|
||||
"Collecting openai>=1.1.0 (from haystack-ai)\n",
|
||||
" Downloading openai-1.30.5-py3-none-any.whl (320 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m320.7/320.7 kB\u001b[0m \u001b[31m15.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (2.0.3)\n",
|
||||
"Collecting posthog (from haystack-ai)\n",
|
||||
" Downloading posthog-3.5.0-py2.py3-none-any.whl (41 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m41.3/41.3 kB\u001b[0m \u001b[31m4.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: python-dateutil in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (2.8.2)\n",
|
||||
"Requirement already satisfied: pyyaml in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (6.0.1)\n",
|
||||
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (2.31.0)\n",
|
||||
"Requirement already satisfied: tenacity in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (8.3.0)\n",
|
||||
"Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (4.66.4)\n",
|
||||
"Requirement already satisfied: typing-extensions>=4.7 in /usr/local/lib/python3.10/dist-packages (from haystack-ai) (4.11.0)\n",
|
||||
"Collecting pymongo[srv] (from mongodb-atlas-haystack)\n",
|
||||
" Downloading pymongo-4.7.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (670 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m670.0/670.0 kB\u001b[0m \u001b[31m12.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from tiktoken) (2024.5.15)\n",
|
||||
"Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai>=1.1.0->haystack-ai) (3.7.1)\n",
|
||||
"Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai>=1.1.0->haystack-ai) (1.7.0)\n",
|
||||
"Collecting httpx<1,>=0.23.0 (from openai>=1.1.0->haystack-ai)\n",
|
||||
" Downloading httpx-0.27.0-py3-none-any.whl (75 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m7.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from openai>=1.1.0->haystack-ai) (2.7.1)\n",
|
||||
"Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai>=1.1.0->haystack-ai) (1.3.1)\n",
|
||||
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->haystack-ai) (3.3.2)\n",
|
||||
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->haystack-ai) (3.7)\n",
|
||||
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->haystack-ai) (2.0.7)\n",
|
||||
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->haystack-ai) (2024.2.2)\n",
|
||||
"Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->haystack-ai) (2.1.5)\n",
|
||||
"Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->haystack-ai) (2023.4)\n",
|
||||
"Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas->haystack-ai) (2024.1)\n",
|
||||
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil->haystack-ai) (1.16.0)\n",
|
||||
"Collecting monotonic>=1.5 (from posthog->haystack-ai)\n",
|
||||
" Downloading monotonic-1.6-py2.py3-none-any.whl (8.2 kB)\n",
|
||||
"Collecting backoff>=1.10.0 (from posthog->haystack-ai)\n",
|
||||
" Downloading backoff-2.2.1-py3-none-any.whl (15 kB)\n",
|
||||
"Collecting dnspython<3.0.0,>=1.16.0 (from pymongo[srv]->mongodb-atlas-haystack)\n",
|
||||
" Downloading dnspython-2.6.1-py3-none-any.whl (307 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m307.7/307.7 kB\u001b[0m \u001b[31m20.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai>=1.1.0->haystack-ai) (1.2.1)\n",
|
||||
"Collecting httpcore==1.* (from httpx<1,>=0.23.0->openai>=1.1.0->haystack-ai)\n",
|
||||
" Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hCollecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1,>=0.23.0->openai>=1.1.0->haystack-ai)\n",
|
||||
" Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m4.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hRequirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai>=1.1.0->haystack-ai) (0.7.0)\n",
|
||||
"Requirement already satisfied: pydantic-core==2.18.2 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai>=1.1.0->haystack-ai) (2.18.2)\n",
|
||||
"Installing collected packages: monotonic, lazy-imports, haystack-bm25, h11, dnspython, boilerpy3, backoff, tiktoken, pymongo, posthog, httpcore, httpx, openai, haystack-ai, mongodb-atlas-haystack\n",
|
||||
"Successfully installed backoff-2.2.1 boilerpy3-1.0.7 dnspython-2.6.1 h11-0.14.0 haystack-ai-2.1.2 haystack-bm25-1.0.2 httpcore-1.0.5 httpx-0.27.0 lazy-imports-0.3.1 mongodb-atlas-haystack-0.3.0 monotonic-1.6 openai-1.30.5 posthog-3.5.0 pymongo-4.7.2 tiktoken-0.7.0\n"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"pip install haystack-ai mongodb-atlas-haystack tiktoken"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "aeg_wcIiPYnY"
|
||||
},
|
||||
"source": [
|
||||
"\n",
|
||||
"## Setup MongoDB Atlas connection and Open AI\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"* Set the MongoDB connection string. Follow the steps [here](https://www.mongodb.com/docs/manual/reference/connection-string/) to get the connection string from the Atlas UI.\n",
|
||||
"\n",
|
||||
"* Set the OpenAI API key. Steps to obtain an API key as [here](https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"id": "MZokdDxIPb9p"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import getpass\n",
|
||||
"import os"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "57gYJTBVPfBX",
|
||||
"outputId": "9a3841f2-7b7d-4250-bed2-6b139415b5d5"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your MongoDB connection string:··········\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"os.environ[\"MONGO_CONNECTION_STRING\"] = getpass.getpass(\n",
|
||||
" \"Enter your MongoDB connection string:\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "J8Gd-SMuRSH-",
|
||||
"outputId": "bd1135cd-c508-4e4a-d57b-a0d15a82d126"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your Open AI Key:··········\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"Enter your Open AI Key:\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "HnG43-yPRIl3"
|
||||
},
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "Fv1pPHqXQFa-"
|
||||
},
|
||||
"source": [
|
||||
"## Create vector search index on collection\n",
|
||||
"\n",
|
||||
"Follow this [tutorial](https://www.mongodb.com/docs/atlas/atlas-vector-search/create-index/) to create a vector index on database: `haystack_test` collection `test_collection`.\n",
|
||||
"\n",
|
||||
"Verify that the index name is `vector_index` and the syntax specify:\n",
|
||||
"```\n",
|
||||
"{\n",
|
||||
" \"fields\": [\n",
|
||||
" {\n",
|
||||
" \"type\": \"vector\",\n",
|
||||
" \"path\": \"embedding\",\n",
|
||||
" \"numDimensions\": 1536,\n",
|
||||
" \"similarity\": \"cosine\"\n",
|
||||
" }\n",
|
||||
" ]\n",
|
||||
"}\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "cOMyplbvOMDk"
|
||||
},
|
||||
"source": [
|
||||
"### Setup vector store to load documents:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"id": "-y9waymAOOgs"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from haystack import Pipeline, Document\n",
|
||||
"from haystack.document_stores.types import DuplicatePolicy\n",
|
||||
"from haystack.components.writers import DocumentWriter\n",
|
||||
"from haystack.components.generators import OpenAIGenerator\n",
|
||||
"from haystack.components.builders.prompt_builder import PromptBuilder\n",
|
||||
"from haystack.components.embedders import OpenAITextEmbedder, OpenAIDocumentEmbedder\n",
|
||||
"from haystack_integrations.document_stores.mongodb_atlas import (\n",
|
||||
" MongoDBAtlasDocumentStore,\n",
|
||||
")\n",
|
||||
"from haystack_integrations.components.retrievers.mongodb_atlas import (\n",
|
||||
" MongoDBAtlasEmbeddingRetriever,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Create some example documents\n",
|
||||
"documents = [\n",
|
||||
" Document(content=\"My name is Jean and I live in Paris.\"),\n",
|
||||
" Document(content=\"My name is Mark and I live in Berlin.\"),\n",
|
||||
" Document(content=\"My name is Giorgio and I live in Rome.\"),\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"document_store = MongoDBAtlasDocumentStore(\n",
|
||||
" database_name=\"haystack_test\",\n",
|
||||
" collection_name=\"test_collection\",\n",
|
||||
" vector_search_index=\"vector_index\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "3MMitwR3P0uj"
|
||||
},
|
||||
"source": [
|
||||
"Build the writer pipeline to load documnets"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "dYEo2ZkMQptv",
|
||||
"outputId": "e6a86d38-7891-4cc8-e8ed-e15e39fc86e8"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Calculating embeddings: 100%|██████████| 1/1 [00:00<00:00, 4.16it/s]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'doc_embedder': {'meta': {'model': 'text-embedding-ada-002',\n",
|
||||
" 'usage': {'prompt_tokens': 32, 'total_tokens': 32}}},\n",
|
||||
" 'doc_writer': {'documents_written': 0}}"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Setting up a document writer to handle the insertion of documents into the MongoDB collection.\n",
|
||||
"doc_writer = DocumentWriter(document_store=document_store, policy=DuplicatePolicy.SKIP)\n",
|
||||
"\n",
|
||||
"# Initializing a document embedder to convert text content into vectorized form.\n",
|
||||
"doc_embedder = OpenAIDocumentEmbedder()\n",
|
||||
"\n",
|
||||
"# Creating a pipeline for indexing documents. The pipeline includes embedding and writing documents.\n",
|
||||
"indexing_pipe = Pipeline()\n",
|
||||
"indexing_pipe.add_component(instance=doc_embedder, name=\"doc_embedder\")\n",
|
||||
"indexing_pipe.add_component(instance=doc_writer, name=\"doc_writer\")\n",
|
||||
"\n",
|
||||
"# Connecting the components of the pipeline for document flow.\n",
|
||||
"indexing_pipe.connect(\"doc_embedder.documents\", \"doc_writer.documents\")\n",
|
||||
"\n",
|
||||
"# Running the pipeline with the list of documents to index them in MongoDB.\n",
|
||||
"indexing_pipe.run({\"doc_embedder\": {\"documents\": documents}})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "TA_ELb_URY0D"
|
||||
},
|
||||
"source": [
|
||||
"## Build a RAG Pipeline\n",
|
||||
"\n",
|
||||
"Lets create a pipeline that will Retrieve Augment and Generate a response for user questions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "RaNFayobRkU9",
|
||||
"outputId": "0152319b-631d-4d45-ec03-7a8c1a2561ea"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<haystack.core.pipeline.pipeline.Pipeline object at 0x7fc98d95bdf0>\n",
|
||||
"🚅 Components\n",
|
||||
" - text_embedder: OpenAITextEmbedder\n",
|
||||
" - retriever: MongoDBAtlasEmbeddingRetriever\n",
|
||||
" - prompt_builder: PromptBuilder\n",
|
||||
" - llm: OpenAIGenerator\n",
|
||||
"🛤️ Connections\n",
|
||||
" - text_embedder.embedding -> retriever.query_embedding (List[float])\n",
|
||||
" - retriever.documents -> prompt_builder.documents (List[Document])\n",
|
||||
" - prompt_builder.prompt -> llm.prompt (str)"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Template for generating prompts for a movie recommendation engine.\n",
|
||||
"prompt_template = \"\"\"\n",
|
||||
" You are an assistant allowed to use the following context documents.\\nDocuments:\n",
|
||||
" {% for doc in documents %}\n",
|
||||
" {{ doc.content }}\n",
|
||||
" {% endfor %}\n",
|
||||
"\n",
|
||||
" \\Query: {{query}}\n",
|
||||
" \\nAnswer:\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"# Setting up a retrieval-augmented generation (RAG) pipeline for generating responses.\n",
|
||||
"rag_pipeline = Pipeline()\n",
|
||||
"rag_pipeline.add_component(\"text_embedder\", OpenAITextEmbedder())\n",
|
||||
"\n",
|
||||
"# Adding a component for retrieving related documents from MongoDB based on the query embedding.\n",
|
||||
"rag_pipeline.add_component(\n",
|
||||
" instance=MongoDBAtlasEmbeddingRetriever(document_store=document_store, top_k=15),\n",
|
||||
" name=\"retriever\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Building prompts based on retrieved documents to be used for generating responses.\n",
|
||||
"rag_pipeline.add_component(\n",
|
||||
" instance=PromptBuilder(template=prompt_template), name=\"prompt_builder\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Adding a language model generator to produce the final text output.\n",
|
||||
"rag_pipeline.add_component(instance=OpenAIGenerator(), name=\"llm\")\n",
|
||||
"\n",
|
||||
"# Connecting the components of the RAG pipeline to ensure proper data flow.\n",
|
||||
"rag_pipeline.connect(\"text_embedder.embedding\", \"retriever.query_embedding\")\n",
|
||||
"rag_pipeline.connect(\"retriever\", \"prompt_builder.documents\")\n",
|
||||
"rag_pipeline.connect(\"prompt_builder\", \"llm\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "n7mEU3ydRz8k"
|
||||
},
|
||||
"source": [
|
||||
"Lets test the pipeline"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "GHV5rRhaR3SX",
|
||||
"outputId": "dfb23085-ef80-4c99-a1c2-dd3f107ef313"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Mark lives in Berlin.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"query = \"Where does mark live?\"\n",
|
||||
"result = rag_pipeline.run(\n",
|
||||
" {\n",
|
||||
" \"text_embedder\": {\"text\": query},\n",
|
||||
" \"prompt_builder\": {\"query\": query},\n",
|
||||
" }\n",
|
||||
")\n",
|
||||
"print(result[\"llm\"][\"replies\"][0])"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
|
||||
+1706
-1659
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@@ -1,194 +1,190 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "Ph_DM1pCjktz"
|
||||
},
|
||||
"source": [
|
||||
"## Data Ingestion into MongoDB Database\n",
|
||||
"\n",
|
||||
"[](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/rag/chat_with_pdf_mongodb_openai_langchain_POLM_AI_Stack.ipynb)\n",
|
||||
"\n",
|
||||
"**Steps to creating a MongoDB Database**\n",
|
||||
"- [Register for a free MongoDB Atlas Account](https://www.mongodb.com/cloud/atlas/register?utm_campaign=devrel&utm_source=workshop&utm_medium=organic_social&utm_content=rag%20to%20agents%20notebook&utm_term=richmond.alake)\n",
|
||||
"- [Create a Cluster](https://www.mongodb.com/docs/guides/atlas/cluster/)\n",
|
||||
"- [Get your connection string](https://www.mongodb.com/docs/guides/atlas/connection-string/)\n",
|
||||
"\n",
|
||||
"## Vector Index Creation\n",
|
||||
"\n",
|
||||
"- [Create an Atlas Vector Search Index](https://www.mongodb.com/docs/compass/current/indexes/create-vector-search-index/)\n",
|
||||
"\n",
|
||||
"- If you are following this notebook ensure that you are creating a vector search index for the right database(anthropic_demo) and collection(research)\n",
|
||||
"\n",
|
||||
"Below is the vector search index definition for this notebook\n",
|
||||
"\n",
|
||||
"```json\n",
|
||||
"{\n",
|
||||
" \"fields\": [\n",
|
||||
" {\n",
|
||||
" \"numDimensions\": 1536,\n",
|
||||
" \"path\": \"embedding\",\n",
|
||||
" \"similarity\": \"cosine\",\n",
|
||||
" \"type\": \"vector\"\n",
|
||||
" }\n",
|
||||
" ]\n",
|
||||
"}\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"- Give your vector search index the name \"vector_index\" if you are following this notebook\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "4FNJEHGdj-cc"
|
||||
},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "JFD8rcTYE-EZ",
|
||||
"outputId": "85b7fc63-40ea-407e-d97b-d92251e37ea8"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m40.3/40.3 kB\u001b[0m \u001b[31m1.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m25.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25h"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"! pip install --quiet langchain pymongo langchain-openai langchain-community pypdf"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "EZwLZmCB_FrY",
|
||||
"outputId": "370ae9b6-4ef1-4ba3-f196-bfc2b5dc8cf3"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Answer: The document is about a significant advance in understanding the inner workings of AI models, specifically focusing on the interpretation of the features inside a large language model called Claude Sonnet. It discusses how millions of concepts are represented within the model, the ability to manipulate these features to see how the model's responses change, and the potential implications for making AI models safer and more trustworthy.\n",
|
||||
"Sources:\n",
|
||||
"- mapping_llms.pdf: As for the scientific risk, the proof is in the pudding.\n",
|
||||
"We successfully extracted millions of featu...\n",
|
||||
"- mapping_llms.pdf: A map of the features near an \"Inner Conflict\" feature, including clusters\n",
|
||||
"related to balancing trad...\n",
|
||||
"- mapping_llms.pdf: Interpret\u0000bility\n",
|
||||
"M apping the M ind of a Large\n",
|
||||
"Language M odel\n",
|
||||
"21 May 2024\n",
|
||||
"Today we report a signifi...\n",
|
||||
"- mapping_llms.pdf: English word in a dictionary is made by combining letters, and\n",
|
||||
"every sentence is made by combining w...\n",
|
||||
"- mapping_llms.pdf: answer I have no physical form, I am an AI model changed\n",
|
||||
"to something much odder: \"I am the Golden...\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"from langchain.embeddings import OpenAIEmbeddings\n",
|
||||
"from langchain.vectorstores import MongoDBAtlasVectorSearch\n",
|
||||
"from langchain_community.document_loaders import PyPDFLoader\n",
|
||||
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
|
||||
"from langchain.chat_models import ChatOpenAI\n",
|
||||
"from langchain.chains import RetrievalQA\n",
|
||||
"from pymongo import MongoClient\n",
|
||||
"from google.colab import userdata\n",
|
||||
"\n",
|
||||
"# Set up your OpenAI API key\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = userdata.get(\"OPENAI_API_KEY\")\n",
|
||||
"\n",
|
||||
"# Set up MongoDB connection\n",
|
||||
"mongo_uri = userdata.get(\"MONGO_URI\")\n",
|
||||
"db_name = \"anthropic_demo\"\n",
|
||||
"collection_name = \"research\"\n",
|
||||
"\n",
|
||||
"client = MongoClient(mongo_uri, appname=\"devrel.showcase.chat_with_pdf\")\n",
|
||||
"db = client[db_name]\n",
|
||||
"collection = db[collection_name]\n",
|
||||
"\n",
|
||||
"# Set up document loading and splitting\n",
|
||||
"loader = PyPDFLoader(\"mapping_llms.pdf\")\n",
|
||||
"documents = loader.load()\n",
|
||||
"\n",
|
||||
"text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)\n",
|
||||
"texts = text_splitter.split_documents(documents)\n",
|
||||
"\n",
|
||||
"# Set up embeddings and vector store\n",
|
||||
"embeddings = OpenAIEmbeddings()\n",
|
||||
"vector_store = MongoDBAtlasVectorSearch.from_documents(\n",
|
||||
" texts,\n",
|
||||
" embeddings,\n",
|
||||
" collection=collection,\n",
|
||||
" index_name=\"vector_index\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Set up retriever and language model\n",
|
||||
"retriever = vector_store.as_retriever(search_type=\"similarity\", search_kwargs={\"k\": 5})\n",
|
||||
"llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)\n",
|
||||
"\n",
|
||||
"# Set up RAG pipeline\n",
|
||||
"qa_chain = RetrievalQA.from_chain_type(\n",
|
||||
" llm=llm,\n",
|
||||
" chain_type=\"stuff\",\n",
|
||||
" retriever=retriever,\n",
|
||||
" return_source_documents=True\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Function to process user query\n",
|
||||
"def process_query(query):\n",
|
||||
" result = qa_chain({\"query\": query})\n",
|
||||
" return result[\"result\"], result[\"source_documents\"]\n",
|
||||
"\n",
|
||||
"# Example usage\n",
|
||||
"query = \"What is the document about?\"\n",
|
||||
"answer, sources = process_query(query)\n",
|
||||
"print(f\"Answer: {answer}\")\n",
|
||||
"print(\"Sources:\")\n",
|
||||
"for doc in sources:\n",
|
||||
" print(f\"- {doc.metadata['source']}: {doc.page_content[:100]}...\")\n",
|
||||
"\n",
|
||||
"# Don't forget to close the MongoDB connection when done\n",
|
||||
"client.close()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "Ph_DM1pCjktz"
|
||||
},
|
||||
"source": [
|
||||
"## Data Ingestion into MongoDB Database\n",
|
||||
"\n",
|
||||
"[](https://colab.research.google.com/github/mongodb-developer/GenAI-Showcase/blob/main/notebooks/rag/chat_with_pdf_mongodb_openai_langchain_POLM_AI_Stack.ipynb)\n",
|
||||
"\n",
|
||||
"**Steps to creating a MongoDB Database**\n",
|
||||
"- [Register for a free MongoDB Atlas Account](https://www.mongodb.com/cloud/atlas/register?utm_campaign=devrel&utm_source=workshop&utm_medium=organic_social&utm_content=rag%20to%20agents%20notebook&utm_term=richmond.alake)\n",
|
||||
"- [Create a Cluster](https://www.mongodb.com/docs/guides/atlas/cluster/)\n",
|
||||
"- [Get your connection string](https://www.mongodb.com/docs/guides/atlas/connection-string/)\n",
|
||||
"\n",
|
||||
"## Vector Index Creation\n",
|
||||
"\n",
|
||||
"- [Create an Atlas Vector Search Index](https://www.mongodb.com/docs/compass/current/indexes/create-vector-search-index/)\n",
|
||||
"\n",
|
||||
"- If you are following this notebook ensure that you are creating a vector search index for the right database(anthropic_demo) and collection(research)\n",
|
||||
"\n",
|
||||
"Below is the vector search index definition for this notebook\n",
|
||||
"\n",
|
||||
"```json\n",
|
||||
"{\n",
|
||||
" \"fields\": [\n",
|
||||
" {\n",
|
||||
" \"numDimensions\": 1536,\n",
|
||||
" \"path\": \"embedding\",\n",
|
||||
" \"similarity\": \"cosine\",\n",
|
||||
" \"type\": \"vector\"\n",
|
||||
" }\n",
|
||||
" ]\n",
|
||||
"}\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"- Give your vector search index the name \"vector_index\" if you are following this notebook\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "4FNJEHGdj-cc"
|
||||
},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "JFD8rcTYE-EZ",
|
||||
"outputId": "85b7fc63-40ea-407e-d97b-d92251e37ea8"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m40.3/40.3 kB\u001b[0m \u001b[31m1.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m25.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25h"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"! pip install --quiet langchain pymongo langchain-openai langchain-community pypdf"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "EZwLZmCB_FrY",
|
||||
"outputId": "370ae9b6-4ef1-4ba3-f196-bfc2b5dc8cf3"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Answer: The document is about a significant advance in understanding the inner workings of AI models, specifically focusing on the interpretation of the features inside a large language model called Claude Sonnet. It discusses how millions of concepts are represented within the model, the ability to manipulate these features to see how the model's responses change, and the potential implications for making AI models safer and more trustworthy.\n",
|
||||
"Sources:\n",
|
||||
"- mapping_llms.pdf: As for the scientific risk, the proof is in the pudding.\n",
|
||||
"We successfully extracted millions of featu...\n",
|
||||
"- mapping_llms.pdf: A map of the features near an \"Inner Conflict\" feature, including clusters\n",
|
||||
"related to balancing trad...\n",
|
||||
"- mapping_llms.pdf: Interpret\u0000bility\n",
|
||||
"M apping the M ind of a Large\n",
|
||||
"Language M odel\n",
|
||||
"21 May 2024\n",
|
||||
"Today we report a signifi...\n",
|
||||
"- mapping_llms.pdf: English word in a dictionary is made by combining letters, and\n",
|
||||
"every sentence is made by combining w...\n",
|
||||
"- mapping_llms.pdf: answer I have no physical form, I am an AI model changed\n",
|
||||
"to something much odder: \"I am the Golden...\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"from langchain.embeddings import OpenAIEmbeddings\n",
|
||||
"from langchain.vectorstores import MongoDBAtlasVectorSearch\n",
|
||||
"from langchain_community.document_loaders import PyPDFLoader\n",
|
||||
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
|
||||
"from langchain.chat_models import ChatOpenAI\n",
|
||||
"from langchain.chains import RetrievalQA\n",
|
||||
"from pymongo import MongoClient\n",
|
||||
"from google.colab import userdata\n",
|
||||
"\n",
|
||||
"# Set up your OpenAI API key\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = userdata.get(\"OPENAI_API_KEY\")\n",
|
||||
"\n",
|
||||
"# Set up MongoDB connection\n",
|
||||
"mongo_uri = userdata.get(\"MONGO_URI\")\n",
|
||||
"db_name = \"anthropic_demo\"\n",
|
||||
"collection_name = \"research\"\n",
|
||||
"\n",
|
||||
"client = MongoClient(mongo_uri, appname=\"devrel.showcase.chat_with_pdf\")\n",
|
||||
"db = client[db_name]\n",
|
||||
"collection = db[collection_name]\n",
|
||||
"\n",
|
||||
"# Set up document loading and splitting\n",
|
||||
"loader = PyPDFLoader(\"mapping_llms.pdf\")\n",
|
||||
"documents = loader.load()\n",
|
||||
"\n",
|
||||
"text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)\n",
|
||||
"texts = text_splitter.split_documents(documents)\n",
|
||||
"\n",
|
||||
"# Set up embeddings and vector store\n",
|
||||
"embeddings = OpenAIEmbeddings()\n",
|
||||
"vector_store = MongoDBAtlasVectorSearch.from_documents(\n",
|
||||
" texts, embeddings, collection=collection, index_name=\"vector_index\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Set up retriever and language model\n",
|
||||
"retriever = vector_store.as_retriever(search_type=\"similarity\", search_kwargs={\"k\": 5})\n",
|
||||
"llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)\n",
|
||||
"\n",
|
||||
"# Set up RAG pipeline\n",
|
||||
"qa_chain = RetrievalQA.from_chain_type(\n",
|
||||
" llm=llm, chain_type=\"stuff\", retriever=retriever, return_source_documents=True\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Function to process user query\n",
|
||||
"def process_query(query):\n",
|
||||
" result = qa_chain({\"query\": query})\n",
|
||||
" return result[\"result\"], result[\"source_documents\"]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Example usage\n",
|
||||
"query = \"What is the document about?\"\n",
|
||||
"answer, sources = process_query(query)\n",
|
||||
"print(f\"Answer: {answer}\")\n",
|
||||
"print(\"Sources:\")\n",
|
||||
"for doc in sources:\n",
|
||||
" print(f\"- {doc.metadata['source']}: {doc.page_content[:100]}...\")\n",
|
||||
"\n",
|
||||
"# Don't forget to close the MongoDB connection when done\n",
|
||||
"client.close()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@@ -125,6 +125,7 @@
|
||||
"source": [
|
||||
"# Optional-- If you want to enable Langsmith -- good for debugging\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
||||
"os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()"
|
||||
]
|
||||
@@ -423,7 +424,7 @@
|
||||
],
|
||||
"source": [
|
||||
"# Data Ingestion\n",
|
||||
"records = df.to_dict('records')\n",
|
||||
"records = df.to_dict(\"records\")\n",
|
||||
"collection.insert_many(records)\n",
|
||||
"\n",
|
||||
"print(\"Data ingestion into MongoDB completed\")"
|
||||
@@ -437,8 +438,11 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_openai import OpenAIEmbeddings\n",
|
||||
"\n",
|
||||
"# Using the text-embedding-ada-002 since that's what was used to create embeddings in the movies dataset\n",
|
||||
"embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY, model=\"text-embedding-ada-002\")"
|
||||
"embeddings = OpenAIEmbeddings(\n",
|
||||
" openai_api_key=OPENAI_API_KEY, model=\"text-embedding-ada-002\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -452,9 +456,9 @@
|
||||
"vector_store = MongoDBAtlasVectorSearch.from_connection_string(\n",
|
||||
" connection_string=MONGODB_URI,\n",
|
||||
" namespace=DB_NAME + \".\" + COLLECTION_NAME,\n",
|
||||
" embedding= embeddings,\n",
|
||||
" embedding=embeddings,\n",
|
||||
" index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,\n",
|
||||
" text_key=\"fullplot\"\n",
|
||||
" text_key=\"fullplot\",\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -482,7 +486,10 @@
|
||||
"from langchain_core.output_parsers import StrOutputParser\n",
|
||||
"\n",
|
||||
"# Generate context using the retriever, and pass the user question through\n",
|
||||
"retrieve = {\"context\": retriever | (lambda docs: \"\\n\\n\".join([d.page_content for d in docs])), \"question\": RunnablePassthrough()}\n",
|
||||
"retrieve = {\n",
|
||||
" \"context\": retriever | (lambda docs: \"\\n\\n\".join([d.page_content for d in docs])),\n",
|
||||
" \"question\": RunnablePassthrough(),\n",
|
||||
"}\n",
|
||||
"template = \"\"\"Answer the question based only on the following context: \\\n",
|
||||
"{context}\n",
|
||||
"\n",
|
||||
@@ -495,13 +502,8 @@
|
||||
"# Parse output as a string\n",
|
||||
"parse_output = StrOutputParser()\n",
|
||||
"\n",
|
||||
"# Naive RAG chain \n",
|
||||
"naive_rag_chain = (\n",
|
||||
" retrieve\n",
|
||||
" | prompt\n",
|
||||
" | model\n",
|
||||
" | parse_output\n",
|
||||
")"
|
||||
"# Naive RAG chain\n",
|
||||
"naive_rag_chain = retrieve | prompt | model | parse_output"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -553,7 +555,9 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_session_history(session_id: str) -> MongoDBChatMessageHistory:\n",
|
||||
" return MongoDBChatMessageHistory(MONGODB_URI, session_id, database_name=DB_NAME, collection_name=\"history\")"
|
||||
" return MongoDBChatMessageHistory(\n",
|
||||
" MONGODB_URI, session_id, database_name=DB_NAME, collection_name=\"history\"\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -588,7 +592,11 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Generate context by passing output of the question_chain i.e. the standalone question to the retriever\n",
|
||||
"retriever_chain = RunnablePassthrough.assign(context=question_chain | retriever | (lambda docs: \"\\n\\n\".join([d.page_content for d in docs])))"
|
||||
"retriever_chain = RunnablePassthrough.assign(\n",
|
||||
" context=question_chain\n",
|
||||
" | retriever\n",
|
||||
" | (lambda docs: \"\\n\\n\".join([d.page_content for d in docs]))\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -619,12 +627,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# RAG chain\n",
|
||||
"rag_chain = (\n",
|
||||
" retriever_chain\n",
|
||||
" | rag_prompt\n",
|
||||
" | model\n",
|
||||
" | parse_output\n",
|
||||
")"
|
||||
"rag_chain = retriever_chain | rag_prompt | model | parse_output"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -652,7 +655,10 @@
|
||||
" input_messages_key=\"question\",\n",
|
||||
" history_messages_key=\"history\",\n",
|
||||
")\n",
|
||||
"with_message_history.invoke({\"question\": \"What is the best movie to watch when sad?\"}, {\"configurable\": {\"session_id\": \"1\"}})"
|
||||
"with_message_history.invoke(\n",
|
||||
" {\"question\": \"What is the best movie to watch when sad?\"},\n",
|
||||
" {\"configurable\": {\"session_id\": \"1\"}},\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -673,7 +679,12 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"with_message_history.invoke({\"question\": \"Hmmm..I don't want to watch that one. Can you suggest something else?\"}, {\"configurable\": {\"session_id\": \"1\"}})"
|
||||
"with_message_history.invoke(\n",
|
||||
" {\n",
|
||||
" \"question\": \"Hmmm..I don't want to watch that one. Can you suggest something else?\"\n",
|
||||
" },\n",
|
||||
" {\"configurable\": {\"session_id\": \"1\"}},\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -694,7 +705,10 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"with_message_history.invoke({\"question\": \"How about something more light?\"}, {\"configurable\": {\"session_id\": \"1\"}})"
|
||||
"with_message_history.invoke(\n",
|
||||
" {\"question\": \"How about something more light?\"},\n",
|
||||
" {\"configurable\": {\"session_id\": \"1\"}},\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -717,14 +731,16 @@
|
||||
"from langchain_mongodb.cache import MongoDBAtlasSemanticCache\n",
|
||||
"from langchain_core.globals import set_llm_cache\n",
|
||||
"\n",
|
||||
"set_llm_cache(MongoDBAtlasSemanticCache(\n",
|
||||
" connection_string=MONGODB_URI,\n",
|
||||
" embedding=embeddings,\n",
|
||||
" collection_name=\"semantic_cache\",\n",
|
||||
" database_name=DB_NAME,\n",
|
||||
" index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,\n",
|
||||
" wait_until_ready=True # Optional, waits until the cache is ready to be used\n",
|
||||
"))"
|
||||
"set_llm_cache(\n",
|
||||
" MongoDBAtlasSemanticCache(\n",
|
||||
" connection_string=MONGODB_URI,\n",
|
||||
" embedding=embeddings,\n",
|
||||
" collection_name=\"semantic_cache\",\n",
|
||||
" database_name=DB_NAME,\n",
|
||||
" index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,\n",
|
||||
" wait_until_ready=True, # Optional, waits until the cache is ready to be used\n",
|
||||
" )\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
File diff suppressed because one or more lines are too long
+3125
-3111
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -1478,25 +1478,27 @@
|
||||
"import pymongo\n",
|
||||
"from google.colab import userdata\n",
|
||||
"\n",
|
||||
"def get_mongo_client(mongo_uri):\n",
|
||||
" \"\"\"Establish connection to the MongoDB.\"\"\"\n",
|
||||
" try:\n",
|
||||
" client = pymongo.MongoClient(mongo_uri, appname=\"devrel.content.python\")\n",
|
||||
" print(\"Connection to MongoDB successful\")\n",
|
||||
" return client\n",
|
||||
" except pymongo.errors.ConnectionFailure as e:\n",
|
||||
" print(f\"Connection failed: {e}\")\n",
|
||||
" return None\n",
|
||||
"\n",
|
||||
"mongo_uri = userdata.get('MONGO_URI')\n",
|
||||
"def get_mongo_client(mongo_uri):\n",
|
||||
" \"\"\"Establish connection to the MongoDB.\"\"\"\n",
|
||||
" try:\n",
|
||||
" client = pymongo.MongoClient(mongo_uri, appname=\"devrel.content.python\")\n",
|
||||
" print(\"Connection to MongoDB successful\")\n",
|
||||
" return client\n",
|
||||
" except pymongo.errors.ConnectionFailure as e:\n",
|
||||
" print(f\"Connection failed: {e}\")\n",
|
||||
" return None\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"mongo_uri = userdata.get(\"MONGO_URI\")\n",
|
||||
"if not mongo_uri:\n",
|
||||
" print(\"MONGO_URI not set in environment variables\")\n",
|
||||
" print(\"MONGO_URI not set in environment variables\")\n",
|
||||
"\n",
|
||||
"mongo_client = get_mongo_client(mongo_uri)\n",
|
||||
"\n",
|
||||
"# Ingest data into MongoDB\n",
|
||||
"db = mongo_client['movies']\n",
|
||||
"collection = db['movie_collection_2']"
|
||||
"db = mongo_client[\"movies\"]\n",
|
||||
"collection = db[\"movie_collection_2\"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1560,7 +1562,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"def vector_search(user_query, collection):\n",
|
||||
" \"\"\"\n",
|
||||
" Perform a vector search in the MongoDB collection based on the user query.\n",
|
||||
@@ -1586,7 +1587,7 @@
|
||||
" \"queryVector\": query_embedding,\n",
|
||||
" \"path\": \"embedding\",\n",
|
||||
" \"numCandidates\": 150, # Number of candidate matches to consider\n",
|
||||
" \"limit\": 4 # Return top 4 matches\n",
|
||||
" \"limit\": 4, # Return top 4 matches\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
@@ -1599,10 +1600,10 @@
|
||||
" \"_id\": 0, # Exclude the _id field\n",
|
||||
" \"fullplot\": 1, # Include the plot field\n",
|
||||
" \"title\": 1, # Include the title field\n",
|
||||
" \"genres\": 1, # Include the genres field\n",
|
||||
" \"genres\": 1, # Include the genres field\n",
|
||||
" \"score\": {\n",
|
||||
" \"$meta\": \"vectorSearchScore\" # Include the search score\n",
|
||||
" }\n",
|
||||
" },\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
@@ -1622,7 +1623,6 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_search_result(query, collection):\n",
|
||||
"\n",
|
||||
" get_knowledge = vector_search(query, collection)\n",
|
||||
"\n",
|
||||
" search_result = \"\"\n",
|
||||
|
||||
+3469
-3388
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
+7130
-7048
File diff suppressed because it is too large
Load Diff
@@ -62,7 +62,7 @@ def get_permissions(request: Request) -> Dict:
|
||||
|
||||
if not api_key:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
status_code=403,
|
||||
detail="API key missing"
|
||||
)
|
||||
|
||||
@@ -78,7 +78,7 @@ def get_permissions(request: Request) -> Dict:
|
||||
return app_permissions
|
||||
|
||||
# Retrieve documents that the app has permissions for
|
||||
def get_documents_with_permissions(
|
||||
def get_documents_with_permissions(
|
||||
app_permissions: Dict
|
||||
) -> Dict:
|
||||
# Build query filter based on allowed tags
|
||||
|
||||
@@ -74,7 +74,7 @@ Edit the Highlighted JSON document to modify the data you generate.
|
||||
|
||||
To use cohere or any other embedding provider supported by the LangChain JS framework just swap the embeddings code:
|
||||
|
||||
### Install
|
||||
### Install
|
||||
```shell
|
||||
npm install cohere-ai @langchain/cohere
|
||||
```js
|
||||
@@ -93,4 +93,3 @@ const plot_embedding = await embeddings.embedDocuments([doc.plot]);
|
||||
```
|
||||
|
||||
To see the available providers list on Langchain go [here](https://js.langchain.com/docs/integrations/text_embedding).
|
||||
|
||||
|
||||
+3767
-3739
File diff suppressed because one or more lines are too long
Vendored
+1
-1
@@ -4,7 +4,7 @@
|
||||
Welcome to the GenAI Showcase repository! This repository contains code and resources for sample AI projects that have been developed for various verticals through the MongoDB MAAP program. In this section, we explore different AI technologies and their applications across diverse industries.
|
||||
|
||||
## About gravity9
|
||||
[gravity9](https://www.gravity9.com/) is a leading software consultancy specializing in turning legacy complexity into digital agility. We are at the intersection of **Product + Engineering + AI**. Our team of experts helps organizations leverage cutting-edge technologies to drive business growth and efficiency. We are committed to delivering high-quality solutions that meet the unique needs of our clients.
|
||||
[gravity9](https://www.gravity9.com/) is a leading software consultancy specializing in turning legacy complexity into digital agility. We are at the intersection of **Product + Engineering + AI**. Our team of experts helps organizations leverage cutting-edge technologies to drive business growth and efficiency. We are committed to delivering high-quality solutions that meet the unique needs of our clients.
|
||||
|
||||
## Industry Showcases
|
||||
In this section, we highlight some of the industry-specific use cases and success stories where our AI solutions have made a significant impact:
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user