MCPcopy
hub / github.com/Tencent/WeKnora / ExampleUsage

Function ExampleUsage

client/example.go:22–253  ·  view source on GitHub ↗

ExampleUsage demonstrates the complete usage flow of the WeKnora client, including: - Creating a client instance - Creating a knowledge base - Uploading knowledge files - Creating a session - Performing question-answering - Using streaming question-answering - Managing models - Managing knowledge ch

()

Source from the content-addressed store, hash-verified

20// - Getting session messages
21// - Cleaning up resources
22func ExampleUsage() {
23 // Create a client instance
24 tenantID := uint64(10000) // default tenant for all requests from this client
25 apiClient := NewClient(
26 "http://localhost:8080",
27 WithToken("your-auth-token"),
28 WithTimeout(30*time.Second),
29 WithTenantID(tenantID), // default tenant for all requests from this client
30 )
31
32 // Per-request tenant override example:
33 // You can override the tenant for a single request by setting the "TenantID" value in the context.
34 // e.g. ctx := context.WithValue(context.Background(), "TenantID", &tenantID)
35 // then pass `ctx` to any client method to use tenant 2 for that request only.
36
37 // 1. Create a knowledge base
38 fmt.Println("1. Creating knowledge base...")
39 kb := &KnowledgeBase{
40 Name: "Test Knowledge Base",
41 Description: "This is a test knowledge base",
42 ChunkingConfig: ChunkingConfig{
43 ChunkSize: 500,
44 ChunkOverlap: 50,
45 Separators: []string{"\n\n", "\n", ". ", "? ", "! "},
46 },
47 ImageProcessingConfig: ImageProcessingConfig{
48 ModelID: "image_model_id",
49 },
50 EmbeddingModelID: "embedding_model_id",
51 SummaryModelID: "summary_model_id",
52 }
53
54 createdKB, err := apiClient.CreateKnowledgeBase(context.Background(), kb)
55 if err != nil {
56 fmt.Printf("Failed to create knowledge base: %v\n", err)
57 return
58 }
59 fmt.Printf("Knowledge base created successfully: ID=%s, Name=%s\n", createdKB.ID, createdKB.Name)
60
61 // 2. Upload knowledge file
62 fmt.Println("\n2. Uploading knowledge file...")
63 filePath := "path/to/sample.pdf" // Sample file path
64
65 // Check if file exists before uploading
66 if _, err := os.Stat(filePath); os.IsNotExist(err) {
67 fmt.Printf("File does not exist: %s, skipping upload step\n", filePath)
68 } else {
69 // Add metadata
70 metadata := map[string]string{
71 "source": "local",
72 "type": "document",
73 }
74 knowledge, err := apiClient.CreateKnowledgeFromFile(context.Background(), createdKB.ID, filePath, metadata, nil, "", "", nil)
75 if err != nil {
76 fmt.Printf("Failed to upload knowledge file: %v\n", err)
77 } else {
78 fmt.Printf("File uploaded successfully: Knowledge ID=%s, Title=%s\n", knowledge.ID, knowledge.Title)
79 }

Callers

nothing calls this directly

Calls 15

CreateKnowledgeBaseMethod · 0.95
CreateModelMethod · 0.95
ListModelsMethod · 0.95
CreateSessionMethod · 0.95
KnowledgeQAStreamMethod · 0.95
GetRecentMessagesMethod · 0.95
ListKnowledgeChunksMethod · 0.95
UpdateChunkMethod · 0.95
DeleteSessionMethod · 0.95
DeleteKnowledgeMethod · 0.95
DeleteKnowledgeBaseMethod · 0.95

Tested by

no test coverage detected