DEV Community

Thom
Thom

Posted on • Edited on

Text-to-Code for ServiceNow

In this blog post, I want to share some good practices for code generation in ServiceNow and some hints about prompt engineering.

Code Generation

Generate programming code has become one of the most popular applications of LLMs, and on the ServiceNow platform, you can generate ServiceNow-specific javascript code through an LLM.

Firstly, the Model that is used for code generation (as of April 2024) is a fine-tuned version of StarCoder (model card). StarCoder 2 was released on February 28, 2024 (StarCoder 2 press release).

StarCoder was trained using sentinel tokens to enable support for Fill In the Middle (FIM) style prompts, and it also expected prompts in a certain format for code generation and code completion.

This means if you create a new prompt, it will use the above code for the context.

Let's go into ServiceNow and create our first prompt. Almost all script editors in ServiceNow now support text-to-code. These steps are based on the assumption, that you already activated the Text-to-code capability in the Now Assist admin console.

Our first try:

first prompt

To write a prompt you can use the comment function. Either you do the "//prompt" or for longer prompts, you can also use the "/* prompt here */". Once the prompt is completed hitting ctrl + Enter will then trigger Now Assist to generate the code.

Every output comes first as a draft (greyed out), so you can refine your prompt or accept it by pressing "tab".

Adding instructions to the prompt

I found out, that if you give the LLM instructions, you can get better results. I like to add the variable name to my prompt like this:

instructions

With that, you can incorporate name conventions into your prompts and get better results. As a general rule of thumb, and this applies to LLMs usage in general too, always ask yourself – could an intern at my company carry out this task based on the description that was given in the prompt?

Fill in the middle

As the model was trained to support code that was written before, it will also use parts above the prompt, which can look like this:

FIM

Here I just referred to the array, and it knew I meant the fruits array.

Those are a couple of tricks I learned when using the Text-to-code features (Now Assist for Creator) over the last weeks. I will try to update this post if I learn more.

Happy coding :)

P.S. Credit to my colleague Badrul Alom for the review and feedback

Top comments (0)