Giter Site home page Giter Site logo

okysu / chatgpt-azureapi-web-backend Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 1.0 121 KB

一个适用于 Azure API 的 ChatGPT 的 Web 界面(后端部分)。 A ChatGPT Web for Azure OpenAI API (Backend Part).

Home Page: https://chat-gpt-azure-api-web-backend.vercel.app

License: MIT License

JavaScript 100.00%

chatgpt-azureapi-web-backend's Introduction

ChatGPT-AzureAPI-Web

一个适用于 Azure API 的 ChatGPT 的 Web 界面(后端部分)。

A ChatGPT Web for Azure OpenAI API (Backend Part).

Tips: If you have built the backend, you can build the frontend.

提示:如果你已经构建了后端,你可以构建前端。

ChatGPT-AzureAPI-Web-Frontend

Overview 概览

image

image

image

Features 特性

  • Authorization Key 授权密钥

You can set your own Authorization Key to prevent others from using your API.

你可以设置自己的授权密钥,以防止他人使用你的 API。

  • History 历史记录

You can view the history of your conversations. and set the messages to be sent.

你可以查看你的对话历史记录,并设置要发送的消息。

  • Prompt 提示

You can set the prompt to facilitate your conversation.

内置中英文双语提示,快捷完成你的对话。

  • i18n 多语言 (In progress)

Usage 使用

dependencies 依赖

pnpm install

dev 开发

Before you start, you need to set the .env.dev file.

# Azure OpenAI
AZURE_API_KEY = ''
AZURE_API_ENDPOINT = ''

# Key if has more than one key, separated by ','
APP_SECRET_KEY = '20030310,MLSA,MSFT'

# Model, if has more than one model, separated by ','
MODEL_NAME = 'gpt-35-turbo'

# Compressed length(Optional)
COMPRESSED_LENGTH = 4000

build 构建

Before you start, you need to set the .env.prod file.

# Azure OpenAI
AZURE_API_KEY = ''
AZURE_API_ENDPOINT = ''

# Key if has more than one key, separated by ','
APP_SECRET_KEY = '20030310,MLSA,MSFT'

# Model, if has more than one model, separated by ','
MODEL_NAME = 'gpt-35-turbo'

# Compressed length(Optional)
COMPRESSED_LENGTH = 4000

run 运行

pnpm run start

Optimization 优化

To avoid the length of the returned message exceeding the maximum length supported by the model, we have set an (optional) environment variable COMPRESSED_LENGTH, with a default value of 4000. If the threshold is exceeded, we will try our best to preserve the messages (messages with the role of "system" will not be deleted), and continuously delete old messages to ensure that the length does not exceed the set threshold.

避免回传的消息长度大于模型所支持的最大长度,我们设置了一个(可选)环境变量COMPRESSED_LENGTH,默认值为 4000,如果超过了设置的阈值,我们将尽可能的保留消息(角色为 system 的消息将不会被删除),不断的删除旧消息来保证长度不会超过设置的阈值。

// count the number of tokens
let totalTokens = 0;
messages.forEach((item) => {
  totalTokens += encode(item.content).length;
});

// if you want to use more tokens, you can change the value of COMPRESSED_LENGTH in env
const maximumTokens = process.env.COMPRESSED_LENGTH
  ? parseInt(process.env.COMPRESSED_LENGTH)
  : 4000;

if (totalTokens > maximumTokens && messages.length > 1) {
  // save all messages that its role is system, and remove others
  let index = 0;
  while (totalTokens > maximumTokens && index < messages.length) {
    if (messages[index].role !== "system") {
      const tokens = encode(messages[index].content).length;
      messages.splice(index, 1);
      totalTokens -= tokens;
    }
    index++;
  }
} else if (totalTokens > maximumTokens) {
  // if there is only one message, then return error
  const response = {
    code: -1,
    msg: "Messages too long.",
    data: null,
  };
  res.status(400).json(response);
  return;
}

chatgpt-azureapi-web-backend's People

Contributors

okysu avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

finrila

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.