Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenAI integration #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mirzaaghazadeh
Copy link

This PR adds OpenAI's language models as an alternative translation provider alongside the existing Google Translate functionality.

Key changes:

Features:

  • Add command-line provider selection (--provider option)
  • Add OpenAI model selection (--model option)
  • Add ChatGPT translation service with customizable models
  • Maintain backward compatibility with Google Translate

Technical Changes:

  • Introduce TranslatorInterface for consistent translator implementations
  • Add TranslatorFactory for provider instantiation
  • Add configuration for OpenAI API settings
  • Update command to handle provider and model selection
  • Improve error handling for invalid configurations

Copy link
Owner

@alisalehi1380 alisalehi1380 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

اول از همه ممنون بابت وقتی که گذاشتی. صمیمانه متشکرم که باعث پیشرفت و اضافه شدن ai به این پکیج شدی. 🙏🌹

به طور کلی اگه خواییم بهش نگاه کنیم در واقع ما داریم درایور openai رو اضافه می‌کنیم. (Drivers not Translators)
یک سرویس هم بیشتر نداریم. دایرکتوری services رو حذف کن. TranslateService.php رو بزار تو commands.

و در نهایت اگه وقت یا حالشو نداری بیخیال همین خوبه. ممنون از مشارکتت. 🌱✌️

public function __construct()
{
$this->apiKey = config('lang-files-translator.chatgpt.api_key');
$this->temperature = config('lang-files-translator.chatgpt.temperature', 0.3);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

به نظرم چون توی کانفیگ نوشتی، از اینجا برش دار.

config('lang-files-translator.chatgpt.temperature')

public function __construct()
{
$this->translator = new GoogleTranslate();
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

میتونیم به این صورت ریفکتورش کنیم.

public function __construct(private GoogleTranslate $translator) {}

private string $model;
private float $temperature;

public function __construct()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

میتونیم از Constructor property promotion ورژن 8 استفاده کنیم و این طوری بنویسیم.

public function __construct(
        private string $source,
        private string $target,
        private string $model,
        private string $apiKey,
        private float $temperature
    ) {

protected $signature = 'translate:lang {from : translate from language} {to : translate to language}';
protected $signature = 'translate:lang {from : translate from language} {to : translate to language}
{--provider=google : translation provider (google or openai)}
{--model= : OpenAI model name (required when provider is openai)}';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

برای راحتی استفاده، امکانش هست یه مدل پیشفرض داشته باشیم؟ اگر کاربر خواست تغییرش بده.

تو README دیدم نوشته بودی. اگه صلاح میدونی که خوبه، اینجا دیفالت همون رو بزار.

public function withProvider(string $provider, ?string $model = null): TranslateService
{
$this->provider = $provider;
$this->model = $model;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

به نظرم دیفالت گذاشتن اینها خیلی منطقی نیست. خطوط 14 و 15

در اینجا

$translateService
            ->to($this->argument('to'))
            ->from($this->argument('from'))
            ->withProvider($provider, $this->option('model'))
            ->translate();

مقادیر provider و model رو داریم پاس میدیم. در خطوط بالایی هم تمام ولیدیشن هایی که برای این دو فیلد نیاز بوده، چک شده. پس اینجا قطعا این مقادیر وجود دارند.

دیفالت null رو هم دیگه نیازی بهش نداریم.

public function setProvider(string $provider, ?string $model): TranslateService

@@ -23,6 +26,13 @@ public function to(string $to): TranslateService
$this->translate_to = $to;
return $this;
}

public function withProvider(string $provider, ?string $model = null): TranslateService
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

بشه setProvider


class TranslatorFactory
{
public static function create(string $provider = 'google', ?string $model = null): TranslatorInterface
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +14 to +16
if (empty($model)) {
throw new \InvalidArgumentException('Model parameter is required for OpenAI provider');
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

نیازی به چک کردن نیست. قبلا چک شده و اینجا حتما هستش

Comment on lines +21 to +75
After installation, publish the configuration file:
```
php artisan vendor:publish --tag=lang-files-translator-config
```

# Configuration

The package supports two translation providers:
- Google Translate (default)
- OpenAI (requires API key)

To use OpenAI for translations, set your API key in `.env`:
```
php artisan translate:lang {from} {to}
OPENAI_API_KEY=your-api-key
```
for example, your locale is English and you have en lang files and want to have these files to Persian(fa) lang too.
just enough to run:

# 💎Usage

The package provides a simple artisan command to translate your language files:

```bash
php artisan translate:lang {from} {to} [options]
```

## Available Options

### Using Google Translate (Default)
```bash
php artisan translate:lang en fa
```
and done!
Go to lang/fa and you will see all the translated files from the en folder.
No configuration needed, just run the command.

### Using OpenAI
First, add your OpenAI API key to `.env`:
```
OPENAI_API_KEY=your-api-key
```

Then run the command with OpenAI options:
```bash
php artisan translate:lang en fa --provider=openai --model=gpt-3.5-turbo
```

how to use video ⤵️
## Command Reference

### Arguments:
- `from`: Source language code (e.g., en)
- `to`: Target language code (e.g., fa)

### Options:
- `--provider`: Translation provider (google or openai)
- `--model`: OpenAI model name (required when using openai)

After running the command, translated files will be created in the `lang/{target-language}` folder.

Copy link
Owner

@alisalehi1380 alisalehi1380 Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

اینو بزار اینجا.
کلا زمانی که زیاد توضیح میدی طرف فکر میکنه خیلی سخته کارکردن با پکیج شما.
تمام توضیحاتی که دادی رو باید طرف با کامند php artisan translate:lang -help در بیاره.

نکته آخر: به طرف اجازه نده فکر کنه. اون دلش میخواد فقط کامند ها رو کپی پیست کنه :)

publish the configuration:

php artisan vendor:publish --tag=lang-files-translator-config

💎Usage

Google Translate (Default)

php artisan translate:lang {from} {to}

OpenAI

set your API key in .env:

OPENAI_API_KEY=your-api-key

Then run the command with OpenAI options:

php artisan translate:lang {from} {to} --provider=openai --model=gpt-3.5-turbo


- [Ali Salehi](https://github.com/alisalehi1380) - Original author and maintainer
- [Amirmohammad Mokhtari](https://github.com/am-mokhtari)
- [Navid Mirzaaghazadeh](https://github.com/mirzaaghazadeh) - Implemented OpenAI integration for natural language translation
Copy link
Owner

@alisalehi1380 alisalehi1380 Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

همون طوری که همه مون میدونیم از اپن سورس پولی در نمیاد. فقط اعتبار هست که بهمون میده.
در همین PR ی که زدی توضیحات خیلی تکمیل تری وجود داره ولی اگر خیلی مهمه برات که در README باشه که دقیقا چه فیچری رو اضافه کردی، بزار باشه.

منم کلا بردار. ما کاری نکردیم که. هر چی بوده زحمات شما و دوستان بوده.

@alisalehi1380 alisalehi1380 added the enhancement New feature or request label Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants