Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa

 Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa

monochromegane

October 23, 2024
Tweet

More Decks by monochromegane

Other Decks in Programming

Transcript

  1. ࡾ୐༔հ / Pepabo R&D Institute, GMO Pepabo, Inc. 2024.10.23 Fukuoka.go#20

    GoݴޠͰλʔϛφϧϑϨϯυϦʔͳ AIίϚϯυafaΛͭͬͨ͘
  2. • ੜ੒AIͷ࣮༻Խʹ൐͍ɺ։ൃ؀ڥͱͷ౷߹͕ਐΜͰ͍Δ • ౰ॳͷੜ੒AIαʔϏεʹΑΔίʔσΟϯάࢧԉ͸ɺϓϥάΠϯతͳҐஔ෇͚Ͱ ͋ͬͨ • ྫ: (Vim, Emacs, VSCode

    etc…) + GitHub Copilot ͳͲ • ۙ೥Ͱ͸ɺίʔσΟϯάࢧԉޮ཰ͷ޲্ͷͨΊɺ͜ΕΒͷػೳ͕༧Ί૊Έࠐ· Εͨ։ൃ؀ڥʢΤσΟλ΍λʔϛφϧΞϓϦʣ͕ओྲྀʹͳ͍ͬͯΔ • ྫ: Cursor, Warp ͳͲ  4 എܠ
  3. • ᶃ ։ൃࢧԉػೳͱ։ൃ؀ڥͷີ݁߹ • ར༻͍ͨ͠ࢧԉػೳʹΑͬͯ։ൃ؀ڥͦͷ΋ͷͷҠߦ͕ٻΊΒΕΔ • ༗ྗͳ։ൃ؀ڥ͸͋Δ͕ɺ·ͩ·ͩ৽͍͠΋ͷ͕ग़ݱ͢ΔՄೳੑ͸ߴ͍ • ᶄ ։ൃࢧԉػೳͷ։ൃ؀ڥ΁ͷแؚ

    • ར༻͍ͨ͠ࢧԉػೳͷͨΊʹɺͦͷ։ൃ؀ڥʢΤσΟλɺλʔϛφϧʣΛ։ ͖ɺͦͷ؀ڥͷྲّྀʹԊͬͯ݁ՌΛಘΔ • ͦͷ؀ڥ୯ମͰॲཧ͕׬݁͠ͳ͍৔߹ɺ֎෦؀ڥͱͷ࿈ܞʹ͸Ұखؒඞཁ  5 ՝୊
  4. • ີ݁߹΍แؚʹىҼ͢Δ՝୊͸ɺUNIX఩ֶͱͷ૬ҧ͔Βઆ໌Ͱ͖ͦ͏  6 UNIX఩ֶ w • Write programs that

    do one thing and do it well. Ұͭͷ͜ͱΛ͏·͘΍ΔϓϩάϥϜΛॻ͘ • Write programs to work together. ࿈ܞ͢ΔϓϩάϥϜΛॻ͘ • Write programs to handle text streams, because that is a universal interface. ීวతͳΠϯλʔϑΣʔεͰ͋ΔςΩετετϦʔϜΛѻ͏ϓϩάϥϜΛॻ͘ IUUQTFOXJLJQFEJBPSHXJLJ6OJY@QIJMPTPQIZ → λʔϛφϧϑϨϯυϦʔͳAIίϚϯυͰղܾ
  5. • ੜ੒AIαʔϏε΁ͷਪ࿦ΛίϚϯυϥΠϯͰ࣮ߦ͢Δπʔϧ • ೖग़ྗͱͯ͠ςΩετετϦʔϜΛલఏͱ͓ͯ͠ΓɺύΠϓ΍ϦμΠϨΫτΛ ༻͍ͯଞͷίϚϯυͱ࿈ܞ͠΍͍͢ͷ͕ಛ௃  8 AFA: λʔϛφϧϑϨϯυϦʔͳAIίϚϯυ $

    echo $ERROR_MESSAGE | afa -p "What is happening?" /path/to/file1 /path/to/file2 • AFA͸ɺAI for AllͷུɻશͯͷίϚϯυͱ࿈ܞͰ͖Δͱ͍͏໨ඪΛදݱ •  AI for All := ∀x ∈ X, ∃AI(x)
  6.  9 AFA: λʔϛφϧϑϨϯυϦʔͳAIίϚϯυ $ echo $ERROR_MESSAGE | afa -p

    "What is happening?" /path/to/file1 /path/to/file2 4ZTUFN 6TFS  QSPNQUUFNQMBUF {{ .Message }} {{ if .MessageStdin }} ``` {{ .MessageStdin }}``` {{- end }} {{ range .Files }} - File: {{ .Name }} ``` {{ .Content }}``` {{ end -}} BGB 4UEJO 'JMF 1SPNQU (FOFSBUJWF"* 4FSWJDF 4UEPVU
  7. • ݸผػೳͷϓϩάϥϜΛ࣮૷͢ΔͷͰ͸ͳ͘ϓϩϯϓτͰࢦࣔ͢Δ  10 AIΛલఏͱͨ͠UNIX఩ֶ w • Prompt programs that

    do one thing and do it well. Ұͭͷ͜ͱΛ͏·͘΍ΔϓϩάϥϜΛࢦࣔ͢Δ • Prompt programs to work together. ࿈ܞ͢ΔϓϩάϥϜΛࢦࣔ͢Δ • Prompt programs to handle text streams, because that is a universal interface. ීวతͳΠϯλʔϑΣʔεͰ͋ΔςΩετετϦʔϜΛѻ͏ϓϩάϥϜΛࢦࣔ͢Δ
  8. • ΫϦοϓϘʔυͷத਎Λஔ͖׵͑  11 ϓϩϯϓτʹΑΔಈతίϚϯυͱͯ͠ͷAFA $ pbpaste | afa -p

    “Ϗδωεϝʔϧͷจମʹͯ͠” | pbcopy • ϑΝΠϧͷઆ໌ afa -p “঎༻ར༻ͷ؍఺Ͱઆ໌ͯ͠” LICENSE.md • ϫϯϥΠφʔͷఏҊ afa -p “JSONϑΝΠϧΛྻڍ” -j suggestion | jq -r ‘.cmd’
  9. • OpenAIͷStructured outputΛޮՌతʹ࢖͏͜ͱͰෆཁͳઆ໌΍ίʔυϒϩο Ϋه๏ͷআڈ͕ෆཁͱͳΓɺग़ྗ݁Ռͷ࿈ܞੑ͕޲্  13 Structured Outputͷαϙʔτ $ cat

    <<< EOS > CONFIG_PATH/schemas/command_suggestion.json { "type": "object", "properties": { "suggested_command": { "type": "string" } }, "additionalProperties": false, "required": [ "suggested_command" ] } EOS $ afa -j command_suggestion -p "JSONϑΝΠϧΛྻڍ" | jq -r '.suggested_command'
  10.  14 σϞ: λʔϛφϧ্ͰͷίϚϯυαδΣετ Zsh Line Editor (ZLE)ͱ૊Έ߹ ΘͤΔ͜ͱͰɺόοϑΝΛೖྗ ͱͯ͠ϫϯϥΠφʔΛαδΣε

    τ͠ɺόοϑΝΛஔ׵ɻ ίϚϯυͷΈΛऔಘ͢ΔͨΊʹ Structured outputΛ׆༻ https://github.com/monochromegane/afa/blob/main/examples/command_suggestion.gif
  11.  15 σϞ: ΤσΟλ্ͰͷίʔυαδΣετ Vimͷൣғબ୒ͱ૊Έ߹ΘͤΔ ͜ͱͰɺબ୒ൣғΛೖྗͱͯ͠ ࢦࣔʹج͍ͮͨमਖ਼Ͱஔ׵ɻ ίϯςΩετͱͯ֘͠౰ϑΝΠ ϧ໊ͱϑΝΠϧશମͷ಺༰΋౉ ͍ͯ͠ΔʢCursorͷ@symbol

    తͳڍಈʣ ൺֱૢ࡞΋ख׳ΕͨΤσΟλͰ ͋Ε͹༻ҙͳ͸ͣ ͜Ε΋ɺमਖ਼ίʔυͷ݁ՌͷΈΛऔಘ ͢ΔͨΊʹStructured outputΛ׆༻ https://github.com/monochromegane/afa/blob/main/examples/code_suggestion.gif
  12. • λʔϛφϧʹදࣔ͞ΕͨΤϥʔϝοηʔδͷ෼ੳ • tmuxͷcapture-paneΛ࢖ͬͯ௚ۙͷ࣮ߦ݁ՌΛ෼ੳɻෳ਺ߦʹΘͨΔΤϥʔ ϝοηʔδͷίϐϖͷख͕ؒল͚ͯศར • GitHubͷϓϧϦΫΤετͷࣗಈੜ੒ • git diffͱgit

    logͷ৘ใΛݩʹλΠτϧͱཁ໿Λੜ੒ͯ͠ϓϧϦΫΤετΛ։͘ • pecoΛ༻͍ͨηογϣϯཤྺͷߜΓࠐΈͱಡΈࠐΈ • ΠϯλϥΫςΟϒͳߜΓࠐΈͰૉૣ͘աڈͷηογϣϯʹΞΫηε  18 ͦͷଞͷ࣮ྫ
  13. • ঺հΤϯτϦ • https://blog.monochromegane.com/blog/2024/09/16/afa/ • ϦϙδτϦ • https://github.com/monochromegane/afa • σϞʹ࢖ͬͨςϯϓϨʔτ΍ઃఆ΋ܝࡌ͍ͯ͠·͢

    • Πϯετʔϧ͸ͪ͜Β  19 ࢖ͬͯΈͯͶ $ brew install monochromegane/tap/afa monochromegane/tap/afa-tui $ afa init
  14. • ϦϦʔεϫʔΫϑϩʔʹ͸ɺSongmu/tagprΛ࠾༻ • ϦϦʔε༻ͷpull requestΛࣗಈ࡞੒͠ɺϚʔδ͞ΕͨΒࣗಈͰλάΛଧͭtagpr • ϦϦʔεϓϩηεΛεςʔτϨεʹ͢Δମݧͷྑ͞ • ͦͷଞɺϏϧυ΍όʔδϣϯ؅ཧ΋ɺSongmu/gocredits, Songmu/goxz,

    x-motemen/ gobump, tcnksm/ghr Λ࢖ͬͯݟ௨͠Α͘Ͱ͖ͨ • Homebrewͷtap؅ཧ͸@k1lowํࣜΛ࠾༻ • homebrew-tapϦϙδτϦ͔ΒͷϓϧܕʹΑΓɺݖݶ؅ཧΛ؆ૉԽͰ͖ͨ  21 CI
  15. • λʔϛφϧૢ࡞ͷ࿥ըʹ͸charmbracelet/vhsΛ࠾༻ • https://github.com/charmbracelet/vhs • Goݴޠ࣮૷ • ೖྗ಺༰ΛઃఆϑΝΠϧʹهड़ͯ͠࠶ੜɾ࿥ը͢ΔͷͰۓு͠ͳ͍ • zshrcͷಡΈࠐΈͳͲΛ࣮ࢪ͢Δ͜ͱͰϩʔΧϧ؀ڥͱͷ෼཭΋༰қ

     22 Demo Type "# AFA: Code suggestions using Vim." Sleep 500ms Ctrl+U Sleep 500ms Type "vim option.go" Sleep 500ms Enter Sleep 1s Type@50ms "jj" Sleep 500ms Type@20ms "11V" Sleep 500ms Type ":Afa" Sleep 500ms Enter