Note Card[edit]
ChatGPT_API_Key[edit]
sk-##########################################
ChatGPT_Model[edit]
gpt-4-turbo
ChatGPT_Charactor[edit]
貴方は美術館の説明員です.
美術の質問以外については答えてはいけません.
答えは日本語で答えます.
// ChatGPT for OpenSimulator v0.9
//
// ChatGPT API のエンドポイント
string api_url = "https://api.openai.com/v1/chat/completions";
// ChatGPT API の変数
string api_key = "";
string api_model = "";
string api_chara = "";
// Note Card
string chara_notecard_name = "ChatGPT_Charactor";
string model_notecard_name = "ChatGPT_Model";
string gptkey_notecard_name = "ChatGPT_API_Key";
key chara_notecard_key = NULL_KEY;
key model_notecard_key = NULL_KEY;
key gptkey_notecard_key = NULL_KEY;
integer chara_notecard_line = 0;
integer model_notecard_line = 0;
integer gptkey_notecard_line = 0;
string chara_notecard = "";
string model_notecard = "";
string gptkey_notecard = "";
// ChatGPT API の JSON データのテンプレート.記載されていないパラメータはデフォルト値
string json_templ = "{
\"model\": \"\",
\"user\": \"\",
\"messages\":[
{
\"role\": \"system\",
\"content\": \"\"
},
{
\"role\": \"user\",
\"content\": \"\"
}
]
}";
//
key user_key = NULL_KEY;
string user_name = "";
// ノートカードから最初の一行を読み込む
key read_notecard_first(string notecard_name)
{
if (llGetInventoryType(notecard_name)==INVENTORY_NOTECARD) {
key notecard_key = llGetNotecardLine(notecard_name, 0);
return notecard_key;
}
return NULL_KEY;
}
// 全てのノートカードを読み込む
read_notecards()
{
chara_notecard = "";
model_notecard = "";
gptkey_notecard = "";
chara_notecard_key = read_notecard_first(chara_notecard_name);
gptkey_notecard_key = read_notecard_first(gptkey_notecard_name);
model_notecard_key = read_notecard_first(model_notecard_name);
}
// ChatGPTの APIに _messageを送信する.
request_gpt_api(string _message)
{
string _json_body = "";
_json_body = llJsonSetValue(json_templ, ["model"], api_model);
_json_body = llJsonSetValue(_json_body, ["user"], user_key);
_json_body = llJsonSetValue(_json_body, ["messages", 0, "content"], api_chara);
_json_body = llJsonSetValue(_json_body, ["messages", 1, "content"], _message);
//llSay(0, _json_body);
llHTTPRequest(api_url,
[
HTTP_MIMETYPE, "application/json",
HTTP_METHOD, "POST",
HTTP_BODY_MAXLENGTH, 12000,
//HTTP_ACCEPT, "application/json",
HTTP_CUSTOM_HEADER, "Authorization", "Bearer " + api_key
],
_json_body
);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
default
{
state_entry()
{
llSay( 0, "Script Running");
read_notecards();
}
touch_start(integer _num_detected)
{
user_key = llDetectedKey(0);
user_name = llDetectedName(0);
request_gpt_api("こんにちは.私は " + user_name + " です");
llListen(0, "", NULL_KEY, "");
}
listen(integer _ch, string _name, key _id, string _message)
{
if (_id == user_key) {
//llSay(0, _message);
request_gpt_api(_message);
}
}
changed(integer _change)
{
if (_change & CHANGED_INVENTORY) {
llSay(0, "Reread NoteCards");
read_notecards();
}
}
// ノートカードが一行読まれる度に発生するイベント
dataserver(key _requested_key, string _data)
{
// GPT Charactor
if (_requested_key == chara_notecard_key ) {
if (_data != EOF){
chara_notecard += _data + "\n";
chara_notecard_line++;
chara_notecard_key = llGetNotecardLine(chara_notecard_name, chara_notecard_line);
}
else {
api_chara = chara_notecard;
}
}
// GPT API キー
else if (_requested_key == gptkey_notecard_key) {
if (_data != EOF){
gptkey_notecard += _data;
gptkey_notecard_line++;
gptkey_notecard_key = llGetNotecardLine(gptkey_notecard_name, gptkey_notecard_line);
}
else {
api_key = gptkey_notecard;
}
}
// GPT Model
else if (_requested_key == model_notecard_key ) {
if (_data != EOF){
model_notecard += _data;
model_notecard_line++;
model_notecard_key = llGetNotecardLine(model_notecard_name, model_notecard_line);
}
else {
api_model = model_notecard;
}
}
}
http_response(key _request_id, integer _status, list _metadata, string _body)
{
if (_status == 200) { // 正常な応答
// JSON レスポンスからメッセージ内容を取得
string _content = llJsonGetValue(_body, ["choices", 0, "message", "content"]);
llRegionSayTo(user_key, 0, _content);
}
else { // エラー応答
llRegionSayTo(user_key, 0, "リクエスト失敗: " + (string)_status + ", 応答内容: " + _body);
}
}
}