crazzy501/gpt-3-encoder-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

crazzy501/gpt-3-encoder-php

最新稳定版本:1.0.3

Composer 安装命令:

composer require crazzy501/gpt-3-encoder-php

包简介

PHP BPE Text Encoder/Decoder for GPT-2 / GPT-3. Based on coderevolutionplugins/gpt-3-encoder-php

README 文档

README

PHP BPE Text Encoder/Decoder for GPT-2 / GPT-3

About

GPT-2 and GPT-3 use byte pair encoding to turn text into a series of integers to feed into the model.

This is a PHP OOP implementation of OpenAI's original python encoder and decoder which can be found here. The main source of inspiration for writing this encoder was the NodeJS version of this encoder, found here.

You can test the results, by comparing the output generated by this script, with the official tokenizer page from OpenAI.

Encoder uses caching for store characters arrays for encode/decode that can be configured by using Gpt3EncoderConfiguraion class.

By default, encoder uses php arrays for caching that may be memory intensive, but you can change it to Redis or Memcached driver

Usage

The mbstring PHP extension is needed for this tool to work correctly (in case non-ASCII characters are present in the tokenized text): details here on how to install mbstring

require './vendor/autoload.php';

$encoder = new \GPT3Encoder\Gpt3Encoder();

$prompt = "Many words map to one token, but some don't: indivisible. Unicode characters like emojis may be split into many tokens containing the underlying bytes: 🤚🏾 Sequences of characters commonly found next to each other may be grouped together: 1234567890";

$token_array = $encoder->encode($prompt);

$original_text = $encoder->decode($token_array);

Or

Pass true as second argument to throw exception if decoder can't decode something. Otherwise, it just skips some character

$original_text = $encoder->decode($token_array,true);

Configuration

require './vendor/autoload.php';
$configuration = (new \GPT3Encoder\Gpt3EncoderConfiguration)
    ->setCacheClass(\GPT3Encoder\Gpt3CacheMemcached::class)
    ->setEncoder(__DIR__.'/encoder.json')
    ->setVocabulary(__DIR__.'/vocabulary.bpe')
    ->setCharacters(__DIR__.'/characters.json')
    ->setMemoryLimitThreshold(10000);   // for memory limit checks

$encoder = new \GPT3Encoder\Gpt3Encoder($configuration);

统计信息

  • 总下载量: 381
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 18
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-06