<mark>此插件，主要社区贡献人（小奶奶花生米）</mark>

```xml
<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-ai-repo-tcvectordb</artifactId>
</dependency>
```


### 1、描述

solon-ai 的主要扩展插件，提供 TcVectorDbRepository 知识库（腾讯云产品）。更多可参考 [《教程 / Solon AI 开发》](/article/learn-solon-ai)


### 2、构建示例

使用 TcVectorDbRepository 时，不需要嵌入模型，但可以配置嵌入模型名字（tcvectordb 在服务端提供了支持）。同时要添加用于查询的索引声明。


```yaml
solon.ai.repo:
  tcvectordb:
    url: "http://localhost:19530" # 参数 ConnectConfig 的类结构配置
    username: "xxx"
    password: "yyy"
```

开始构建

```java
@Configuration
public class DemoConfig {
    //构建知识库的连接客户端
    @Bean
    public VectorDBClient client(@Inject("solon.ai.repo.tcvectordb") Props props) {
        ConnectParam connectParam = ConnectParam.newBuilder()
                .withUrl(props.get("url"))
                .withUsername(props.get("username"))
                .withKey(props.get("password"))
                .build();
                
        return new VectorDBClient(connectParam, ReadConsistencyEnum.EVENTUAL_CONSISTENCY);
    }
    
    //构建知识库
    @Bean
    public TcVectorDbRepository repository(VectorDBClient client){
        //创建元数据字段定义，用于构建索引（按需）
        List<MetadataField> metadataFields = new ArrayList<>();
        metadataFields.add(new MetadataField("title", FieldType.String));
        metadataFields.add(new MetadataField("category", FieldType.String));
        metadataFields.add(new MetadataField("price", FieldType.Uint64));
        metadataFields.add(new MetadataField("stock", FieldType.Uint64));
            
        return TcVectorDbRepository.builder(client)
                    .embeddingModel(EmbeddingModelEnum.BGE_M3) //不同的模型，效果不同。
                    .metadataFields(metadataFields)
                    .build();
    }
}
```

### 3、应用效果

```java
@Component
public class DemoService {
    //可使用统一的 RepositoryStorable 接口注入
    @Inject
    RepositoryStorable repository;
    
    //添加资源
    public void addDocument(List<Document> docs) {
        repository.insert(docs);
    }
    
    //查找资料
    public List<Document> findDocument(String query) {
       //参考 solon-expression 语法。例如：price > 12 AND category == 'book'
       return repository.search(query);
    }
}
```