{ "cells": [ { "cell_type": "markdown", "id": "ded02088-c568-4c38-b1a8-023eda8bb484", "metadata": {}, "source": [] }, { "cell_type": "markdown", "id": "096e18da", "metadata": {}, "source": [ "# Agentic RAG with Letta\n", "\n", "> Make sure you run the Letta server before running this example using `letta server`\n", "\n", "In this lab, we'll go over how to implement agentic RAG in Letta, that is, agents which can connect to external data sources. \n", "\n", "In Letta, there are two ways to do this: \n", "1. Copy external data into the agent's archival memory\n", "2. Connect the agent to external data via a tool (e.g. with Langchain, CrewAI, or custom tools) \n", "\n", "Each of these approaches has their pros and cons for agentic RAG, which we'll cover in this lab. " ] }, { "cell_type": "code", "execution_count": 1, "id": "d996e615-8ba1-41f7-a4cf-a1a831a0e77a", "metadata": {}, "outputs": [], "source": [ "from letta_client import CreateBlock, Letta, MessageCreate\n", "\n", "client = Letta(base_url=\"http://localhost:8283\")" ] }, { "cell_type": "markdown", "id": "fe86076e-88eb-4d43-aa6b-42a13b5d63cb", "metadata": {}, "source": [ "## Loading data into archival memory " ] }, { "cell_type": "code", "execution_count": 63, "id": "f44fe3fd-bbdb-47a1-86a0-16248f849bd7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Source(id='source-28fa7bb4-6c3d-463f-ac0c-3000189f920e', name='employee_handbook', description=None, embedding_config=EmbeddingConfig(embedding_endpoint_type='openai', embedding_endpoint='https://api.openai.com/v1', embedding_model='text-embedding-ada-002', embedding_dim=1536, embedding_chunk_size=300, azure_endpoint=None, azure_version=None, azure_deployment=None), organization_id='org-00000000-0000-4000-8000-000000000000', metadata_=None, created_by_id='user-00000000-0000-4000-8000-000000000000', last_updated_by_id='user-00000000-0000-4000-8000-000000000000', created_at=datetime.datetime(2024, 11, 14, 1, 46, 20), updated_at=datetime.datetime(2024, 11, 14, 1, 46, 20))" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "source = client.sources.create(name=\"employee_handbook\")\n", "source" ] }, { "cell_type": "code", "execution_count": 64, "id": "925b109e-7b42-4cf5-88bc-63df092b3288", "metadata": {}, "outputs": [], "source": [ "job = client.sources.files.upload(\n", " source_id=source.id,\n", " file=\"data/handbook.pdf\"\n", ")" ] }, { "cell_type": "code", "execution_count": 71, "id": "b7243422-7ed2-4c4c-afd0-f7311292b177", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'type': 'embedding',\n", " 'filename': 'data/handbook.pdf',\n", " 'source_id': 'source-28fa7bb4-6c3d-463f-ac0c-3000189f920e',\n", " 'num_passages': 15,\n", " 'num_documents': 1}" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "client.jobs.get(job_id=job.id).metadata" ] }, { "cell_type": "code", "execution_count": null, "id": "c6d823fc-3e6e-4d32-a5a6-4c42dca60d94", "metadata": {}, "outputs": [], "source": [ "agent_state = client.agents.create(\n", " memory_blocks=[\n", " CreateBlock(\n", " label=\"human\",\n", " value=\"Name: Sarah\",\n", " ),\n", " ],\n", " model=\"openai/gpt-4\",\n", " embedding=\"openai/text-embedding-3-small\",\n", ")" ] }, { "cell_type": "code", "execution_count": 73, "id": "3e554713-77ce-4b88-ba3e-c743692cb9e1", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 20.21it/s]\n" ] } ], "source": [ "client.sources.attach(\n", " source_id=source.id,\n", " agent_id=agent_state.id\n", ")" ] }, { "cell_type": "code", "execution_count": 74, "id": "0f9c58be-116f-47dd-8f91-9c7c2fe5d8f8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", "
\n", "