私の最初のオープンソースへの貢献
問題を提出する
私の最初の貢献として、プロンプトと補完の生成に使用されるトークンを表示する新しいフラグ オプションを追加する新しい機能を別のプロジェクトに追加する問題を提出しました。
機能: チャット完了トークン情報フラグ オプション
#8

説明
送受信されたトークンの数をユーザーに与えるフラグ オプション。 これは、チャット完了リクエストを行うときにユーザーがトークンの予算内に収まるように誘導する重要な機能だと思います!
実装
これを行うには、-t および --token-usage などの別のオプション フラグを追加する必要があります。ユーザーがこのフラグをコマンドに含めると、補完の生成に使用されたトークンの数と、プロンプトで使用されたトークンの数が明確に詳細に表示される必要があります。
私は、fadingNA のオープンソース プロジェクト、chat-minal に貢献することにしました。これは、Python で書かれた CLI ツールで、OpenAI を利用して、コード レビューの生成、ファイル変換、マークダウンの生成など、さまざまな作業を行うことができます。テキストと要約テキスト。
私のプルリクエスト
私は以前に Python でコードを書いたことがありますが、それは私の最も得意なスキルではありません。したがって、このプロジェクトに貢献することは、私にとって挑戦的ではありますが、良い学習経験になります。
課題は、他の人のコードを読んで理解し、コードの設計を壊さない方法で適切な解決策を提供しなければならないことです。コードに大きな変更を加えずに機能を効率的に追加し、コードの一貫性を保つためには、フローを理解することが重要です。
機能: トークン使用フラグ
#9

機能
ユーザーに --token_usage フラグ オプションを含める機能を追加しました。 このオプションは、プロンプトと生成された補完に使用されたトークンの数に関する情報をユーザーに提供します。
実装
コード設計に基づいて私が思いついた解決策は、token_usage フラグの存在を確認することです。 token_usage フラグが使用されていない場合にコードで不要な if ステートメントをチェックしたくないので、チャンク内の use_metadata の存在をチェックするという違いを除き、2 つの別々の同一のループ ロジックを作成しました。
if token_usage: for chunk in runnable.stream({"input_text": input_text}): print(chunk.content, end="", flush=True) answer.append(chunk.content) if chunk.usage_metadata: completion_tokens = chunk.usage_metadata.get('output_tokens') prompt_tokens = chunk.usage_metadata.get('input_tokens') else: for chunk in runnable.stream({"input_text": input_text}): print(chunk.content, end="", flush=True) answer.append(chunk.content)
ディスプレイ
At the end of the execution of get_completions() method, a check for the flag token_usage is added, which then displays the token usage details to stderr if the flag was used.
if token_usage: logger.error(f"Tokens used for completion: <span class="pl-s1"><span class="pl-kos">{completion_tokens}</span>"</span>) logger.error(f"Tokens used for prompt: <span class="pl-s1"><span class="pl-kos">{prompt_tokens}</span>"</span>)
My solution
Retrieving the token usage
if token_usage: for chunk in runnable.stream({"input_text": input_text}): print(chunk.content, end="", flush=True) answer.append(chunk.content) if chunk.usage_metadata: completion_tokens = chunk.usage_metadata.get('output_tokens') prompt_tokens = chunk.usage_metadata.get('input_tokens') else: for chunk in runnable.stream({"input_text": input_text}): print(chunk.content, end="", flush=True) answer.append(chunk.content)
Originally, the code only had one for loop which retrieves the content from a stream and appends it to an array which forms the response of the completion.
Why did I write it this way?
My reasoning behind duplicating the for while adding the distinct if block is to prevent the code from repeatedly checking the if block even if the user is not using the newly added --token_usage flag. So instead, I check for the existence of the flag firstly, and then decide which for loop to execute.
Realization
Even though my pull request has been accepted by the project owner, I realized late that this way adds complexity to the code's maintainability. For example, if there are changes required in the for loop for processing the stream, that means modifying the code twice since there are two identical for loops.
What I think I could do as an improvement for it is to make it into a function so that any changes required can be done in one function only, keeping the maintainability of the code. This just proves that even if I wrote the code with optimization in mind, there are still other things that I can miss which is crucial to a project, which in this case, is maintainability.
Receiving a pull request
My tool, genereadme, also received a contribution. I received a PR from Mounayer, which is to add the same feature to my project.
feat: added a new flag that displays the number of tokens sent in prompt and received in completion
#13

Description
Closes #12.
- Added a new flag --token-usage which when given, prints the number of tokens that were sent in the prompt and the number of tokens that were returned in the completion to `stderr.
This simply required the addition for another flag check --token-usage:
.option("--token-usage", "Show prompt and completion token usage")
I've also made sure to keep your naming conventions/formatting style consistent, in the for loop that does the chat completion for each file processed, I have accumulated the total tokens sent and received:
promptTokens += response.usage.prompt_tokens; completionTokens += response.usage.completion_tokens;
which I then display at the end of program run-time if the --token-usage flag is provided as such:
if (program.opts().tokenUsage) { console.error(`Prompt tokens: <span class="pl-s1"><span class="pl-kos">${promptTokens}</span>`</span>); console.error(`Completion tokens: <span class="pl-s1"><span class="pl-kos">${completionTokens}</span>`</span>); }
- Updated README.md to explain the new flag.
Testing
Test 1
genereadme examples/sum.js --token-usage
This should display something like:
Test 2
You can try it out with multiple files too, i.e.:
genereadme examples/sum.js examples/createUser.js --token-usage
This time, instead of having to read someone else's code, someone had to read mine and contribute to it. It is nice knowing that someone is able to contribute to my project. To me, it means that they understood how my code works, so they were able to add the feature without breaking anything or adding any complexity to the code base.
With that being mentioned, reading code is also a skill that is not to be underestimated. My code is nowhere near perfect and I know there are still places I can improve on, so credit is also due to being able to read and understand code.
This specific pull request did not really require any back and forth changes as the code that was written by Mounayer is what I would have written myself.
以上が私の最初のオープンソースへの貢献の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Pythonは、データサイエンス、Web開発、自動化タスクに適していますが、Cはシステムプログラミング、ゲーム開発、組み込みシステムに適しています。 Pythonは、そのシンプルさと強力なエコシステムで知られていますが、Cは高性能および基礎となる制御機能で知られています。

2時間以内にPythonの基本を学ぶことができます。 1。変数とデータ型を学習します。2。ステートメントやループの場合などのマスター制御構造、3。関数の定義と使用を理解します。これらは、簡単なPythonプログラムの作成を開始するのに役立ちます。

PythonはゲームとGUI開発に優れています。 1)ゲーム開発は、2Dゲームの作成に適した図面、オーディオ、その他の機能を提供し、Pygameを使用します。 2)GUI開発は、TKINTERまたはPYQTを選択できます。 TKINTERはシンプルで使いやすく、PYQTは豊富な機能を備えており、専門能力開発に適しています。

2時間以内にPythonの基本的なプログラミングの概念とスキルを学ぶことができます。 1.変数とデータ型、2。マスターコントロールフロー(条件付きステートメントとループ)、3。機能の定義と使用を理解する4。

Pythonは、Web開発、データサイエンス、機械学習、自動化、スクリプトの分野で広く使用されています。 1)Web開発では、DjangoおよびFlask Frameworksが開発プロセスを簡素化します。 2)データサイエンスと機械学習の分野では、Numpy、Pandas、Scikit-Learn、Tensorflowライブラリが強力なサポートを提供します。 3)自動化とスクリプトの観点から、Pythonは自動テストやシステム管理などのタスクに適しています。

Pythonは学習と使用が簡単ですが、Cはより強力ですが複雑です。 1。Python構文は簡潔で初心者に適しています。動的なタイピングと自動メモリ管理により、使いやすくなりますが、ランタイムエラーを引き起こす可能性があります。 2.Cは、高性能アプリケーションに適した低レベルの制御と高度な機能を提供しますが、学習しきい値が高く、手動メモリとタイプの安全管理が必要です。

限られた時間でPythonの学習効率を最大化するには、PythonのDateTime、時間、およびスケジュールモジュールを使用できます。 1. DateTimeモジュールは、学習時間を記録および計画するために使用されます。 2。時間モジュールは、勉強と休息の時間を設定するのに役立ちます。 3.スケジュールモジュールは、毎週の学習タスクを自動的に配置します。

Pythonは、自動化、スクリプト、およびタスク管理に優れています。 1)自動化:OSやShutilなどの標準ライブラリを介してファイルバックアップが実現されます。 2)スクリプトの書き込み:Psutilライブラリを使用してシステムリソースを監視します。 3)タスク管理:スケジュールライブラリを使用してタスクをスケジュールします。 Pythonの使いやすさと豊富なライブラリサポートにより、これらの分野で優先ツールになります。
