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.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay