-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For more information see https://anthropic.com/news/message-batches-api
- Loading branch information
1 parent
d225d1d
commit 1c404b2
Showing
19 changed files
with
2,187 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
configured_endpoints: 3 | ||
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-286f00929e2a4d28d991e6a7e660fa801dca7ec91d8ecb2fc17654bb8173eb0d.yml | ||
configured_endpoints: 9 | ||
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-aedee7570aa925baba404fc5bd3c8c1fffe8845517e492751db9b175c5cae9da.yml |
This file contains 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 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 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,20 @@ | ||
import Anthropic from '@anthropic-ai/sdk/index'; | ||
|
||
const anthropic = new Anthropic(); | ||
|
||
async function main() { | ||
const batch_id = process.argv[2]; | ||
if (!batch_id) { | ||
throw new Error('must specify a message batch ID, `yarn tsn examples/batch-results.ts msgbatch_123`'); | ||
} | ||
|
||
console.log(`fetching results for ${batch_id}`); | ||
|
||
const results = await anthropic.beta.messages.batches.results(batch_id); | ||
|
||
for await (const result of results) { | ||
console.log(result); | ||
} | ||
} | ||
|
||
main(); |
This file contains 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 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,41 @@ | ||
import { AnthropicError } from '../../error'; | ||
import { readableStreamAsyncIterable } from '../../streaming'; | ||
import { type Response } from '../../_shims/index'; | ||
import { LineDecoder, type Bytes } from './line'; | ||
|
||
export class JSONLDecoder<T> { | ||
controller: AbortController; | ||
|
||
constructor( | ||
private iterator: AsyncIterableIterator<Bytes>, | ||
controller: AbortController, | ||
) { | ||
this.controller = controller; | ||
} | ||
|
||
private async *decoder(): AsyncIterator<T, any, undefined> { | ||
const lineDecoder = new LineDecoder(); | ||
for await (const chunk of this.iterator) { | ||
for (const line of lineDecoder.decode(chunk)) { | ||
yield JSON.parse(line); | ||
} | ||
} | ||
|
||
for (const line of lineDecoder.flush()) { | ||
yield JSON.parse(line); | ||
} | ||
} | ||
|
||
[Symbol.asyncIterator](): AsyncIterator<T> { | ||
return this.decoder(); | ||
} | ||
|
||
static fromResponse<T>(response: Response, controller: AbortController): JSONLDecoder<T> { | ||
if (!response.body) { | ||
controller.abort(); | ||
throw new AnthropicError(`Attempted to iterate over a response with no body`); | ||
} | ||
|
||
return new JSONLDecoder(readableStreamAsyncIterable<Bytes>(response.body), controller); | ||
} | ||
} |
This file contains 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 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,84 @@ | ||
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. | ||
|
||
import { AbstractPage, Response, APIClient, FinalRequestOptions, PageInfo } from './core'; | ||
|
||
export interface PageResponse<Item> { | ||
data: Array<Item>; | ||
|
||
has_more: boolean; | ||
|
||
first_id: string | null; | ||
|
||
last_id: string | null; | ||
} | ||
|
||
export interface PageParams { | ||
/** | ||
* Number of items per page. | ||
*/ | ||
limit?: number; | ||
|
||
before_id?: string; | ||
|
||
after_id?: string; | ||
} | ||
|
||
export class Page<Item> extends AbstractPage<Item> implements PageResponse<Item> { | ||
data: Array<Item>; | ||
|
||
has_more: boolean; | ||
|
||
first_id: string | null; | ||
|
||
last_id: string | null; | ||
|
||
constructor(client: APIClient, response: Response, body: PageResponse<Item>, options: FinalRequestOptions) { | ||
super(client, response, body, options); | ||
|
||
this.data = body.data || []; | ||
this.has_more = body.has_more || false; | ||
this.first_id = body.first_id || null; | ||
this.last_id = body.last_id || null; | ||
} | ||
|
||
getPaginatedItems(): Item[] { | ||
return this.data ?? []; | ||
} | ||
|
||
// @deprecated Please use `nextPageInfo()` instead | ||
nextPageParams(): Partial<PageParams> | null { | ||
const info = this.nextPageInfo(); | ||
if (!info) return null; | ||
if ('params' in info) return info.params; | ||
const params = Object.fromEntries(info.url.searchParams); | ||
if (!Object.keys(params).length) return null; | ||
return params; | ||
} | ||
|
||
nextPageInfo(): PageInfo | null { | ||
if ((this.options.query as Record<string, unknown>)?.['before_id']) { | ||
// in reverse | ||
const firstId = this.first_id; | ||
if (!firstId) { | ||
return null; | ||
} | ||
|
||
return { | ||
params: { | ||
before_id: firstId, | ||
}, | ||
}; | ||
} | ||
|
||
const cursor = this.last_id; | ||
if (!cursor) { | ||
return null; | ||
} | ||
|
||
return { | ||
params: { | ||
after_id: cursor, | ||
}, | ||
}; | ||
} | ||
} |
Oops, something went wrong.