Skip to content

Commit a69077d

Browse files
martinschaerEkwuno
andauthored
Add agent rules page and files (#1575)
Co-authored-by: ekwuno <Obinnacodes@gmail.com>
1 parent f265026 commit a69077d

7 files changed

Lines changed: 575 additions & 2 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
description: This rule provides information using SurrealDB embedded in python
3+
alwaysApply: false
4+
---
5+
6+
The data can be stored:
7+
8+
- in-memory with `Surreal("mem://")` and `AsyncSurreal("mem://")`
9+
- persistence with `Surreal(f"file://{db_path}")` and `AsyncSurreal(f"file://{db_path}")`
10+
11+
## Example:
12+
13+
```python
14+
"""File-based persistent embedded SurrealDB example."""
15+
16+
import asyncio
17+
import tempfile
18+
from pathlib import Path
19+
20+
from surrealdb import AsyncSurreal
21+
22+
23+
async def main() -> None:
24+
"""Run persistent file-based database operations."""
25+
# Create a temporary directory for the database
26+
with tempfile.TemporaryDirectory() as tmpdir:
27+
db_path = Path(tmpdir) / "mydb"
28+
db_url = f"file://{db_path}"
29+
30+
print(f"Using database at: {db_path}")
31+
32+
# First connection: create data
33+
print("\n=== First connection: Creating data ===")
34+
async with AsyncSurreal(db_url) as db:
35+
await db.use("test", "test")
36+
37+
# Create some records
38+
await db.create(
39+
"company", {"name": "Acme Corp", "founded": 2020, "employees": 100}
40+
)
41+
42+
await db.create(
43+
"company", {"name": "TechStart Inc", "founded": 2021, "employees": 50}
44+
)
45+
46+
companies = await db.select("company")
47+
print(f"Created companies: {companies}")
48+
49+
# Second connection: verify persistence
50+
print("\n=== Second connection: Verifying persistence ===")
51+
async with AsyncSurreal(db_url) as db:
52+
await db.use("test", "test")
53+
54+
# Query the persisted data
55+
companies = await db.select("company")
56+
print(f"Loaded companies from disk: {companies}")
57+
58+
# Update a company
59+
updated = await db.query("""
60+
UPDATE company SET employees = employees + 10 WHERE name = "Acme Corp"
61+
""")
62+
print(f"Updated company: {updated}")
63+
64+
# Verify the update
65+
all_companies = await db.select("company")
66+
print(f"All companies after update: {all_companies}")
67+
68+
print("\n=== Database file will be cleaned up automatically ===")
69+
70+
71+
if __name__ == "__main__":
72+
asyncio.run(main())
73+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
description: This rule provides information using SurrealDB in python
3+
alwaysApply: false
4+
---
5+
6+
## Running SurrealDB
7+
8+
This will persist the data in a `database` folder:
9+
10+
```bash
11+
surreal start -u root -p root rocksdb:database
12+
```
13+
14+
This will store the data in-memory:
15+
16+
```bash
17+
surreal start -u root -p root
18+
```
19+
20+
## Example
21+
22+
```python
23+
# Import the Surreal class
24+
from surrealdb import Surreal
25+
26+
# Using a context manger to automatically connect and disconnect
27+
with Surreal("ws://localhost:8000/rpc") as db:
28+
db.signin({"username": 'root', "password": 'root'})
29+
db.use("namepace_test", "database_test")
30+
31+
# Create a record in the person table
32+
db.create(
33+
"person",
34+
{
35+
"user": "me",
36+
"password": "safe",
37+
"marketing": True,
38+
"tags": ["python", "documentation"],
39+
},
40+
)
41+
42+
# Read all the records in the table
43+
print(db.select("person"))
44+
45+
# Update all records in the table
46+
print(db.update("person", {
47+
"user":"you",
48+
"password":"very_safe",
49+
"marketing": False,
50+
"tags": ["Awesome"]
51+
}))
52+
53+
# Delete all records in the table
54+
print(db.delete("person"))
55+
56+
# You can also use the query method
57+
# doing all of the above and more in SurrealQl
58+
59+
# In SurrealQL you can do a direct insert
60+
# and the table will be created if it doesn't exist
61+
62+
# Create
63+
db.query("""
64+
insert into person {
65+
user: 'me',
66+
password: 'very_safe',
67+
tags: ['python', 'documentation']
68+
};
69+
""")
70+
71+
# Read
72+
print(db.query("select * from person"))
73+
74+
# Update
75+
print(db.query("""
76+
update person content {
77+
user: 'you',
78+
password: 'more_safe',
79+
tags: ['awesome']
80+
};
81+
"""))
82+
83+
# Delete
84+
print(db.query("delete person"))
85+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
description: This rule provides information for creating vector indexes and vector queries in SurrealDB
3+
alwaysApply: false
4+
---
5+
6+
## HNSW Index
7+
8+
### Examples
9+
10+
```surql
11+
DEFINE INDEX hnsw_idx ON pts FIELDS point HNSW DIMENSION 4;
12+
```
13+
14+
With specific distance function `EUCLIDEAN` and type `F64`. Other available types: F64, F32, I64, I32, I16.
15+
16+
```surql
17+
DEFINE INDEX hnsw_idx ON pts FIELDS point HNSW DIMENSION 4 DIST EUCLIDEAN TYPE F64;
18+
```
19+
20+
Full-table example:
21+
22+
```surql
23+
DEFINE TABLE OVERWRITE document SCHEMALESS;
24+
DEFINE FIELD OVERWRITE embedding ON document TYPE array<float>;
25+
DEFINE INDEX OVERWRITE hnsw_idx_document ON document
26+
FIELDS embedding
27+
HNSW DIMENSION 384
28+
DIST COSINE
29+
TYPE F32
30+
EFC 150 M 12 M0 24;
31+
```
32+
33+
### Querying
34+
35+
When using HNSW, the `<|10, 40|>` notation in the WHERE clause means you want the top 10, using an effort value of 40.
36+
37+
For effort values, 40 is a default recommended value. 17 is very fast, but misses some results.
38+
39+
```surql
40+
SELECT
41+
*,
42+
vector::distance::knn() AS dist
43+
FROM document
44+
WHERE embedding <|10, 40|> $vector;
45+
```
46+
47+
The example above will use the distance function defined by the index. That's what `vector::distance::knn()` means.
48+
49+
A more complex example, returning score and filtering by a threshold:
50+
51+
```surql
52+
SELECT *, score
53+
FROM (
54+
SELECT *, (1 - vector::distance::knn()) AS score
55+
FROM document
56+
WHERE embedding <|20,40|> $vector
57+
)
58+
WHERE score >= $threshold
59+
ORDER BY score DESC;
60+
```

0 commit comments

Comments
 (0)