一个强大的 Laravel 框架,用于连接到 OpenAI、Gemini 和 Ollama 等人工智能 API,发送提示,收集响应,创建代理和代理网络,并通过易于设置和高度可定制的状态机将其投入使用。

v0.2.23 2024-07-03 12:47 UTC

This package is auto-updated.

Last update: 2024-10-03 13:15:21 UTC


README

Build Status Coveralls Packagist Version Packagist Downloads

加入 LaravelNeuro Discord 服务器上的讨论!

此 Laravel 包通过引入两个强大的功能来增强您的 PHP Laravel 应用程序

  1. 通过管道和提示类将人工智能 API 集成到您的应用程序代码中。
  2. 设置和运行复杂的状态机,将任意数量的生成式人工智能代理和函数网络化,以自动化任务和内容生成。

安装

composer require laravel-neuro/core

功能

预构建的管道

LaravelNeuro 附带一套预配置的管道,可快速实现,包括

  • ElevenLabs 文本到语音
    • 命名空间: LaravelNeuro\LaravelNeuro\Pipelines\ElevenLabs\AudioTTS
    • 提示类: LaravelNeuro\LaravelNeuro\Prompts\IVFSprompt
  • OpenAI
    • ChatCompletion
      • 命名空间: LaravelNeuro\LaravelNeuro\Pipelines\OpenAI\ChatCompletion
      • 提示类: LaravelNeuro\LaravelNeuro\Prompts\SUAprompt
    • DallE
      • 命名空间: LaravelNeuro\LaravelNeuro\Pipelines\OpenAI\DallE
      • 提示类: LaravelNeuro\LaravelNeuro\Prompts\PNSQFprompt
    • AudioTTS
      • 命名空间: LaravelNeuro\LaravelNeuro\Pipelines\OpenAI\AudioTTS
      • 提示类: LaravelNeuro\LaravelNeuro\Prompts\IVFSprompt

所有管道都扩展了基本的 LaravelNeuro\LaravelNeuro\Pipeline,它本身扩展了 ApiAdapter 类,通过 Guzzle 促进了提示的传输和响应的接收。

启用管道

通过将您的 API 密钥添加到 Laravel 应用程序的 .env 文件中,启用 OpenAI 和 ElevenLabs 管道

OPENAI_API_KEY="your_api_key"
ELEVENLABS_API_KEY="your_api_key"

如果需要,通过发布 lneuro 配置文件来更改每个管道的默认模型

php artisan vendor:publish --tag=laravelneuro-config

这不是强制性的。即使使用预构建的管道,您也可以通过在您的 Pipeline 对象上调用 setModel 方法,并传递模型名称作为字符串参数,从默认模型切换到任何兼容的模型。示例

use LaravelNeuro\LaravelNeuro\Pipelines\OpenAI\ChatCompletion;

$pipeline = new ChatCompletion();
$pipeline->setModel('gpt-4-turbo-preview'); //this changes the model from the default gpt-3.5-turbo-0125

示例用法

以下是在 Laravel 脚本中使用 OpenAI 的 ChatCompletion 并具有流式响应的示例

use LaravelNeuro\LaravelNeuro\Pipelines\OpenAI\ChatCompletion;
use LaravelNeuro\LaravelNeuro\Prompts\SUAprompt;

$prompt = new SUAprompt();
$pipeline = new ChatCompletion();

$prompt->pushSystem("You are a seasoned dungeonmaster and play Dungeons and Dragons 3.5th Edition with me.");
$prompt->pushUser("I want to create a new D&D character.");
$prompt->pushAgent("How can I help you with your character creation?");
$prompt->pushUser("My character is a shadow kenku...");

echo "response:\n";
$pipeline->setPrompt($prompt);

$stream = $pipeline->streamText();
foreach ($stream as $output) {
    print_r($output);
}

//sample response:
/* 
That sounds like an interesting character concept! The shadow kenku is a homebrew race that combines the traits of kenku and shadow creatures. Let's work on creating your shadow kenku character together. 

First, let's determine your ability scores. As a shadow kenku, you might want to focus on Dexterity and Charisma for your abilities. What ability scores do you want to prioritize for your character? 
*/

输出方法

OpenAi ChatCompletion 管道还允许使用各种输出方法,包括 textjsonarrayjsonStreamarrayStream,而基本的 Pipeline 类则配备了更通用的 outputstream 方法。

状态机

LaravelNeuro 状态机,称为公司,易于设置,可以网络化人工智能 API 和脚本以执行复杂任务。

设置

为了使 LaravelNeuro 状态机工作,使用以下命令迁移其 Eloquent 模型

php artisan lneuro:migrate

创建语音助手

语音到语音聊天助手的示例设置

  1. 创建公司文件夹和示例设置文件
php artisan lneuro:prebuild VoiceAssistant
  1. 填写 setup.json 文件,其中包含必要的 AI 模型(语音识别、文本生成、语音合成)。

//setup.json 示例

{
    "name": "Voice Assistant",
    "nameSpace": "VoiceAssistant",
    "description": "Ingest natural speech audio, query a chat-completion agent, then apply a TTS model to the output.",
    "charta": "",
    "units": [
      {
        "name": "Transcription",
        "description": "This Unit ingests a file path and outputs transcribed text.",
        "agents": [
          {
            "name": "Transcriber",
            "model": "whisper-1",
            "pipeline": "LaravelNeuro\\LaravelNeuro\\Pipelines\\OpenAI\\Whisper",   
            "promptClass": "LaravelNeuro\\LaravelNeuro\\Prompts\\FSprompt",   
            "validateOutput": false
          }
        ],
        "defaultReceiver": {
          "type": "AGENT",
          "name": "Transcriber"
        }
      },
      {
        "name": "ChatCompletion",
        "description": "This unit uses transcribed text as prompts to generate text responses.",
        "agents": [
          {
            "name": "Chatbot",
            "model": "gpt-3.5-turbo-1106",
            "pipeline": "LaravelNeuro\\LaravelNeuro\\Pipelines\\OpenAI\\ChatCompletion",     
            "role": "You are a helpful assistant.",   
            "validateOutput": false
          }
        ],
        "defaultReceiver": {
          "type": "AGENT",
          "name": "Chatbot"
        }
      },
      {
        "name": "Studio",
        "description": "This unit takes generated text responses and outputs voice-over.",
        "agents": [
          {
            "name": "Speaker",
            "model": "tts-1",
            "apiType": "TTS",
            "pipeline": "LaravelNeuro\\LaravelNeuro\\Pipelines\\OpenAI\\AudioTTS",
            "promptClass": "LaravelNeuro\\LaravelNeuro\\Prompts\\IVFSprompt",    
            "prompt": "{{Head:data}}{{VFS:nova}}",
            "validateOutput": false
          }
        ],
        "defaultReceiver": {
          "type": "AGENT",
          "name": "Speaker"
        }
      }
    ],
    "transitions": [
      {
        "type": "UNIT",
        "transitionName": "Transcription",
        "transitionHandle": "Transcription"
      },
      {
        "type": "UNIT",
        "transitionName": "ChatCompletion",
        "transitionHandle": "ChatCompletion"
      },
      {
        "type": "UNIT",
        "transitionName": "Studio",
        "transitionHandle": "Studio"
      }
    ]
}
  1. 安装并运行您的公司
php artisan lneuro:install VoiceAssistant

此设置不需要编写代码,但提供了注入自定义逻辑的钩子。

高级用例

更详细的使用案例和文档将很快在单独的文档网站上提供。