Send Messages
Learn how to send messages and receive responses.
Basic Send
Section titled “Basic Send”ctx := context.Background()resp, err := conv.Send(ctx, "What is the capital of France?")if err != nil { log.Fatal(err)}
fmt.Println(resp.Text()) // "The capital of France is Paris."Response Methods
Section titled “Response Methods”Get Text
Section titled “Get Text”text := resp.Text()Check for Tool Calls
Section titled “Check for Tool Calls”if resp.HasToolCalls() { for _, call := range resp.ToolCalls() { fmt.Printf("Tool: %s\n", call.Name) }}Check for Pending Approvals
Section titled “Check for Pending Approvals”pending := resp.PendingTools()if len(pending) > 0 { // Handle HITL approvals}Streaming
Section titled “Streaming”Basic Stream
Section titled “Basic Stream”for chunk := range conv.Stream(ctx, "Tell me a story") { if chunk.Error != nil { log.Printf("Error: %v", chunk.Error) break } if chunk.Type == sdk.ChunkDone { break } fmt.Print(chunk.Text)}Collect Full Response
Section titled “Collect Full Response”var fullText stringfor chunk := range conv.Stream(ctx, "Write a poem") { if chunk.Type == sdk.ChunkDone { break } if chunk.Error != nil { break } fullText += chunk.Text fmt.Print(chunk.Text) // Show progress}Multi-Turn Context
Section titled “Multi-Turn Context”The SDK maintains conversation history:
// Turn 1resp1, _ := conv.Send(ctx, "My name is Alice")
// Turn 2 - context rememberedresp2, _ := conv.Send(ctx, "What's my name?")fmt.Println(resp2.Text()) // "Your name is Alice."Message History
Section titled “Message History”Get All Messages
Section titled “Get All Messages”messages := conv.Messages()for _, msg := range messages { fmt.Printf("[%s] %s\n", msg.Role, msg.Content)}Clear History
Section titled “Clear History”conv.Clear() // Starts freshTimeouts
Section titled “Timeouts”With Context Timeout
Section titled “With Context Timeout”ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)defer cancel()
resp, err := conv.Send(ctx, "Tell me a long story")if errors.Is(err, context.DeadlineExceeded) { log.Println("Request timed out")}Error Handling
Section titled “Error Handling”resp, err := conv.Send(ctx, message)if err != nil { switch { case errors.Is(err, sdk.ErrConversationClosed): log.Fatal("Conversation was closed") case errors.Is(err, sdk.ErrProviderError): log.Printf("Provider error: %v", err) case errors.Is(err, context.DeadlineExceeded): log.Println("Request timed out") default: log.Printf("Send failed: %v", err) }}Sending Structured Messages
Section titled “Sending Structured Messages”String Message
Section titled “String Message”resp, _ := conv.Send(ctx, "Hello!")Message Type
Section titled “Message Type”import "github.com/AltairaLabs/PromptKit/runtime/types"
msg := types.Message{ Role: "user", Content: "Hello!",}resp, _ := conv.Send(ctx, msg)Complete Example
Section titled “Complete Example”package main
import ( "context" "fmt" "log"
"github.com/AltairaLabs/PromptKit/sdk")
func main() { conv, _ := sdk.Open("./app.pack.json", "chat") defer conv.Close()
ctx := context.Background()
// Multi-turn conversation questions := []string{ "What is machine learning?", "How is it different from AI?", "Give me an example.", }
for _, q := range questions { fmt.Printf("Q: %s\n", q)
resp, err := conv.Send(ctx, q) if err != nil { log.Printf("Error: %v", err) continue }
fmt.Printf("A: %s\n\n", resp.Text()) }}