-
Notifications
You must be signed in to change notification settings - Fork 0
Make document ID optional with auto-generation of human-readable IDs #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,10 @@ | |
|
|
||
| import ( | ||
| "fmt" | ||
| "math/rand" | ||
| "regexp" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| type TextChunker struct { | ||
|
|
@@ -258,3 +260,29 @@ | |
| } | ||
| return fmt.Sprintf("%s_chunk_%d", documentID, chunkIndex) | ||
| } | ||
|
|
||
| // GenerateDocumentID generates a human-readable document ID when none is provided | ||
| func GenerateDocumentID() string { | ||
| // List of friendly adjectives and nouns for human-readable IDs | ||
| adjectives := []string{ | ||
| "happy", "bright", "swift", "clever", "gentle", "bold", "calm", "wise", | ||
| "brave", "quick", "sharp", "smart", "clean", "fresh", "light", "clear", | ||
| } | ||
|
|
||
|
Check failure on line 271 in pkg/minirag/chunker.go
|
||
| nouns := []string{ | ||
| "doc", "file", "text", "note", "page", "item", "data", "content", | ||
| "record", "entry", "memo", "paper", "sheet", "digest", "brief", "piece", | ||
| } | ||
|
|
||
| // Use current time for uniqueness and randomness for variety | ||
| now := time.Now() | ||
| r := rand.New(rand.NewSource(now.UnixNano())) | ||
|
||
|
|
||
| adjective := adjectives[r.Intn(len(adjectives))] | ||
| noun := nouns[r.Intn(len(nouns))] | ||
|
|
||
| // Create timestamp suffix for uniqueness (YYMMDD-HHMM format for brevity) | ||
| timestamp := now.Format("060102-1504") | ||
|
|
||
| return fmt.Sprintf("%s-%s-%s", adjective, noun, timestamp) | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,8 +2,10 @@ package minirag | |||||||||||
|
|
||||||||||||
| import ( | ||||||||||||
| "reflect" | ||||||||||||
| "regexp" | ||||||||||||
| "strings" | ||||||||||||
| "testing" | ||||||||||||
| "time" | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| func TestNewTextChunker(t *testing.T) { | ||||||||||||
|
|
@@ -436,6 +438,65 @@ func TestGetChunkID(t *testing.T) { | |||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| func TestGenerateDocumentID(t *testing.T) { | ||||||||||||
| // Test that the function generates valid IDs | ||||||||||||
| for i := 0; i < 10; i++ { | ||||||||||||
| id := GenerateDocumentID() | ||||||||||||
|
|
||||||||||||
| // Check format: should be adjective-noun-YYMMDD-HHMM (4 parts) | ||||||||||||
| parts := strings.Split(id, "-") | ||||||||||||
| if len(parts) != 4 { | ||||||||||||
| t.Errorf("Expected ID to have 4 parts separated by hyphens, got %d parts: %s", len(parts), id) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Check that it's not empty | ||||||||||||
| if id == "" { | ||||||||||||
| t.Error("Generated ID should not be empty") | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Check that it contains only valid characters (alphanumeric and hyphens) | ||||||||||||
| if matched, _ := regexp.MatchString("^[a-z0-9-]+$", id); !matched { | ||||||||||||
| t.Errorf("Generated ID contains invalid characters: %s", id) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Check length is reasonable (should be under 30 characters for readability) | ||||||||||||
| if len(id) > 30 { | ||||||||||||
| t.Errorf("Generated ID is too long (%d chars): %s", len(id), id) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Check that first part is from adjectives list | ||||||||||||
| adjective := parts[0] | ||||||||||||
| adjectives := []string{ | ||||||||||||
| "happy", "bright", "swift", "clever", "gentle", "bold", "calm", "wise", | ||||||||||||
| "brave", "quick", "sharp", "smart", "clean", "fresh", "light", "clear", | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+469
to
+472
|
||||||||||||
| adjectives := []string{ | |
| "happy", "bright", "swift", "clever", "gentle", "bold", "calm", "wise", | |
| "brave", "quick", "sharp", "smart", "clean", "fresh", "light", "clear", | |
| } | |
| adjectives := Adjectives |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Ollama connection error handling logic is duplicated in multiple test cases. Consider extracting this into a helper function to reduce code duplication and improve maintainability.