Skip to content

Latest commit

 

History

History
111 lines (87 loc) · 2.94 KB

README.md

File metadata and controls

111 lines (87 loc) · 2.94 KB

TabbyAPI Services

This section provides detailed documentation for each of the services available in the go-tabbyapi client library.

Available Services

Accessing Services

Each service is accessible through the main client interface:

// Create a client
client := tabby.NewClient(
    tabby.WithBaseURL("http://localhost:8080"),
    tabby.WithAPIKey("your-api-key"),
)

// Access a specific service
completionsService := client.Completions()
chatService := client.Chat()
modelsService := client.Models()
// ... and so on

Common Patterns

Most service methods follow these common patterns:

  1. Resource Creation:

    resource, err := service.Create(ctx, request)
  2. Resource Listing:

    list, err := service.List(ctx)
  3. Resource Retrieval:

    resource, err := service.Get(ctx)
  4. Resource Management:

    err := service.Load(ctx, request)
    err := service.Unload(ctx)
  5. Streaming Operations:

    stream, err := service.CreateStream(ctx, request)
    defer stream.Close()
    
    for {
        chunk, err := stream.Recv()
        if err == io.EOF {
            break
        }
        if err != nil {
            return err
        }
        // Process chunk
    }

Context Usage

All service methods accept a context.Context as their first parameter, which can be used for:

  • Timeouts: Set deadlines for API requests
  • Cancellation: Cancel in-progress requests
  • Request scoping: Associate values with the request lifecycle

Example with timeout:

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

resp, err := client.Completions().Create(ctx, request)

Error Handling

Service methods return strongly typed errors that implement the tabby.Error interface:

resp, err := client.Completions().Create(ctx, request)
if err != nil {
    var apiErr *tabby.APIError
    if errors.As(err, &apiErr) {
        // Handle API-specific errors
        log.Printf("API Error (Code: %s, Status: %d): %s", 
            apiErr.Code(), apiErr.HTTPStatusCode(), apiErr.Error())
    } else {
        // Handle other errors
        log.Printf("Error: %v", err)
    }
}