Learning Library

← Back to Library

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.

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
0:00Today, we'll be taking a look at using generative AI 0:02to do some developer tasks and help us to work with our existing code. 0:06For this we'll be using IBM Granite. 0:09It's a model based development environment built to provide developers 0:13with the tools to create, test and deploy machine learning models. 0:16It also provides an integrated development environment 0:19for building applications using natural language processing 0:22or NLP and text analytics. 0:25Here are some ways you can leverage the 0:27Granite LLM when working with code. 0:29The first example we'll use is code summarization. 0:32So, to help fellow coders summarize the main points of a code snippet. 0:36So not much code, but kind of you need to get the idea over to them. 0:40So come over to our Watson studio. 0:44And you'll notice that I'm using Granite 13B chat v2. 0:46So this is one of the latest versions of Granite. 0:49And I'll simply put in summarize the main points of the following code snippet. 0:54In this case, I'm not telling you what language it's in. 0:56Not really telling what it's doing. 0:57I'm asking it to explain to me what this is doing. 1:00So prompts like this can help you highlight 1:03key concept variables and functions. 1:05And this is just a simple code snippet. 1:07As you see Granite, it gives back a lot of information explaining 1:11what each variable is doing, what the function is doing, 1:14what the overall code snippet should be doing, 1:16and summarize it for you. 1:18This is helpful when you're working on a specific piece of code 1:21that may see other people jumping in and making changes. 1:23The need to understand all of these pieces to ensure success. 1:26So let's take a look at something a little bit bigger. 1:29Let's take a look at an example using a class. 1:31So first I'll hit new prompt. 1:33And I don't want to say this, usually I'd want to save, 1:35but in this particular case, because we're just doing this, for example, 1:38we're not trying to build on what we've previously done before. 1:41So this this particular situation, instead of a code snippet, 1:44we'll get a beefier class in there and ask it to be summarized by Granite. 1:49We'll hit send. And so as you can see, the first thing you'll notice 1:52is that it formatted the code of the code a little bit. 1:55Not completely, but a little bit so it's a little more clear when we're looking at it. 1:59And as we can see here, the entire class is summarized 2:02in a way that makes its purpose completely clear to the next developer. 2:06Very clear, very concise. 2:08And it could be be used as a jumping point for the next piece. 2:11So.. 2:13The next group of people to come along will now 2:15understand our code snippet and our class. 2:17But let's take a look at generating code. 2:20A lot of developers are already familiar with this concept. 2:23It's not new, but it's also not used universally. 2:27The concept of using AI in LLM's to generate code. 2:30So when it comes to LLM's, they really want to focus on code reliability. 2:34And developers want to be able to gain trust 2:36in the various activities surrounding the generation of code. 2:40So let's generate a function that achieves a specific task. 2:44Again, we'll say new prompts.. 2:46We won't save. 2:47In this case, we're going to ask it to generate a function 2:52to concatenate any number of inputs by a user. 2:56And in this case, we're going to be specific 2:57and say use the elixir programing language. 3:00So I'm going to hit send and Granite's 3:03Going to say, okay, great, you want elixir, We're going to give you Elixir. 3:06And it's going to build some functions that actually 3:08help us to do exactly what we were asking to do. 3:11This is helpful for data processing or algorithm implementation. 3:15Leveraging the AI's response can help you avoid 3:17long searching for answers from sources that are outdated or don't solve your specific problem. 3:22And I think as developers, we all know what I'm referring to here. 3:25So that's great. 3:27We've generated a function, we're not generating a class 3:30to model a specific concept. 3:32Like, we can use that - we can write our prompt 3:34and it could even get it to write tests for frameworks like Cucumber and Mini Test. 3:39But for this example, what we're actually going to ask is just can generate some code 3:43for something I do every day or something I do regularly, 3:46I should say, which is building a log, a log in for a website. 3:50So a new prompt. 3:51I'm not going to save it. 3:53I'll come in and say, Hey, as a developer, generate a 3:56Ruby class to model logging into a website. 3:59And this test case I'm saying as developer because just to be sure, 4:02Ruby is a common word, it's a common English word that's used for other things. 4:06I'm letting you know that I'm looking for programing information. 4:09I want to do this as a developer. 4:11That's my role. 4:12So now I want this to be the AI's role. 4:14So, hey, Granite, you're a developer now. 4:16Congratulations. 4:17Now model me some logging for a website using Ruby. 4:21And I'll enter it in. 4:22And as you can see, they'll start building login servers 4:25using all of the rules that are that are important to Ruby. 4:27You'll notice that with our Elixir example and use the rule specific to Elixir. 4:31Now with Ruby, it's using the rule specific to Ruby. 4:34And we can complete, you know, any version want. 4:36It could get very like start up-y, it can get very, 4:39you know, to a place where we want to begin. We want to not start from scratch. 4:43We want to build on the things that we already know, 4:45but we don't want to actually have to write every single line of that code every single day. 4:49We want to be able to build. 4:51So finally, we have an example of getting to the point where we want to complete our code. 4:57So this example, we're going to use JavaScript and this prompt is going to help us to generate missing code from a snippet. 5:03So kind of bringing this background at the beginning 5:05where we had a code snippet and we understood what it did, 5:07but we want other people to understand. This time we have a code snippet. 5:11It's not really doing what we want, 5:13so we're going to ask Granite to help us complete that code snippet. 5:16We say complete this code snippet and because it's JavaScript, it already knows. 5:20So we go ahead and send it. 5:22And as you can see, it starts to say, okay, I think 5:25I understand what you're trying to do here. 5:27Let me explain what's being done and then 5:30actually try to fix the things that need to be done. 5:33So in this particular situation, it's going to serialize things, it's going to build, 5:38is going to function and build the function that you want 5:40so you can get over that finish line. 5:43And this is a fun use of when you need to get things over the finish line. 5:46It can give you the opportunity to build on code examples that you have, 5:49or maybe partially functional code snippets and get to a place where you have 5:52complete code that's actually functional and ready for production. 5:56These are just a few examples of how you can use 5:58Granite as a code research and generation tool. 6:01By leveraging the power of Granite LLMs, you can 6:04streamline your coding workflow, improve code quality 6:07and accelerate your development process.