【Python】大規模言語モデル達の間ではReStructuredTextスタイルはあまり知られていないらしい

Code

はじまり

リサちゃん
リサちゃん

あれっ、またGoogleスタイルだ・・・

135ml
135ml

コイツら知らないのかもしれない。

コイツら、Googleスタイルばかり返してくる

息抜きの記事として、書いていきます。

Pythonのプロジェクトで、ドキュメンテーションする時に「Sphinx」などがよく使われると思います。

そのSphinxでは、「reStructuredText」スタイルで記載されたdocstringから、Python機能群をドキュメントにしていきます。

Sphinx uses the reStructuredText markup language by default, and can read MyST markdown via third-party extensions. Both of these are powerful and straightforward to use, and have functionality for complex documentation and publishing workflows. They both build upon Docutils to parse and write documents.

ようこそ! — Sphinx documentation
ようこそ! — Sphinx documentation

「reStructuredText」スタイルとは、以下のような形式のdocstringを指します。

コロンからフィールド名を指定して、説明文を記述していく文書形式になります。「:param」で引数の説明文、「:type」で引数の型を記載できたりします。

def my_function(my_arg, my_other_arg):
    """A function just for me.

    :param my_arg: The first of my arguments.
    :type my_arg: str
    :param my_other_arg: The second of my arguments.
    :type my_other_arg: int
    :returns: A message (just for me, of course).
    """

そして、本記事は、Sphinxで利用できるdocstringを大規模言語モデル達に、出力してもらった時のことになります。

大規模言語モデルへのプロンプト

今回使った大規模言語モデルは、以下の通りになります。

  • Gemini 1.5
  • Claude 3 Sonnet
  • Command R+
  • ChatGPT 4
  • ChatGPT 3.5

そして、それらの大規模言語モデルに今回プロンプトする内容は以下になります。

以下のPythonで書かれた[# ソースコード]に、[# スタイル]でdocstringを英語で追記して。

[# スタイル]
-----------------------------------------
ReStructuredTextスタイル
-----------------------------------------

[# ソースコード]
-----------------------------------------
def fetch_repositories(github, fetch_type: str, username: str | None):
    if fetch_type not in ["all", "owner", "public", "private", "forks"]:
        raise ValueError(f"'fetch_type' is not support '{fetch_type}'")
    if username == None:
        user = github.get_user()
    else:
        user = github.get_user(username)
    repos = user.get_repos(type=fetch_type)
    return repos
-----------------------------------------

プロンプトした結果

Claude 3 Sonnet

まずは、Claude 3 Sonnetに対してプロンプトした結果です。

このモデルでは、「ReStructuredTextスタイル」と指定したのに、「Googleスタイル」で出力されてしまっています。

「Googleスタイル」とは、docstringの記法の中の一つです。「Args:」や、「Returns:」という風にフィールドを指定して、説明文を記述していきます。

確かによく使われている記法ではありますが・・・。

def fetch_repositories(github, fetch_type: str, username: str | None):
   """Fetch repositories from GitHub based on the specified fetch type and username.

   Args:
       github (github.MainClass.Github): An instance of the PyGitHub library's Github class,
           representing the authenticated connection to GitHub.
       fetch_type (str): The type of repositories to fetch. Must be one of "all", "owner",
           "public", "private", or "forks". Raises a ValueError if an unsupported type is provided.
       username (str | None): The username of the GitHub user whose repositories should be fetched.
           If None, the authenticated user's repositories will be fetched.

   Returns:
       github.PaginatedList.PaginatedList: A paginated list of repositories matching the specified
           fetch type and username.

   Raises:
       ValueError: If an unsupported fetch_type is provided.
   """
   if fetch_type not in ["all", "owner", "public", "private", "forks"]:
       raise ValueError(f"'fetch_type' is not support '{fetch_type}'")
   if username == None:
       user = github.get_user()
   else:
       user = github.get_user(username)
   repos = user.get_repos(type=fetch_type)
   return repos

Gemini 1.5

次に、Gemini 1.5に対してプロンプトした結果です。

コイツも、Googleスタイルで出力してしまっています・・・。(さすがGoogleのAIだなぁ。)

def fetch_repositories(github, fetch_type: str, username: str | None) -> List[Repository]:
    """
    指定されたGitHubユーザーのリポジトリを取得します。

    引数:
        github (Github): GitHubオブジェクト
        fetch_type (str): 取得するリポジトリの種類。 "all", "owner", "public", "private", "forks" のいずれかを指定できます。
        username (str | None): リポジトリを取得するユーザー名。省略した場合、現在のユーザーのリポジトリを取得します。

    戻り値:
        List[Repository]: 取得したリポジトリのリスト

    例:
        >>> github = Github(login="your-username", password="your-password")
        >>> repos = fetch_repositories(github, fetch_type="all")
        >>> for repo in repos:
        ...     print(repo.name)
        my-repo
        another-repo

        >>> repos = fetch_repositories(github, fetch_type="public", username="github")
        >>> for repo in repos:
        ...     print(repo.name)
        git
        github-cli

    エラー:
        - 'fetch_type' には無効な値が渡されました。有効な値は "all", "owner", "public", "private", "forks" です。
    """

    if fetch_type not in ["all", "owner", "public", "private", "forks"]:
        raise ValueError(f"'fetch_type' is not support '{fetch_type}'")

    if username == None:
        user = github.get_user()
    else:
        user = github.get_user(username)

    repos = user.get_repos(type=fetch_type)
    return repos

Command R+

Command R+に対してプロンプトした結果です。コイツも、Googleスタイルで出力しています・・・。

```python
def fetch_repositories(github, fetch_type: str, username: str | None):
    """
    Fetches a user's repositories from GitHub based on the provided fetch type.

    Args:
        github (object): Authenticated GitHub API object.
        fetch_type (str): Type of repositories to fetch. Can be "all", "owner", "public", "private", or "forks".
        username (str, optional): Username of the user whose repositories are to be fetched. If not provided, defaults to the authenticated user.

    Returns:
        list: List of repository objects fetched from GitHub.

    Raises:
        ValueError: If an unsupported fetch type is provided.
    """
    if fetch_type not in ["all", "owner", "public", "private", "forks"]:
        raise ValueError(f"'fetch_type' does not support the value '{fetch_type}'")
    if username is None:
        user = github.get_user()
    else:
        user = github.get_user(username)
    repos = user.get_repos(type=fetch_type)
    return repos
```

ChatGPT 4

しかし、ここで他の大規模言語モデルと違う動きを見せたのがChatGPTです。

このモデル群は、しっかり「ReStructuredTextスタイル」でdosctringを生成してくれます。

それでは、ChatGPT 4に対してプロンプトした結果です。

def fetch_repositories(github, fetch_type: str, username: str | None):
    """
    Fetches a list of GitHub repositories based on the specified fetch type and username.

    :param github: The GitHub instance.
    :type github: GitHub
    :param fetch_type: Type of repositories to fetch, valid options are "all", "owner", "public", "private", "forks".
    :type fetch_type: str
    :param username: Username of the GitHub account to fetch repositories for. If None, fetches repositories for the authenticated user.
    :type username: str | None, optional
    :returns: A list of repository objects.
    :rtype: List[Repository]
    :raises ValueError: If `fetch_type` is not a supported type.
    """
    if fetch_type not in ["all", "owner", "public", "private", "forks"]:
        raise ValueError(f"'fetch_type' is not supported '{fetch_type}'")
    if username is None:
        user = github.get_user()
    else:
        user = github.get_user(username)
    repos = user.get_repos(type=fetch_type)
    return repos

ChatGPT 3.5

次に、ChatGPT 3.5に対してプロンプトした結果です。コイツも、「ReStructuredTextスタイル」で生成してくれます。

def fetch_repositories(github, fetch_type: str, username: str | None):
    """
    Fetches repositories based on the specified type and optionally, username.

    :param github: GitHub instance.
    :type github: GitHub
    :param fetch_type: Type of repositories to fetch. It can be one of "all", "owner", "public", "private", or "forks".
    :type fetch_type: str
    :param username: Optional. Username of the user whose repositories are to be fetched. If not provided, fetches repositories of the authenticated user.
    :type username: str or None
    :return: List of repositories.
    :rtype: list of Repository
    :raises ValueError: If the provided fetch_type is not supported.
    """
    if fetch_type not in ["all", "owner", "public", "private", "forks"]:
        raise ValueError(f"'fetch_type' is not supported: '{fetch_type}'")
    if username == None:
        user = github.get_user()
    else:
        user = github.get_user(username)
    repos = user.get_repos(type=fetch_type)
    return repos

それじゃあ、ChatGPTはGoogleスタイルを知らなかったりするのか?

おまけで、ChatGPTに「Googleスタイル」でdosctringを生成するように依頼してみます。逆に「Googleスタイル」を存じあげなかったりするかもしれません。

以下のPythonで書かれた[# ソースコード]に、[# スタイル]でdocstringを英語で追記して。

[# スタイル]
-----------------------------------------
Googleスタイル
-----------------------------------------

[# ソースコード]
-----------------------------------------
def fetch_repositories(github, fetch_type: str, username: str | None):
    if fetch_type not in ["all", "owner", "public", "private", "forks"]:
        raise ValueError(f"'fetch_type' is not support '{fetch_type}'")
    if username == None:
        user = github.get_user()
    else:
        user = github.get_user(username)
    repos = user.get_repos(type=fetch_type)
    return repos
-----------------------------------------

ChatGPT 3.5に対してプロンプトした結果がこちらです。ちゃんと「Googleスタイル」で生成できていますね。デキるヤツです。

def fetch_repositories(github, fetch_type: str, username: str | None):
    """
    Fetch repositories from GitHub for a given user.

    Args:
        github: An instance of the GitHub API client.
        fetch_type (str): The type of repositories to fetch. Can be one of ["all", "owner", "public", "private", "forks"].
        username (str | None): The username of the GitHub user. If None, fetches repositories for the authenticated user.

    Returns:
        List: A list of repository objects matching the specified criteria.
    """
    if fetch_type not in ["all", "owner", "public", "private", "forks"]:
        raise ValueError(f"'fetch_type' is not supported: '{fetch_type}'")
    if username is None:
        user = github.get_user()
    else:
        user = github.get_user(username)
    repos = user.get_repos(type=fetch_type)
    return repos

まとめ

今回は、いくつかの大規模言語モデルにPythonのSphinx用のdocstringを生成することをテーマとした記事となりました。

プロンプトした結果は以下のとおりです。

大規模言語モデルReStructuredTextスタイルGoogleスタイル
Gemini 1.5
Claude 3 Sonnet
Command R+
ChatGPT 4
ChatGPT 3.5

本記事は、以下の流れの構成にでお届けしました。

  • ReStructuredTextスタイルについて説明しました。
  • 5つの大規模言語モデル(Gemini 1.5、Claude 3 Sonnet、Command R+、ChatGPT 4、ChatGPT 3.5)にプロンプトし、その結果を紹介しました。
  • 特に、ChatGPT 4とChatGPT 3.5がReStructuredTextスタイルに対応していることを確認しました。

Pythonでドキュメントを作る時は、ChatGPTを使うことにしよう・・・。

おしまい

リサちゃん
リサちゃん

ハハア、こんなところに違いがあるのか。

135ml
135ml

「汎用的な」大規模言語モデルでも、やっぱり得手不得手があるんだな。

以上になります!

コメント

タイトルとURLをコピーしました