-
-
Notifications
You must be signed in to change notification settings - Fork 141
feat(anthropic): text stream support #266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
899a60a
anthropic stream support
c33102f
anthropic stream tests and sse files
1eee715
updated for v0.51
7156c1b
remove comments
bb482ae
remove complexity in ToolCallMap
2316c30
added doc blocks
5ef4580
Revert "added doc blocks"
c91b5a9
added doc blocks
01ac8cc
tool call improvement + exception on argument failure
471e592
sendRequest tidyup
23bfdc8
next line improvement
9e336d7
finish reason fix
95ca741
stop reason was needed
9843acd
properly handle retry-after header in Anthropic ProcessesRateLimits
b337883
fixed parameter name for tools + mapToolCalls moved to stream class
44d0c90
stream with citations using MessagePartWithCitations
8bfb4f2
rate limit trait to match abstract rate limit
04cc5c7
code tidy up
58a4ec8
thinking test
f708c9c
formatting
6bfad7a
formatting and phpstan
843a686
rm comment
bf5461f
thinking sse file - rm empty spaces
e6fa801
Merge branch 'main' of https://github.com/prism-php/prism into feat/a…
1baa2ee
exception handling
ChrisB-TL 5b50646
add meta to final chunk
ChrisB-TL 4602539
refactor to use match for readability + reasoning
ChrisB-TL 9b239d8
simplify/clarify state
ChrisB-TL e696ab0
add comments to match statement + yield thinking chunks
ChrisB-TL 83819f5
fix citations
ChrisB-TL ce814f7
add note to docs re citation streaming
ChrisB-TL 83d5123
use text request payload method
ChrisB-TL 354fe7b
stop_reason and FinishReason fix
665b6d8
formatting
0e20894
'error' case added
e58271a
PrismProviderOverloadedException exception for "overloaded" case
c60a008
phpstan
77dcd03
pint formatting
465a6c7
Merge branch 'main' into feat/anthropic-stream-0.51
0ed862b
added peck.json from main
798866a
Refactors
sixlive 0fc990b
types
sixlive 54e2464
ChunkType::Message to ChunkType::Text
sixlive e4af687
wip
sixlive File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Prism\Prism\Providers\Anthropic\Concerns; | ||
|
||
use Illuminate\Http\Client\Response; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Str; | ||
use Prism\Prism\ValueObjects\ProviderRateLimit; | ||
|
||
trait ProcessesRateLimits | ||
{ | ||
/** | ||
* Process rate limit headers from Anthropic API responses. | ||
* | ||
* @return ProviderRateLimit[] | ||
*/ | ||
protected function processRateLimits(Response $response): array | ||
{ | ||
$limitHeaders = array_filter($response->getHeaders(), fn ($headerName) => Str::startsWith($headerName, 'x-ratelimit-'), ARRAY_FILTER_USE_KEY); | ||
|
||
$rateLimits = []; | ||
|
||
foreach ($limitHeaders as $headerName => $headerValues) { | ||
// Parse header name according to Anthropic's format: x-ratelimit-{limit/remaining/reset}-{resource_type} | ||
$parts = explode('-', $headerName); | ||
|
||
// Expect format like x-ratelimit-limit-requests or x-ratelimit-remaining-tokens | ||
if (count($parts) >= 3) { | ||
$fieldName = $parts[1]; // limit, remaining, or reset | ||
$limitName = $parts[2]; // requests, tokens, etc. | ||
|
||
$rateLimits[$limitName][$fieldName] = $headerValues[0]; | ||
} | ||
} | ||
|
||
// Also check for retry-after header | ||
$retryAfter = $response->header('retry-after'); | ||
if ($retryAfter !== null) { | ||
$rateLimits['global']['reset'] = $retryAfter . 's'; | ||
sentiasa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return array_values(Arr::map($rateLimits, function ($fields, $limitName): ProviderRateLimit { | ||
sentiasa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$resetsAt = data_get($fields, 'reset', ''); | ||
|
||
// Anthropic typically provides reset times in seconds | ||
if (is_numeric($resetsAt)) { | ||
$resetSeconds = (int) $resetsAt; | ||
$resetMilliseconds = 0; | ||
$resetMinutes = 0; | ||
} elseif (str_contains($resetsAt, 's')) { | ||
$resetSeconds = (int) Str::of($resetsAt)->before('s')->toString(); | ||
$resetMilliseconds = 0; | ||
$resetMinutes = 0; | ||
} else { | ||
$resetSeconds = 0; | ||
$resetMilliseconds = 0; | ||
$resetMinutes = 0; | ||
} | ||
|
||
return new ProviderRateLimit( | ||
name: $limitName, | ||
limit: data_get($fields, 'limit') !== null | ||
? (int) data_get($fields, 'limit') | ||
: null, | ||
remaining: data_get($fields, 'remaining') !== null | ||
? (int) data_get($fields, 'remaining') | ||
: null, | ||
resetsAt: data_get($fields, 'reset') !== null | ||
? Carbon::now()->addMinutes((int) $resetMinutes)->addSeconds((int) $resetSeconds)->addMilliseconds((int) $resetMilliseconds) | ||
: null | ||
); | ||
})); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.