-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemory.docs.upload-pdf.py
More file actions
48 lines (34 loc) · 1.26 KB
/
memory.docs.upload-pdf.py
File metadata and controls
48 lines (34 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Example demonstrating how to upload documents to a memory in Langbase.
"""
import os
import pathlib
from dotenv import load_dotenv
from langbase import Langbase
def main():
load_dotenv()
# Get API key from environment variable
langbase_api_key = os.getenv("LANGBASE_API_KEY")
# Initialize the client
langbase = Langbase(api_key=langbase_api_key)
# Memory name to upload documents to
memory_name = "product-knowledge" # Replace with your memory name
# Upload documents to the memory
try:
document_path = pathlib.Path(__file__).parent / "document.pdf"
# Read the file
with open(document_path, "rb") as file:
document_content = file.read()
content = "Langbase is a powerful platform for building AI applications with composable AI."
response = langbase.memories.documents.upload(
memory_name=memory_name,
document_name="document.pdf",
document=document_content, # Convert string to bytes
content_type="application/pdf",
)
print("Document uploaded successfully!")
print(f"Status: {response.status_code}")
except Exception as e:
print(f"Error uploading documents: {e}")
if __name__ == "__main__":
main()