@objectiveai/langchain
Version:
Objective AI integrations for LangChain.js
186 lines (185 loc) • 6.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryObjectiveAICustomOutputParser = exports.QueryObjectiveAIJsonSchemaOutputParser = exports.QueryObjectiveAIJsonObjectOutputParser = exports.QueryObjectiveAITextOutputParser = void 0;
const output_parsers_1 = require("@langchain/core/output_parsers");
const zod_1 = require("zod");
function throwNotObjectiveAIResponseError() {
throw new Error("ObjectiveAI Query parser should only be used with an ObjectiveAI Query response.");
}
function parseObjectiveAIResponse(text) {
let parsed;
try {
parsed = JSON.parse(text);
}
catch {
throwNotObjectiveAIResponseError();
}
if (!Array.isArray(parsed)) {
throwNotObjectiveAIResponseError();
}
const content = [];
for (const item of parsed) {
if (typeof item === "object" &&
item !== null &&
"output" in item &&
typeof item.output === "string" &&
"confidence" in item &&
typeof item.confidence === "number") {
content.push({
confidence: item.confidence,
output: item.output,
});
}
else {
throwNotObjectiveAIResponseError();
}
}
return content;
}
class QueryObjectiveAITextOutputParser extends output_parsers_1.BaseOutputParser {
constructor() {
super();
Object.defineProperty(this, "lc_namespace", {
enumerable: true,
configurable: true,
writable: true,
value: ["langchain", "output_parsers", "objectiveai"]
});
}
parse(text) {
return Promise.resolve(parseObjectiveAIResponse(text));
}
getFormatInstructions() {
return "";
}
}
exports.QueryObjectiveAITextOutputParser = QueryObjectiveAITextOutputParser;
class QueryObjectiveAIJsonObjectOutputParser extends output_parsers_1.BaseOutputParser {
constructor() {
super();
Object.defineProperty(this, "structuredParser", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "lc_namespace", {
enumerable: true,
configurable: true,
writable: true,
value: ["langchain", "output_parsers", "objectiveai"]
});
this.structuredParser = new output_parsers_1.StructuredOutputParser(zod_1.z.record(zod_1.z.unknown()));
}
async parse(text) {
const choicesRaw = parseObjectiveAIResponse(text);
// only throw for the winning choice, otherwise omit failures
const choicesPromises = choicesRaw.map(async ({ confidence, output: rawOutput }, i) => {
if (i === 0) {
const output = (await this.structuredParser.parse(rawOutput));
return { confidence, output };
}
else {
try {
const output = (await this.structuredParser.parse(rawOutput));
return { confidence, output };
}
catch {
return undefined;
}
}
});
const choices = await Promise.all(choicesPromises);
return choices.filter((c) => c !== undefined);
}
getFormatInstructions() {
return "";
}
}
exports.QueryObjectiveAIJsonObjectOutputParser = QueryObjectiveAIJsonObjectOutputParser;
class QueryObjectiveAIJsonSchemaOutputParser extends output_parsers_1.BaseOutputParser {
constructor(schema) {
super(schema);
Object.defineProperty(this, "structuredParser", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "lc_namespace", {
enumerable: true,
configurable: true,
writable: true,
value: ["langchain", "output_parsers", "objectiveai"]
});
this.structuredParser = new output_parsers_1.StructuredOutputParser(schema);
}
async parse(text) {
const choicesRaw = parseObjectiveAIResponse(text);
// only throw for the winning choice, otherwise omit failures
const choicesPromises = choicesRaw.map(async ({ confidence, output: rawOutput }, i) => {
if (i === 0) {
const output = await this.structuredParser.parse(rawOutput);
return { confidence, output };
}
else {
try {
const output = await this.structuredParser.parse(rawOutput);
return { confidence, output };
}
catch {
return undefined;
}
}
});
const choices = await Promise.all(choicesPromises);
return choices.filter((c) => c !== undefined);
}
getFormatInstructions() {
return "";
}
}
exports.QueryObjectiveAIJsonSchemaOutputParser = QueryObjectiveAIJsonSchemaOutputParser;
class QueryObjectiveAICustomOutputParser extends output_parsers_1.BaseOutputParser {
constructor(parseFunction, kwargs, ..._args) {
super(kwargs, ..._args);
Object.defineProperty(this, "parseFunction", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "lc_namespace", {
enumerable: true,
configurable: true,
writable: true,
value: ["langchain", "output_parsers", "objectiveai"]
});
this.parseFunction = parseFunction;
}
async parse(text) {
const choicesRaw = parseObjectiveAIResponse(text);
// only throw for the winning choice, otherwise omit failures
const choicesPromises = choicesRaw.map(async ({ confidence, output: rawOutput }, i) => {
if (i === 0) {
const output = await this.parseFunction(rawOutput);
return { confidence, output };
}
else {
try {
const output = await this.parseFunction(rawOutput);
return { confidence, output };
}
catch {
return undefined;
}
}
});
const choices = await Promise.all(choicesPromises);
return choices.filter((c) => c !== undefined);
}
getFormatInstructions() {
return "";
}
}
exports.QueryObjectiveAICustomOutputParser = QueryObjectiveAICustomOutputParser;