mantoufan/yzhantranslator 问题修复 & 功能扩展

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

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

mantoufan/yzhantranslator

最新稳定版本:2.1.2

Composer 安装命令:

composer require mantoufan/yzhantranslator

包简介

Language translater using OpenAI with cache. 使用 AI 翻译内容到不同语言,缓存结果以节省费用.

README 文档

README

YZhanTranslator is a PHP library for translating content using OpenAI's language models with caching capabilities. It supports translating strings, JSON objects and images arrays, as well as language detection.
YZhanTranslator can automatically cache (configurable) results to save costs.

Features

  • Translate strings and JSON objects
  • Detect language of input text (including JSON content)
  • Translate images by providing URLs and receiving descriptions
  • Cache translation results to save costs
  • Customizable prompts for fine-tuned translations
  • Built on top of YZhanGateway for flexible API interactions

Requirements

  • PHP 5.4 or higher
  • OpenAI API key
  • Composer for dependency management

Installation

Install YZhanTranslator using Composer:

composer require mantoufan/yzhantranslator

Usage

Basic Translation

use YZhanTranslator\YZhanTranslator;

$translator = new YZhanTranslator([
    'client' => 'OpenAI',
    'apiKey' => 'your_openai_api_key',
    'apiUrl' => 'https://api.openai.com',
    'organization' => 'your_openai_organization_id',
]);

$result = $translator->translate('Hello, world!', 'zh-CN');
echo $result; // 你好,世界!

JSON Translation

$json = json_encode(['greeting' => 'Hello']);
$result = $translator->translate($json, 'zh-CN', ['type' => 'json']);
print_r($result); // ['greeting' => '你好']

Language Detection

$languages = ['en', 'zh-CN', 'zh-TW', 'jp'];
$detectedLang = $translator->detect('こんにちは', $languages);
echo $detectedLang; // jp

Language JSON Detection

$json = json_encode(['greeting' => 'こんにちは']);
$languages = ['en', 'zh-CN', 'zh-TW', 'jp'];
$detectedLang = $translator->detect($json, $languages);
echo $detectedLang; // jp

Custom Prompts

$json = json_encode(['k' => '你好']);
$prompt = 'If there is a key named \'k\', retain the original value, but add a new key \'k2\' at the same level, containing the translated value.';
$result = $translator->translate($json, 'en', [
    'type' => 'json',
    'prompt' => $prompt
]);
print_r($result); // ['k' => '你好', 'k2' => 'Hello']

Images Descriptions

You can now translate images by providing an array of image URLs.
The translator will return descriptions for each image in the specified language.

use YZhanTranslator\YZhanTranslator;

$translator = new YZhanTranslator([
    'client' => 'OpenAI',
    'apiKey' => 'your_openai_api_key',
    'apiUrl' => 'https://api.openai.com',
    'organization' => 'your_openai_organization_id',
]);

$images = [
    'https://example.com/image1.jpg',
    'https://example.com/image2.jpg',
];
$result = $translator->translate(json_encode($images), 'zh-CN', ['type' => 'images']);
print_r($result); // ['https://example.com/image1.jpg' => ['description' => ''], 'https://example.com/image2.jpg' => ['description' => '']]

Configuration

YZhanTranslator uses environment variables for configuration. Create a .env file in your project root with the following contents:

OPENAI_APIKEY=your_openai_api_key
OPENAI_APIURL=https://api.openai.com
OPENAI_ORGANIZATION=your_openai_organization_id

Parameters

The translate() and detect() methods accept an optional $params array for additional configuration:

  • prompt: (string) Additional prompt to customize results, e.g., 'do not translate the href attribute'
  • cache: (array) Caching options
    • type: (string) Cache type (default is 'File')
    • params: (array) Cache-specific parameters
      • dir: (string) Directory for file-based caching
    • maxAge: (int) Cache expiration time in seconds
  • timeout: (int) Request timeout in seconds (default is 6 seconds)

Example usage with parameters:

$result = $translator->translate('Hello, world!', 'zh-CN', [
    'prompt' => 'Translate in a formal tone',
    'cache' => [
        'type' => 'File',
        'params' => ['dir' => '/tmp/cache'],
        'maxAge' => 3600
    ],
    'timeout' => 10
]);

Testing

Run the test suite using PHPUnit:

composer test

Generate a code coverage report:

composer coverage

License

This project is open-sourced software licensed under the MIT license.

Author

Shon Wu - GitHub

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

统计信息

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

GitHub 信息

  • Stars: 12
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-05