This is the complete example implementation for the Basic Chat Application Tutorial. It demonstrates how to build a chat application using LangChainGo, progressing from a simple implementation to an advanced one with conversation memory and chains.
- Go 1.21 or later
- OpenAI API key
Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY="your-api-key-here"This example includes implementations of all steps from the tutorial:
Sends a single message to the LLM and prints the response.
go run . step3
# or
go run . basicInteractive chat session without memory (each message is independent).
go run . step4
# or
go run . interactiveInteractive chat that remembers the conversation history.
go run . step5
# or
go run . memoryFull-featured chat using chains with automatic memory management.
go run . step6
# or
go run . advanced
# or just run without arguments (default)
go run .- Simple LLM initialization
- Single prompt/response interaction
- Basic error handling
- Interactive input loop
- Graceful exit with 'quit' command
- Error handling for failed requests
- Conversation buffer memory
- Context preservation across messages
- Manual prompt construction with history
- Conversation chains with built-in templates
- Automatic memory management
- Structured conversation flow
- Clean separation of concerns
main.go- Complete implementation with all stepsstep3_basic.go- Basic chat implementation (build tag: example)step4_interactive.go- Interactive chat implementation (build tag: example)step5_memory.go- Chat with memory implementation (build tag: example)step6_advanced.go- Advanced chat with chains (build tag: example)
This example implements the tutorial from: Building a Basic Chat Application
You can customize the behavior by modifying:
- Prompt Template (Step 6): Edit the template string to change the AI's personality
- Model Selection: Pass options to
openai.New()to use different models - Memory Type: Replace
memory.NewConversationBuffer()with other memory types - Error Handling: Add retry logic or custom error messages
$ go run .
=== Step 6: Advanced Chat with Chains ===
Advanced Chat Application (type 'quit' to exit)
----------------------------------------
You: Hello! Who are you?
AI: Hello! I'm an AI assistant created to help answer questions and have conversations. I'm here to provide helpful, accurate, and friendly responses to whatever you'd like to discuss. How can I assist you today?
You: Can you remember what I just asked?
AI: Yes, I can remember our conversation! You just asked me "Hello! Who are you?" and I introduced myself as an AI assistant who is here to help answer questions and have conversations with you.
You: quit
Goodbye!