Tutorial 1: Your First Conversation
Build a chatbot in 5 lines of code using the PromptKit SDK.
What You’ll Learn
Section titled “What You’ll Learn”- Open a conversation from a pack file
- Send messages and receive responses
- Use template variables
- Multi-turn conversations
Prerequisites
Section titled “Prerequisites”- Go 1.21+ installed
- OpenAI API key (get one at platform.openai.com)
Step 1: Set Up Your Project
Section titled “Step 1: Set Up Your Project”Create a new Go module:
mkdir my-chatbotcd my-chatbotgo mod init my-chatbotInstall the SDK:
go get github.com/AltairaLabs/PromptKit/sdkStep 2: Create a PromptPack
Section titled “Step 2: Create a PromptPack”Create hello.pack.json:
{ "id": "hello-chatbot", "name": "Hello Chatbot", "version": "1.0.0", "template_engine": { "version": "v1", "syntax": "{{variable}}" }, "prompts": { "chat": { "id": "chat", "name": "Chat Assistant", "version": "1.0.0", "system_template": "You are a helpful AI assistant. Be concise and friendly. The user's name is {{user_name}}.", "parameters": { "temperature": 0.7, "max_tokens": 1000 } } }}This pack defines your assistant’s behavior and configuration.
Step 3: Write Your First Chatbot
Section titled “Step 3: Write Your First Chatbot”Create main.go:
package main
import ( "context" "fmt" "log"
"github.com/AltairaLabs/PromptKit/sdk")
func main() { // Open a conversation from a pack file conv, err := sdk.Open("./hello.pack.json", "chat") if err != nil { log.Fatal(err) } defer conv.Close()
// Set template variables (optional) conv.SetVar("user_name", "World")
// Send a message and get a response ctx := context.Background() resp, err := conv.Send(ctx, "Hello!") if err != nil { log.Fatal(err) }
// Print the response fmt.Println(resp.Text())}That’s it! 5 lines of functional code (excluding error handling).
Step 4: Run Your Chatbot
Section titled “Step 4: Run Your Chatbot”Set your API key:
export OPENAI_API_KEY="your-api-key-here"Run the program:
go run main.goYou should see:
Hello World! How can I help you today?🎉 Congratulations! You’ve built your first chatbot.
Understanding the Code
Section titled “Understanding the Code”sdk.Open()
Section titled “sdk.Open()”conv, err := sdk.Open("./hello.pack.json", "chat")- First argument: path to your pack file
- Second argument: prompt name from the pack
- Returns a
Conversationready to use
conv.SetVar()
Section titled “conv.SetVar()”conv.SetVar("user_name", "World")Sets template variables that are substituted into the system prompt.
conv.Send()
Section titled “conv.Send()”resp, err := conv.Send(ctx, "Hello!")Sends a message and returns the response. The conversation context is maintained automatically.
resp.Text()
Section titled “resp.Text()”fmt.Println(resp.Text())Gets the text content from the response.
Multi-Turn Conversations
Section titled “Multi-Turn Conversations”The SDK automatically maintains conversation history:
// Turn 1resp1, _ := conv.Send(ctx, "My name is Alice")fmt.Println(resp1.Text()) // "Nice to meet you, Alice!"
// Turn 2 - context is rememberedresp2, _ := conv.Send(ctx, "What's my name?")fmt.Println(resp2.Text()) // "Your name is Alice."Configuration Options
Section titled “Configuration Options”Open with Options
Section titled “Open with Options”conv, err := sdk.Open("./hello.pack.json", "chat", sdk.WithModel("gpt-4o"), sdk.WithTemperature(0.9),)Different Providers
Section titled “Different Providers”The pack file can specify different providers:
{ "id": "my-chatbot", "name": "My Chatbot", "version": "1.0.0", "template_engine": { "version": "v1", "syntax": "{{variable}}" }, "provider": { "name": "anthropic", "model": "claude-3-5-sonnet-20241022" }, "prompts": { "chat": { "id": "chat", "name": "Chat", "version": "1.0.0", "system_template": "You are a helpful assistant." } }}Interactive Chat Loop
Section titled “Interactive Chat Loop”Make it interactive:
package main
import ( "bufio" "context" "fmt" "log" "os" "strings"
"github.com/AltairaLabs/PromptKit/sdk")
func main() { conv, err := sdk.Open("./hello.pack.json", "chat") if err != nil { log.Fatal(err) } defer conv.Close()
ctx := context.Background() scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Chat ready! Type 'quit' to exit.") for { fmt.Print("You: ") if !scanner.Scan() { break }
msg := strings.TrimSpace(scanner.Text()) if msg == "quit" { break }
resp, err := conv.Send(ctx, msg) if err != nil { log.Printf("Error: %v", err) continue }
fmt.Printf("Assistant: %s\n\n", resp.Text()) }}What You’ve Learned
Section titled “What You’ve Learned”✅ Open conversations with sdk.Open()
✅ Set template variables with SetVar()
✅ Send messages with Send()
✅ Multi-turn conversation context
✅ Configuration options
Next Steps
Section titled “Next Steps”- Tutorial 2: Streaming - Real-time responses
- Tutorial 3: Tools - Add function calling
- How-To: Send Messages - Advanced messaging
Complete Example
Section titled “Complete Example”See the full example at sdk/examples/hello/.