Open2

Flutterアプリで、Gemini APIに平易な英語の例文を作らせるために試したこと

kboy (Kei Fujikawa)kboy (Kei Fujikawa)

やりたかったこと

英単語帳アプリを作っていて、Geminiに例文を作ってもらっているのだが、たまに難しい単語を使った例文を出してくるので、調べたい単語以外の単語にはOxford3000に含まれている単語を使ってという指示を与えたかった。

Oxford3000の単語はネットにあるこちらからClaudeとか使ってjsonに変換。(jsonにすることで一覧表をアプリにも表示できた)

その変換したjsonからカンマ区切りのstringに変換したOxford3000の単語をGemini APIのプロンプトに投げてみた。以下みたいなプロンプト。

  Future<String> createExampleSentence(String word,
      {motherLanguage = 'ja'}) async {
    final languageName = Language.fromIsoCode(motherLanguage).name;

    final oxford3000 =
        await JsonRepository().getCommaDividedWordListString('oxford3000');
    print('oxford3000: $oxford3000');

    final prompt = '''
    Please provide three example sentences in English using the following word or phrase.
    
    ---
    $word
    ---
    
    Note that here's 3 conditions to create the sentences:
    
    1. If the word has multiple meanings or uses, show different meanings or uses in each example sentence. For instance, if the word can be used as both a verb and a noun, demonstrate each usage in separate sentences.
    
    2. Apart from the 「$word」, use word from 「$oxford3000」 as much as possible because the words I provided is from Oxford3000 and it's appropriate for studying English .
    
    3. Leave one blank line between each example sentence. Do not include example numbers such as 1, 2, 3 before the sentences. Also, provide a $languageName translation below each example sentence.
    
    For example, if the word you want to look up is “word,” with Japanese translation, the output should be as follows.
    
    ---
    The word "hello" is used to greet someone.
    「hello」という言葉は誰かに挨拶するために使われます。
    
    The doctor used a medical word that I didn't understand.
    医者は、私が理解できない医学用語を使いました。
    
    Please choose your words carefully.
    あなたの言葉は慎重に選んでください。
    ---
    ''';
    return await _generateContent(prompt) ??
        'No response because Gemini API went wrong.';
  }

結果

結果的に、投げた単語が多すぎたのか、「この単語を使え」以外のプロンプト無視された感じだったので、この方法はやめて、普通に 2. Apart from the 「$word」, use easy words (like contained in Oxford3000) as well as possible. みたいなプロンプトに戻した。

kboy (Kei Fujikawa)kboy (Kei Fujikawa)

多分やりたいことやるには、2往復くらいして、一回一回単語リストと照合して、もしOxford3000にない難しい単語使ってたら、やり直してくださいっていうとかかな?

実は裏でAPIを2回叩くってのは悪くないのかもしれない