Refactor calculating score. Add sort node for vanity
This commit is contained in:
@@ -2,49 +2,59 @@ import { GraphNode } from "@langchain/langgraph";
|
||||
import { MessagesState } from "../state";
|
||||
import { BaseMessage } from "@langchain/core/messages";
|
||||
|
||||
type Priority = keyof typeof mapping;
|
||||
//TODO: Each of these might need different weights
|
||||
const keys = ["CONFIDENCE", "RAGAS", "RELATION"];
|
||||
|
||||
const mapping = {
|
||||
VERYHIGH: 1.0,
|
||||
HIGH: 0.75,
|
||||
MEDIUM: 0.5,
|
||||
LOW: 0.25,
|
||||
VERYLOW: 0.0
|
||||
VERYLOW: 0.0,
|
||||
} as const;
|
||||
|
||||
function mapResponse(value: string): number {
|
||||
const upper = value.toUpperCase() as Priority;
|
||||
type Priority = keyof typeof mapping;
|
||||
|
||||
if (upper in mapping) {
|
||||
return mapping[upper];
|
||||
}
|
||||
function mapResponse(value: string | undefined | null): number {
|
||||
if (!value) return 0;
|
||||
|
||||
return 0;
|
||||
const trimmed = value.trim();
|
||||
const num = parseFloat(trimmed);
|
||||
|
||||
// If number, return it
|
||||
if (!isNaN(num)) return num;
|
||||
|
||||
// Otherwise, map to value
|
||||
const upper = trimmed.toUpperCase() as Priority;
|
||||
return mapping[upper] ?? 0;
|
||||
}
|
||||
|
||||
function getLastMessageContaining(
|
||||
messages: BaseMessage[],
|
||||
searchString: string
|
||||
): string {
|
||||
): string | null {
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
const content = messages[i].content;
|
||||
if (typeof content === "string" && content.includes(searchString)) {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
|
||||
export const produceRanking: GraphNode<typeof MessagesState> = async (state) => {
|
||||
//TODO: what should these weights be
|
||||
let conf = getLastMessageContaining(state.messages, "CONFIDENCE")?.split(":")[1] //TODO: we can better error handle here
|
||||
let ragas = getLastMessageContaining(state.messages, "RAGAS")?.split(":")[1] //TODO: we can genericify this too surely
|
||||
let rel = getLastMessageContaining(state.messages, "RELATION")?.split(":")[1]
|
||||
// Extract and map values
|
||||
const values = keys.map((key) => {
|
||||
const msg = getLastMessageContaining(state.messages, key);
|
||||
const part = msg?.split(":").at(1);
|
||||
return mapResponse(part);
|
||||
});
|
||||
|
||||
let result = mapResponse(conf) * Number.parseFloat(ragas) * mapResponse(rel)
|
||||
|
||||
let current = state.proposedTriggerEvent;
|
||||
// Multiply!
|
||||
const result = values.reduce((acc, val) => acc * val, 1);
|
||||
|
||||
const current = state.proposedTriggerEvent;
|
||||
current[state.proposedTriggerEventIndex].score = result;
|
||||
|
||||
return { proposedTriggerEvent: current };
|
||||
};
|
||||
return { proposedTriggerEvent: current };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user