The feature is particularly useful when working with complex codebases or when trying to achieve specific outcomes in the generated code. By providing these instructions, users can ensure that the AI model produces code that meets their exact requirements and specifications.
Inline instruction is a special mechanism that allows users to provide specific guidance to AI models during the process of source code related tasks.
One key benefit of using inline instruction is that it allows users to fine-tune the output of the AI model without having to manually edit the code afterwards. This can save time and effort, as users can quickly and easily make adjustments to the generated code without having to start from scratch.
Users are able to give instructions on how certain parts of the code should be modified or optimized by using <ai>...</ai>
tags in comments within the code.
For example, the instruction tells to CodePorting AI engine to rename Python function crc_check to verifyCrc using Python to Java converter and Model1:
# <ai>Rename function crc_check to verifyCrc</ai>
def crc_check(data, div):
...
return crc
the converter's output is Java code that have verifyCrc method instead of crc_check or crcCheck:
// This code is a translation from Python to Java
public class CrcVerifier {
public static int verifyCrc(byte[] data, byte[] divisor) {
// Implementation of CRC check
int crc = 0; // Placeholder for CRC calculation
// Add CRC calculation logic here
return crc;
}
}
Here is another example of how to make AI Engine to create a scaffold code using Python to Java converter and Model1:
# <ai>Rename function crc_check to verifyCrc</ai>
# <ai>Create class SecurityServiceImpl and ISecurityService interface</ai>
def crc_check(data, div):
...
return crc
as result, the output code contains ISecurityService interface implemented by SecurityServiceImpl:
// This code is translated from Python to Java
public interface ISecurityService {
int verifyCrc(byte[] data, byte[] div);
}
public class SecurityServiceImpl implements ISecurityService {
@Override
public int verifyCrc(byte[] data, byte[] div) {
// Implement the CRC verification logic here
int crc = 0;
// ...
return crc;
}
}
Here is an example of how to add logging to the class using using Python to Java converter and Model1:
# <ai>Rename function crc_check to verifyCrc</ai>
# <ai>Create class SecurityServiceImpl and ISecurityService interface with application context dependency in ctor</ai>
# <ai>Add logging dependency the the class</ai>
def crc_check(data, div):
...
# <ai>throw an exception instead of returning result</ai>
return crc
as result, a logger is injected via constructor of SecurityServiceImpl :
// This code is a translation from Python to Java, implementing a security service with a CRC verification function.
public interface ISecurityService {
// Define methods that this interface will expose
}
public class SecurityServiceImpl implements ISecurityService {
private final Logger logger;
public SecurityServiceImpl(Logger logger) {
this.logger = logger;
}
public void verifyCrc(byte[] data, byte[] div) throws Exception {
// Implementation details here...
// throw an exception instead of returning result
throw new Exception("CRC verification failed");
}
}