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

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

はじまり#

リサちゃん avatar
リサちゃん
あれっ、またGoogleスタイルだ・・・
135ml avatar
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

「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スタイルで出力しています・・・。

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を使うことにしよう・・・。

おしまい#

リサちゃん avatar
リサちゃん
ハハア、こんなところに違いがあるのか。
135ml avatar
135ml
「汎用的な」大規模言語モデルでも、やっぱり得手不得手があるんだな。

以上になります!

記事を共有

この記事が役に立ったなら、ぜひ他の人と共有してください!

【Python】大規模言語モデル達の間ではReStructuredTextスタイルはあまり知られていないらしい
https://endorphinbath.com/posts/llm-knows-python-restructuredtextstyle/
著者
kinkinbeer135ml
公開日
2024-05-31
ライセンス
CC BY-NC-SA 4.0
関連記事 スマート
1
【Python】死ぬほどモックで沼ってたのでその過程でClaude君が出してくれた対策を並べた
Code Pythonでテストメソッドを作る時にかなり沼っていたので、その過程でClaudeが教えてくれたことを並べました。色々とpytest-mockの書き方があるようです。
2
NVIDIA NIMで利用できる生成AI
Software NVIDIA NIMで利用できるNemotron-340B-InstructやShutterstockのEdify-3D等の生成AIを色々と試してみた記事です。
3
【Notion、Bash】Clineで綴るPythonのtypingモジュール完全ガイド
Code Clineで記事を書くために、自分が今まで書いた記事の文体や特徴をまとめるための資料を作る工程を紹介した記事です。NotionからMarkdownをエクスポートしてBashで加工してWordPressで反映します。
4
【Luma Dream Machine】プロンプトガイドやFAQを検証してみた
Software この記事は、動画生成AI「Luma Dream Machine」のプロンプトガイドとFAQについて解説しています。実際にガイドにある内容を使ってプロンプトしてみています。
5
【Python】Pydanticのvalidatorが非推奨だからfield_validatorを使って2段階バリデーションを実装する
Code @validatorはdeprecatedになってるし、@field_validatorにはpreとかalwaysフラグが無いから、from pydantic.functional_validators import field_validatorでインポートしたfield_validatorを使用する方法と、BeforeValidatorとAfterValidatorをAnnotateの中に入れる実装方法を試した。
ランダム記事 ランダム
Profile Image of the Author
kinkinbeer135ml
SIerをやめて、プログラミングを勉強しています。※Amazonアソシエイトに参加しています。
お知らせ
私のブログへようこそ!これはサンプルのお知らせです。
音楽
カバー

音楽

再生中なし

0:00 0:00
歌詞なし
カテゴリ
タグ
サイト統計
記事
287
カテゴリー
8
タグ
93
総文字数
486,174
運用日数
0
最終活動
0 日前

目次