0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

完成イメージ

C2に日本銀行の会見の文字起こしが入力されています。
GASを使いchatgptに要約してもらいそれをD2に出力しました。
スクリーンショット 2024-07-14 23.59.14.png

GASコード

function createArticle() {
  const secretKey = 'APIを入力してください';
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('シート1');
  const articlePart = sheet.getRange('C2').getValue();
  
  const prompt = `以下の文章を使って要約してください。. Text: "${articlePart}"`;
  
  const apiUrl = 'https://api.openai.com/v1/chat/completions';
  const payload = {
    model: 'gpt-3.5-turbo',
    messages: [
      {role: 'system', content: 'You are a helpful assistant.'},
      {role: 'user', content: prompt}
    ],
    max_tokens: 500
  };

  const options = {
    method: 'post',
    contentType: 'application/json',
    headers: {
      'Authorization': 'Bearer ' + secretKey
    },
    payload: JSON.stringify(payload)
  };

  const apiResponse = UrlFetchApp.fetch(apiUrl, options);
  const responseText = JSON.parse(apiResponse.getContentText());
  const article = responseText.choices[0].message.content.trim();

  sheet.getRange('D2').setValue(article);
}

シークレットキーの定義

const secretKey = 'APIキーを入力してください';

OpenAI APIを呼び出すためのシークレットキーを定義しています。このキーは認証に使用されます。

スプレッドシートの取得

const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('シート1');

スプレッドシートから名前が「シート1」のシートを取得しています。

記事の部分の取得

const articlePart = sheet.getRange('C2').getValue();

B2セルの値を取得しています。

プロンプトの作成

  const prompt = `以下の文章を使って要約してください。. Text: "${articlePart}"`;

OpenAI APIに送信するプロンプトを作成しています。このプロンプトには取得した記事の一部とリンクが含まれています。

APIリクエストのペイロード

const apiUrl = 'https://api.openai.com/v1/chat/completions';
const payload = {
  model: 'gpt-3.5-turbo',
  messages: [
    {role: 'system', content: 'You are a helpful assistant.'},
    {role: 'user', content: prompt}
  ],
  max_tokens: 500
};

APIのエンドポイントURLとリクエストペイロードを設定しています。ペイロードにはモデル、メッセージ、トークン数の制限が含まれています。

APIリクエストのオプション設定

const options = {
  method: 'post',
  contentType: 'application/json',
  headers: {
    'Authorization': 'Bearer ' + secretKey
  },
  payload: JSON.stringify(payload)
};

APIリクエストのメソッド、コンテンツタイプ、ヘッダー、ペイロードを含むオプションを設定しています。ヘッダーには認証用のシークレットキーが含まれています。

APIリクエストの送信とレスポンスの処理

const apiResponse = UrlFetchApp.fetch(apiUrl, options);
const responseText = JSON.parse(apiResponse.getContentText());
const article = responseText.choices[0].message.content.trim();

APIリクエストを送信し、レスポンスを取得しています。レスポンスのテキストをJSON形式でパースし、生成された記事の内容を取得しています。

生成された記事のスプレッドシートへの書き込み

sheet.getRange('D2').setValue(article);

取得した記事をD2セルに書き込んでいます。

最後に

今回はchatgptとGASを使い日銀の会見の文字起こしを要約してスプシに出力しました。

今後も経済や金融関係の発信をしていきますのでよろしくおねがいします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?