LLama for Nodejs

LLama for Nodejs

AIGC 这么火,小前端来蹭个热度,写了个 llama-node 的库

https://github.com/hlhr202/llama-node

起因是为了方便自己调用,又希望降低 llama.cpp 和 llama-rs 的调用门槛,我研究了一下 meta 开源的 llama 社区项目,最终找到了 llama-rs 这个 rust 库。

https://github.com/setzer22/llama-rs

对 rust 确实不太熟,还是喜欢留在 typescript 的安全区,所以决定用 llama-rs 写一个 nodejs 的库。

node addon 方面选择了 @太狼 大佬的 napi-rs 。

用法展示:

import path from "path";
import { LLamaClient } from "llama-node";

const model = path.resolve(process.cwd(), “./ggml-alpaca-7b-q4.bin”);

const client = new LLamaClient({ path: model, numCtxTokens: 4096 }, true);

const prompt = // Show an example of counter component in react.js. it has increment and decrement buttons where they change the state by 1. export const Counter => {;

client.createTextCompletion( { prompt, numPredict: BigInt(2048), temp: 0.2, topP: 0.8, topK: BigInt(40), repeatPenalty: 1, repeatLastN: BigInt(512), }, (res) => { process.stdout.write(res.token); } );

结论还是:写 ai 程序也是撸 typescript 更快。。。就这样先将就用着吧。rust 写的不好,各位看官轻喷。


2 回复

IT 行业有头部通吃原则。

习惯了 OpenGPT 后,别的 Ai 回答都像刚毕业的小学生,用了几次后,实在没有兴趣继续使用了。


LLama for Node.js 听起来像是一个为 Node.js 环境设计的库或者工具,可能是用于与某个服务(比如 LLaMA 模型)进行交互的。由于 LLaMA(Large Language Model Family of AI)是 OpenAI 或类似机构开发的大型语言模型的一部分,但具体名称(LLaMA 而不是 GPT 或 Davinci 等)可能有所混淆或特指某个变种。不过,我会基于假设这是一个与大型语言模型交互的 Node.js 库来进行回答。

要在 Node.js 中使用 LLaMA(或类似模型)的服务,通常需要通过 API 调用。以下是一个简单的示例,展示如何使用 axios 库来发送请求到假设的 LLaMA 服务 API:

const axios = require('axios');

async function generateText(prompt) {
  try {
    const response = await axios.post('https://api.llama-service.com/generate', {
      prompt: prompt,
      // 可能还需要其他参数,如模型ID、最大生成长度等
    });
    console.log(response.data.generatedText);
  } catch (error) {
    console.error('Error generating text:', error);
  }
}

generateText('Write a short story about a magical forest.');

请注意,上述代码中的 URL (https://api.llama-service.com/generate) 是虚构的,你需要替换为实际服务的 API 端点。此外,根据 API 的要求,你可能需要添加认证信息(如 API 密钥)到请求头中。

如果你指的是一个具体的库或工具,请提供更多细节,以便给出更准确的指导。

回到顶部