Visual Studio CodeからOpen AI ChatGPTを使う拡張機能

以前、テキストエディタMeryからOpenAI APIをたたくマクロをつくりました。

テキストエディタMeryからOpenAI ChatGPTを使うマクロ - attosci diary

しかし、最近はVSCodeを使うことも多くなりましたので、拡張機能として同様な動作をさせたいと思い、TypeScritバージョンを作りました。環境変数としてOPENAI_API_KEYにOpenAI API Keyを設定する必要があります。

拡張機能の作り方は以下のサイトを参考にしました。使うモデルはコードの中に埋め込んでいるので必要に応じて修正する必要があります。

VSCode Extensions(拡張機能) 自作入門 〜VSCodeにおみくじ機能を追加する〜 #VSCode - Qiita

import * as vscode from 'vscode';
import { OpenAI } from 'openai';

export function activate(context: vscode.ExtensionContext) {

    console.log('Extension "vscode-openai" is now active!');

    const disposable = vscode.commands.registerCommand('vscode-openai.sendToChatGPT', async () => {
        const editor = vscode.window.activeTextEditor;

        if (!editor) {
            vscode.window.showInformationMessage('No active editor!');
            return; // No active editor
        }

        const document = editor.document;
        const selection = editor.selection;
        const text = selection.isEmpty ? document.getText() : document.getText(selection);

        try {
            const response = await sendToChatGPT(text);
            appendTextToDocument(editor, response);
        } catch (error) {
            if (error instanceof Error) {
                // We can now safely read the message property of the Error instance
                vscode.window.showErrorMessage('Error communicating with OpenAI: ' + error.message);
            } else {
                // For other types of thrown values that are not Error instances
                vscode.window.showErrorMessage('An unknown error occurred.');
            }
        }
    });

    context.subscriptions.push(disposable);
}

async function sendToChatGPT(text: string): Promise<string> {
    // Obtain OpenAI API Key from environment variable OPENAI_API_KEY
    const apiKey = process.env.OPENAI_API_KEY;
    if (!apiKey) {
        throw new Error('OpenAI API key is not set in the environment variable OPENAI_API_KEY.');
    }
    const openai = new OpenAI({ apiKey: apiKey });
    const model = vscode.workspace.getConfiguration().get('vscode-openai.model', 'gpt-4-turbo');
    const messages: any[] = makeMessages(text);
    const payload = {
        model: model,
        messages: messages
    };

    const response = await openai.chat.completions.create(payload);
    if (response.choices && response.choices.length > 0 && response.choices[0].message.content) {
        return response.choices[0].message.content;
    } else {
        throw new Error('Invalid response from the API.');
    }
}

function appendTextToDocument(editor: vscode.TextEditor, text: string) {
    editor.edit(editBuilder => {
        const document = editor.document;
        let selection = editor.selection;
        let position: vscode.Position;

        if (!selection.isEmpty) {
            position = selection.end;
        } else {
            const lastLine = document.lineAt(document.lineCount - 1);
            position = new vscode.Position(document.lineCount - 1, lastLine.text.length);
        }

        const assistantMarker = "\n\n### assistant\n\n";
        const userMarker = "\n\n### user\n\n";
        const fullText = assistantMarker + text + userMarker;
        editBuilder.insert(position, fullText);
    });
}

function makeMessages(text: string): any {
    const lines = text.split('\n');

    let messages: any[] = [];
    const roles: string[] = ['system', 'assistant', 'user'];
    let current_role: string = 'user';
    let content: string = '';

    for (const line of lines) {
        let roleHeadline: boolean = false;
        for (const role in roles) {
            if (line.startsWith(`### ${role}`)) {
                if (content.length > 0) {
                    messages.push({ "role": current_role, "content": content});
                }
                current_role = role;
                content = '';
                roleHeadline = true;
                break;
            }
        }
        if (!roleHeadline) {
            content += line + '\n';
        }
    }
    if (content.length > 0) {
        messages.push({ "role": current_role, "content": content});
    }
    return messages;
}

export function deactivate() {}

テキストエディタMeryからOpenAI ChatGPTを使うマクロ

ここ数年はテキストエディタとしてMeryを使っています。最近はVisual Studio Codeをコーディングに使う事がめっきり多くなりましたが、軽量なテキストエディタはなかなか手放せません。さて、最近流行りのChatGPTですが、これをエディタの中で使うと便利ではないかと思い立ちMeryのマクロを作ってみました。質問したい内容を選択状態にしておきマクロを実行します。しばらくすると答えが帰ってきます。Pythonマクロなので若干の設定が必要なことと、OpenAI API Keyの取得が必要です。OpenAI API Keyの値をOPENAI_API_KEYという名前の環境変数として設定してください。

from openai import OpenAI
# import json


def make_messages(text):

    roles = ['system', 'assistant', 'user']
    messages = []
    role = 'user'
    content = ''

    lines = text.splitlines()
    for line in lines:
        role_headline = False
        for key in roles:
            if line == f'### {key}':
                if content:
                    messages.append({"role": role, "content": content})
                role = key
                content = ''
                role_headline = True
                break
        if not role_headline:
            content += line + '\n'    

    if content:
        messages.append({"role": role, "content": content})
    
    return messages


client = OpenAI()

if not window.document.selection.Text:
    window.document.selection.SelectAll()

text = window.document.selection.Text

messages = make_messages(text)

# window.alert(json.dumps(messages))

if messages:
    response = client.chat.completions.create(model="gpt-4-turbo", messages=messages)
    content = response.choices[0].message.content.lstrip()    
    window.document.selection.Text = text + '\n\n### assistant\n\n' + content + '\n\n### user\n\n'
  • MeryマクロをPythonで記述できる様にする方法は次のリンクを参考にしました。 知っておくと便利なテキストエディター「Mery」の 10 個の小技

  • このマクロを実行するためには OpenAI API のキーを入手する必要があります。

  • 見出し### systemをつけると以下をsystem contentとして扱います。

  • 見出し### userをつけると以下をuser contentとして扱います。

  • 見出し### assistantをつけると以下をassistant contentとして扱います。

ユピテルY-300dPの画像から加速度データを得る

最近、ドライブレコーダーを買いました。 ユピテル YupiteruY-300dPです。 発売は2021年と少々古いのですが、性能が良く値段が手頃なので気に入っています。 このドラレコは衝突や急ブレーキ、急ハンドルなどで異常な加速度を検知した場合、 前後40秒のデータを別フォルダに格納する機能がついています。 その際に加速度の閾値を適切に決めないと頻繁にアラーム音とともに動作してしまうので 設定を調整する必要があります。 画像をPCで確認するプログラムとのことで、 調べてみると字幕データとしてフレームごとに記録されているようです。 というわけでPythonコードを書いてみました。

# AVI Audio Video Interface
# RIFF Resource Interchange File Format

import struct

#
# Read RIFF Header
#
def read_RIFF_header(f):
    buffer = f.read(12)
    riff, size, type = struct.unpack('<4sI4s', buffer)
    if riff != b'RIFF' or type != b'AVI ':
        raise ValueError('Not a valid AVI file')
    return size

def read_list(f, read_data=True):
    buffer = f.read(12)
    id, size, type = struct.unpack('<4sI4s', buffer)
    if id != b'LIST':
        raise ValueError('Not LIST.')
    if read_data:
        buffer = f.read(size - 4)
        if size % 2 == 1:
            f.read(1) # Read one more byte if size is odd.
        return size, type, buffer
    return size, type

def read_chunk(f, read_data=True):
    buffer = f.read(8)
    id, size = struct.unpack('<4sI', buffer)
    if read_data:
        buffer = f.read(size)
        if size % 2 == 1:
            f.read(1) # Read one more byte if the size is odd.
        return id, size, buffer
    return id, size

file = '~~~.avi'
f = open(file, 'rb')
#
# Check RIFF Header
#
size = read_RIFF_header(f)
size, type = read_list(f, read_data=False)
if type != b'hdrl':
    raise ValueError('list_type is not hdrl.')
#
# Main AVI Header
#
id, size, buffer = read_chunk(f)
if id != b'avih':
    raise ValueError('chunk_type is not avih.')
#
# Stream Header Lists
# fccType = 'vids', 'auds', 'txts'
#
for i in range(3):
    size, type, buffer = read_list(f)
    if type != b'strl':
        raise ValueError('list_type is not strl.')
#
# JUNK Chunks
#
for i in range(2):
    id, size, buffer = read_chunk(f)
    if id != b'JUNK':
        raise ValueError('chunk_type is not JUNK.')
#
# movi List
#
movi_size, type = read_list(f, read_data=False)
if type != b'movi':
    raise ValueError('listtype is not movi.')


current_size = 0
ax_list = []
ay_list = []
az_list = []
while current_size < movi_size:
    id, size, buffer = read_chunk(f)
    if id == b'02tx':
        f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, \
            ax, ay, az, n, speed, latitude, longitude, unix_time = struct.unpack('<13fI3fI', buffer)
        ax_list.append(ax)
        ay_list.append(ay)
        az_list.append(az)
        print(ax, ay, az, n, speed, latitude, longitude, unix_time)
    current_size += size

f.close()

私が購入したドラレコの型番はY-300dpなのですが、 LaBoon!!さんのサイトでは、 同様なハードが様々な型番(WDT700c, SN-TW9900d, SN-TW9800d, Y-300C, Y-300R, Y-210R, SN-TW99c, SN-TW84d)で販売されているとの事で、 ひょっとしたらこれらのドラレコでもデータが抽出できるかもしれません。

Pythonメモ

全くのところ、自分用のメモです。

始めの宣言

%matplotlib widget
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()

区切りの良いカラー番号

Pythonとは関係ありませんが、0から255まで等間隔に区切りたいだけです。17の倍数です。255を素因数分解すると、

255 = 17 * 5 * 3

となる訳で。

# 0から255まで4段階に分ける
0, 85, 170, 255,
# 0から255まで6段階に分ける
0, 51, 102, 153, 204, 255,
# 0から255まで16段階に分ける
0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255

matplotlibのデフォルトカラーコード

'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'

グラフの描画

# seaborn-paper, seaborn-notebook, seaborn-talk, seaborn-posterの順で文字が大きくなる
with plt.style.context('seaborn-poster'):
    fig, axes = plt.subplots(2, 2, figsize=(8,6))
    ax = np.rabel(axes)
    ....
    plt.tight_layout()
    plt.show()

インタラクティブなmatplotlibグラフ表示時のショートカット

Command Shortcut(s)
Home/Reset 'h', 'r', 'home'
Back 'left', 'c', 'backspace'
Forward 'right', 'v'
Pan/Zoom 'p'
Zoom-to-rect 'o'
Save 's', 'ctrl+s'
Toggle fullscreen 'f', 'ctrl+f'
Toggle major grids 'g'
Toggle minor grids 'G'
Toggle x axis scale (log/linear) 'k', 'L'
Toggle y axis scale (log/linear) 'l'
Close Figure 'ctrl+w', 'cmd+w', 'q'
Constrain pan/zoom to x axis hold x when panning/zooming with mouse
Constrain pan/zoom to y axis hold y when panning/zooming with mouse
Preserve aspect ratio hold CONTROL when panning/zooming with mouse

好みのグラフにする

# グラフのサイズ
plt.rcParams['figure.figsize'] = (12, 5.5)
# 外枠を消す
#plt.rcParams['axes.spines.left'] = False
plt.rcParams['axes.spines.right'] = False
#plt.rcParams['axes.spines.bottom'] = False
plt.rcParams['axes.spines.top'] = False
# 左の目盛りを消す
plt.rcParams['ytick.left'] = False
# 水平グリッドを表示する
plt.rcParams['axes.grid.axis'] = 'y'
plt.rcParams['axes.grid'] = True
plt.rcParams['grid.linestyle'] = 'dotted'
# カラーマップを再定義する
colors = plt.cm.get_cmap('tab20c')(np.arange(0, 1, 0.05))
plt.rcParams['axes.prop_cycle'] = plt.cycler(color=colors)

Pythonによる分散分析

分散分析(Analysis Of VAriance; ANOVA)は3標本以上の差の検定です。

Pythonの統計パッケージには、scipy.statsやstatsmodelsがあります。最近はpingouinというものもあります。いずれもANOVAをサポートしています。それぞれのパッケージでの使用方法を説明します。

scipy.statsによるANOVA

from scipy import stats

x = [43, 55, 57, 72, 51, 52, 48, 46, 58, 54]
y = [53, 44, 54, 51, 68, 64, 45, 67, 49, 56]
z = [77, 55, 67, 54, 46, 75, 65, 57, 49, 61]
print(stats.f_oneway(x, y, z))

結果

F_onewayResult(statistic=1.6463168290164742, pvalue=0.21152459574247603)

statsmodelsによるANOVA

import pandas as pd
import statsmodels.api as sm
from statsmodels.formula.api import ols

x = [43, 55, 57, 72, 51, 52, 48, 46, 58, 54]
y = [53, 44, 54, 51, 68, 64, 45, 67, 49, 56]
z = [77, 55, 67, 54, 46, 75, 65, 57, 49, 61]

values = x + y + z
groups = ['x'] * len(x) + ['y'] * len(y) + ['z'] * len(z)
data = pd.DataFrame({'values': values, 'groups': groups})

lm = ols('values ~ groups', data=data).fit()
sm.stats.anova_lm(lm, typ=2) # Type 2 Anova DataFrame

結果

               sum_sq    df         F    PR(>F)
groups     271.666667   2.0  1.646317  0.211525
Residual  2227.700000  27.0       NaN       NaN

pingouinによるANOVA

import pandas as pd
import pingouin as pg

x = [43, 55, 57, 72, 51, 52, 48, 46, 58, 54]
y = [53, 44, 54, 51, 68, 64, 45, 67, 49, 56]
z = [77, 55, 67, 54, 46, 75, 65, 57, 49, 61]

values = x + y + z
groups = ['x'] * len(x) + ['y'] * len(y) + ['z'] * len(z)
data = pd.DataFrame({'values': values, 'groups': groups})

print(pg.anova(data, dv='values', between='groups'))

結果

   Source  ddof1  ddof2         F     p-unc       np2
0  groups      2     27  1.646317  0.211525  0.108694