-
Notifications
You must be signed in to change notification settings - Fork 55
[WIP] knowledge: fix metadata filter(break changes) and simplify example #754
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
Open
hyprh
wants to merge
2
commits into
trpc-group:main
Choose a base branch
from
hyprh:knowledgesimplify
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -183,34 +183,35 @@ kb := knowledge.New( | |
| knowledge.WithEmbedder(embedder), | ||
| ) | ||
|
|
||
| // Note: Metadata fields must use the metadata. prefix | ||
| filterCondition := &searchfilter.UniversalFilterCondition{ | ||
| Operator: searchfilter.OperatorAnd, | ||
| Value: []*searchfilter.UniversalFilterCondition{ | ||
| { | ||
| Field: "tag", | ||
| Field: "metadata.tag", // Metadata fields use metadata. prefix | ||
| Operator: searchfilter.OperatorEqual, | ||
| Value: "tag", | ||
| }, | ||
| { | ||
| Field: "age", | ||
| Field: "metadata.age", | ||
| Operator: searchfilter.OperatorGreaterThanOrEqual, | ||
| Value: 18, | ||
| }, | ||
| { | ||
| Field: "create_time", | ||
| Field: "metadata.create_time", | ||
| Operator: searchfilter.OperatorBetween, | ||
| Value: []string{"2024-10-11 12:11:00", "2025-10-11 12:11:00"}, | ||
| }, | ||
| { | ||
| Operator: searchfilter.OperatorOr, | ||
| Value: []*searchfilter.UniversalFilterCondition{ | ||
| { | ||
| Field: "login_time", | ||
| Field: "metadata.login_time", | ||
| Operator: searchfilter.OperatorLessThanOrEqual, | ||
| Value: "2025-01-11 12:11:00", | ||
| }, | ||
| { | ||
| Field: "status", | ||
| Field: "metadata.status", | ||
| Operator: searchfilter.OperatorEqual, | ||
| Value: "logout", | ||
| }, | ||
|
|
@@ -828,9 +829,9 @@ llmAgent := llmagent.New( | |
| "source": "official", // Official source | ||
| "category": "documentation", // Documentation category | ||
| }), | ||
| // Agent-level complex condition filter | ||
| // Agent-level complex condition filter (metadata fields use metadata. prefix) | ||
| llmagent.WithKnowledgeConditionedFilter( | ||
| searchfilter.Equal("status", "published"), // Published status | ||
| searchfilter.Equal("metadata.status", "published"), // Published status | ||
| ), | ||
| ) | ||
|
|
||
|
|
@@ -844,12 +845,12 @@ eventCh, err := runner.Run( | |
| }), | ||
| // Runner-level complex condition filter | ||
| agent.WithKnowledgeConditionedFilter( | ||
| searchfilter.GreaterThan("priority", 5), // Priority greater than 5 | ||
| searchfilter.GreaterThan("metadata.priority", 5), // Priority greater than 5 | ||
| ), | ||
| ) | ||
|
|
||
| // 3. LLM intelligent filter (dynamically generated by LLM) | ||
| // Example: User asks "find API related docs", LLM might generate {"topic": "api"} | ||
| // Example: User asks "find API related docs", LLM might generate {"field": "metadata.topic", "value": "api"} | ||
|
|
||
| // Final effective filter conditions (all combined with AND): | ||
| // source = "official" AND | ||
|
|
@@ -873,11 +874,11 @@ searchTool := tool.NewKnowledgeSearchTool( | |
| tool.WithFilter(map[string]interface{}{ | ||
| "source": "official", | ||
| }), | ||
| // Agent-level complex condition filter | ||
| // Agent-level complex condition filter (metadata fields use metadata. prefix) | ||
| tool.WithConditionedFilter( | ||
| searchfilter.Or( | ||
| searchfilter.Equal("topic", "programming"), | ||
| searchfilter.Equal("topic", "llm"), | ||
| searchfilter.Equal("metadata.topic", "programming"), | ||
| searchfilter.Equal("metadata.topic", "llm"), | ||
| ), | ||
| ), | ||
| ) | ||
|
|
@@ -889,35 +890,57 @@ llmAgent := llmagent.New( | |
| ) | ||
|
|
||
| // Final filter conditions: | ||
| // source = "official" AND (topic = "programming" OR topic = "llm") | ||
| // metadata.source = "official" AND (metadata.topic = "programming" OR metadata.topic = "llm") | ||
| // i.e., must be official source AND topic is either programming or LLM | ||
| ``` | ||
|
|
||
| ##### Filter Field Naming Convention | ||
|
|
||
| When using `FilterCondition`, **metadata fields must use the `metadata.` prefix**: | ||
|
|
||
| ```go | ||
| // ✅ Correct: Use metadata. prefix | ||
| searchfilter.Equal("metadata.topic", "programming") | ||
| searchfilter.Equal("metadata.category", "documentation") | ||
|
|
||
| // ❌ Wrong: Missing metadata. prefix | ||
| searchfilter.Equal("topic", "programming") | ||
| ``` | ||
|
|
||
| > **Notes**: | ||
| > - The `metadata.` prefix distinguishes metadata fields from system fields (e.g., `id`, `name`, `content`) | ||
| > - If you customized the metadata field name via `WithMetadataField()`, still use the `metadata.` prefix; the framework will automatically convert it to the actual field name | ||
| > - System fields (`id`, `name`, `content`, `created_at`, `updated_at`) use the field name directly without prefix | ||
|
|
||
| ##### Common Filter Helper Functions | ||
|
|
||
| ```go | ||
| // Comparison operators | ||
| searchfilter.Equal(field, value) // field = value | ||
| searchfilter.NotEqual(field, value) // field != value | ||
| searchfilter.GreaterThan(field, value) // field > value | ||
| searchfilter.GreaterThanOrEqual(field, value) // field >= value | ||
| searchfilter.LessThan(field, value) // field < value | ||
| searchfilter.LessThanOrEqual(field, value) // field <= value | ||
| searchfilter.In(field, values...) // field IN (...) | ||
| searchfilter.NotIn(field, values...) // field NOT IN (...) | ||
| searchfilter.Like(field, pattern) // field LIKE pattern | ||
| searchfilter.Between(field, min, max) // field BETWEEN min AND max | ||
| // Comparison operators (Note: metadata fields need metadata. prefix) | ||
| searchfilter.Equal("metadata.topic", value) // metadata.topic = value | ||
| searchfilter.NotEqual("metadata.status", value) // metadata.status != value | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 也可以在加一个searchfilter.NotEqual("status", value),表示用户自定义表结构时使用的查询字段 |
||
| searchfilter.GreaterThan("metadata.priority", value) // metadata.priority > value | ||
| searchfilter.GreaterThanOrEqual("metadata.score", value) // metadata.score >= value | ||
| searchfilter.LessThan("metadata.age", value) // metadata.age < value | ||
| searchfilter.LessThanOrEqual("metadata.level", value) // metadata.level <= value | ||
| searchfilter.In("metadata.category", values...) // metadata.category IN (...) | ||
| searchfilter.NotIn("metadata.type", values...) // metadata.type NOT IN (...) | ||
| searchfilter.Like("metadata.title", pattern) // metadata.title LIKE pattern | ||
| searchfilter.Between("metadata.date", min, max) // metadata.date BETWEEN min AND max | ||
|
|
||
| // System fields don't need prefix | ||
| searchfilter.Equal("id", "doc-123") // id = "doc-123" | ||
| searchfilter.In("name", "doc1", "doc2") // name IN ("doc1", "doc2") | ||
|
|
||
| // Logical operators | ||
| searchfilter.And(conditions...) // AND combination | ||
| searchfilter.Or(conditions...) // OR combination | ||
|
|
||
| // Nested example: (status = 'published') AND (category = 'doc' OR category = 'tutorial') | ||
| // Nested example: (metadata.status = 'published') AND (metadata.category = 'doc' OR metadata.category = 'tutorial') | ||
| searchfilter.And( | ||
| searchfilter.Equal("status", "published"), | ||
| searchfilter.Equal("metadata.status", "published"), | ||
| searchfilter.Or( | ||
| searchfilter.Equal("category", "documentation"), | ||
| searchfilter.Equal("category", "tutorial"), | ||
| searchfilter.Equal("metadata.category", "documentation"), | ||
| searchfilter.Equal("metadata.category", "tutorial"), | ||
| ), | ||
| ) | ||
| ``` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export OPENAI_BASE_URL="http://wujipt.woa.com/api/v1" | ||
| export OPENAI_API_KEY="2fe60304d5192f22" | ||
| export MODEL_NAME="qwen3-omni-30b-a3b-thinking" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Knowledge Examples | ||
|
|
||
| Knowledge-enhanced AI agents examples. | ||
|
|
||
| ## Environment | ||
|
|
||
| ```bash | ||
| export OPENAI_BASE_URL=xxx | ||
| export OPENAI_API_KEY=xxx | ||
| export MODEL_NAME=xxx | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| ### basic/ | ||
| Basic example with file source and in-memory vector store. | ||
|
|
||
| ### sources/ | ||
| Different data source types: | ||
|
|
||
| | Case | Description | | ||
| |------|-------------| | ||
| | file-source/ | Load individual files (Markdown, PDF, DOCX, CSV, JSON) | | ||
| | directory-source/ | Recursively load entire directory | | ||
| | url-source/ | Fetch and parse web pages | | ||
| | auto-source/ | Auto-detect source type from path | | ||
|
|
||
| ### vectorstores/ | ||
| Persistent vector storage options: | ||
|
|
||
| | Case | Description | Extra Environment | | ||
| |------|-------------|-------------------| | ||
| | postgres/ | PostgreSQL with pgvector extension | `PGVECTOR_HOST`, `PGVECTOR_PORT`, `PGVECTOR_USER`, `PGVECTOR_PASSWORD`, `PGVECTOR_DATABASE` | | ||
| | elasticsearch/ | Elasticsearch (v7/v8/v9) | `ELASTICSEARCH_HOSTS`, `ELASTICSEARCH_USERNAME`, `ELASTICSEARCH_PASSWORD` | | ||
| | tcvector/ | Tencent VectorDB | `TCVECTOR_URL`, `TCVECTOR_USERNAME`, `TCVECTOR_PASSWORD` | | ||
|
|
||
| ### features/ | ||
| Advanced features: | ||
|
|
||
| | Case | Description | | ||
| |------|-------------| | ||
| | agentic-filter/ | LLM automatically generates metadata filter based on user query | | ||
| | metadata-filter/ | Programmatic metadata filtering with AND/OR/NOT operations | | ||
| | management/ | Dynamic source management: AddSource, RemoveSource, ReloadSource | |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
这里可以加一个searchfilter.GreaterThanOrEqual("updated_at", value),表示使用外层字段, 区别于metadata字段的过滤场景