UNPKG

@langchain/community

Version:
114 lines (113 loc) 3.87 kB
import { __exportAll } from "../_virtual/_rolldown/runtime.js"; import { convertEventStreamToIterableReadableDataStream } from "../utils/event_source_parse.js"; import { GenerationChunk } from "@langchain/core/outputs"; import { getEnvironmentVariable } from "@langchain/core/utils/env"; import { LLM } from "@langchain/core/language_models/llms"; //#region src/llms/friendli.ts var friendli_exports = /* @__PURE__ */ __exportAll({ Friendli: () => Friendli }); /** * The Friendli class is used to interact with Friendli inference Endpoint models. * This requires your Friendli Token and Friendli Team which is autoloaded if not specified. */ var Friendli = class extends LLM { lc_serializable = true; static lc_name() { return "Friendli"; } get lc_secrets() { return { friendliToken: "FRIENDLI_TOKEN", friendliTeam: "FRIENDLI_TEAM" }; } model = "mixtral-8x7b-instruct-v0-1"; baseUrl = "https://inference.friendli.ai"; friendliToken; friendliTeam; frequencyPenalty; maxTokens; stop; temperature; topP; modelKwargs; constructor(fields) { super(fields); this.model = fields?.model ?? this.model; this.baseUrl = fields?.baseUrl ?? this.baseUrl; this.friendliToken = fields?.friendliToken ?? getEnvironmentVariable("FRIENDLI_TOKEN"); this.friendliTeam = fields?.friendliTeam ?? getEnvironmentVariable("FRIENDLI_TEAM"); this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty; this.maxTokens = fields?.maxTokens ?? this.maxTokens; this.stop = fields?.stop ?? this.stop; this.temperature = fields?.temperature ?? this.temperature; this.topP = fields?.topP ?? this.topP; this.modelKwargs = fields?.modelKwargs ?? {}; if (!this.friendliToken) throw new Error("Missing Friendli Token"); } _llmType() { return "friendli"; } constructHeaders(stream) { return { "Content-Type": "application/json", Accept: stream ? "text/event-stream" : "application/json", Authorization: `Bearer ${this.friendliToken}`, "X-Friendli-Team": this.friendliTeam ?? "" }; } constructBody(prompt, stream, _options) { return JSON.stringify({ prompt, stream, model: this.model, max_tokens: this.maxTokens, frequency_penalty: this.frequencyPenalty, stop: this.stop, temperature: this.temperature, top_p: this.topP, ...this.modelKwargs }); } /** * Calls the Friendli endpoint and retrieves the result. * @param {string} prompt The input prompt. * @returns {Promise<string>} A promise that resolves to the generated string. */ /** @ignore */ async _call(prompt, _options) { return (await this.caller.call(async () => fetch(`${this.baseUrl}/v1/completions`, { method: "POST", headers: this.constructHeaders(false), body: this.constructBody(prompt, false, _options) }).then((res) => res.json()))).choices[0].text; } async *_streamResponseChunks(prompt, _options, runManager) { const response = await this.caller.call(async () => fetch(`${this.baseUrl}/v1/completions`, { method: "POST", headers: this.constructHeaders(true), body: this.constructBody(prompt, true, _options) })); if (response.status !== 200 || !response.body) { const errorResponse = await response.json(); throw new Error(JSON.stringify(errorResponse)); } const stream = convertEventStreamToIterableReadableDataStream(response.body); for await (const chunk of stream) if (chunk.event !== "complete") { const generationChunk = new GenerationChunk({ text: JSON.parse(chunk).text ?? "" }); yield generationChunk; runManager?.handleLLMNewToken(generationChunk.text ?? ""); } else { const parsedChunk = JSON.parse(chunk); yield new GenerationChunk({ text: "", generationInfo: { choices: parsedChunk.choices, usage: parsedChunk.usage } }); } } }; //#endregion export { Friendli, friendli_exports }; //# sourceMappingURL=friendli.js.map