定制 timkley/denk 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

timkley/denk

最新稳定版本:0.5.2

Composer 安装命令:

composer create-project timkley/denk

包简介

OpenAI helpers

README 文档

README

The package name is derived from the German word "denken" which means "to think".

Installation

composer require timkley/denk

How to use

Make sure that you have an OpenAI API key. You can get one here.

Initialization

Using Laravel

The package comes with a service provider that will automatically register the OpenAI client. You can use the Denk facade to access the service.

Make sure to publish the configuration file to set your API key and/or change the API url.

php artisan vendor:publish --tag=config --provider="Denk\DenkServiceProvider"

After that, you can use the Denk facade to access the service like this:

use Denk\Facades\Denk;

$text = Denk::text()->prompt('Once upon a time')->generate();

Manually or non-Laravel projects

To initialize the service, you need create an OpenAI client and pass it to the DenkService.

use Denk\DenkService;
use OpenAI;

$client = OpenAI::client('your-api-key');
$denk = new DenkService($client);

Generating text

use Denk\DenkService;
use OpenAI;

$client = OpenAI::client('your-api-key');
$denk = new DenkService($client);

// using only a simple prompt
$text = $denk->text()->prompt('Once upon a time')->generate();

// Manually provide messages
use Denk\ValueObjects\DeveloperMessage;
use Denk\ValueObjects\UserMessage;
use Denk\ValueObjects\AssistantMessage;

$text = $denk->text()
    ->messages([
        new DeveloperMessage('Write as a pirate'),
        new UserMessage('Once upon a time'),
    ])
    ->generate();

// Generate a streamed response
$stream = $denk->text()
    ->prompt('Write a long story about a pirate')
    ->generateStreamed();

// Iterate over the stream to get chunks of the response as they arrive
foreach ($stream as $response) {
    echo $response->choices[0]->delta->content;
}

Available models

  • gpt-4o-mini (default)
  • gpt-4o
  • gpt-4-turbo
  • gpt-4
  • gpt-3.5-turbo

Temperature

You can control the randomness of the response by setting the temperature:

$text = $denk->text()
    ->prompt('Once upon a time')
    ->temperature(0.5) // Between 0.0 and 2.0, default is 1.0
    ->generate();

Generating JSON

To generate Structured Output, you can use the json() method. Set the properties of the JSON object and provide a prompt, additionally you may also set a system prompt.

use Denk\DenkService;
use OpenAI;

$client = OpenAI::client('your-api-key');
$denk = new DenkService($client);

$json = $denk->json()
    ->properties([
        'title' => [
            'type' => 'string',
            'description' => 'The title of the story',
        ],
        'story' => [
            'type' => 'string',
            'description' => 'The story itself',
        ],
    ])
    ->prompt('Write a store about Chewbacca winning a game against Han Solo')
    ->generate();

Available models

  • gpt-4o-mini (default)
  • gpt-4o

Temperature

You can control the randomness of the response by setting the temperature:

$json = $denk->json()
    ->properties([...])
    ->prompt('...')
    ->temperature(0.5) // Between 0.0 and 2.0, default is 1.0
    ->generate();

Generating Images

To generate images, you can use the image() method. Set the properties of the image and provide a prompt.

use Denk\DenkService;
use OpenAI;

$client = OpenAI::client('your-api-key');
$denk = new DenkService($client);

$image = $denk->image()
    ->prompt('A beautiful sunset over the ocean')
    ->size('square') // or '1024x1024'
    ->quality('standard')
    ->model('dall-e-3') // optional, dall-e-3 is default
    ->generate();

// Returns the URL of the generated image

Available image sizes

  • 1024x1024 (square)
  • 1792x1024 (landscape)
  • 1024x1792 (portrait)

Or use the convenience aliases:

  • square
  • landscape
  • portrait

Available image qualities

  • standard
  • hd

Available image models

  • dall-e-3 (default)
  • dall-e-2

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-29