GithubHelp home page GithubHelp logo

deno-simple-proxy's Introduction

一分钟将代理OpenAI 的API

作为前端开发,一直听说Deno的大名,但遗憾的是至今还没有亲自尝试过编写一个简单的"Hello World"程序。现在,我打算利用Deno来实现一个简单的API代理服务,这旨在用于技术交流和学习,才不是因 为我发现了可以通过Deno Deploy的免费套餐来代理访问OpenAI的接口。这是一个探索和学习的过程,希望我们可以一起讨论和优化🐶。

第一步

登录https://dash.deno.com/projects,New Playground

第二步

复制下面代码

const targetOrigin = "https://api.openai.com";

Deno.serve(async (request) => {
  // 获取原始请求的 URL 和方法
  const { method, url, headers } = request;
  const originalUrl = new URL(url);
  
  // 记录日志
  console.info(`Incoming request: ${method} ${originalUrl.pathname}${originalUrl.search}`);

  // 构建目标 URL
  const targetUrl = new URL(originalUrl.pathname + originalUrl.search, targetOrigin);
  
  try {
    // 准备转发请求的选项
    const requestOptions: RequestInit = {
      method,
      headers: new Headers(headers), // 复制 headers
    };

    // 对于 POST、PUT、PATCH 方法,透传请求体
    if (request.method === "POST" || request.method === "PUT" || request.method === "PATCH") {
      requestOptions.body = await request.arrayBuffer();
    }

    console.info(`targetUrl: ${targetUrl}`);
    // console.debug('requestOptions:', requestOptions); // 里面会有API KEY,可另外处理。
    // 向目标 API 发起请求
    const response = await fetch(targetUrl.toString(), requestOptions);

    // 检查是否是 SSE
    const contentType = response.headers.get("Content-Type");
    console.info(`${targetUrl} response content type is:`, contentType);
    if (contentType === "text/event-stream") {
      // 返回一个新的 ReadableStream,用于将事件流传递给客户端
      const stream = new ReadableStream({
        start(controller) {
          const reader = response.body.getReader();
          return (function pump() {
            reader.read().then(({ done, value }) => {
              if (done) {
                controller.close();
                return;
              }
              controller.enqueue(value);
              pump();
            }).catch(error => {
              console.error('Error while reading the stream', error);
              controller.error(error);
              controller.close();
            });
          })();
        },
        cancel() {
          reader.cancel();
        }
      });

      // 返回 Response 对象,透传 SSE
      return new Response(stream, {
        headers: {
          "Content-Type": "text/event-stream",
          "Cache-Control": "no-cache",
          "Connection": "keep-alive",
        },
      });
    } else {
      // 其他类型的响应,直接透传
      const responseBody = await response.arrayBuffer();
      return new Response(responseBody, {
        status: response.status,
        headers: response.headers,
      });
    }
  } catch (error) {
    console.error(`Error proxying request to ${targetUrl}:`, error);
    return new Response(`Server error: ${error.message}`, { status: 500 });
  }
}, { port: 8000 });

console.log(`Proxy server running at http://localhost:8000/`);

第三步

https://platform.openai.com/api-keys 从里面来个OpenAI 的API key,发请求时候带上。 没Key的可以 https://bewildcard.com/i/HB0O8Q0Y 从里面注册,按照教程生成,有提供限时虚拟机的梯子,充值可以直接使用支付宝,省事儿;不过手续费不便宜。

客户端

可以去下载chatBox,将当前服务的域名设置在API域名既可。

第四步

Save & Deploy

验证

终端curl一哈

curl --location --request POST 'https://{{YOUR_PROJECT}}.deno.dev' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer {{YOUR_API_KEY}}' \
--data-raw '{
    "model": "gpt-3.5-turbo",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "Hello!"
      }
    ]
  }'

最后

当然,里面代码只是提供一些思路,仅供参考。Deno牛逼🐶。

deno-simple-proxy's People

Contributors

690591397 avatar

Stargazers

MuChen avatar

deno-simple-proxy's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.