DEV Community

sugiarto
sugiarto

Posted on

Generate tags using gemini AI

A week ago, I was working on a Flutter project to generate tags based on a post sentence.

The requirement was to generate tags separated by commas using the Gemini API.

Todo that, I used the flutter_gemini package to call the Gemini API.

Here is sample code to call the API.

// main.dart
Gemini.init(apiKey: dotenv.env['GEMINI_API_KEY']!);
Enter fullscreen mode Exit fullscreen mode
final sampleText = "Thanks for the iPhone and food."
final gemini = Gemini.instance;
gemini.streamGenerateContent("For this text, return only the name of people or things (separated by commas), that you're highly confident that has a very high positive sentiment: $sampleText").listen((Candidates value){
  if (value?.content?.parts != null){
    if (value!.content!.parts!.isNotEmpty){
      String? text = value!.content!.parts![0].text;
      if (text != null){
        var parsedTags = text.split(", ");
        setState(() {
          for (String tag in parsedTags){
            // push to tags variable
            if (!tags.contains(tag)){
              tags.add(tag);
            }
          }
        });
      }
    }
  }
  print(value);
}).onError((error){
  print('streamGenerateContent exception $error');
});
Enter fullscreen mode Exit fullscreen mode

The result would be a String with content iPhone, food.

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay