Files
Travis Cline dff679c481 docs: complete the incomplete basic chat app tutorial (#1399)
docs: complete basic chat application tutorial with progressive examples

Completely rewrite the basic chat application tutorial with a structured, step-by-step approach:

**Tutorial Documentation**:
- Restructure tutorial into 6 clear progressive steps
- Add proper setup instructions and prerequisites
- Include code examples for each step with explanations
- Improve clarity and flow from basic to advanced concepts

**Complete Working Example**:
- Add `examples/tutorial-basic-chat-app/` with full implementation
- Include separate files for each tutorial step (step3-step6)
- Add comprehensive README with usage instructions
- Support multiple execution modes via command-line arguments

**Progressive Implementation Steps**:
- Step 3: Basic single-shot LLM interaction
- Step 4: Interactive chat loop without memory
- Step 5: Chat with manual conversation memory management
- Step 6: Advanced chat using chains with automatic memory

**Features Added**:
- Go module setup with proper dependencies
- Error handling and graceful exit functionality
- Multiple chat implementations demonstrating different approaches
- Clear documentation linking tutorial to working code
- Support for running individual steps or complete implementation

The tutorial now provides a complete learning path from basic LLM usage to sophisticated conversation management using LangChainGo's chains and memory systems.
2025-09-14 20:20:59 +02:00

58 lines
1.1 KiB
Go

package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"strings"
"github.com/tmc/langchaingo/chains"
"github.com/tmc/langchaingo/llms/openai"
"github.com/tmc/langchaingo/memory"
)
// Step 6: Advanced Chat with Chains
func advancedChat() {
// Initialize LLM
llm, err := openai.New()
if err != nil {
log.Fatal(err)
}
// Create conversation memory
chatMemory := memory.NewConversationBuffer()
// Create conversation chain
// The built-in conversation chain includes a default prompt template
// and handles memory automatically
conversationChain := chains.NewConversation(llm, chatMemory)
ctx := context.Background()
reader := bufio.NewReader(os.Stdin)
fmt.Println("Advanced Chat Application (type 'quit' to exit)")
fmt.Println("----------------------------------------")
for {
fmt.Print("You: ")
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
if input == "quit" {
break
}
// Run the chain with the input
result, err := chains.Run(ctx, conversationChain, input)
if err != nil {
fmt.Printf("Error: %v\n", err)
continue
}
fmt.Printf("AI: %s\n\n", result)
}
fmt.Println("Goodbye!")
}