Open API Platformのexamplesをnode.jsで試す旅です。この記事ではNatural language to Stripeを試してみます。
サンプルのソースはこちらにあります。
https://platform.openai.com/examples/default-stripe-api
サンプルソースのままだと動かないので少し修正を加えます。ソースコードは次の通りです。
[someone@somewhere stub]$ cat exsamples_Natual_language_to_stripe_api.js
require('dotenv').config();
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
async function run(){
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: "\"\"\"\nUtil exposes the following:\n\nutil.stripe() -> authenticates & returns the stripe module; usable as stripe.Charge.create etc\n\"\"\"\nimport util\n\"\"\"\nCreate a Stripe token using the users credit card: 5555-4444-3333-2222, expiration date 12 / 28, cvc 521\n\"\"\"",
temperature: 0,
max_tokens: 100,
top_p: 1.0,
frequency_penalty: 0.0,
presence_penalty: 0.0,
stop: ["\"\"\""],
});
if (response.data && response.data.choices && response.data.choices.length > 0) {
console.log(response.data.choices[0].text);
} else {
console.log("No response received or empty choices.");
}
}
run();
実行結果は次のようになりました。
[rocky@www stub]$ node exsamples_Natual_language_to_stripe_api.js
token = stripe.Token.create(
card={
"number": "5555-4444-3333-2222",
"exp_month": 12,
"exp_year": 28,
"cvc": 521
},
)