outl1ne/nova-openai

Laravel 应用程序的 OpenAI SDK,同时存储 OpenAI 通信并在 Laravel Nova 管理面板中展示。

0.10.13 2024-08-14 12:39 UTC

This package is auto-updated.

Last update: 2024-08-28 06:03:50 UTC


README

Laravel 应用程序的 OpenAI SDK,同时存储 OpenAI 通信并在 Laravel Nova 管理面板中展示。

这个包目前处于不稳定状态,预计会有破坏性更改。目前的主要目标是使其更容易作为依赖项包含在我们的客户端项目中。这样我们可以使其更加成熟,并在我们觉得它满足我们的实际需求时发布 v1 版本。这就是为什么我们还没有实现所有 OpenAI 端点,而将在实际需要时逐一添加。

如果您需要实现任何功能或提高其在我们的待办事项列表中的优先级,请随时通过电子邮件info@outl1ne.com进行咨询。

屏幕截图

Screenshot

要求

  • php: >=8.1
  • laravel/nova: ^4.0

安装

# Install nova-openai
composer require outl1ne/nova-openai

# Run migrations
php artisan migrate

# Publish config file (optional)
php artisan vendor:publish --tag=nova-openai-config

NovaServiceProvidertools() 方法中注册此工具

// in app/Providers/NovaServiceProvider.php

public function tools()
{
    return [
        \Outl1ne\NovaOpenAI\NovaOpenAI::make(),
    ];
}

用法

助手

$assistant = OpenAI::assistants()->create(
  'gpt-3.5-turbo',
  'Allan\'s assistant',
  'For testing purposes of nova-openai package.',
  'You are a kindergarten teacher. When asked a questions, anwser shortly and as a young child could understand.'
);
$assistantModified = OpenAI::assistants()->modify($assistant->id, null, 'Allan\'s assistant!');
$deletedAssistant = OpenAI::assistants()->delete($assistant->id);
// dd($assistant->response->json(), $assistantModified->response->json(), $deletedAssistant->response->json());

附加、列出和删除文件。

$assistant = OpenAI::assistants()->create(
    'gpt-3.5-turbo',
    'Allan\'s assistant',
    'For testing purposes of nova-openai package.',
    'You are a kindergarten teacher. When asked a questions, anwser shortly and as a young child could understand.',
    [
        [
            'type' => 'retrieval',
        ],
    ],
);
$file = OpenAI::files()->upload(
    file_get_contents('files/file.txt'),
    'file.txt',
    'assistants',
);
$assistantFile = OpenAI::assistants()->files()->create($assistant->id, $file->id);
$assistantFiles = OpenAI::assistants()->files()->list($assistant->id);
$deletedAssistantFile = OpenAI::assistants()->files()->delete($assistant->id, $file->id);

// Cleanup
$deletedAssistant = OpenAI::assistants()->delete($assistant->id);
$deletedFile = OpenAI::files()->delete($file->id);
// dd(
//     $assistantFile->response->json(),
//     $assistantFiles->response->json(),
//     $deletedAssistantFile->response->json(),
// );

聊天

$response = OpenAI::chat()->create(
    model: 'gpt-3.5-turbo',
    messages: (new Messages)->system('You are a helpful assistant.')->user('Hello!'),
);

启用 JSON 响应格式化

$response = OpenAI::chat()->create(
    model: 'gpt-3.5-turbo',
    messages: (new Messages)->system('You are a helpful assistant.')->user('Suggest me tasty fruits as JSON array of fruits.'),
    responseFormat: (new ResponseFormat)->json(),
);

流式传输

$response = OpenAI::chat()->stream(function (string $newChunk, string $message) {
    echo $newChunk;
})->create(
    model: 'gpt-3.5-turbo',
    messages: (new Messages)->system('You are a helpful assistant.')->user('Hello!'),
);

嵌入

$response = OpenAI::embeddings()->create(
    'text-embedding-3-small',
    'The food was delicious and the waiter...'
);
// dd($response->embedding);

如果您已经在应用程序的其他地方存储了嵌入向量,那么您可能希望禁用在此包中存储它,通过传递一个带有 storing(fn ($model) => $model) 方法的回调函数。

$response = OpenAI::embeddings()->storing(function ($model) {
    $model->output = null;
    return $model;
})->create(
    'text-embedding-3-small',
    'The food was delicious and the waiter...'
);

文件

上传文件、检索它并在之后删除它。

$file = OpenAI::files()->upload(
    file_get_contents('files/file.txt'),
    'file.txt',
    'assistants',
);
$files = OpenAI::files()->list();
$file2 = OpenAI::files()->retrieve($file->id);
$deletedFile = OpenAI::files()->delete($file->id);
// dd($file->response->json(), $file2->response->json(), $deletedFile->response->json());

检索文件内容。

$fileContent = OpenAI::files()->retrieveContent($file->id);

线程

$assistant = OpenAI::assistants()->create(
    'gpt-3.5-turbo',
    'Allan',
    'nova-openai testimiseks',
    'You are a kindergarten teacher. When asked a questions, anwser shortly and as a young child could understand.'
);
$thread = OpenAI::threads()
    ->create((new ThreadMessages)->user('What is your purpose in one short sentence?'));
$message = OpenAI::threads()->messages()
    ->create($thread->id, ThreadMessage::user('How does AI work? Explain it in simple terms in one sentence.'));
$run = OpenAI::threads()->run()->execute($thread->id, $assistant->id)->wait();
$messages = OpenAI::threads()->messages()->list($thread->id);

// cleanup
$deletedThread = OpenAI::threads()->delete($thread->id);
$deletedAssistant = OpenAI::assistants()->delete($assistant->id);
// dd(
//     $assistant->response->json(),
//     $thread->response->json(),
//     $message->response->json(),
//     $run->response->json(),
//     $messages->response->json(),
//     $deletedThread->response->json(),
//     $deletedAssistant->response->json(),
// );

测试

您可以使用 OpenAIRequest 工厂创建用于测试目的的请求。

$mockOpenAIChat = Mockery::mock(Chat::class);
$mockOpenAIChat->shouldReceive('create')->andReturn((object) [
    'choices' => [
        [
            'message' => [
                'content' => 'Mocked response'
            ]
        ]
    ],
    'request' => OpenAIRequest::factory()->create()
]);
OpenAI::shouldReceive('chat')->andReturn($mockOpenAIChat);

贡献

composer install
testbench workbench:build
testbench serve