Skip to content

Completions ​

Overview ​

Completions provide a lower-level API for text generation compared to the Chat API. While the Chat API is recommended for most use cases, Completions can be useful for specific scenarios like code generation, text completion, or when you need more direct control over the generation process.

Quick Start ​

csharp
using LlmTornado;
using LlmTornado.Completions;

TornadoApi api = new TornadoApi("your-api-key");

// Simple completion
CompletionResult? result = await api.Completions.CreateCompletion(
    "Once upon a time", 
    maxTokens: 100);

Console.WriteLine(result?.Choices?[0].Text);

Prerequisites ​

  • The LlmTornado package installed
  • A valid API key
  • Understanding of token limits and costs

Basic Usage ​

Text Completion ​

csharp
CompletionResult? result = await api.Completions.CreateCompletion(
    new CompletionRequest
    {
        Prompt = "The future of AI is",
        MaxTokens = 150,
        Temperature = 0.7
    });

Console.WriteLine(result?.Choices?[0].Text);

Multiple Completions ​

csharp
CompletionResult? result = await api.Completions.CreateCompletion(
    new CompletionRequest
    {
        Prompt = "Write a tagline for an ice cream shop:",
        MaxTokens = 50,
        N = 3  // Generate 3 alternatives
    });

foreach (Choice choice in result.Choices)
{
    Console.WriteLine($"Option: {choice.Text}");
}

Best Practices ​

  1. Use Chat API When Possible - The Chat API is generally more flexible
  2. Control Token Usage - Set appropriate MaxTokens limits
  3. Experiment with Temperature - Adjust creativity vs consistency
  4. Handle Stop Sequences - Use stop tokens to control output

API Reference ​

CompletionRequest ​

  • string Prompt - Text prompt to complete
  • int? MaxTokens - Maximum tokens to generate
  • double? Temperature - Randomness (0.0-2.0)
  • int? N - Number of completions to generate

CompletionResult ​

  • List<Choice> Choices - Generated completions
  • Usage Usage - Token usage information