ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

WeKnora Go SDK 怎么在 Go 应用中完成认证并调用知识库与流式问答接口

WeKnora Go SDK 怎么在 Go 应用中完成认证并调用知识库与流式问答接口 WeKnora Go SDK 怎么在 Go 应用中完成认证并调用知识库与流式问答接口【免费下载链接】WeKnoraOpen-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.项目地址: https://gitcode.com/GitHub_Trending/we/WeKnora如果你的 Go 服务要接入 WeKnora 的知识平台——先完成认证再调用知识库接口并发起流式SSE问答——这篇文章给出一条用官方 Go SDK 完成的连续操作路径。SDK 源码位于仓库client/目录以独立 Go module 发布官方 CLI 和服务端内部调用也复用这个 SDK你需要一个运行中的 WeKnora 服务和一种可用凭证。准备条件Go module 在 client/go.mod 中声明go 1.24.2本地 Go 工具链需要不低于该版本。服务地址方面官方文档的示例统一使用http://localhost:8080部署到其他环境时替换为实际地址。凭证有两种API Key长期以X-API-Key请求头发送账号邮箱 密码先登录换取短期 JWT再以Authorization: Bearer token请求头发送。安装 SDKgo get github.com/Tencent/WeKnora/client导入import github.com/Tencent/WeKnora/client创建客户端API Key 与 JWT 两种认证客户端由NewClient(baseURL string, options ...ClientOption)构造。默认普通请求超时 30 秒流式SSE请求默认无超时生命周期由传入的context控制除非显式调用WithTimeout会同时给普通请求和流式请求设置上限。路径一API Keyctx : context.Background() apiClient : client.NewClient( http://localhost:8080, // 替换为实际 WeKnora 服务地址 client.WithAPIKey(your-api-key), // 替换为你的 API Key )WithAPIKey设置凭证后每个请求都会自动附加X-API-Key头注入逻辑见 client/client.go 的applyAuthHeaders。路径二邮箱密码登录换取 JWTLoginclient/auth.go对应POST /api/v1/auth/login返回 JWT access token、refresh token 和主体信息c : client.NewClient(http://localhost:8080) loginResp, err : c.Login(ctx, client.LoginRequest{ Email: userexample.com, // 替换为账号邮箱 Password: your-password, // 替换为账号密码 }) if err ! nil { return err } // 用返回的 access token 重建带认证的客户端 authed : client.NewClient(http://localhost:8080, client.WithBearerToken(loginResp.Token))这里注意LoginResponse中 access token 的字段名是TokenSDK 文档示例里写作AccessToken但实际结构体中该字段名是Token以 client/auth.go 为准。token 过期时用RefreshToken(ctx, refreshToken)换新 token 对RefreshTokenResponse.AccessToken/.RefreshToken用GetCurrentUser(ctx)对应GET /api/v1/auth/me可以验证 bearer 是否有效并拿到当前用户与租户信息。凭证并存与租户头的边界两种凭证可同时配置但 HTTP 层X-API-Key优先WithTenantID会在每个请求上附加X-Tenant-ID仅用于具备CanAccessAllTenants权限的跨租户显式访问。JWT 与租户级 API Key 本身已携带租户身份普通用户不应设置该头——服务端 auth 中间件会对携带该头的 bearer 请求执行跨租户校验普通用户会得到 403WithToken是WithAPIKey的 v0.x 兼容别名将在下个大版本移除新代码应使用WithAPIKey。调用知识库接口知识库接口在 client/knowledgebase.go。下面这段取自官方文档示例改编自 client/example.go 中的真实代码创建一个带分块配置的知识库ctx : context.Background() kb : client.KnowledgeBase{ Name: Test Knowledge Base, Description: This is a test knowledge base, ChunkingConfig: client.ChunkingConfig{ ChunkSize: 500, ChunkOverlap: 50, Separators: []string{\n\n, \n, . , ? , ! }, }, EmbeddingModelID: embedding_model_id, // 文档占位值替换为服务端实际 Embedding 模型 ID SummaryModelID: summary_model_id, // 文档占位值替换为实际摘要模型 ID } createdKB, err : apiClient.CreateKnowledgeBase(ctx, kb) if err ! nil { // 处理错误 } fmt.Printf(Knowledge base created: ID%s, Name%s\n, createdKB.ID, createdKB.Name)embedding_model_id和summary_model_id是文档示例中的占位值必须替换为服务端实际配置的模型 ID可用ListModels(ctx)拉取模型列表核对。CreateKnowledgeBase会在服务端创建真实资源验证完可用DeleteKnowledgeBase(ctx, createdKB.ID)清理。如果需要把文档灌入知识库用CreateKnowledgeFromFile(ctx, kbID, filePath, metadata, nil, , , nil)上传本地文件见 client/knowledge.go。创建会话并进行流式问答流式问答在 client/session.go。先用CreateSession建会话再用KnowledgeQAStream发起 SSE 流以下代码为节选实际程序需自行导入context、fmt、strings等包// 1. 创建会话 session, err : apiClient.CreateSession(ctx, client.CreateSessionRequest{ Title: Test Session, Description: A test session for knowledge QA, }) if err ! nil { return err } // 2. 流式问答累积答案与引用 question : What is artificial intelligence? var answer strings.Builder var references []*client.SearchResult err apiClient.KnowledgeQAStream(ctx, session.ID, client.KnowledgeQARequest{Query: question}, func(response *client.StreamResponse) error { if response.ResponseType client.ResponseTypeAnswer { answer.WriteString(response.Content) } if response.Done len(response.KnowledgeReferences) 0 { references response.KnowledgeReferences } return nil }) if err ! nil { return err } fmt.Printf(Answer: %s\n, answer.String()) for i, ref : range references { fmt.Printf(Reference %d: %s\n, i1, ref.Content) }流式机制的几个关键点KnowledgeQAStream内部用bufio.Scanner逐行解析 SSEevent:/data:前缀空行分帧每解析出一帧调用一次回调回调返回非 nil error 会立即中止流每帧StreamResponse携带ResponseTypeanswer、references、thinking、tool_call、tool_result、error、reflection、session_title、complete等、增量Content、结束标记Done以及Done帧上的KnowledgeReferences引用来源该方法实际请求POST /api/v1/knowledge-chat/{sessionID}KnowledgeQARequest的KnowledgeBaseIDs字段可以把这次问答限定到指定知识库接口签名末尾有变参opts ...ResourceURLOptions传入后可在流中拿到引用文件的公开 HTTP(S) 直链不需要时忽略。结果验证与错误处理成功判断方式与文档示例一致KnowledgeQAStream返回 nil 错误回调中累积的answer非空若服务端给出引用Done帧上的KnowledgeReferences非空可逐条打印ref.Content。SDK 分两层错误处理方式不同HTTP 层APIErrorclient/client.go所有非 2xx 响应封装为*APIError用errors.As按状态码或服务端结构化错误码分支var apiErr *client.APIError if errors.As(err, apiErr) { switch { case apiErr.StatusCode 404: // 资源不存在 case apiErr.Code client.ServerErrUnauthorized: // 1001 // token 失效触发重新登录或刷新 } }Code取自响应体{code:N}包内常量从ServerErrBadRequest(1000) 到ServerErrValidation(1010)。流层SSEStreamErrorclient/stream_errors.go当服务端在流上发出终止错误帧response_typeerror, donetrue时SDK 会先把该帧交给回调然后返回*SSEStreamError。判断方式两者等价推荐前者if errors.Is(err, client.ErrSSEStreamTerminal) { fmt.Printf(Stream terminated by server error: %v\n, err) } // 等价写法 if client.IsSSEStreamError(err) { ... }可选调试日志与链路追踪需要观察 SSE 逐行解析过程或请求失败原因时在程序启动时调用一次输出到 stderrclient.SetDebugLevel(debug) // debug/info/warn其他值含 error、静默该函数非并发安全必须在任何 SDK 调用发起前调用一次。链路追踪方面在 context 中放入RequestIDstringSDK 会自动作为X-Request-ID请求头发送ctx context.WithValue(ctx, RequestID, req-20260727-0001)限制流式请求默认无超时长问答要靠ctx如context.WithTimeout控制生命周期需要统一上限时显式调用WithTimeout它同时作用于普通请求与流式请求WithTenantID/X-Tenant-ID仅限CanAccessAllTenants主体做跨租户访问普通用户设置会被 403 拒绝WithAPIKey与WithBearerToken同时配置时实际生效的是X-API-KeyJWT 不会被使用。完整方法清单与更多示例见 website-docs/05-clients/03-go-sdk.md可运行示例见 client/example.go。【免费下载链接】WeKnoraOpen-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.项目地址: https://gitcode.com/GitHub_Trending/we/WeKnora创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表