Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 6x 5x 5x 5x 5x 5x 6x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 5x 5x 5x 6x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 6x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 4x 4x 4x 4x 4x 4x 4x 6x 2x 2x 2x 2x 2x 2x 2x 6x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 1x 1x 8x 9x 9x 1x 1x 7x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 9x 1x 1x 1x | "use strict";
import Message from "./message.js";
import { SECURITY_INSTRUCTIONS_LIST } from "./moderator.js";
import OpenAI from "openai";
const SECURITY_INSTRUCTIONS = SECURITY_INSTRUCTIONS_LIST.join("\n");
const COMPLETION_TEMPERATURE = 0;
const COMPLETION_LOGPROBS = true;
const COMPLETION_TOP_LOGPROBS = 3;
/**
* OpenAI chat client wrapper.
*
* Builds the conversation payload, sends it to OpenAI, and returns the reply
* with an inferred confidence score.
*
* @class
*/
class Client {
/**
* Initializes a new client.
*
* @param {string} apiKey - OpenAI API key.
* @param {object} [opts={}] - Client options.
* @param {string} [opts.model='gpt-5.2'] - Chat completion model.
* @param {string} [opts.instructions] - Base developer instructions.
* @param {boolean} [opts.enableSecurityInstructions=true] - Append security instructions to the base instructions.
*/
constructor(apiKey, opts) {
opts = opts || {};
const baseInstructions =
opts.instructions ||
"You are a helpful assistant in a Minecraft world. Answer questions and provide information relevant to the game.";
const enableSecurityInstructions = opts.enableSecurityInstructions ?? true;
this.opts = {
model: opts.model || "gpt-5.2",
instructions: enableSecurityInstructions
? `${baseInstructions}\n${SECURITY_INSTRUCTIONS}`
: baseInstructions,
};
this.openAI = new OpenAI({
apiKey: apiKey,
});
}
/**
* Send a message to OpenAI and return the generated reply.
*
* @param {Memory} memory - Per-player conversation memory.
* @param {string} player - Player name or id.
* @param {string} message - Player message.
* @returns {Promise<{reply: string, confidenceScore: number}>} Reply and confidence score.
*/
async chat(memory, player, message) {
const params = {
model: this.opts.model,
temperature: COMPLETION_TEMPERATURE,
logprobs: COMPLETION_LOGPROBS,
top_logprobs: COMPLETION_TOP_LOGPROBS,
messages: [{ role: "developer", content: this.opts.instructions }],
};
let conversation;
if (memory.exists(player)) {
// If there's prior conversation for the player,
// the conversation history will be included in the messages
// sent to OpenAI API in order to provide context
conversation = memory.retrieve(player);
} else {
// If there's no prior conversation for the player,
// then initialize a new conversation
memory.initialize(player);
conversation = memory.retrieve(player);
}
for (const message of conversation.getMessages()) {
params.messages.push({
role: message.getRole(),
content: message.getContent(),
});
}
const userMessage = new Message("user", message, Date.now());
params.messages.push({
role: userMessage.getRole(),
content: userMessage.getContent(),
});
let reply;
let confidenceScore;
try {
const chatCompletion = await this.openAI.chat.completions.create(params);
const firstChoice = chatCompletion.choices[0];
reply = firstChoice.message.content;
confidenceScore = this._extractConfidenceScore(firstChoice);
} catch (error) {
if (error instanceof OpenAI.APIError) {
error = new Error(
`An OpenAI error has occurred: ${error.status} ${error.type} ${error.code} ${error.message}`,
);
}
throw error;
}
// register the user message and assistant reply in memory
memory.register(player, userMessage);
const assistantMessage = new Message("assistant", reply, Date.now());
memory.register(player, assistantMessage);
return {
reply: reply,
confidenceScore: confidenceScore,
};
}
/**
* Use OpenAI's moderation API to check if the message violates content policy.
*
* @param {string} message - Message text to moderate.
* @returns {Promise<object>} Moderation result object.
*/
async moderate(message) {
try {
const moderation = await this.openAI.moderations.create({
input: message,
});
const result = moderation.results[0];
return {
flagged: result.flagged,
categories: result.categories,
category_scores: result.category_scores,
message: message,
};
} catch (error) {
if (error instanceof OpenAI.APIError) {
error = new Error(
`An OpenAI error has occurred: ${error.status} ${error.type} ${error.code} ${error.message}`,
);
}
throw error;
}
}
/**
* Clamp a confidence score to the range 0..1.
*
* @param {number} confidenceScore - Raw confidence score.
* @returns {number} Clamped confidence score.
*/
_clampConfidenceScore(confidenceScore) {
return Math.max(0, Math.min(1, confidenceScore));
}
/**
* Extract a confidence score from an OpenAI completion choice.
*
* @param {object} choice - OpenAI completion choice.
* @returns {number} Confidence score.
*/
_extractConfidenceScore(choice) {
const messageConfidence = choice?.message?.confidenceScore;
if (typeof messageConfidence === "number") {
return this._clampConfidenceScore(messageConfidence);
}
const messageConfidenceSnakeCase = choice?.message?.confidence_score;
if (typeof messageConfidenceSnakeCase === "number") {
return this._clampConfidenceScore(messageConfidenceSnakeCase);
}
const tokenLogProbs = choice?.logprobs?.content;
if (Array.isArray(tokenLogProbs) && tokenLogProbs.length > 0) {
const validLogProbs = tokenLogProbs
.map((item) => item?.logprob)
.filter((value) => typeof value === "number");
if (validLogProbs.length > 0) {
const averageLogProb =
validLogProbs.reduce((sum, value) => sum + value, 0) /
validLogProbs.length;
return this._clampConfidenceScore(Math.exp(averageLogProb));
}
}
return 1;
}
}
export { Client as default };
|