Table of Contents

Class CodeModelBase<T>

Namespace
AiDotNet.ProgramSynthesis.Engines
Assembly
AiDotNet.dll

Base class for code models that provides shared tokenization, task dispatch, and structured outputs.

public abstract class CodeModelBase<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, INeuralNetwork<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable, ICodeModel<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>

Type Parameters

T

The numeric type used for calculations (e.g., double, float).

Inheritance
CodeModelBase<T>
Implements
IFullModel<T, Tensor<T>, Tensor<T>>
IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>
IParameterizable<T, Tensor<T>, Tensor<T>>
ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>
IGradientComputable<T, Tensor<T>, Tensor<T>>
Derived
Inherited Members
Extension Methods

Constructors

CodeModelBase(CodeSynthesisArchitecture<T>, ILossFunction<T>, ITokenizer?)

protected CodeModelBase(CodeSynthesisArchitecture<T> architecture, ILossFunction<T> lossFunction, ITokenizer? tokenizer = null)

Parameters

architecture CodeSynthesisArchitecture<T>
lossFunction ILossFunction<T>
tokenizer ITokenizer

Properties

CodeArchitecture

protected CodeSynthesisArchitecture<T> CodeArchitecture { get; }

Property Value

CodeSynthesisArchitecture<T>

MaxSequenceLength

Gets the maximum sequence length (in tokens) that the model can process.

public int MaxSequenceLength { get; }

Property Value

int

Remarks

Code models process code as sequences of tokens. This property specifies the maximum number of tokens the model can handle at once.

For Beginners: This is like the maximum length of code the model can read at once.

Code is broken into pieces called "tokens" (like words in a sentence). This number tells you the maximum number of tokens the model can process, which roughly corresponds to how long a code file can be.

TargetLanguage

Gets the target programming language for this model.

public ProgramLanguage TargetLanguage { get; }

Property Value

ProgramLanguage

Remarks

Specifies which programming language this model is designed to work with. Some models are language-specific, while others can work with multiple languages.

For Beginners: This tells you which programming language the model knows.

Like a translator who specializes in French or Spanish, code models often specialize in specific programming languages like Python or Java.

Tokenizer

protected ITokenizer Tokenizer { get; }

Property Value

ITokenizer

VocabularySize

Gets the vocabulary size of the model.

public int VocabularySize { get; }

Property Value

int

Remarks

The vocabulary consists of all the tokens (keywords, operators, identifiers, etc.) that the model knows and can work with.

For Beginners: This is like the model's dictionary size.

It tells you how many different code tokens the model knows. A larger vocabulary means the model can handle more diverse code patterns and identifiers.

Methods

CreateTransformerModelMetadata(string, IReadOnlyDictionary<string, object>?, string)

protected ModelMetadata<T> CreateTransformerModelMetadata(string modelName, IReadOnlyDictionary<string, object>? extraInfo, string optimizerName)

Parameters

modelName string
extraInfo IReadOnlyDictionary<string, object>
optimizerName string

Returns

ModelMetadata<T>

DecodeCode(Tensor<T>)

Decodes a vector representation back into source code.

public string DecodeCode(Tensor<T> encoding)

Parameters

encoding Tensor<T>

The encoded representation to decode.

Returns

string

The decoded source code as a string.

Remarks

Decoding transforms the model's internal numerical representation back into human-readable source code.

For Beginners: Decoding converts the AI's numerical format back to readable code.

After the AI processes code in numerical form, we need to convert it back to text that humans can read and computers can execute. This is the reverse of encoding.

EncodeCode(string)

Encodes source code into a vector representation.

public Tensor<T> EncodeCode(string code)

Parameters

code string

The source code to encode.

Returns

Tensor<T>

A tensor representing the encoded code.

Remarks

Encoding transforms source code (text) into a numerical representation that the model can process. This representation captures semantic information about the code.

For Beginners: Encoding converts code text into numbers the AI can understand.

Computers can't directly work with text, so we convert code into numerical form. This encoding captures the meaning of the code, not just the characters. Like translating emotions into emoji - different form, same meaning.

GetEmbeddings(string)

Gets embeddings for code tokens.

public virtual Tensor<T> GetEmbeddings(string code)

Parameters

code string

The source code to get embeddings for.

Returns

Tensor<T>

A tensor containing token embeddings.

Remarks

Embeddings are dense vector representations of code tokens that capture semantic similarities. Similar code constructs have similar embeddings.

For Beginners: Embeddings represent each piece of code as a point in space.

Code with similar meaning is placed close together in this space. For example, "for loop" and "while loop" would be near each other because they're both loops, but far from "function definition" because that's a different concept.

PerformBugDetection(CodeBugDetectionRequest)

protected virtual CodeBugDetectionResult PerformBugDetection(CodeBugDetectionRequest request)

Parameters

request CodeBugDetectionRequest

Returns

CodeBugDetectionResult

PerformBugFixing(CodeBugFixingRequest)

protected virtual CodeBugFixingResult PerformBugFixing(CodeBugFixingRequest request)

Parameters

request CodeBugFixingRequest

Returns

CodeBugFixingResult

PerformCloneDetection(CodeCloneDetectionRequest)

protected virtual CodeCloneDetectionResult PerformCloneDetection(CodeCloneDetectionRequest request)

Parameters

request CodeCloneDetectionRequest

Returns

CodeCloneDetectionResult

PerformCodeReview(CodeReviewRequest)

protected virtual CodeReviewResult PerformCodeReview(CodeReviewRequest request)

Parameters

request CodeReviewRequest

Returns

CodeReviewResult

PerformCompletion(CodeCompletionRequest)

protected virtual CodeCompletionResult PerformCompletion(CodeCompletionRequest request)

Parameters

request CodeCompletionRequest

Returns

CodeCompletionResult

PerformDocumentation(CodeDocumentationRequest)

protected virtual CodeDocumentationResult PerformDocumentation(CodeDocumentationRequest request)

Parameters

request CodeDocumentationRequest

Returns

CodeDocumentationResult

PerformGeneration(CodeGenerationRequest)

protected virtual CodeGenerationResult PerformGeneration(CodeGenerationRequest request)

Parameters

request CodeGenerationRequest

Returns

CodeGenerationResult

PerformRefactoring(CodeRefactoringRequest)

protected virtual CodeRefactoringResult PerformRefactoring(CodeRefactoringRequest request)

Parameters

request CodeRefactoringRequest

Returns

CodeRefactoringResult

PerformSearch(CodeSearchRequest)

protected virtual CodeSearchResult PerformSearch(CodeSearchRequest request)

Parameters

request CodeSearchRequest

Returns

CodeSearchResult

PerformSummarization(CodeSummarizationRequest)

protected virtual CodeSummarizationResult PerformSummarization(CodeSummarizationRequest request)

Parameters

request CodeSummarizationRequest

Returns

CodeSummarizationResult

PerformTask(CodeTaskRequestBase)

Performs a code-related task and returns a structured result type.

public CodeTaskResultBase PerformTask(CodeTaskRequestBase request)

Parameters

request CodeTaskRequestBase

The task request.

Returns

CodeTaskResultBase

A structured task result.

PerformTask(string, CodeTask)

Performs a code-related task on the input code.

[Obsolete("Use PerformTask(CodeTaskRequestBase) for structured outputs.")]
public string PerformTask(string code, CodeTask task)

Parameters

code string

The source code to process.

task CodeTask

The type of task to perform.

Returns

string

The result of the task as a string.

Remarks

This method allows the model to perform various code-related tasks such as completion, summarization, bug detection, etc. based on the specified task type.

For Beginners: This method lets you tell the model what to do with the code.

You provide code and specify what you want done with it:

  • Complete it
  • Summarize it
  • Find bugs
  • Generate documentation

The model then performs that specific task and returns the result.

PerformTestGeneration(CodeTestGenerationRequest)

protected virtual CodeTestGenerationResult PerformTestGeneration(CodeTestGenerationRequest request)

Parameters

request CodeTestGenerationRequest

Returns

CodeTestGenerationResult

PerformTranslation(CodeTranslationRequest)

protected virtual CodeTranslationResult PerformTranslation(CodeTranslationRequest request)

Parameters

request CodeTranslationRequest

Returns

CodeTranslationResult

PerformUnderstanding(CodeUnderstandingRequest)

protected virtual CodeUnderstandingResult PerformUnderstanding(CodeUnderstandingRequest request)

Parameters

request CodeUnderstandingRequest

Returns

CodeUnderstandingResult

Predict(Tensor<T>)

Makes a prediction using the neural network.

public override Tensor<T> Predict(Tensor<T> input)

Parameters

input Tensor<T>

The input data to process.

Returns

Tensor<T>

The network's prediction.

Remarks

For Beginners: This is the main method you'll use to get results from your trained neural network. You provide some input data (like an image or text), and the network processes it through all its layers to produce an output (like a classification or prediction).

TrainWithOptimizer(Tensor<T>, Tensor<T>, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>)

protected void TrainWithOptimizer(Tensor<T> input, Tensor<T> expectedOutput, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer)

Parameters

input Tensor<T>
expectedOutput Tensor<T>
optimizer IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>

UpdateParameters(Vector<T>)

Updates the network's parameters with new values.

public override void UpdateParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

The new parameter values to set.

Remarks

For Beginners: During training, a neural network's internal values (parameters) get adjusted to improve its performance. This method allows you to update all those values at once by providing a complete set of new parameters.

This is typically used by optimization algorithms that calculate better parameter values based on training data.