How to Send Messages

Learn how to send messages and receive responses.

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

Get Text

text := resp.Text()

Check for Tool Calls

if resp.HasToolCalls() {
    for _, call := range resp.ToolCalls() {
        fmt.Printf("Tool: %s\n", call.Name)
    }
}

Check for Pending Approvals

pending := resp.PendingTools()
if len(pending) > 0 {
    // Handle HITL approvals
}

Streaming

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

var fullText string
for 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

The SDK maintains conversation history:

// Turn 1
resp1, _ := conv.Send(ctx, "My name is Alice")

// Turn 2 - context remembered
resp2, _ := conv.Send(ctx, "What's my name?")
fmt.Println(resp2.Text())  // "Your name is Alice."

Message History

Get All Messages

messages := conv.Messages()
for _, msg := range messages {
    fmt.Printf("[%s] %s\n", msg.Role, msg.Content)
}

Clear History

conv.Clear()  // Starts fresh

Timeouts

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

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

String Message

resp, _ := conv.Send(ctx, "Hello!")

Message Type

import "github.com/AltairaLabs/PromptKit/runtime/types"

msg := types.Message{
    Role:    "user",
    Content: "Hello!",
}
resp, _ := conv.Send(ctx, msg)

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())
    }
}

See Also