Documentation
¶
Overview ¶
Package knowledge 提供知识图谱能力
实现 Property Graph 和 Text2Cypher 功能:
- PropertyGraph: 属性图存储和查询
- Entity/Relation: 实体和关系定义
- Text2Cypher: 自然语言转 Cypher 查询
- GraphRAG: 基于图的 RAG 检索
对标 LlamaIndex 的 Property Graph Index + Text2Cypher。
使用示例:
store := NewMemoryGraphStore()
graph := NewPropertyGraph(store)
// 添加实体和关系
graph.AddEntity(ctx, Entity{Name: "Go", Type: "Language"})
graph.AddEntity(ctx, Entity{Name: "Google", Type: "Company"})
graph.AddRelation(ctx, Relation{From: "Go", To: "Google", Type: "created_by"})
// 自然语言查询
t2c := NewText2Cypher(llmProvider, graph)
results, err := t2c.Query(ctx, "哪些语言是 Google 创建的?")
Index ¶
- type Entity
- type GraphQueryResult
- type GraphRetriever
- type GraphRetrieverOption
- type GraphStore
- type MemoryGraphStore
- func (s *MemoryGraphStore) AddEntity(_ context.Context, entity Entity) error
- func (s *MemoryGraphStore) AddRelation(_ context.Context, relation Relation) error
- func (s *MemoryGraphStore) Clear(_ context.Context) error
- func (s *MemoryGraphStore) DeleteEntity(_ context.Context, nameOrID string) error
- func (s *MemoryGraphStore) DeleteRelation(_ context.Context, id string) error
- func (s *MemoryGraphStore) ExecuteCypher(_ context.Context, cypher string, _ map[string]any) (*GraphQueryResult, error)
- func (s *MemoryGraphStore) GetEntity(_ context.Context, nameOrID string) (*Entity, error)
- func (s *MemoryGraphStore) GetNeighbors(_ context.Context, entityNameOrID string, hops int) ([]Entity, []Relation, error)
- func (s *MemoryGraphStore) GetRelations(_ context.Context, entityNameOrID string, direction string) ([]Relation, error)
- func (s *MemoryGraphStore) SearchEntities(_ context.Context, query string, entityType string, limit int) ([]Entity, error)
- func (s *MemoryGraphStore) Stats(_ context.Context) (int, int, error)
- type PropertyGraph
- func (pg *PropertyGraph) AddEntity(ctx context.Context, entity Entity) error
- func (pg *PropertyGraph) AddRelation(ctx context.Context, relation Relation) error
- func (pg *PropertyGraph) AddTriple(ctx context.Context, subject, predicate, object string) error
- func (pg *PropertyGraph) GetSubgraph(ctx context.Context, entityName string, hops int) (*GraphQueryResult, error)
- func (pg *PropertyGraph) Query(ctx context.Context, cypher string, params map[string]any) (*GraphQueryResult, error)
- func (pg *PropertyGraph) Store() GraphStore
- type Relation
- type Text2Cypher
- type Text2CypherOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entity ¶
type Entity struct {
// ID 实体唯一标识
ID string `json:"id"`
// Name 实体名称
Name string `json:"name"`
// Type 实体类型(如 Person, Company, Language)
Type string `json:"type"`
// Properties 属性
Properties map[string]any `json:"properties,omitempty"`
// CreatedAt 创建时间
CreatedAt time.Time `json:"created_at,omitempty"`
}
Entity 实体(图节点)
type GraphQueryResult ¶
type GraphQueryResult struct {
// Entities 匹配的实体
Entities []Entity `json:"entities,omitempty"`
// Relations 匹配的关系
Relations []Relation `json:"relations,omitempty"`
// Paths 匹配的路径
Paths [][]string `json:"paths,omitempty"`
// RawResult 原始查询结果
RawResult any `json:"raw_result,omitempty"`
}
GraphQueryResult 图查询结果
type GraphRetriever ¶
type GraphRetriever struct {
// contains filtered or unexported fields
}
GraphRetriever 基于图的 RAG 检索器 结合知识图谱和传统向量检索
func NewGraphRetriever ¶
func NewGraphRetriever(graph *PropertyGraph, opts ...GraphRetrieverOption) *GraphRetriever
NewGraphRetriever 创建图 RAG 检索器
type GraphRetrieverOption ¶
type GraphRetrieverOption func(*GraphRetriever)
GraphRetrieverOption 选项
func WithGraphIncludeRelations ¶
func WithGraphIncludeRelations(include bool) GraphRetrieverOption
WithGraphIncludeRelations 是否包含关系信息
func WithGraphMaxEntities ¶
func WithGraphMaxEntities(max int) GraphRetrieverOption
WithGraphMaxEntities 设置最大返回实体数
type GraphStore ¶
type GraphStore interface {
// AddEntity 添加实体
AddEntity(ctx context.Context, entity Entity) error
// AddRelation 添加关系
AddRelation(ctx context.Context, relation Relation) error
// GetEntity 获取实体
GetEntity(ctx context.Context, nameOrID string) (*Entity, error)
// GetRelations 获取实体的关系
GetRelations(ctx context.Context, entityNameOrID string, direction string) ([]Relation, error)
// SearchEntities 搜索实体(按名称或类型)
SearchEntities(ctx context.Context, query string, entityType string, limit int) ([]Entity, error)
// ExecuteCypher 执行 Cypher 查询
ExecuteCypher(ctx context.Context, cypher string, params map[string]any) (*GraphQueryResult, error)
// GetNeighbors 获取 N 跳邻居
GetNeighbors(ctx context.Context, entityNameOrID string, hops int) ([]Entity, []Relation, error)
// DeleteEntity 删除实体
DeleteEntity(ctx context.Context, nameOrID string) error
// DeleteRelation 删除关系
DeleteRelation(ctx context.Context, id string) error
// Clear 清空所有数据
Clear(ctx context.Context) error
// Stats 统计信息
Stats(ctx context.Context) (entityCount, relationCount int, err error)
}
GraphStore 图存储接口 支持不同后端(内存、Neo4j、ArangoDB 等)
type MemoryGraphStore ¶
type MemoryGraphStore struct {
// contains filtered or unexported fields
}
MemoryGraphStore 内存图存储实现 适用于测试和小规模数据
func (*MemoryGraphStore) AddEntity ¶
func (s *MemoryGraphStore) AddEntity(_ context.Context, entity Entity) error
func (*MemoryGraphStore) AddRelation ¶
func (s *MemoryGraphStore) AddRelation(_ context.Context, relation Relation) error
func (*MemoryGraphStore) DeleteEntity ¶
func (s *MemoryGraphStore) DeleteEntity(_ context.Context, nameOrID string) error
func (*MemoryGraphStore) DeleteRelation ¶
func (s *MemoryGraphStore) DeleteRelation(_ context.Context, id string) error
func (*MemoryGraphStore) ExecuteCypher ¶
func (s *MemoryGraphStore) ExecuteCypher(_ context.Context, cypher string, _ map[string]any) (*GraphQueryResult, error)
ExecuteCypher 简化 Cypher 执行(内存存储仅支持基础查询模式)
func (*MemoryGraphStore) GetNeighbors ¶
func (*MemoryGraphStore) GetRelations ¶
func (*MemoryGraphStore) SearchEntities ¶
type PropertyGraph ¶
type PropertyGraph struct {
// contains filtered or unexported fields
}
PropertyGraph 属性图 封装 GraphStore 提供高级操作
func NewPropertyGraph ¶
func NewPropertyGraph(store GraphStore) *PropertyGraph
NewPropertyGraph 创建属性图
func (*PropertyGraph) AddEntity ¶
func (pg *PropertyGraph) AddEntity(ctx context.Context, entity Entity) error
AddEntity 添加实体
func (*PropertyGraph) AddRelation ¶
func (pg *PropertyGraph) AddRelation(ctx context.Context, relation Relation) error
AddRelation 添加关系
func (*PropertyGraph) AddTriple ¶
func (pg *PropertyGraph) AddTriple(ctx context.Context, subject, predicate, object string) error
AddTriple 添加三元组 (subject, predicate, object)
func (*PropertyGraph) GetSubgraph ¶
func (pg *PropertyGraph) GetSubgraph(ctx context.Context, entityName string, hops int) (*GraphQueryResult, error)
GetSubgraph 获取以指定实体为中心的子图
func (*PropertyGraph) Query ¶
func (pg *PropertyGraph) Query(ctx context.Context, cypher string, params map[string]any) (*GraphQueryResult, error)
Query 执行 Cypher 查询
type Relation ¶
type Relation struct {
// ID 关系唯一标识
ID string `json:"id"`
// From 源实体名称或 ID
From string `json:"from"`
// To 目标实体名称或 ID
To string `json:"to"`
// Type 关系类型(如 created_by, works_at, has_skill)
Type string `json:"type"`
// Properties 关系属性
Properties map[string]any `json:"properties,omitempty"`
// Weight 关系权重
Weight float64 `json:"weight,omitempty"`
// CreatedAt 创建时间
CreatedAt time.Time `json:"created_at,omitempty"`
}
Relation 关系(图边)
type Text2Cypher ¶
type Text2Cypher struct {
// contains filtered or unexported fields
}
Text2Cypher 自然语言转 Cypher 查询 使用 LLM 将用户的自然语言问题转换为 Cypher 查询
func NewText2Cypher ¶
func NewText2Cypher(provider llm.Provider, graph *PropertyGraph, opts ...Text2CypherOption) *Text2Cypher
NewText2Cypher 创建 Text2Cypher
func (*Text2Cypher) Query ¶
func (t *Text2Cypher) Query(ctx context.Context, question string) (*GraphQueryResult, error)
Query 使用自然语言查询图
type Text2CypherOption ¶
type Text2CypherOption func(*Text2Cypher)
Text2CypherOption 选项
func WithText2CypherModel ¶
func WithText2CypherModel(model string) Text2CypherOption
WithText2CypherModel 设置模型