nextbuild/sarvam-ai-laravel 问题修复 & 功能扩展

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

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

nextbuild/sarvam-ai-laravel

Composer 安装命令:

composer require nextbuild/sarvam-ai-laravel

包简介

Laravel package for Sarvam AI integration with speech-to-text, text-to-speech, translation, and chat completion capabilities

README 文档

README

A comprehensive Laravel package for integrating Sarvam AI's powerful speech-to-text, text-to-speech, translation, and chat completion capabilities into your Laravel applications.

Features

  • Speech to Text: Convert audio files to text
  • Speech to Text with Translation: Convert audio to text with automatic translation
  • Text to Speech: Generate audio from text
  • Text Translation: Translate text between languages
  • Language Identification: Detect the language of text
  • Text Transliteration: Convert text between scripts
  • Chat Completions: AI-powered chat responses
  • Laravel Integration: Built specifically for Laravel with facades and service providers
  • Error Handling: Comprehensive error handling with custom exceptions
  • Configuration: Flexible configuration options

Installation

Install the package via Composer:

composer require nextbuild/sarvam-ai-laravel

Laravel Auto-Discovery

The package will automatically register itself with Laravel's package auto-discovery feature.

Manual Registration (if needed)

If auto-discovery is disabled, manually add the service provider to your config/app.php:

'providers' => [
    // Other providers...
    NextBuild\SarvamAI\SarvamAIServiceProvider::class,
],

'aliases' => [
    // Other aliases...
    'SarvamAI' => NextBuild\SarvamAI\Facades\SarvamAI::class,
],

Configuration

Publish the configuration file:

php artisan vendor:publish --tag=sarvam-ai-config

Add your Sarvam AI API key to your .env file:

SARVAM_AI_API_KEY=your-api-key-here

Usage

Using the Facade

use NextBuild\SarvamAI\Facades\SarvamAI;

// Speech to Text
$response = SarvamAI::speechToText('/path/to/audio/file.wav', 'saarika:v1', 'hi-IN');
$transcript = $response->getTranscript();

// Text to Speech
$response = SarvamAI::textToSpeech('Hello world', 'en-IN');
$audioUrl = $response->getAudioUrl();

// Translation
$response = SarvamAI::translateText('Hello world', 'en-IN', 'hi-IN');
$translatedText = $response->getTranslatedText();

// Chat Completions
$messages = [
    ['role' => 'user', 'content' => 'Hello, how are you?']
];
$response = SarvamAI::chatCompletions($messages);
$reply = $response->getChatCompletionContent();

Using Dependency Injection

use NextBuild\SarvamAI\SarvamAI;

class YourController extends Controller
{
    protected $sarvamAI;

    public function __construct(SarvamAI $sarvamAI)
    {
        $this->sarvamAI = $sarvamAI;
    }

    public function translateText(Request $request)
    {
        $response = $this->sarvamAI->translateText(
            $request->input('text'),
            $request->input('source_language', 'auto'),
            $request->input('target_language', 'en-IN')
        );

        return response()->json([
            'translated_text' => $response->getTranslatedText(),
            'original_data' => $response->getData()
        ]);
    }
}

API Methods

Speech to Text

Convert audio files to text:

$response = SarvamAI::speechToText('/path/to/audio/file.wav');
$transcript = $response->getTranscript();

Speech to Text with Translation

Convert audio to text with automatic translation:

$response = SarvamAI::speechToTextTranslate('/path/to/audio/file.wav', null, 'saarika:v1');
$transcript = $response->getTranscript();

Text to Speech

Generate audio from text:

$response = SarvamAI::textToSpeech('Hello world', 'en-IN');
$audioUrl = $response->getAudioUrl();

Text Translation

Translate text between languages:

$response = SarvamAI::translateText('Hello world', 'en-IN', 'hi-IN');
$translatedText = $response->getTranslatedText();

Language Identification

Detect the language of text:

$response = SarvamAI::identifyLanguage('Hello world');
$detectedLanguage = $response->getDetectedLanguage();

Text Transliteration

Convert text between scripts:

$response = SarvamAI::transliterateText('Hello world', 'en-IN', 'hi-IN');
$transliteratedText = $response->getTransliteratedText();

Chat Completions

AI-powered chat responses:

$messages = [
    ['role' => 'user', 'content' => 'What is the capital of India?']
];
$response = SarvamAI::chatCompletions($messages, 'sarvam-m');
$reply = $response->getChatCompletionContent();

Response Handling

All methods return a SarvamAIResponse object with the following methods:

$response = SarvamAI::translateText('Hello', 'en-IN', 'hi-IN');

// Get specific data
$translatedText = $response->getTranslatedText();
$statusCode = $response->getStatusCode();
$headers = $response->getHeaders();

// Get raw data
$data = $response->getData();
$body = $response->getBody();

// Convert to array or JSON
$array = $response->toArray();
$json = $response->toJson();

// Check if successful
$isSuccessful = $response->isSuccessful();

Error Handling

The package throws SarvamAIException for API errors:

use NextBuild\SarvamAI\Exceptions\SarvamAIException;

try {
    $response = SarvamAI::translateText('Hello world', 'en-IN', 'hi-IN');
    $translatedText = $response->getTranslatedText();
} catch (SarvamAIException $e) {
    // Handle the error
    Log::error('Sarvam AI Error: ' . $e->getMessage());
    return response()->json(['error' => 'Translation failed'], 500);
}

Configuration Options

The configuration file includes the following options:

return [
    'api_key' => env('SARVAM_AI_API_KEY'),
    'default_source_language' => env('SARVAM_AI_DEFAULT_SOURCE_LANGUAGE', 'auto'),
    'default_target_language' => env('SARVAM_AI_DEFAULT_TARGET_LANGUAGE', 'en-IN'),
    'timeout' => env('SARVAM_AI_TIMEOUT', 30),
    'retry' => env('SARVAM_AI_RETRY', 3),
    'chat' => [
        'default_model' => env('SARVAM_AI_DEFAULT_MODEL', 'sarvam-m'),
        'max_tokens' => env('SARVAM_AI_MAX_TOKENS=1000
SARVAM_AI_TEMPERATURE=0.7

Advanced Usage

Custom API Key

You can set a custom API key at runtime:

SarvamAI::setApiKey('your-custom-api-key')->translateText('Hello', 'en-IN', 'hi-IN');

Custom Timeout

Set a custom timeout for requests:

SarvamAI::setTimeout(60)->speechToText('/path/to/long/audio/file.wav');

Chaining Methods

You can chain configuration methods:

$response = SarvamAI::setApiKey('custom-key')
    ->setTimeout(60)
    ->translateText('Hello world', 'en-IN', 'hi-IN');

Working with Files

For speech-to-text operations, ensure your audio files are in supported formats:

// Supported formats: WAV, MP3, FLAC, etc.
$audioFile = storage_path('app/audio/sample.wav');
$response = SarvamAI::speechToText($audioFile);

if ($response->isSuccessful()) {
    $transcript = $response->getTranscript();
    // Process the transcript
}

Testing

The package includes comprehensive tests. Run them with:

composer test

For test coverage:

composer test-coverage

Examples

Complete Translation Service

<?php

namespace App\Services;

use NextBuild\SarvamAI\Facades\SarvamAI;
use NextBuild\SarvamAI\Exceptions\SarvamAIException;

class TranslationService
{
    public function translateContent(string $content, string $targetLanguage = 'hi-IN'): array
    {
        try {
            // First, identify the language
            $langResponse = SarvamAI::identifyLanguage($content);
            $detectedLanguage = $langResponse->getDetectedLanguage();
            
            // Then translate
            $translateResponse = SarvamAI::translateText($content, $detectedLanguage, $targetLanguage);
            
            return [
                'success' => true,
                'original_text' => $content,
                'detected_language' => $detectedLanguage,
                'target_language' => $targetLanguage,
                'translated_text' => $translateResponse->getTranslatedText(),
            ];
        } catch (SarvamAIException $e) {
            return [
                'success' => false,
                'error' => $e->getMessage(),
            ];
        }
    }
}

Audio Processing Service

<?php

namespace App\Services;

use NextBuild\SarvamAI\Facades\SarvamAI;
use NextBuild\SarvamAI\Exceptions\SarvamAIException;
use Illuminate\Http\UploadedFile;

class AudioProcessingService
{
    public function processAudioFile(UploadedFile $audioFile, bool $withTranslation = false): array
    {
        try {
            // Store the uploaded file temporarily
            $path = $audioFile->storeAs('temp', uniqid() . '.' . $audioFile->getClientOriginalExtension());
            $fullPath = storage_path('app/' . $path);
            
            // Process based on requirements
            if ($withTranslation) {
                $response = SarvamAI::speechToTextTranslate($fullPath);
            } else {
                $response = SarvamAI::speechToText($fullPath);
            }
            
            // Clean up temporary file
            unlink($fullPath);
            
            return [
                'success' => true,
                'transcript' => $response->getTranscript(),
                'full_response' => $response->getData(),
            ];
        } catch (SarvamAIException $e) {
            return [
                'success' => false,
                'error' => $e->getMessage(),
            ];
        }
    }
}

Chat Service

<?php

namespace App\Services;

use NextBuild\SarvamAI\Facades\SarvamAI;
use NextBuild\SarvamAI\Exceptions\SarvamAIException;

class ChatService
{
    public function generateResponse(array $conversationHistory, string $userMessage): array
    {
        try {
            // Prepare messages array
            $messages = [];
            
            // Add conversation history
            foreach ($conversationHistory as $message) {
                $messages[] = [
                    'role' => $message['role'],
                    'content' => $message['content'],
                ];
            }
            
            // Add current user message
            $messages[] = [
                'role' => 'user',
                'content' => $userMessage,
            ];
            
            // Get AI response
            $response = SarvamAI::chatCompletions($messages);
            
            return [
                'success' => true,
                'response' => $response->getChatCompletionContent(),
                'full_response' => $response->getData(),
            ];
        } catch (SarvamAIException $e) {
            return [
                'success' => false,
                'error' => $e->getMessage(),
            ];
        }
    }
}

API Reference

SarvamAI Class Methods

Method Parameters Return Type Description
speechToText() string $filePath SarvamAIResponse Convert audio to text
speechToTextTranslate() string $filePath SarvamAIResponse Convert audio to text with translation
textToSpeech() string $text, string $targetLanguageCode SarvamAIResponse Convert text to speech
translateText() string $input, string $sourceLanguageCode = 'auto', string $targetLanguageCode = 'en-IN' SarvamAIResponse Translate text
identifyLanguage() string $input SarvamAIResponse Identify language of text
transliterateText() string $input, string $sourceLanguageCode = 'auto', string $targetLanguageCode = 'en-IN' SarvamAIResponse Transliterate text
chatCompletions() array $messages, string $model = 'sarvam-m' SarvamAIResponse Get chat completions
setApiKey() string $apiKey SarvamAI Set custom API key
setTimeout() int $timeout SarvamAI Set request timeout
getApiKey() - string Get current API key

SarvamAIResponse Class Methods

Method Return Type Description
getData() array Get raw response data
getStatusCode() int Get HTTP status code
getBody() string Get raw response body
getHeaders() array Get response headers
isSuccessful() bool Check if request was successful
get() mixed Get specific field from response
getTranscript() `string null`
getTranslatedText() `string null`
getDetectedLanguage() `string null`
getTransliteratedText() `string null`
getAudioUrl() `string null`
getChatCompletion() `array null`
getChatCompletionContent() `string null`
toArray() array Convert response to array
toJson() string Convert response to JSON

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

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

Support

For support, please open an issue on the GitHub repository or contact the maintainers.

Changelog

See CHANGELOG.md for more information on what has changed recently.

Security

If you discover any security related issues, please email the maintainers instead of using the issue tracker.

Credits

About Sarvam AI

Sarvam AI provides cutting-edge AI solutions for Indian languages. Learn more at sarvam.ai.TOKENS', 1000), 'temperature' => env('SARVAM_AI_TEMPERATURE', 0.7), ], ];


## Supported Languages

The package supports multiple Indian languages:

- Hindi (hi-IN)
- English (en-IN)
- Bengali (bn-IN)
- Tamil (ta-IN)
- Telugu (te-IN)
- Kannada (kn-IN)
- Malayalam (ml-IN)
- Marathi (mr-IN)
- Gujarati (gu-IN)
- Punjabi (pa-IN)
- Odia (or-IN)
- Urdu (ur-IN)
- Assamese (as-IN)
- Nepali (ne-IN)
- Sinhala (si-IN)
- Myanmar (my-IN)

## Environment Variables

```env
SARVAM_AI_API_KEY=your-api-key-here
SARVAM_AI_DEFAULT_SOURCE_LANGUAGE=auto
SARVAM_AI_DEFAULT_TARGET_LANGUAGE=en-IN
SARVAM_AI_TIMEOUT=30
SARVAM_AI_RETRY=3
SARVAM_AI_DEFAULT_MODEL=sarvam-m
SARVAM_AI_MAX_

nextbuild/sarvam-ai-laravel 适用场景与选型建议

nextbuild/sarvam-ai-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「translation」 「chat」 「laravel」 「nlp」 「text-to-speech」 「ai」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nextbuild/sarvam-ai-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nextbuild/sarvam-ai-laravel 我们能提供哪些服务?
定制开发 / 二次开发

基于 nextbuild/sarvam-ai-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-20