Class SingleTurnConversation
- Namespace
- LMKit.TextGeneration
- Assembly
- LM-Kit.NET.dll
A class designed for handling single-turn question answering.
Unlike a multi-turn conversation service, it does not preserve context between questions and answers.
public sealed class SingleTurnConversation : IConversation, ITextGenerationSettings
- Inheritance
-
SingleTurnConversation
- Implements
- Inherited Members
Examples
Example: Basic single-turn question answering
using LMKit.Model;
using LMKit.TextGeneration;
using System;
// Load the language model
LM model = LM.LoadFromModelID("llama-3.2-1b");
// Create the single-turn conversation
SingleTurnConversation conversation = new SingleTurnConversation(model);
// Submit a question
var result = conversation.Submit("What is the capital of France?");
Console.WriteLine(result.Completion);
Console.WriteLine($"Tokens generated: {result.CompletionTokens}");
Example: Custom system prompt and sampling
using LMKit.Model;
using LMKit.TextGeneration;
using LMKit.TextGeneration.Sampling;
using System;
LM model = LM.LoadFromModelID("llama-3.2-1b");
SingleTurnConversation conversation = new SingleTurnConversation(model);
// Customize the system prompt
conversation.SystemPrompt = "You are a helpful coding assistant. Provide concise code examples.";
// Configure sampling for more deterministic output
conversation.SamplingMode = new TopPSampling { TopP = 0.9f, Temperature = 0.7f };
// Limit completion length
conversation.MaximumCompletionTokens = 500;
var result = conversation.Submit("Write a function to calculate factorial in Python.");
Console.WriteLine(result.Completion);
Remarks
Use this class when you need independent question-answer interactions where each request is processed without memory of previous exchanges. This is ideal for:
- Simple Q&A systems
- Text completion tasks
- One-off queries that don't require conversation history
- Parallel processing of independent requests
Key Features
- Customizable system prompt via SystemPrompt
- Token sampling control via SamplingMode
- Repetition penalty configuration via RepetitionPenalty
- Grammar-constrained generation via Grammar
- Stop sequences for controlled output termination
- Multimodal input support (text and images)
For conversations that need to maintain context across multiple exchanges, use MultiTurnConversation instead.
Constructors
- SingleTurnConversation(LM)
Creates an instance of the SingleTurnConversation class.
- SingleTurnConversation(LM, ITextGenerationSettings)
Initializes a new SingleTurnConversation instance using the specified language model and text-generation settings. This constructor configures the conversation for single-turn interactions, applying any provided ITextGenerationSettings.
Properties
- Grammar
Gets or sets the Grammar object used to enforce grammatical rules during text generation. This ensures controlled and structured output from the model.
- InferencePolicies
Gets the InferencePolicies instance that defines how inference is conducted for this single-turn conversation. This includes settings such as how to handle context overflow and input length overflow during generation.
- LogitBias
A LogitBias object designed to adjust the likelihood of particular tokens (or text chunks) appearing during text completion.
- MaximumCompletionTokens
Defines the maximum number of tokens (text chunks) permitted for text completion or generation.
- MaximumContextLength
Gets or sets the maximum context length for inputs to the model.
- MaximumInputTokens
Specifies the maximum number of tokens allowed as input to the model.
- Model
Gets the Model instance associated with this object.
- ReasoningLevel
Controls how (and whether) intermediate "reasoning"/"thinking" content is produced and/or exposed.
Use None to fully disable reasoning. Higher levels hint the model to allocate more budget to chain-of-thought style tokens when the model supports it. Support depends on model and template capabilities.
Typical semantics: Level Intended behavior None No reasoning tokens requested or exposed. Low Minimal reasoning; terse scratch space when helpful. Medium Balanced reasoning (default if enabled). High Maximize reasoning depth; may trade off speed.
- RepetitionPenalty
A RepetitionPenalty object specifying the rules for repetition penalties applied during text completion.
- SamplingMode
A TokenSampling object specifying the sampling strategy followed during text completion.
- StopSequences
Specifies a set of sequences for which the API will stop generating additional tokens (or text chunks). The resultant text completion will exclude any occurrence of the specified stop sequences.
- SystemPrompt
Specifies the system prompt that is applied to the model before forwarding the user's request.
Methods
- Submit(Message, CancellationToken)
Prompts the model with a structured request encapsulated in a ChatHistory.Message object.
- Submit(string, CancellationToken)
Submits a request to the model for text generation.
- SubmitAsync(Message, CancellationToken)
Prompts the model with a structured request encapsulated in a ChatHistory.Message object (asynchronously).
- SubmitAsync(string, CancellationToken)
Submits a request to the model for text generation.
Events
- AfterTextCompletion
This event is triggered following the execution of a text completion.
- AfterTokenSampling
This event is triggered just after the generation of a token.
The provided AfterTokenSamplingEventArgs argument enables detailed modifications to be made to the token selection process.
- BeforeTokenSampling
This event is triggered just prior to the generation of a token.
The provided BeforeTokenSamplingEventArgs argument allows for precise adjustments to the token sampling process.