Skip to content

Instantly share code, notes, and snippets.

@hyuki

hyuki/README.md Secret

Last active January 28, 2024 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyuki/190b1f66b0678f0ceae19d1de245d488 to your computer and use it in GitHub Desktop.
Save hyuki/190b1f66b0678f0ceae19d1de245d488 to your computer and use it in GitHub Desktop.
OpenAI APIを使って翻訳を行うシンプルなRubyスクリプト

From Wikipedia, the free encyclopedia

A computer is a machine that can be programmed to carry out sequences of arithmetic or logical operations (computation) automatically. Modern digital electronic computers can perform generic sets of operations known as programs. These programs enable computers to perform a wide range of tasks. The term computer system may refer to a nominally complete computer that includes the hardware, operating system, software, and peripheral equipment needed and used for full operation; or to a group of computers that are linked and function together, such as a computer network or computer cluster.

A broad range of industrial and consumer products use computers as control systems. Simple special-purpose devices like microwave ovens and remote controls are included, as are factory devices like industrial robots and computer-aided design, as well as general-purpose devices such as personal computers and mobile devices such as smartphones. Computers power the Internet, which links billions of computers and users.

Early computers were meant to be used only for calculations. Simple manual instruments like the abacus have aided people in doing calculations since ancient times. Early in the Industrial Revolution, some mechanical devices were built to automate long, tedious tasks, such as guiding patterns for looms. More sophisticated electrical machines did specialized analog calculations in the early 20th century. The first digital electronic calculating machines were developed during World War II, both electromechanical and using thermionic valves. The first semiconductor transistors in the late 1940s were followed by the silicon-based MOSFET (MOS transistor) and monolithic integrated circuit chip technologies in the late 1950s, leading to the microprocessor and the microcomputer revolution in the 1970s. The speed, power and versatility of computers have been increasing dramatically ever since then, with transistor counts increasing at a rapid pace (Moore's law noted that counts doubled every two years), leading to the Digital Revolution during the late 20th to early 21st centuries.

ウィキペディア、フリー百科事典より

コンピューターは、算術または論理演算(計算)のシーケンスを自動的に実行するようにプログラム可能な機械です。現代のデジタル電子コンピューターは、プログラムとして知られる一般的な操作セットを実行できます。これらのプログラムにより、コンピューターは幅広いタスクを実行できます。コンピューターシステムという用語は、ハードウェア、オペレーティングシステム、ソフトウェア、周辺機器を含んで完全に動作するために必要な、名目上完全なコンピューターを指す場合や、リンクされて一緒に機能する、例えばコンピューターネットワークやコンピュータークラスターのような一群のコンピューターを指す場合があります。

産業用および消費者用製品の広範囲にわたって、コンピューターが制御システムとして使われています。単純な特定目的のデバイス、例えば電子レンジやリモコンから、工場のデバイス、例えば産業用ロボットやコンピューター支援設計、そしてパーソナルコンピューターやスマートフォンなどの一般的なデバイスまで、含まれています。コンピューターは、何十億ものコンピューターとユーザーを接続するインターネットを動かしています。

初期のコンピューターは計算専用であることを意図していました。古代より、算盤のような単純な手動の器具が計算を助けてきました。産業革命初期には、織機のパターンを案内するなど、長く退屈なタスクを自動化するための機械デバイスがいくつか作られました。20世紀初頭には、もっと洗練された電気機械が特殊なアナログ計算を行いました。第一次世界大戦中に、電気機械式および熱電子管を使用した最初のデジタル電子計算機が開発されました。1940年代後半には最初の半導体トランジスタ、1950年代後半にはシリコンベースのMOSFET(MOSトランジスタ)およびモノリシック集積回路チップ技術が続き、1970年代にはマイクロプロセッサーとマイクロコンピューター革命につながりました。それ以来、コンピューターの速度、パワー、汎用性は劇的に増加し続けており、トランジスタの数は迅速なペースで増加しています(ムーアの法則は、2年ごとに数が倍増することを指摘しています)、これが20世紀後半から21世紀初頭にかけてのデジタル革命につながりました。

# https://github.com/alexrudall/ruby-openai
# gem install ruby-openai
require "openai"
PROMPT =<<"EOD"
あなたは多国語を理解できる優秀な翻訳家であり、
わかりやすい日本語を書くことができます。
ユーザが与えた文章を日本語に翻訳してください。
EOD
# API keys: https://platform.openai.com/api-keys
# Billing: https://platform.openai.com/account/billing/overview
client = OpenAI::Client.new(access_token: ENV["OPENAI_API_KEY"])
response = client.chat(
parameters: {
model: "gpt-4-0125-preview", # https://platform.openai.com/docs/models/
messages: [
{ role: "system", "content": PROMPT },
{ role: "user", content: STDIN.read}
],
})
puts response.dig("choices", 0, "message", "content")
# $ cat input.md | ruby translate.rb > output.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment