Granite LLM: Summarize and Generate Code
Key Points
- IBM Granite, accessed via Watson Studio, lets developers use a large language model (Granite 13B chat v2) to quickly summarize the purpose, variables, and functions of a code snippet, aiding onboarding and collaboration.
- When presented with larger code structures like a class, Granite not only provides a concise summary but also partially re‑formats the code for clearer readability, giving subsequent developers a clear jumping‑off point.
- Beyond summarization, Granite can generate new code (e.g., functions for specific tasks), addressing developers’ need for trustworthy, reliable AI‑produced code within a familiar IDE environment.
- The workflow emphasizes iterative prompting—starting with simple summarization, moving to more complex examples, and then to code generation—demonstrating how LLMs can streamline code review, documentation, and creation.
- Overall, Granite serves as an integrated development assistant that enhances code understanding, maintains consistency across team changes, and supports rapid development through AI‑driven code generation.
Sections
- Granite AI Code Summarization Demo - The speaker demonstrates how IBM Granite’s large language model in Watson Studio can generate concise explanations of code snippets, helping developers quickly understand and share code functionality.
- Prompting AI for Ruby Login - The speaker demonstrates how developers can specify their role and ask an AI to generate targeted code—here, a Ruby class for website login—illustrating rapid, function-focused development without building full classes or extensive searches.
Full Transcript
# Granite LLM: Summarize and Generate Code **Source:** [https://www.youtube.com/watch?v=1GWac3Vi5uo](https://www.youtube.com/watch?v=1GWac3Vi5uo) **Duration:** 00:06:09 ## Summary - IBM Granite, accessed via Watson Studio, lets developers use a large language model (Granite 13B chat v2) to quickly summarize the purpose, variables, and functions of a code snippet, aiding onboarding and collaboration. - When presented with larger code structures like a class, Granite not only provides a concise summary but also partially re‑formats the code for clearer readability, giving subsequent developers a clear jumping‑off point. - Beyond summarization, Granite can generate new code (e.g., functions for specific tasks), addressing developers’ need for trustworthy, reliable AI‑produced code within a familiar IDE environment. - The workflow emphasizes iterative prompting—starting with simple summarization, moving to more complex examples, and then to code generation—demonstrating how LLMs can streamline code review, documentation, and creation. - Overall, Granite serves as an integrated development assistant that enhances code understanding, maintains consistency across team changes, and supports rapid development through AI‑driven code generation. ## Sections - [00:00:00](https://www.youtube.com/watch?v=1GWac3Vi5uo&t=0s) **Granite AI Code Summarization Demo** - The speaker demonstrates how IBM Granite’s large language model in Watson Studio can generate concise explanations of code snippets, helping developers quickly understand and share code functionality. - [00:03:05](https://www.youtube.com/watch?v=1GWac3Vi5uo&t=185s) **Prompting AI for Ruby Login** - The speaker demonstrates how developers can specify their role and ask an AI to generate targeted code—here, a Ruby class for website login—illustrating rapid, function-focused development without building full classes or extensive searches. ## Full Transcript
Today, we'll be taking a look at using generative AI
to do some developer tasks and help us to work with our existing code.
For this we'll be using IBM Granite.
It's a model based development environment built to provide developers
with the tools to create, test and deploy machine learning models.
It also provides an integrated development environment
for building applications using natural language processing
or NLP and text analytics.
Here are some ways you can leverage the
Granite LLM when working with code.
The first example we'll use is code summarization.
So, to help fellow coders summarize the main points of a code snippet.
So not much code, but kind of you need to get the idea over to them.
So come over to our Watson studio.
And you'll notice that I'm using Granite 13B chat v2.
So this is one of the latest versions of Granite.
And I'll simply put in summarize the main points of the following code snippet.
In this case, I'm not telling you what language it's in.
Not really telling what it's doing.
I'm asking it to explain to me what this is doing.
So prompts like this can help you highlight
key concept variables and functions.
And this is just a simple code snippet.
As you see Granite, it gives back a lot of information explaining
what each variable is doing, what the function is doing,
what the overall code snippet should be doing,
and summarize it for you.
This is helpful when you're working on a specific piece of code
that may see other people jumping in and making changes.
The need to understand all of these pieces to ensure success.
So let's take a look at something a little bit bigger.
Let's take a look at an example using a class.
So first I'll hit new prompt.
And I don't want to say this, usually I'd want to save,
but in this particular case, because we're just doing this, for example,
we're not trying to build on what we've previously done before.
So this this particular situation, instead of a code snippet,
we'll get a beefier class in there and ask it to be summarized by Granite.
We'll hit send. And so as you can see, the first thing you'll notice
is that it formatted the code of the code a little bit.
Not completely, but a little bit so it's a little more clear when we're looking at it.
And as we can see here, the entire class is summarized
in a way that makes its purpose completely clear to the next developer.
Very clear, very concise.
And it could be be used as a jumping point for the next piece.
So..
The next group of people to come along will now
understand our code snippet and our class.
But let's take a look at generating code.
A lot of developers are already familiar with this concept.
It's not new, but it's also not used universally.
The concept of using AI in LLM's to generate code.
So when it comes to LLM's, they really want to focus on code reliability.
And developers want to be able to gain trust
in the various activities surrounding the generation of code.
So let's generate a function that achieves a specific task.
Again, we'll say new prompts..
We won't save.
In this case, we're going to ask it to generate a function
to concatenate any number of inputs by a user.
And in this case, we're going to be specific
and say use the elixir programing language.
So I'm going to hit send and Granite's
Going to say, okay, great, you want elixir, We're going to give you Elixir.
And it's going to build some functions that actually
help us to do exactly what we were asking to do.
This is helpful for data processing or algorithm implementation.
Leveraging the AI's response can help you avoid
long searching for answers from sources that are outdated or don't solve your specific problem.
And I think as developers, we all know what I'm referring to here.
So that's great.
We've generated a function, we're not generating a class
to model a specific concept.
Like, we can use that - we can write our prompt
and it could even get it to write tests for frameworks like Cucumber and Mini Test.
But for this example, what we're actually going to ask is just can generate some code
for something I do every day or something I do regularly,
I should say, which is building a log, a log in for a website.
So a new prompt.
I'm not going to save it.
I'll come in and say, Hey, as a developer, generate a
Ruby class to model logging into a website.
And this test case I'm saying as developer because just to be sure,
Ruby is a common word, it's a common English word that's used for other things.
I'm letting you know that I'm looking for programing information.
I want to do this as a developer.
That's my role.
So now I want this to be the AI's role.
So, hey, Granite, you're a developer now.
Congratulations.
Now model me some logging for a website using Ruby.
And I'll enter it in.
And as you can see, they'll start building login servers
using all of the rules that are that are important to Ruby.
You'll notice that with our Elixir example and use the rule specific to Elixir.
Now with Ruby, it's using the rule specific to Ruby.
And we can complete, you know, any version want.
It could get very like start up-y, it can get very,
you know, to a place where we want to begin. We want to not start from scratch.
We want to build on the things that we already know,
but we don't want to actually have to write every single line of that code every single day.
We want to be able to build.
So finally, we have an example of getting to the point where we want to complete our code.
So this example, we're going to use JavaScript and this prompt is going to help us to generate missing code from a snippet.
So kind of bringing this background at the beginning
where we had a code snippet and we understood what it did,
but we want other people to understand. This time we have a code snippet.
It's not really doing what we want,
so we're going to ask Granite to help us complete that code snippet.
We say complete this code snippet and because it's JavaScript, it already knows.
So we go ahead and send it.
And as you can see, it starts to say, okay, I think
I understand what you're trying to do here.
Let me explain what's being done and then
actually try to fix the things that need to be done.
So in this particular situation, it's going to serialize things, it's going to build,
is going to function and build the function that you want
so you can get over that finish line.
And this is a fun use of when you need to get things over the finish line.
It can give you the opportunity to build on code examples that you have,
or maybe partially functional code snippets and get to a place where you have
complete code that's actually functional and ready for production.
These are just a few examples of how you can use
Granite as a code research and generation tool.
By leveraging the power of Granite LLMs, you can
streamline your coding workflow, improve code quality
and accelerate your development process.