Skip to content

Commit 87fc2d5

Browse files
authored
Merge pull request #22 from lihq1403/master
1. 新增 Gemini 原生接入 2. 新增 阿里百炼 接入 3. 流式采用自定义的 cURL 流式传输客户端来减少数据丢失问题
2 parents a6d2280 + 090218c commit 87fc2d5

File tree

136 files changed

+12820
-502
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+12820
-502
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on: [ push, pull_request ]
44

55
env:
66
SWOOLE_VERSION: '5.1.5'
7-
SWOW_VERSION: 'v1.2.0'
7+
SWOW_VERSION: 'v1.6.2'
88

99
jobs:
1010
ci:

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
],
1717
"exclude-from-classmap": [
1818
"vendor/aws/aws-sdk-php/src/Api/Validator.php"
19+
],
20+
"files": [
21+
"src/Api/Transport/SimpleCURLClient.php"
1922
]
2023
},
2124
"autoload-dev": {
@@ -28,19 +31,22 @@
2831
"ext-bcmath": "*",
2932
"ext-mbstring": "*",
3033
"aws/aws-sdk-php": "^3.0",
34+
"ext-curl": "*",
3135
"dtyq/php-mcp": "0.1.*",
3236
"guzzlehttp/guzzle": "^7.0|^6.0",
3337
"hyperf/cache": "~2.2.0 || 3.0.* || 3.1.*",
3438
"hyperf/config": "~2.2.0 || 3.0.* || 3.1.*",
3539
"hyperf/di": "~2.2.0 || 3.0.* || 3.1.*",
3640
"hyperf/logger": "~2.2.0 || 3.0.* || 3.1.*",
3741
"hyperf/retry": "~2.2.0 || 3.0.* || 3.1.*",
42+
"hyperf/event": "~2.2.0 || 3.0.* || 3.1.*",
3843
"hyperf/qdrant-client": "*",
3944
"justinrainbow/json-schema": "^6.3",
4045
"yethee/tiktoken": "^0.1.2"
4146
},
4247
"require-dev": {
4348
"friendsofphp/php-cs-fixer": "^3.0",
49+
"hyperf/engine": "^2.0",
4450
"mockery/mockery": "^1.0",
4551
"phpstan/phpstan": "^1.0",
4652
"phpunit/phpunit": ">=7.0",

examples/aws/aws_chat.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,12 @@
7373

7474
echo PHP_EOL;
7575
echo '耗时' . (microtime(true) - $start) . '' . PHP_EOL;
76+
77+
// Display usage information
78+
$usage = $response->getUsage();
79+
if ($usage) {
80+
echo PHP_EOL . '=== Token 使用情况 ===' . PHP_EOL;
81+
echo '输入 Tokens: ' . $usage->getPromptTokens() . PHP_EOL;
82+
echo '输出 Tokens: ' . $usage->getCompletionTokens() . PHP_EOL;
83+
echo '总计 Tokens: ' . $usage->getTotalTokens() . PHP_EOL;
84+
}

examples/aws/aws_chat_custom.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 2));
13+
14+
require_once dirname(__FILE__, 3) . '/vendor/autoload.php';
15+
16+
use Hyperf\Context\ApplicationContext;
17+
use Hyperf\Di\ClassLoader;
18+
use Hyperf\Di\Container;
19+
use Hyperf\Di\Definition\DefinitionSourceFactory;
20+
use Hyperf\Odin\Api\Providers\AwsBedrock\AwsType;
21+
use Hyperf\Odin\Api\Request\ChatCompletionRequest;
22+
use Hyperf\Odin\Api\RequestOptions\ApiOptions;
23+
use Hyperf\Odin\Logger;
24+
use Hyperf\Odin\Message\AssistantMessage;
25+
use Hyperf\Odin\Message\SystemMessage;
26+
use Hyperf\Odin\Message\UserMessage;
27+
use Hyperf\Odin\Model\AwsBedrockModel;
28+
29+
use function Hyperf\Support\env;
30+
31+
ClassLoader::init();
32+
33+
$container = ApplicationContext::setContainer(new Container((new DefinitionSourceFactory())()));
34+
35+
echo '=== AWS Bedrock Custom Client Test (Without AWS SDK) ===' . PHP_EOL . PHP_EOL;
36+
37+
// Create AWS Bedrock model instance with CONVERSE_CUSTOM type
38+
// This uses custom Guzzle + SigV4 implementation instead of AWS SDK
39+
$model = new AwsBedrockModel(
40+
'us.anthropic.claude-3-7-sonnet-20250219-v1:0',
41+
[
42+
'access_key' => env('AWS_ACCESS_KEY'),
43+
'secret_key' => env('AWS_SECRET_KEY'),
44+
'region' => env('AWS_REGION', 'us-east-1'),
45+
'type' => AwsType::CONVERSE_CUSTOM, // Use custom client without AWS SDK
46+
],
47+
new Logger(),
48+
);
49+
$model->setApiRequestOptions(new ApiOptions([
50+
'proxy' => env('HTTP_CLIENT_PROXY'),
51+
'http_handler' => env('ODIN_HTTP_HANDLER', 'auto'),
52+
]));
53+
54+
$messages = [
55+
new SystemMessage('You are a helpful AI assistant. Always include emoji in your responses.'),
56+
new UserMessage('Explain quantum entanglement in simple terms.'),
57+
];
58+
59+
$start = microtime(true);
60+
61+
// Use non-streaming API
62+
$request = new ChatCompletionRequest($messages);
63+
$request->setThinking([
64+
'type' => 'enabled',
65+
'budget_tokens' => 4000,
66+
]);
67+
$response = $model->chatWithRequest($request);
68+
69+
// Output full response
70+
$message = $response->getFirstChoice()->getMessage();
71+
if ($message instanceof AssistantMessage) {
72+
echo 'Response: ' . ($message->getReasoningContent() ?? $message->getContent()) . PHP_EOL;
73+
}
74+
75+
echo PHP_EOL . 'Duration: ' . round(microtime(true) - $start, 2) . ' seconds' . PHP_EOL;
76+
77+
// Output usage information
78+
$usage = $response->getUsage();
79+
echo PHP_EOL . '=== Token Usage ===' . PHP_EOL;
80+
echo 'Input Tokens: ' . $usage->getPromptTokens() . PHP_EOL;
81+
echo 'Output Tokens: ' . $usage->getCompletionTokens() . PHP_EOL;
82+
echo 'Total Tokens: ' . $usage->getTotalTokens() . PHP_EOL;
83+
84+
if ($usage->getCachedTokens() > 0) {
85+
echo PHP_EOL . 'Cache Hit: ' . $usage->getCachedTokens() . ' tokens' . PHP_EOL;
86+
echo 'Cache Hit Rate: ' . $usage->getCacheHitRatePercentage() . '%' . PHP_EOL;
87+
}
88+
89+
echo PHP_EOL . '✅ Custom client (without AWS SDK) works perfectly!' . PHP_EOL;

examples/aws/aws_chat_stream.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,12 @@
7474
}
7575

7676
echo PHP_EOL . '耗时: ' . round(microtime(true) - $start, 2) . '' . PHP_EOL;
77+
78+
// Display usage information
79+
$usage = $streamResponse->getUsage();
80+
if ($usage) {
81+
echo PHP_EOL . '=== Token 使用情况 ===' . PHP_EOL;
82+
echo '输入 Tokens: ' . $usage->getPromptTokens() . PHP_EOL;
83+
echo '输出 Tokens: ' . $usage->getCompletionTokens() . PHP_EOL;
84+
echo '总计 Tokens: ' . $usage->getTotalTokens() . PHP_EOL;
85+
}

0 commit comments

Comments
 (0)