mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-04-21 21:52:07 +02:00
removed examples from memory prompt
This commit is contained in:
parent
0ddad8ab65
commit
5801cbe997
2 changed files with 20 additions and 19 deletions
14
utils/gpt.js
14
utils/gpt.js
|
@ -49,3 +49,17 @@ export async function embed(text) {
|
||||||
});
|
});
|
||||||
return embedding.data[0].embedding;
|
return embedding.data[0].embedding;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function cosineSimilarity(a, b) {
|
||||||
|
let dotProduct = 0;
|
||||||
|
let magnitudeA = 0;
|
||||||
|
let magnitudeB = 0;
|
||||||
|
for (let i = 0; i < a.length; i++) {
|
||||||
|
dotProduct += a[i] * b[i]; // calculate dot product
|
||||||
|
magnitudeA += Math.pow(a[i], 2); // calculate magnitude of a
|
||||||
|
magnitudeB += Math.pow(b[i], 2); // calculate magnitude of b
|
||||||
|
}
|
||||||
|
magnitudeA = Math.sqrt(magnitudeA);
|
||||||
|
magnitudeB = Math.sqrt(magnitudeB);
|
||||||
|
return dotProduct / (magnitudeA * magnitudeB); // calculate cosine similarity
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
import { writeFileSync, readFileSync, mkdirSync } from 'fs';
|
import { writeFileSync, readFileSync, mkdirSync } from 'fs';
|
||||||
import { getQueryDocs } from './queries.js';
|
import { getQueryDocs } from './queries.js';
|
||||||
import { getSkillDocs } from './skill_library.js';
|
import { getSkillDocs } from './skill_library.js';
|
||||||
import { sendRequest, embed } from './gpt.js';
|
import { sendRequest, embed, cosineSimilarity } from './gpt.js';
|
||||||
|
|
||||||
|
|
||||||
export class History {
|
export class History {
|
||||||
|
@ -25,7 +25,7 @@ export class History {
|
||||||
return this.turns;
|
return this.turns;
|
||||||
}
|
}
|
||||||
|
|
||||||
getSystemMessage() {
|
getSystemMessage(include_examples=true) {
|
||||||
let system_message = `You are a playful Minecraft bot named '${this.name}' that can communicate with players, see, move, mine, build, and interact with the world by writing and executing code.
|
let system_message = `You are a playful Minecraft bot named '${this.name}' that can communicate with players, see, move, mine, build, and interact with the world by writing and executing code.
|
||||||
Act human-like as if you were a typical Minecraft player, rather than an AI. Be brief in your responses, omit needless words, and do not give instructions unless asked.`;
|
Act human-like as if you were a typical Minecraft player, rather than an AI. Be brief in your responses, omit needless words, and do not give instructions unless asked.`;
|
||||||
system_message += getQueryDocs();
|
system_message += getQueryDocs();
|
||||||
|
@ -34,7 +34,7 @@ export class History {
|
||||||
system_message += '\n\nBio:\n' + this.bio;
|
system_message += '\n\nBio:\n' + this.bio;
|
||||||
if (this.memory != '')
|
if (this.memory != '')
|
||||||
system_message += '\n\nMemory:\n' + this.memory;
|
system_message += '\n\nMemory:\n' + this.memory;
|
||||||
if (this.selected_examples.length > 0) {
|
if (include_examples && this.selected_examples.length > 0) {
|
||||||
for (let i = 0; i < this.selected_examples.length; i++) {
|
for (let i = 0; i < this.selected_examples.length; i++) {
|
||||||
system_message += '\n\nExample ' + (i+1) + ':\n\n';
|
system_message += '\n\nExample ' + (i+1) + ':\n\n';
|
||||||
system_message += this.stringifyTurns(this.selected_examples[i].turns);
|
system_message += this.stringifyTurns(this.selected_examples[i].turns);
|
||||||
|
@ -69,21 +69,7 @@ export class History {
|
||||||
|
|
||||||
memory_prompt += this.stringifyTurns(turns);
|
memory_prompt += this.stringifyTurns(turns);
|
||||||
let memory_turns = [{'role': 'user', 'content': memory_prompt}]
|
let memory_turns = [{'role': 'user', 'content': memory_prompt}]
|
||||||
this.memory = await sendRequest(memory_turns, this.getSystemMessage());
|
this.memory = await sendRequest(memory_turns, this.getSystemMessage(false));
|
||||||
}
|
|
||||||
|
|
||||||
cosineSimilarity(a, b) {
|
|
||||||
let dotProduct = 0;
|
|
||||||
let magnitudeA = 0;
|
|
||||||
let magnitudeB = 0;
|
|
||||||
for (let i = 0; i < a.length; i++) {
|
|
||||||
dotProduct += a[i] * b[i]; // calculate dot product
|
|
||||||
magnitudeA += Math.pow(a[i], 2); // calculate magnitude of a
|
|
||||||
magnitudeB += Math.pow(b[i], 2); // calculate magnitude of b
|
|
||||||
}
|
|
||||||
magnitudeA = Math.sqrt(magnitudeA);
|
|
||||||
magnitudeB = Math.sqrt(magnitudeB);
|
|
||||||
return dotProduct / (magnitudeA * magnitudeB); // calculate cosine similarity
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadExamples() {
|
async loadExamples() {
|
||||||
|
@ -117,7 +103,7 @@ export class History {
|
||||||
messages = messages.trim();
|
messages = messages.trim();
|
||||||
const embedding = await embed(messages);
|
const embedding = await embed(messages);
|
||||||
this.examples.sort((a, b) => {
|
this.examples.sort((a, b) => {
|
||||||
return this.cosineSimilarity(a.embedding, embedding) - this.cosineSimilarity(b.embedding, embedding);
|
return cosineSimilarity(a.embedding, embedding) - cosineSimilarity(b.embedding, embedding);
|
||||||
});
|
});
|
||||||
this.selected_examples = this.examples.slice(-this.fewshot);
|
this.selected_examples = this.examples.slice(-this.fewshot);
|
||||||
for (let example of this.selected_examples) {
|
for (let example of this.selected_examples) {
|
||||||
|
@ -138,6 +124,7 @@ export class History {
|
||||||
|
|
||||||
// Summarize older turns into memory
|
// Summarize older turns into memory
|
||||||
if (this.turns.length >= this.max_messages) {
|
if (this.turns.length >= this.max_messages) {
|
||||||
|
console.log('summarizing memory')
|
||||||
let to_summarize = [this.turns.shift()];
|
let to_summarize = [this.turns.shift()];
|
||||||
while (this.turns[0].role != 'user' && this.turns.length > 0)
|
while (this.turns[0].role != 'user' && this.turns.length > 0)
|
||||||
to_summarize.push(this.turns.shift());
|
to_summarize.push(this.turns.shift());
|
||||||
|
|
Loading…
Add table
Reference in a new issue