outl1ne / nova-openai
Laravel 应用程序的 OpenAI SDK,同时存储 OpenAI 通信并在 Laravel Nova 管理面板中展示。
0.10.13
2024-08-14 12:39 UTC
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.8
- laravel/nova: ^4.32
- nova-kit/nova-packages-tool: ^1.14
- outl1ne/nova-translations-loader: ^5.0
Requires (Dev)
- nova-kit/nova-devtool: ^1.5
- phpunit/phpunit: ^11.1
- dev-main
- 0.10.13
- 0.10.12
- 0.10.11
- 0.10.10
- 0.10.9
- 0.10.8
- 0.10.7
- 0.10.6
- 0.10.5
- 0.10.4
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.2
- 0.9.1
- 0.9.0
- 0.8.0
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.0
- dev-Virtual-branch
- dev-assistants-v2
- dev-update-streamhandler-chunks-filter-join
- dev-update-openai-pricing-models
- dev-wip-streaming-support
- dev-fix/nova-cache-badge-missing
- dev-add-gpt-4-turbo-2024-04-09-model
- dev-add-request-timeout-configuration
- dev-update-response-format-handling
- dev-update-embedding-storage-model-branch
- dev-update-text-embedding-version
- dev-update-embedding-pricing-models
- dev-add-storing-callback-to-capability
- dev-attaching-listing-deleting-files
- dev-update-openai-table-columns
- dev-fix-assistant-name-escaping
- dev-add-threads-assistant-message-response
- dev-add-files-capability-changes
- dev-model-pricing-update-turbo-gpt-4
- dev-calculate-embedding-pricing-models
- dev-update-openai-sdk-structure
- dev-update-pricing-path-logic
- dev-update-pricing-path-logic-branch
- dev-change-cost-calculation-data-type
- dev-create-modify-delete-assistant-branch
- dev-should-store
- dev-update-run-response-usage-parsing
- dev-Add-threads-capability-to-openai
- dev-feature/add-factory-and-request-data
- dev-Update-rate-limit-class
- dev-fix/headers-error
- dev-Refactor-openai-capabilities
- dev-update-readme
- dev-feature/update-readme
- dev-pricing
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进行咨询。
屏幕截图
要求
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
在 NovaServiceProvider
的 tools()
方法中注册此工具
// 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