-
Notifications
You must be signed in to change notification settings - Fork 105
feat: add runnable examples for search and chat streaming #703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d95b87b
feat: add runnable examples for search and chat streaming
r-sahni ffc404a
feat: Add comprehensive examples with CodeRabbit AI improvements
r-sahni 79b5b90
fix: Apply final CodeRabbit suggestions
r-sahni 2616275
docs: Align all README files with actual Go implementations
r-sahni 81a4241
docs: Align README files with comprehensive structure
r-sahni c10d1bf
Aligned the readmes
r-sahni 1fe04d8
updated docs
r-sahni 85712c9
Merge branch 'main' into add-examples-usage
RISHABH4SAHNI 425515a
Corrected linter error
r-sahni 83fb23c
Merge branch 'add-examples-usage' of github.com:RISHABH4SAHNI/meilise…
r-sahni 42723d6
Corrected linter error
r-sahni b0651c9
Corrected linter error
r-sahni 9c0ea23
Corrected linter error
r-sahni dad3465
Corrected linter error
r-sahni 0f77661
Corrected linter error
r-sahni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| .PHONY: help search chat_stream all clean | ||
|
|
||
| # Default target | ||
| help: | ||
| @echo "Available commands:" | ||
| @echo " make search - Run the basic search example" | ||
| @echo " make chat_stream - Run the chat streaming example" | ||
| @echo " make all - Run all examples" | ||
| @echo " make clean - Clean up any temporary files" | ||
| @echo "" | ||
| @echo "Prerequisites:" | ||
| @echo " - Meilisearch server running on http://localhost:7700" | ||
| @echo " - Go 1.20 or higher installed" | ||
|
|
||
| # Run the basic search example | ||
| search: | ||
| @echo "Running basic search example..." | ||
| go run ./search/main.go | ||
|
|
||
| # Run the chat streaming example | ||
| chat_stream: | ||
| @echo "Running chat streaming example..." | ||
| go run ./chat_stream/main.go | ||
|
|
||
| # Run all examples | ||
| all: search chat_stream | ||
|
|
||
| # Clean up temporary files | ||
| clean: | ||
| @echo "Cleaning up..." | ||
| go clean ./... |
RISHABH4SAHNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Meilisearch Go SDK Examples | ||
|
|
||
| This directory contains runnable examples demonstrating how to use the Meilisearch Go SDK for common and advanced operations. | ||
|
|
||
| ## Available Examples | ||
|
|
||
| ### 📝 [Basic Search](./search) | ||
| Demonstrates core search functionality including: | ||
| - Client initialization and connection testing | ||
| - Index creation and management | ||
| - Document addition and indexing | ||
| - Various search operations (basic, filtered, faceted) | ||
| - Task management and waiting for operations | ||
|
|
||
| ### 💬 [Chat Streaming](./chat_stream) | ||
| Shows streaming capabilities and conversational search: | ||
| - Chat workspace management | ||
| - Real-time streaming responses | ||
| - Interactive chat sessions | ||
| - Knowledge base integration | ||
| - Context-aware responses | ||
|
|
||
| ## Running Examples | ||
|
|
||
| Each example is self-contained and can be run with: | ||
|
|
||
| ```bash | ||
| go run ./examples/<example-name> | ||
| ``` | ||
|
|
||
| For example: | ||
| ```bash | ||
| go run ./examples/search | ||
| go run ./examples/chat_stream | ||
| ``` | ||
|
|
||
| Or using the provided Makefile: | ||
| ```bash | ||
| make search | ||
| make chat_stream | ||
| make all | ||
| ``` | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Go 1.20 or higher | ||
| - Meilisearch server running (default: http://localhost:7700) | ||
| - Valid API key (if authentication is enabled) | ||
|
|
||
| ## Configuration | ||
|
|
||
| Before running the examples, make sure to: | ||
| 1. Update the Meilisearch server URL in each example | ||
| 2. Set the appropriate API key | ||
| 3. Ensure your Meilisearch instance has the required features enabled | ||
|
|
||
| ## Code Style | ||
|
|
||
| All examples follow idiomatic Go style and include: | ||
| - Comprehensive error handling | ||
| - Clear documentation and comments | ||
| - Realistic use cases | ||
| - Best practices for production usage | ||
|
|
||
| ## Contributing | ||
|
|
||
| When adding new examples: | ||
| - Follow the established directory structure (`examples/<feature>/main.go`) | ||
| - Include a detailed README for each example | ||
| - Ensure examples are self-contained and runnable | ||
| - Add comprehensive comments explaining each step | ||
| - Handle errors appropriately | ||
| - Test examples against a real Meilisearch instance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # Meilisearch Go SDK Examples | ||
|
|
||
| This directory contains runnable examples demonstrating how to use the Meilisearch Go SDK for common and advanced operations. | ||
|
|
||
| ## Available Examples | ||
|
|
||
| ### 📝 [Basic Search](./search) | ||
RISHABH4SAHNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Demonstrates core search functionality including: | ||
| - Client initialization and connection testing | ||
| - Index creation and management | ||
| - Document addition and indexing | ||
| - Various search operations (basic, filtered, faceted) | ||
| - Task management and waiting for operations | ||
|
|
||
| ### 💬 [Chat Streaming](./chat_stream) | ||
| Shows streaming capabilities and conversational search: | ||
| - Chat workspace management | ||
| - Real-time streaming responses | ||
| - Interactive chat sessions | ||
| - Knowledge base integration | ||
| - Context-aware responses | ||
|
|
||
| ## Running Examples | ||
|
|
||
| Each example is self-contained and can be run with: | ||
|
|
||
| ```bash | ||
| go run ./examples/<example-name> | ||
| ``` | ||
|
|
||
| For example: | ||
| ```bash | ||
| go run ./examples/search | ||
| go run ./examples/chat_stream | ||
| ``` | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Go 1.20 or higher | ||
| - Meilisearch server running (default: http://localhost:7700) | ||
| - Valid API key (if authentication is enabled) | ||
|
|
||
| ## Configuration | ||
RISHABH4SAHNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Before running the examples, make sure to: | ||
| 1. Update the Meilisearch server URL in each example | ||
| 2. Set the appropriate API key | ||
| 3. Ensure your Meilisearch instance has the required features enabled | ||
|
|
||
| ## Code Style | ||
|
|
||
| All examples follow idiomatic Go style and include: | ||
| - Comprehensive error handling | ||
| - Clear documentation and comments | ||
| - Realistic use cases | ||
| - Best practices for production usage | ||
|
|
||
| ## Contributing | ||
|
|
||
| When adding new examples: | ||
| - Follow the established directory structure | ||
| - Include a detailed README for each example | ||
| - Ensure examples are self-contained and runnable | ||
| - Add comprehensive comments explaining each step | ||
| - Handle errors appropriately | ||
| - Test examples against a real Meilisearch instance | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
RISHABH4SAHNI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/meilisearch/meilisearch-go" | ||
| ) | ||
|
|
||
RISHABH4SAHNI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Document represents a knowledge base document for chat context | ||
| type Document struct { | ||
| ID int `json:"id"` | ||
| Title string `json:"title"` | ||
| Content string `json:"content"` | ||
| Topic string `json:"topic"` | ||
| } | ||
|
|
||
| func main() { | ||
| // Initialize the Meilisearch client | ||
| // Replace with your Meilisearch server URL and API key | ||
| client := meilisearch.New("http://localhost:7700", meilisearch.WithAPIKey("your-api-key")) | ||
|
|
||
| // Test connection | ||
| fmt.Println("Testing connection to Meilisearch...") | ||
| health, err := client.Health() | ||
| if err != nil { | ||
| log.Fatalf("Failed to connect to Meilisearch: %v", err) | ||
| } | ||
| fmt.Printf("Meilisearch is %s\n", health.Status) | ||
|
|
||
| // Setup knowledge base for chat context | ||
| if err := setupKnowledgeBase(client); err != nil { | ||
| log.Fatalf("Failed to setup knowledge base: %v", err) | ||
| } | ||
|
|
||
| // List available chat workspaces | ||
| fmt.Println("\nListing chat workspaces...") | ||
| workspaces, err := client.ListChatWorkspaces(&meilisearch.ListChatWorkSpaceQuery{ | ||
| Limit: 10, | ||
| Offset: 0, | ||
| }) | ||
| if err != nil { | ||
| log.Printf("Warning: Could not list chat workspaces: %v", err) | ||
| } else { | ||
| fmt.Printf("Found %d chat workspaces\n", len(workspaces.Results)) | ||
| for _, workspace := range workspaces.Results { | ||
| fmt.Printf(" - Workspace: %s\n", workspace.UID) | ||
| } | ||
| } | ||
RISHABH4SAHNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Start interactive chat session | ||
| fmt.Println("\n--- Interactive Chat Session ---") | ||
| fmt.Println("Type your questions (or 'quit' to exit):") | ||
|
|
||
| scanner := bufio.NewScanner(os.Stdin) | ||
|
|
||
| for { | ||
| fmt.Print("\nYou: ") | ||
| if !scanner.Scan() { | ||
| break | ||
| } | ||
|
|
||
| userInput := strings.TrimSpace(scanner.Text()) | ||
| if userInput == "" { | ||
| continue | ||
| } | ||
| if strings.ToLower(userInput) == "quit" { | ||
| fmt.Println("Goodbye!") | ||
| break | ||
| } | ||
|
|
||
| // Demonstrate chat streaming | ||
| if err := performChatStream(client, userInput); err != nil { | ||
| log.Printf("Chat stream error: %v", err) | ||
| } | ||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| log.Printf("Scanner error: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func setupKnowledgeBase(client meilisearch.ServiceManager) error { | ||
| fmt.Println("Setting up knowledge base...") | ||
|
|
||
| // Create knowledge base index | ||
| indexUID := "knowledge_base" | ||
| index := client.Index(indexUID) | ||
|
|
||
| // Create index | ||
| task, err := client.CreateIndex(&meilisearch.IndexConfig{ | ||
| Uid: indexUID, | ||
| PrimaryKey: "id", | ||
| }) | ||
| if err != nil { | ||
| log.Printf("Index might already exist: %v", err) | ||
| } else { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
| defer cancel() | ||
|
|
||
| _, err = client.WaitForTaskWithContext(ctx, task.TaskUID, 100*time.Millisecond) | ||
| if err != nil { | ||
| return fmt.Errorf("index creation failed: %w", err) | ||
| } | ||
| } | ||
|
|
||
| // Add sample knowledge base documents | ||
| documents := []Document{ | ||
| { | ||
| ID: 1, | ||
| Title: "Getting Started with Meilisearch", | ||
| Content: "Meilisearch is a powerful, fast, open-source, easy to use and deploy search engine. It provides instant search capabilities with typo tolerance and filtering.", | ||
| Topic: "basics", | ||
| }, | ||
| { | ||
| ID: 2, | ||
| Title: "Search Features", | ||
| Content: "Meilisearch offers faceted search, geo-search, full-text search with typo tolerance, synonyms, and custom ranking rules.", | ||
| Topic: "features", | ||
| }, | ||
| { | ||
| ID: 3, | ||
| Title: "API Integration", | ||
| Content: "The Meilisearch API is RESTful and supports multiple SDKs including Go, JavaScript, Python, and more for easy integration.", | ||
| Topic: "integration", | ||
| }, | ||
| } | ||
|
|
||
| addTask, err := index.AddDocuments(documents) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to add documents: %w", err) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
|
|
||
| _, err = client.WaitForTaskWithContext(ctx, addTask.TaskUID, 100*time.Millisecond) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to wait for document addition: %w", err) | ||
| } | ||
|
|
||
| fmt.Println("Knowledge base setup completed!") | ||
| return nil | ||
| } | ||
|
|
||
| func performChatStream(client meilisearch.ServiceManager, query string) error { | ||
| // Create a chat completion query with streaming enabled | ||
| chatQuery := &meilisearch.ChatCompletionQuery{ | ||
| Model: "gpt-3.5-turbo", | ||
| Messages: []meilisearch.ChatMessage{ | ||
| { | ||
| Role: "system", | ||
| Content: "You are a helpful assistant that answers questions about Meilisearch. Use the provided knowledge base to give accurate answers.", | ||
| }, | ||
| { | ||
| Role: "user", | ||
| Content: query, | ||
| }, | ||
| }, | ||
| Stream: true, | ||
| MaxTokens: 200, | ||
| Temperature: 0.7, | ||
| } | ||
RISHABH4SAHNI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Note: This example assumes you have a chat workspace configured | ||
| // In a real implementation, you would need to have Meilisearch configured | ||
| // with chat capabilities and appropriate workspace setup | ||
| workspaceID := "default" // Replace with your actual workspace ID | ||
|
|
||
| fmt.Print("Assistant: ") | ||
|
|
||
| // Start streaming chat completion | ||
| stream, err := client.ChatCompletionStream(workspaceID, chatQuery) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to start chat stream: %w", err) | ||
| } | ||
| defer stream.Close() | ||
|
|
||
| // Process streaming responses | ||
| for { | ||
| chunk, err := stream.Next() | ||
| if err != nil { | ||
| if err.Error() == "EOF" { | ||
| break | ||
| } | ||
| return fmt.Errorf("stream error: %w", err) | ||
RISHABH4SAHNI marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if chunk != nil && len(chunk.Choices) > 0 { | ||
| delta := chunk.Choices[0].Delta | ||
| if delta.Content != "" { | ||
| fmt.Print(delta.Content) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fmt.Println() // New line after streaming response | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.