cloudlflare推理llama2 - plus studio

2024-02-29 08:26:05 浏览数 (1)

cloudlflare推理llama2

最近,cloudlfare悄悄上线了一项新功能,全球网络上的gpu加速推理,显然的,我们可以用它推理llama2,cloudflare也提供了一个库进行推理。

新建一个cloudflare,然后,代码改成下面的,就可以进行推理了

代码语言:text复制
import { Ai } from './vendor/@cloudflare/ai.js';

export default {
  async fetch(request, env) {
    const tasks = [];
    const ai = new Ai(env.AI);

    // Get the request body
    const requestBody = await request.json();

    // messages - chat style input
    let chat = {
      messages: [
        { role: 'system', content: 'You are a helpful, kind, honest, friendly, good at writing and never fails to answer my requests immediately and with details and precision.'},
        { role: 'user', content: requestBody.prompt }
      ]
    };
    let response = await ai.run('@cf/meta/llama-2-7b-chat-int8', chat);
    tasks.push({ inputs: chat, response });

    return Response.json(tasks);
  }
};

我们可以测试一下,在命令行运行下面的命令,将https://example.workers.dev/ 换成你的网址。

代码语言:text复制
curl -X POST https://example.workers.dev/ -d '{"prompt":"Write a poem that talks about the connectivity cloud"}' 

看到类似下面的返回值代表成功

代码语言:text复制
[{"inputs":{"messages":[{"role":"system","content":"You are a helpful, kind, honest, friendly, good at writing and never fails to answer my requests immediately and with details and precision."},{"role":"user","content":"Write a poem that talks about the connectivity cloud"}]},"response":{"response":"In the realm of the digital sky,nWhere information flows, and data fly,nThere's a place that brings us all together,nA connectivity cloud, a true forever.nnIt's a space that's vast and wide,nWhere thoughts and ideas collide,nA hub of communication and exchange,nWhere the world's voices all combine and blend.nnIn this cloud of connectivity,nWe find our voices, our identity,nA platform for sharing and growth,nWhere our stories are told and our dreams take flight.nnWith just a click or tap of a key,nWe can connect with anyone, anywhere,nSharing laughter, love, and tears,nIn this digital embrace, we all share.nnSo let us cherish this cloud of connectivity,nThis gift that brings us all sovereignty,nFor in its depths, we find our tribe,nAnd our voices, heard, can never be denied.nnIn this cloud of connectivity,nWe are all connected, you see,nA global community, united and free,nIn this digital age, where we all can be"}}]

0 人点赞