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
- Use Chat API When Possible - The Chat API is generally more flexible
- Control Token Usage - Set appropriate
MaxTokenslimits - Experiment with Temperature - Adjust creativity vs consistency
- Handle Stop Sequences - Use stop tokens to control output
API Reference
CompletionRequest
string Prompt- Text prompt to completeint? MaxTokens- Maximum tokens to generatedouble? Temperature- Randomness (0.0-2.0)int? N- Number of completions to generate
CompletionResult
List<Choice> Choices- Generated completionsUsage Usage- Token usage information
Related Topics
- Chat Basics - Recommended chat interface
- Models - Available models