diff --git a/main.py b/main.py index f89cfa3..0cf6544 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,10 @@ """ -Letta Files and Streaming Demo +Letta Filesystem This demo shows how to: -1. Create a data source and upload files (both from disk and from strings) -2. Create an agent and attach the data source -3. Stream the agent's responses in real-time with token-by-token output +1. Create a folder and upload files (both from disk and from strings) +2. Create an agent and attach the data folder +3. Stream the agent's responses 4. Query the agent about the uploaded files The demo uploads: @@ -25,7 +25,7 @@ LETTA_API_KEY = os.getenv("LETTA_API_KEY") if LETTA_API_KEY is None: raise ValueError("LETTA_API_KEY is not set") -SOURCE_NAME = "Example Source" +FOLDER_NAME = "Example Folder" # Connect to our Letta server client = Letta(token=LETTA_API_KEY) @@ -34,20 +34,20 @@ client = Letta(token=LETTA_API_KEY) embedding_configs = client.embedding_models.list() embedding_config = embedding_configs[0] -# Check if the source already exists +# Check if the folder already exists try: - source_id = client.sources.retrieve_by_name(SOURCE_NAME) -# We got an API error. Check if it's a 404, meaning the source doesn't exist. + folder_id = client.folders.retrieve_by_name(FOLDER_NAME) + +# We got an API error. Check if it's a 404, meaning the folder doesn't exist. except ApiError as e: if e.status_code == 404: - # Create a new source - source = client.sources.create( - name=SOURCE_NAME, - description="This is an example source", - instructions="Use this data source to see how Letta works.", - embedding_config=embedding_config, + # Create a new folder + folder = client.folders.create( + name=FOLDER_NAME, + description="This is an example folder", + instructions="Use this data folder to see how Letta works.", ) - source_id = source.id + folder_id = folder.id else: raise e @@ -56,21 +56,21 @@ except Exception as e: raise e -######################################################## -# There's two ways to upload a file to a source. +# +# There's two ways to upload a file to a folder. # # 1. From an existing file # 2. From a string by encoding it into a base64 string # -######################################################## +# # 1. From an existing file # "rb" means "read binary" file = open("example-on-disk.txt", "rb") -# Upload the file to the source -file = client.sources.files.upload( - source_id=source_id, +# Upload the file to the folder +file = client.folders.files.upload( + folder_id=folder_id, file=file, duplicate_handling="skip" ) @@ -89,16 +89,17 @@ file_object = io.BytesIO(content.encode("utf-8")) # Set the name of the file file_object.name = "example.txt" -# Upload the file to the source -file = client.sources.files.upload( - source_id=source_id, +# Upload the file to the folder +file = client.folders.files.upload( + folder_id=folder_id, file=file_object, duplicate_handling="skip" ) -############################# -# You can also upload PDFs -############################# +# +# You can also upload PDFs! +# Letta extracts text from PDFs using OCR. +# # Download the PDF to the local directory if it doesn't exist if not os.path.exists("memgpt.pdf"): @@ -108,16 +109,16 @@ if not os.path.exists("memgpt.pdf"): with open("memgpt.pdf", "wb") as f: f.write(response.content) -# Upload the PDF to the source -file = client.sources.files.upload( - source_id=source_id, +# Upload the PDF to the folder +file = client.folders.files.upload( + folder_id=folder_id, file=open("memgpt.pdf", "rb"), duplicate_handling="skip" ) -########## -# Now we need to create an agent that can use this source -########## +# +# Now we need to create an agent that can use this folder +# # Create an agent agent = client.agents.create( @@ -136,12 +137,18 @@ agent = client.agents.create( ] ) -# Attach the data source to the agent -client.agents.sources.attach( +# Attach the data folder to the agent. +# Once the folder is attached, the agent will be able to see all +# files in the folder. +client.agents.folders.attach( agent_id=agent.id, - source_id=source_id + folder_id=folder_id ) +######################################################## +# This code makes a simple chatbot interface to the agent +######################################################## + # Wrap this in a try/catch block to remove the agent in the event of an error try: print(f"🤖 Connected to agent: {agent.name}")