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