毎度、ググっても出てこない小ネタを取り扱っております。
本記事は個人的な見解であり、筆者の所属するいかなる団体にも関係ございません。
0. はじめに
GPTScriptを試してみるシリーズです。
exampleディレクトリのファイルの中身を動かしてみながら、日本語でも動くかどうか検証していきます。
https://github.com/gptscript-ai/gptscript/tree/main/examples
1. describe-code.gpt
describe-code.gpt
のスクリプトは以下のようになっています。
describe-code.gpt
Tools: sys.find, count, summarize, compare
Find the top 10 go files that have the most lines of code. Then summarize each one.
Looking at the summary of all files write a short description of this program and its function.
---
name: compare
description: Sorts a list of number from smallest to biggest
arg: list: A comma separated list of numbers to sort
#!/bin/bash
for i in $(echo "${LIST}" | sed 's/[[,\]]/ /g'); do
echo $i
done | sort -n
---
name: count
description: Count the lines a file
arg: file: The filename to count the lines of
#!/bin/bash
wc -l "${FILE}"
---
name: summarize
tools: sys.read
description: Read a go file
arg: file: The filename to read
First read the passed file and then summarize it's content into no more than 100 words
2. 動かしてみる
まずは、動かしてみましょう。
2-1. ファイルを用意
ファイルは以下の1つを用意します
bob.gpt
以下のURLから上記の1つのファイルをダウンロードしておきます。
さらにファイルの説明を要約するサンプルなので、GPTScriptのソースコードをダウンロードしておきます。
wget https://raw.githubusercontent.com/gptscript-ai/gptscript/main/pkg/gptscript/gptscript.go
wget https://raw.githubusercontent.com/gptscript-ai/gptscript/main/pkg/builtin/builtin.go
wget https://raw.githubusercontent.com/gptscript-ai/gptscript/main/pkg/builtin/defaults.go
wget https://raw.githubusercontent.com/gptscript-ai/gptscript/main/pkg/builtin/defaults.go
wget https://raw.githubusercontent.com/gptscript-ai/gptscript/main/pkg/builtin/log.go
wget https://raw.githubusercontent.com/gptscript-ai/gptscript/main/pkg/cache/cache.go
wget https://raw.githubusercontent.com/gptscript-ai/gptscript/main/pkg/chat/readline.go
wget https://raw.githubusercontent.com/gptscript-ai/gptscript/main/pkg/chat/chat.go
2-2. 実行する
実行バージョンは以下の通りです。
$ gptscript -v
gptscript version v0.5.0+5a8a685c
以下のように実行します。
--default-model gpt-4-turbo
を指定する必要はありませんが、新しいModelの方がいいので指定しています。
gptscript ./describe-code.gpt --default-model gpt-4-turbo
3. 実行結果
以下のような回答が返ります。
OUTPUT:
The program is designed to facilitate interaction with GPT models and manage various system operations. It includes functionalities for:
- Managing system and workspace operations, including file handling, command execution, HTTP requests, and user input.
- Caching system responses, particularly for LLM API, with customizable behavior.
- Handling chat interactions, supporting dynamic prompts and responses based on configuration and user input.
- Interacting with OpenAI models, including setting default models and parameters.
- Executing scripts with GPT models, integrating components for model registry, script execution, workspace management, and external services like OpenAI.
- Logging and chat functionality through command-line interfaces, enhancing user experience with features like history support and text coloring.
Overall, the program appears to be a comprehensive tool for scripting, executing, and managing interactions with GPT models, along with supporting system operations and user interactions through a CLI.
3-2. 実行結果の出力を日本語にする
そのままだと結果が英語なので、実行時のオプションを追加します。
gptscript ./describe-code.gpt --default-model gpt-4-turbo -i 実行後の結果を日本語で回答してください。
以下のような結果が得られます。
OUTPUT:
このプログラムは、Go言語で書かれたスクリプト実行とチャット機能を提供するシステムです。主にファイルやディレクトリの管理、コマンド実行、HTTP操作、ユーザーとのインタラクションを扱うための機能を含んでいます。また、OpenAIモデルを利用してスクリプトを実行し、チャットセッションを管理する機能も備えています。キャッシュシステムを通じてAPIレスポンスの保存と取得、コ マンドラインインターフェースでの入出力処理、環境変数の管理、ワークスペースの作成と削除など、開発者がスクリプトを簡単に実行し、ユーザーと対話できるようにするための包括的なツールセットを提供します。
微妙に出力が違いますね。
4. 日本語にしてみる
4-1. 日本語にしたgptファイルを用意
describe-code_ja.gpt
Tools: sys.find, count, summarize, compare
コードの行数が最も多いものの上から10ファイルを見つけて、それをそれぞれ要約してください。
全ファイルの要約を見て、このプログラムとその機能の簡単な説明を書いてください。
---
name: compare
description: 数値の小さいものから大きいものへリストをソートする
arg: list: ソートするカンマ区切りの数値リスト
#!/bin/bash
for i in $(echo "${LIST}" | sed 's/[[,\]]/ /g'); do
echo $i
done | sort -n
---
name: count
description: ファイルの行数を数える
arg: file: 行数をカウントするファイル名
#!/bin/bash
wc -l "${FILE}"
---
name: summarize
tools: sys.read
description: 読み込むgoファイル
arg: file: 読み込むファイル名
まず渡されたファイルを読み、その内容を100字以内にまとめる。
4-2. 実行結果
OUTPUT:
### 要約
1. **builtin.go**: Go言語で書かれたビルトインツールの定義を含む。各ツールは特定の機能を実行し、様々なシステム操作(ファイル読み書き、HTTPリクエスト、環境変数取得など)をサポート する。
2. **cache.go**: Goのキャッシュ管理ライブラリ。LLM API応答のキャッシングを制御するオプションを提供。キャッシュ無効化やディレクトリ指定が可能。
3. **chat.go**: Go言語で書かれたチャットアプリケーション。PrompterとChatterインターフェースを定義し、チャットセッションを開始するStart関数を含む。
4. **defaults.go**: デフォルトのモデルを設定・取得する関数と、ツールのデフォルト設定を行う関数を含むGoパッケージ。
5. **gptscript.go**: 設定オプションを完了させ、レジストリ、キャッシュ、OpenAIクライアント、リモートクライアントを設定し、実行環境を提供するGoパッケージ。
6. **log.go**: パッケージbuiltinがmvlパッケージからlogをインポートしている。
7. **readline.go**: Go言語で書かれたコードは、readlineを使用してプロンプトを提供し、ユーザー入力を読み取り、履歴を管理する。
### プログラムの概要
このプログラムは、Go言語で書かれた複数のコンポーネントから構成されており、ビルトインツールの定義、キャッシュ管理、チャットアプリケーションの機能、デフォルト設定の管理、スクリプト実行環境の設定、ログ管理、およびユーザー入力の読み取りと履歴管理の機能を提供します。これらのコンポーネントは、システム操作のサポート、API応答のキャッシング、チャットセッションの 管理、設定オプションの管理など、様々な機能を実現しています。
5. まとめ
まともに動きますね。