-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemory.add.py
More file actions
76 lines (63 loc) · 2.97 KB
/
memory.add.py
File metadata and controls
76 lines (63 loc) · 2.97 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Example: Add text directly to memory
1. Run memory.create.py first to create a memory.
2. Add your API key to the environment variables or replace it in the code.
"""
import os
from langbase import Langbase
# Initialize the Langbase client
langbase = Langbase(api_key=os.getenv("LANGBASE_API_KEY"))
def main():
try:
# Basic text addition
print("📝 Adding basic text to memory...")
basic_result = langbase.memories.add(
memory_name="my-knowledge-base",
text="This is important information about machine learning fundamentals. "
"It covers supervised learning, unsupervised learning, and reinforcement learning concepts.",
)
print(f"✅ Basic text added: {basic_result['document_name']}")
# Text addition with custom name and metadata
print("\n📝 Adding detailed text with metadata...")
detailed_result = langbase.memories.add(
memory_name="my-knowledge-base",
text="Deep learning is a subset of machine learning that uses artificial neural networks "
"with multiple layers to model and understand complex patterns in data. It has "
"revolutionized fields like computer vision, natural language processing, and speech recognition.",
document_name="deep-learning-intro",
metadata={
"category": "machine-learning",
"topic": "deep-learning",
"difficulty": "intermediate",
"source": "manual-entry",
},
)
print(f"✅ Detailed text added: {detailed_result['document_name']}")
# Multiple text entries
texts = [
{
"text": "Supervised learning uses labeled training data to learn a mapping from inputs to outputs.",
"document_name": "supervised-learning",
"metadata": {"type": "definition", "category": "ml-concepts"},
},
{
"text": "Unsupervised learning finds hidden patterns in data without using labeled examples.",
"document_name": "unsupervised-learning",
"metadata": {"type": "definition", "category": "ml-concepts"},
},
{
"text": "Reinforcement learning learns optimal actions through trial and error interactions with an environment.",
"document_name": "reinforcement-learning",
"metadata": {"type": "definition", "category": "ml-concepts"},
},
]
print("\n📝 Adding multiple texts...")
for item in texts:
result = langbase.memories.add(memory_name="my-knowledge-base", **item)
print(f"✅ Added: {result['document_name']}")
print("\n🎉 All texts have been added to the memory!")
print("You can now query this memory to retrieve relevant information.")
except Exception as error:
print(f"❌ Error: {error}")
if __name__ == "__main__":
main()