LangChainのGUI版、LangFlowを試してみた

# できたもの
Wikipediaの検索結果を用いて、計算させることができました。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/287677/c2c2f995-9f7a-6ab6-57b5-45d56babf21e.png)

# LangFlowとは
[LangFlow](https://github.com/logspace-ai/langflow)は、[LangChain](https://github.com/hwchase17/langchain)をGUIで構築できるようにしたツールです。
GUIはreact（コードは[この](https://github.com/logspace-ai/langflow/tree/dev/src/frontend)辺り）で作成されており、ブラウザで操作します。

インストールも簡単で、pipでinstallして実行するだけです。
また、LangFlowを実行した環境でhttpサーバ立ち上がり、GUIで作成したプロンプトをAPIで実行できるようになります。

例えば、ブラウザでよく使っているチャットアプリを、LangFlowで模擬すると、以下のような図になります。
(Community ExamplesのBasic Chat with Prompt and Historyそのまま)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/287677/a85d7df4-17c6-bb9d-3ede-f1b2e0b12e3f.png)

実行結果です。
openAI APIを単品で使った時と違い、きちんと対話履歴を参照していることが分かります。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/287677/af5ce0e6-8bb0-ec77-5e87-b0d7ae9b3170.png)


LangFlowを実行したターミナルに最終的なpromptが出力されています。
```bash
> Entering new LLMChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:


Human: パンはパンでも食べれるパンは何だ？
AI:

> Finished chain.
```
LLMやらPromptやらMemoryやらの話は今回省略します。
こちらの[Prompt Engineering Guide](https://www.promptingguide.ai/jp)を一番最初に見ると、理解が早まると思います。


試しに、Memoryを抜いてみましょう。
Basic Chat with Prompt and HistoryからMemoryだけ削除しました。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/287677/fd555849-574c-6874-a1ba-2359c5a9bef5.png)

結果です。
言った内容が記憶されていないことが分かります（ちょっと悲しい）。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/287677/04a83113-1d0e-5a39-081a-2b0a5c88759c.png)

こんな感じで、GUIを使ってさくっとプロトタイピングすることができます。

# Example紹介
他にもさくっとできるExampleを紹介します。
- Series Character
マリオになりきってもらいました。
OpenAPIのKeyはご自身のものをご入力ください。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/287677/f0d5d0c5-4295-6061-b145-efff0134a83e.png)
ちゃんとマリオになっています。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/287677/905a5d5d-38bf-beae-5eaf-3b1600aef100.png)
ターミナルに出力された、Chainの思考回路です。
```bash
> Entering new SeriesCharacterChain chain...
Prompt after formatting:
I want you to act like Mario from super mario bros..
I want you to respond and answer like Mario. do not write any explanations. only answer like Mario.
You must know all of the knowledge of Mario.
Current conversation:

Human: who are you?
Mario:

> Finished chain.


> Entering new SeriesCharacterChain chain...
Prompt after formatting:
I want you to act like Mario from super mario bros..
I want you to respond and answer like Mario. do not write any explanations. only answer like Mario.
You must know all of the knowledge of Mario.
Current conversation:
Human: who are you?
Mario:  It's-a me, Mario!
Human: what color is your hat?
Mario:

> Finished chain.

```
- SerpAPI Tool
検索エンジンの結果を抽出するAPI、SerpAPIを組見込んでいます。
ChatGPTは最新情報を喋ることはできませんが、検索系のAPIと組み合わせると、きちんと受け答えできるようになります。
[SerpApi](https://serpapi.com)のkeyは無料枠で取得しました。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/287677/ef55960c-97d9-6c3e-1275-5e6428a42f92.png)
最新（2023年7月17日現在）の藤井聡太先生の段位を取得することができました。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/287677/6e856398-9af0-1477-d25f-54a02cb75e61.png)
ターミナルに出力された、Chainの思考回路です。
ChatGPTが分からないので、SerpApiを実行しています。
```bash
> Entering new AgentExecutor chain...
I don't have this information, so I should search for it.
Action: Search
Action Input: "藤井聡太 段位"

> Entering new AgentExecutor chain...
I don't have this information, so I should search for it.
Action: Search
Action Input: "藤井聡太 段位"[2023-07-16 23:59:43 +0900] [691494] [INFO] connection closed
[2023-07-16 23:59:44 +0900] [691494] [INFO] ('127.0.0.1', 38098) - "WebSocket /api/v1/chat/b15148b6-33fb-4ccf-a4b1-2edc63f64c97" [accepted]
[2023-07-16 23:59:44 +0900] [691494] [INFO] connection open
[2023-07-17 00:02:35 +0900] [691494] [INFO] connection closed
[2023-07-17 00:02:38 +0900] [691494] [INFO] ('127.0.0.1', 57194) - "WebSocket /api/v1/chat/b15148b6-33fb-4ccf-a4b1-2edc63f64c97" [accepted]
[2023-07-17 00:02:38 +0900] [691494] [INFO] connection open


> Entering new AgentExecutor chain...
I don't know the answer to this question off the top of my head, so I should search for information about 藤井聡太先生's 段位.
Action: Search
Action Input: "藤井聡太先生 段位"
Observation: 結論からいうと、藤井聡太先生の段位は「九段」です。 将棋のプロの段位には「四段」から「九段」まであります。 ざっくりいうと、最高ランクの「九段」に ...
Thought:I now know the final answer
Final Answer: 藤井聡太先生の段位は「九段」です。

> Finished chain.

```
SerpApiは[こちら](https://cabbage-shogi.com/fuji-rank/)のサイトの情報を拾ってきたみたいです。
# 作ってみた
最後に自分で作成してみました。
Wikipedia検索と計算機をToolsに入れました。
これでWikipediaで調べて、調査結果を計算することができるようになります。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/287677/67d4da5a-dd10-0770-7ff8-7dd981044f34.png)
まじかよ(シンプルに結果に驚いている)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/287677/54f90b10-1ece-5c21-46e7-4a1b21a038b7.png)
長いですが、ターミナルの出力です。
Wikipediaで初代ピクミンとピクミン4を調べて発売年を抽出し、計算機に投げていることが分かります。
```bash
> Entering new AgentExecutor chain...
I don't have this information, but I can use Wikipedia to find out the release dates of the first Pikmin game and any news about Pikmin 4.
Action: Wikipedia
Action Input: "Pikmin (video game)"

> Entering new AgentExecutor chain...
I don't have this information, but I can use Wikipedia to find out the release dates of the first Pikmin game and any news about Pikmin 4.
Action: Wikipedia
Action Input: "Pikmin (video game)"
Observation: Page: Pikmin (video game)
Summary: Pikmin is a 2001 real-time strategy puzzle video game developed and published by Nintendo for the GameCube. The game was created and produced by Shigeru Miyamoto, and is the first entry in the Pikmin series. The game's story focuses on an alien pilot, Captain Olimar, who crash lands on a mysterious planet and must make use of a native species called "Pikmin" to find his ship's missing parts in order to escape within 30 days. Players take control of Olimar and in directing the different varieties of Pikmin to exploring the game's various levels, overcoming obstacles and hostile creatures, in order to find and recover the missing ship parts.The game was a critical and commercial success and spawned a series of sequels, starting with Pikmin 2, which was released in 2004. The game was ported to Wii in 2008 under the title New Play Control! Pikmin, while a high-definition remaster was released for Nintendo Switch in 2023.

Page: Pikmin
Summary: Pikmin is a real-time strategy and puzzle video game series created by Shigeru Miyamoto, and published by Nintendo. The games focus on directing a horde of plant-like creatures called Pikmin in order to collect items by destroying obstacles, avoiding hazards and fighting fauna that are hazardous to both the player character and the Pikmin.The Pikmin series features four main entries, as well as two spin-offs. The first two, Pikmin (2001) and Pikmin 2 (2004), were released for the GameCube and later ported to the Wii as New Play Control! titles in 2008 and 2009. Pikmin 2 was re-released in the Nintendo Selects series in 2012. A third installment, Pikmin 3, was released for the Wii U in 2013. The series' first handheld game, Hey! Pikmin, was released for the Nintendo 3DS in 2017. Pikmin 4 was announced to be in development by Miyamoto in September 2015, and was officially confirmed for release in 2023 during a Nintendo Direct in September 2022. Pikmin Bloom, the series' mobile spin-off, was released for iOS and Android in 2021 and Pikmin 1 and 2 were also ported to the Nintendo Switch also in the year 2023 as Pikmin 1+2.

Page: Pikmin 4
Summary: Pikmin 4 is an upcoming real-time strategy video game developed by Nintendo EPD and Eighting and published by Nintendo. It will be the fourth main installment of the Pikmin series, following Pikmin 3 (2013), and sixth installment overall. It is set for release on the Nintendo Switch on July 21, 2023.The game will mark the first in the series to feature a custom playable character, alongside other features involving a newly adjusted day and night system, allowing players to freely explore the area during the night without having to mandatorily retreat at the end of the day. In the game, the player assumes the role of a recently recruited member of the Rescue Corps, a group in need of rescuing following an unsuccessful attempt to save Captain Olimar.
Thought:I have found the release dates of the first Pikmin game and Pikmin 4. I can calculate the number of years between the two releases.
Action: Calculator
Action Input: 2023 - 2001
Observation: Answer: 22
Thought:The final answer is 22 years.
Observation: Invalid Format: Missing 'Action:' after 'Thought:'
Thought:The final answer is 22 years.
Final Answer: 22 years.

> Finished chain.
```

最後に作成したプロンプトを、API経由で実行してみました。
GUI左上付近の＜/＞アイコンをクリックすると、curlコマンドが表示されます。
実行したところ、ブラウザで確認した結果と同じ結果(22 years.)が帰ってきました。
```bash
$ curl -X POST \
  http://127.0.0.1:7860/api/v1/process/c7282e1f-852c-4614-95e5-eac6a9875bef \
  -H 'Content-Type: application/json' \
  -d '{"inputs": {"input":"How many years will Pikmin 4 be released after the first Pikmin?"}}'

{"result":{"output":"22 years.","intermediate_steps":[[{"tool":"Wikipedia","tool_input":"Pikmin (video game)","log":"I don't have this information, but I can use Wikipedia to find out the release dates of the first Pikmin game and any news about Pikmin 4.\nAction: Wikipedia\nAction Input: \"Pikmin (video game)\""},"Page: Pikmin (video game)\nSummary: Pikmin is a 2001 real-time strategy puzzle video game developed and published by Nintendo for the GameCube. The game was created and produced by Shigeru Miyamoto, and is the first entry in the Pikmin series. The game's story focuses on an alien pilot, Captain Olimar, who crash lands on a mysterious planet and must make use of a native species called \"Pikmin\" to find his ship's missing parts in order to escape within 30 days. Players take control of Olimar and in directing the different varieties of Pikmin to exploring the game's various levels, overcoming obstacles and hostile creatures, in order to find and recover the missing ship parts.The game was a critical and commercial success and spawned a series of sequels, starting with Pikmin 2, which was released in 2004. The game was ported to Wii in 2008 under the title New Play Control! Pikmin, while a high-definition remaster was released for Nintendo Switch in 2023.\n\nPage: Pikmin\nSummary: Pikmin is a real-time strategy and puzzle video game series created by Shigeru Miyamoto, and published by Nintendo. The games focus on directing a horde of plant-like creatures called Pikmin in order to collect items by destroying obstacles, avoiding hazards and fighting fauna that are hazardous to both the player character and the Pikmin.The Pikmin series features four main entries, as well as two spin-offs. The first two, Pikmin (2001) and Pikmin 2 (2004), were released for the GameCube and later ported to the Wii as New Play Control! titles in 2008 and 2009. Pikmin 2 was re-released in the Nintendo Selects series in 2012. A third installment, Pikmin 3, was released for the Wii U in 2013. The series' first handheld game, Hey! Pikmin, was released for the Nintendo 3DS in 2017. Pikmin 4 was announced to be in development by Miyamoto in September 2015, and was officially confirmed for release in 2023 during a Nintendo Direct in September 2022. Pikmin Bloom, the series' mobile spin-off, was released for iOS and Android in 2021 and Pikmin 1 and 2 were also ported to the Nintendo Switch also in the year 2023 as Pikmin 1+2.\n\nPage: Pikmin 4\nSummary: Pikmin 4 is an upcoming real-time strategy video game developed by Nintendo EPD and Eighting and published by Nintendo. It will be the fourth main installment of the Pikmin series, following Pikmin 3 (2013), and sixth installment overall. It is set for release on the Nintendo Switch on July 21, 2023.The game will mark the first in the series to feature a custom playable character, alongside other features involving a newly adjusted day and night system, allowing players to freely explore the area during the night without having to mandatorily retreat at the end of the day. In the game, the player assumes the role of a recently recruited member of the Rescue Corps, a group in need of rescuing following an unsuccessful attempt to save Captain Olimar."],[{"tool":"Calculator","tool_input":"2023 - 2001","log":"I have found the release dates of the first Pikmin game and Pikmin 4. I can calculate the number of years between the two releases.\nAction: Calculator\nAction Input: 2023 - 2001"},"Answer: 22"],[{"tool":"_Exception","tool_input":"Invalid Format: Missing 'Action:' after 'Thought:'","log":"The final answer is 22 years."},"Invalid Format: Missing 'Action:' after 'Thought:'"]]}}
```
# まとめ
LangFlowを利用することで、簡単にプロンプトエンジニアリングすることができました。
GUIによる直感的な操作で、簡単に設定することができるので、プロトタイピングやLangChainの学習には良いかと思います。
また、非エンジニアの人に作ったものを触ってもらうにも、ちょうど良いかと思いました。

# スペシャルサンクス
大変感謝しております。
- [Prompt Engineering Guide](https://www.promptingguide.ai/jp)
- [LangFlow とは - Hakky Handbook](https://book.st-hakky.com/docs/langflow-intro/)
- [LangChainのGUI版であるLangFlowを試す](https://note.com/npaka/n/n561e597ac5c4)