service_chat 支持基于一个已创建的知识服务进行检索/问答。
| 参数 | 类型 | 是否必选 | 默认值 | 参数说明 |
|---|---|---|---|---|
ServiceResourceID | string | 是 | -- | 知识服务唯一 ID |
Messages | []model.ChatMessage | 是 | -- | 检索/问答多轮对话消息
图文对话:
|
QueryParam | map[string]interface{} | 否 | nil | 检索过滤条件
多层 filter:
|
Stream | *bool | 否 | true | 是否采用流式返回
|
目前知识服务主要分为两类:检索类型和问答类型。针对不同类型的知识服务,返回的消息格式也有所不同。
检索/问答/流式的差异体现在 Data 内字段是否出现及返回时机:
Count、RewriteQuery、ResultList 通常在首流返回;TokenUsage 通常在尾流返回;GeneratedAnswer、ReasoningContent 在中间流分段返回(SSE)。
| 字段 | 类型 | 参数说明 |
|---|---|---|
| Code | int | 状态码 |
| Message | string | 返回信息 |
| RequestID | string | 标识每个请求的唯一标识符 |
| Data | *ServiceChatData | ServiceChatData |
| 字段 | 类型 | 参数说明 |
|---|---|---|
| Count | *int | 检索结果返回的条数 |
| RewriteQuery | *string | query 改写的结果 |
| TokenUsage | interface{} | Token 使用信息 |
| ResultList | []ServiceChatRetrieveItem | 检索返回的信息 |
| GeneratedAnswer | *string | LLM 模型生成的回答 |
| ReasoningContent | *string | 推理模型生成的内容 |
| Prompt | *string | prompt 内容 |
| End | *bool | 是否结束(流式场景用于标识最后一段) |
| 字段 | 类型 | 参数说明 |
|---|---|---|
| ID | *string | 索引的主键 |
Content | *string | 切片内容 |
| MDContent | *string | markdown 格式的解析结果(表格切片可通过 ChunkType == table 判断) |
| Score | *float64 | 向量化语义检索得分 |
| PointID | *string | 切片 id |
| OriginText | *string | 原始文本 |
| OriginalQuestion | *string | faq 数据检索召回的答案对应的原始问题 |
| ChunkTitle | *string | 切片标题 |
| ChunkID | *int64 | 切片位次 id(代表在原始文档中的位次顺序) |
| ProcessTime | *int64 | 检索耗时(s) |
| RerankScore | *float64 | 重排得分 |
| DocInfo | *ServiceChatRetrieveItemDocInfo | ServiceChatRetrieveItemDocInfo |
| RecallPosition | *int | 向量化语义检索召回位次 |
| RerankPosition | *int | 重排位次 |
| ChunkType | *string | 切片所属类型(如 doc-image、image、video、table、mixed-table、text、structured、faq 等) |
| ChunkSource | *string | 切片来源 |
| UpdateTime | *int64 | 更新时间 |
| ChunkAttachment | []ChunkAttachment | 检索召回附件的临时下载链接,有效期 10 分钟 |
| TableChunkFields | []PointTableChunkField | 结构化数据检索返回单行全量数据 |
| OriginalCoordinate | map[string]interface{} | 切片在所属文档的原始位置坐标 |
| 字段 | 类型 | 参数说明 |
|---|---|---|
| DocID | *string | 文档 id |
| DocName | *string | 文档名字 |
| CreateTime | *int64 | 文档的创建时间 |
| DocType | *string | 知识所属原始文档的类型 |
| DocMeta | *string | 文档相关元信息 |
| Source | *string | 知识来源类型 |
| Title | *string | 知识所属文档的标题 |
| 字段 | 类型 | 参数说明 |
|---|---|---|
| UUID | *string | 附件的唯一标识 |
| Caption | *string | 图片所属标题,若未识别到标题则值为 "\n" |
| Type | *string | image 等 |
| Link | *string | 临时下载链接,有效期 10 分钟 |
| InfoLink | *string | 附件 info_link |
| ColumnName | *string | 附件列名 |
| 字段 | 类型 | 参数说明 |
|---|---|---|
| FieldName | *string | 字段名 |
| FieldValue | interface{} | 字段值 |
首次使用知识库 SDK,可参考 使用说明
本示例演示了知识库 Go SDK 中 ServiceChat 的基础使用方法,包含普通调用和流式调用两种方式;该功能需使用 API Key 鉴权,且需配置知识服务 ID。
package main import ( "context" "encoding/json" "fmt" "github.com/volcengine/vikingdb-go-sdk/knowledge" "github.com/volcengine/vikingdb-go-sdk/knowledge/model" "os" "time" ) func main() { var ( apiKey = os.Getenv("VIKINGDB_API_KEY") endpoint = "https://api-knowledgebase.mlp.cn-beijing.volces.com" region = "cn-beijing" ) client, err := knowledge.New( knowledge.AuthAPIKey(apiKey), knowledge.WithEndpoint(endpoint), knowledge.WithRegion(region), knowledge.WithTimeout(time.Second*30), ) if err != nil { fmt.Printf("New client failed, err: %v\n", err) return } ctx := context.Background() // 1. Prepare messages messages := []model.ChatMessage{ { Role: "user", Content: "Help me find some documents.", }, } // 2. Call ServiceChat stream := false resp, err := client.ServiceChat(ctx, model.ServiceChatRequest{ ServiceResourceID: "<YOUR_SERVICE_RESOURCE_ID>", // Replace with your service resource id Messages: messages, Stream: &stream, }) if err != nil { fmt.Printf("ServiceChat failed, err: %v\n", err) return } jsonData, _ := json.Marshal(resp) fmt.Printf("Response: %s\n", string(jsonData)) // 3. Call ServiceChat Stream stream = true ch, err := client.ServiceChatStream(ctx, model.ServiceChatRequest{ ServiceResourceID: "<YOUR_SERVICE_RESOURCE_ID>", // Replace with your service resource id Messages: messages, Stream: &stream, }) if err != nil { fmt.Printf("ServiceChatStream failed, err: %v\n", err) return } // Read from channel for resp := range ch { jsonData, _ := json.Marshal(resp) fmt.Printf("Chunk: %s\n", string(jsonData)) } }