@visactor/vmind
Version:
<div align="center"> <a href="https://github.com/VisActor#gh-light-mode-only" target="_blank"> <img alt="VisActor Logo" width="200" src="https://github.com/VisActor/.github/blob/main/profile/logo_500_200_light.svg"/> </a> <a href="https://githu
129 lines (124 loc) • 4.62 kB
JavaScript
var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, generator) {
return new (P || (P = Promise))((function(resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
}
function step(result) {
var value;
result.done ? resolve(result.value) : (value = result.value, value instanceof P ? value : new P((function(resolve) {
resolve(value);
}))).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
}));
};
import { isFunction, merge } from "@visactor/vutils";
import axios from "axios";
import { Model } from "../types/llm";
import { matchJSONStr, parseLLMJson, revisedJSONStr } from "../utils/json";
export class LLMManage {
constructor(options) {
this.options = merge({}, this.getDefaultOptions(), options), this.historys = {};
}
getDefaultOptions() {
return {
url: "https://api.openai.com/v1/chat/completions",
headers: {
"Content-Type": "application/json"
},
method: "POST",
model: Model.GPT_4o,
maxTokens: 2048,
temperature: 0,
frequencyPenalty: 0
};
}
updateOptions(options) {
this.options = merge({}, this.options, options);
}
run(name, messages, tools) {
return __awaiter(this, void 0, void 0, (function*() {
const {url: url = "", headers: headers, method: method, maxTokens: maxTokens, temperature: temperature, model: model, frequencyPenalty: frequency_penalty, topP: top_p, customRequestFunc: customRequestFunc} = this.options;
this.historys[name] || (this.historys[name] = []);
try {
const res = (null == customRequestFunc ? void 0 : customRequestFunc[name]) && isFunction(customRequestFunc[name]) ? yield customRequestFunc[name](messages, tools, this.options) : yield axios(url, {
method: method,
headers: headers,
data: {
model: model,
messages: messages,
tools: tools,
max_tokens: maxTokens,
temperature: temperature,
stream: !1,
frequency_penalty: frequency_penalty,
top_p: top_p
}
}).then((response => response.data)), {logId: logId, id: id} = res;
return this.historys[name].push({
logId: logId,
id: id
}), res.error ? {
error: res.error
} : res;
} catch (err) {
return {
error: err
};
}
}));
}
parseTools(res) {
var _a;
const {choices: choices = [], error: error} = res;
if (error) return {
error: error
};
if (!choices.length) return {
error: "llm response is empty"
};
try {
return ((null === (_a = choices[0].message) || void 0 === _a ? void 0 : _a.tool_calls) || []).map((toolCall => Object.assign(Object.assign({}, toolCall), {
function: Object.assign(Object.assign({}, toolCall.function), {
arguments: parseLLMJson(toolCall.function.arguments)
})
})));
} catch (err) {
return {
error: err
};
}
}
parseJson(res) {
var _a;
const {choices: choices = [], error: error} = res;
if (error) return {
error: error
};
if (!choices.length) return {
error: "llm response is empty"
};
try {
const content = choices[0].message.content;
if ("tool_calls" === (null === (_a = choices[0]) || void 0 === _a ? void 0 : _a.finish_reason)) return {};
const jsonStr = revisedJSONStr(matchJSONStr(content));
return parseLLMJson(jsonStr, "```");
} catch (err) {
return {
error: err
};
}
}
}
//# sourceMappingURL=llm.js.map