MCPcopy Create free account
hub / github.com/astercloud/aster / main

Function main

examples/memory-semantic/main.go:15–73  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13)
14
15func main() {
16 ctx := context.Background()
17
18 // 1. 创建向量存储和 embedder
19 store := vector.NewMemoryStore()
20 embedder := vector.NewMockEmbedder(16)
21
22 // 2. 创建语义记忆组件
23 semMem := memory.NewSemanticMemory(memory.SemanticMemoryConfig{
24 Store: store,
25 Embedder: embedder,
26 NamespaceScope: "resource",
27 TopK: 3,
28 })
29
30 // 3. 索引几段示例文本
31 docs := []struct {
32 id string
33 text string
34 meta map[string]any
35 }{
36 {
37 id: "doc-1",
38 text: "Paris is the capital of France.",
39 meta: map[string]any{"user_id": "alice", "resource_id": "europe-notes"},
40 },
41 {
42 id: "doc-2",
43 text: "Berlin is the capital of Germany.",
44 meta: map[string]any{"user_id": "alice", "resource_id": "europe-notes"},
45 },
46 {
47 id: "doc-3",
48 text: "Tokyo is the capital of Japan.",
49 meta: map[string]any{"user_id": "bob", "resource_id": "asia-notes"},
50 },
51 }
52
53 for _, d := range docs {
54 if err := semMem.Index(ctx, d.id, d.text, d.meta); err != nil {
55 panic(fmt.Sprintf("index %s: %v", d.id, err))
56 }
57 }
58
59 // 4. 在 Alice 的 europe-notes 命名空间内进行语义检索
60 query := "What is the capital of France?"
61 meta := map[string]any{"user_id": "alice", "resource_id": "europe-notes"}
62
63 hits, err := semMem.Search(ctx, query, meta, 3)
64 if err != nil {
65 panic(fmt.Sprintf("semantic search failed: %v", err))
66 }
67
68 fmt.Printf("Query: %q\n", query)
69 fmt.Println("Semantic search hits:")
70 for _, h := range hits {
71 fmt.Printf(" ID=%s, score=%.4f, metadata=%v\n", h.ID, h.Score, h.Metadata)
72 }

Callers

nothing calls this directly

Calls 6

IndexMethod · 0.95
SearchMethod · 0.95
NewMemoryStoreFunction · 0.92
NewMockEmbedderFunction · 0.92
NewSemanticMemoryFunction · 0.92
PrintfMethod · 0.80

Tested by

no test coverage detected