| Crates.io | hey_chat_gpt |
| lib.rs | hey_chat_gpt |
| version | 1.2.1 |
| created_at | 2025-01-03 03:48:27.624643+00 |
| updated_at | 2025-01-07 05:40:14.26128+00 |
| description | A proc-macro to delegate implementation to the ChatGPT API. ChatGPT APIに実装を代行してもらうマクロです。 |
| homepage | |
| repository | https://github.com/anotherhollow1125/hey_chat_gpt |
| max_upload_size | |
| id | 1502102 |
| size | 72,987 |
A macro to delegate implementation to the ChatGPT API.
This macro sends the entire file containing it to the OpenAI API, and replaces it with the result returned by the API.
use hey_chat_gpt::take_care_of_the_rest;
fn main() {
println!("{}", fib(10));
}
take_care_of_the_rest!();
You can pass several options in the form of key = value.
Additionally, although it's not strictly necessary to mention here,
you can pass string literals as well, which can be used to provide a prompt.
| key | Type | Default | Possible Values | Description |
|---|---|---|---|---|
| model | String | "gpt-4o" | "o1-preview", etc. | Specifies the GPT model to use. |
| seed | Integer | File hash | Integer value ≤ 9223372036854775807 | Provides a seed for reproducibility. Try this if the default results are unsatisfactory. |
| max_completion_tokens | Integer | None | Sets the maximum number of tokens for the response. Might help when the output is truncated (unverified). |
Example with options:
fn main() {
println!("{}", fib(10));
}
hey_chat_gpt::take_care_of_the_rest!(
model = "o1-preview";
seed = 20;
max_completion_tokens = 4096;
"Implement a function that generates a Fibonacci sequence (a_{n-1} + a_{n} = a_{n+1}) starting with 1, 1."
);
[!IMPORTANT] This crate requires
nightlytoolchain!
cargo addcommand also needs to benightly.cargo +nightly add hey_chat_gpt
To compile, run the below command.
OPENAI_API_KEY=sk-YOUR-API-KEY RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo +nightly run
OPENAI_API_KEY: api key.RUSTFLAGS=...: to enable source_file method of Span.cargo +nightly run: the reason of specify nightly is same as above.These settings can also be enable by setting files.
.cargo/config.toml
[build]
rustflags = ["--cfg=procmacro2_semver_exempt"]
[env]
OPENAI_API_KEY = "sk-YOUR-API-KEY"
rust-toolchain.toml
[toolchain]
channel = "nightly"
In this case, the options are not necessary.
cargo run
Responses from ChatGPT are stored in the gpt_responses directory and used as a cache when the code remains unchanged. If you get undesirable code or an error response from ChatGPT, remove those cache files before trying to compile again.
ChatGPT APIに実装を代行してもらうマクロです。
このマクロを記述したファイル全体をOpenAI APIに投げ、返ってきた結果で置換します。
use hey_chat_gpt::あとは任せた;
fn main() {
println!("{}", fib(10));
}
あとは任せた!();
key = value 形式でいくつかのオプションを渡すことができます。また、(ここに記述する必要性はないですが)文字列リテラルも受け取れるのでここにプロンプトを記述することもできます。
| key | 型 | デフォルト | 候補 | 説明 |
|---|---|---|---|---|
| model | 文字列 | "gpt-4o" | "o1-preview" 等 | 使用するGPTのモデルを指定します。 |
| seed | 整数値 | ファイルハッシュ | 9223372036854775807 以下の整数値 | 再現性確保のために与えるシード値を与えます。デフォルトだと芳しくない結果になった時に指定してみてください。 |
| max_completion_tokens | 整数値 | 指定なし | 返答の最大トークン数を設定します。生成が中途半端になった時に使えるかも...?(未検証) |
オプションを指定した場合の例
fn main() {
println!("{}", fib(10));
}
hey_chat_gpt::あとは任せた!(
model = "o1-preview";
seed = 20;
max_completion_tokens = 4096;
"1, 1で始まるフィボナッチ数列(a_{n-1} + a_{n} = a_{n+1})を生成する関数を実装してください。"
);
[!IMPORTANT] 本クレートでは
nightlyツールチェイン必須です!
cargo addコマンドもまたnightlyを必要とします。cargo +nightly add hey_chat_gpt
コンパイルするには以下を実行します。
OPENAI_API_KEY=sk-YOUR-API-KEY RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo +nightly run
OPENAI_API_KEY: 取得してきたOpenAIのAPIキーを設定してください。RUSTFLAGS=...: Span の source_file メソッドを使用するために指定しています。cargo +nightly run: nightly の指定理由は上記と同じです。設定ファイルを通しての設定も可能です。
.cargo/config.toml
[build]
rustflags = ["--cfg=procmacro2_semver_exempt"]
[env]
OPENAI_API_KEY = "sk-YOUR-API-KEY"
rust-toolchain.toml
[toolchain]
channel = "nightly"
この場合オプションは不要になります。
cargo run
ChatGPTからの返答は gpt_responses ディレクトリに保存され、コードが変わらないうちはこちらのキャッシュがコンパイルに利用されます。もし望まない結果になったりエラーレスポンスが帰ってきた場合は、キャッシュファイルを削除の上再コンパイルしてみてください。