-
Notifications
You must be signed in to change notification settings - Fork 43
Add basic OpenRouter SDK example extracted from wrapper repo #46
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
Open
subtleGradient
wants to merge
3
commits into
05-prompt-caching-orsdk
Choose a base branch
from
basic-01-orsdk
base: 05-prompt-caching-orsdk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+94
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /** | ||
| * Example usage of the @openrouter/sdk package | ||
| * | ||
| * This demonstrates the OpenRouter TypeScript SDK's idiomatic usage patterns: | ||
| * - Type-safe client initialization | ||
| * - Non-streaming chat completions | ||
| * - Streaming chat completions with async iteration | ||
| * - Automatic usage tracking | ||
| */ | ||
|
|
||
| import { OpenRouter } from '@openrouter/sdk'; | ||
|
|
||
| // Initialize the OpenRouter SDK client | ||
| // The SDK automatically reads OPENROUTER_API_KEY from environment | ||
| const openRouter = new OpenRouter({ | ||
| apiKey: process.env.OPENROUTER_API_KEY ?? '', | ||
| }); | ||
|
|
||
| async function nonStreamingExample() { | ||
| console.log('=== Non-Streaming Example ===\n'); | ||
|
|
||
| // Basic chat completion - returns the full response at once | ||
| const result = await openRouter.chat.send({ | ||
| model: 'openai/gpt-4o-mini', | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: 'What is the capital of France?', | ||
| }, | ||
| ], | ||
| stream: false, // Explicitly set stream to false for non-streaming | ||
| }); | ||
|
|
||
| // The SDK provides strong typing - result has 'choices' property | ||
| if ('choices' in result && result.choices[0]) { | ||
| console.log('Model:', result.model); | ||
| console.log('Response:', result.choices[0].message.content); | ||
| console.log('Usage:', result.usage); | ||
| console.log(); | ||
| } | ||
| } | ||
|
|
||
| async function streamingExample() { | ||
| console.log('=== Streaming Example ===\n'); | ||
|
|
||
| // Streaming chat completion - returns an async iterable | ||
| const stream = await openRouter.chat.send({ | ||
| model: 'openai/gpt-4o-mini', | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: 'Write a haiku about TypeScript', | ||
| }, | ||
| ], | ||
| stream: true, // Enable streaming mode | ||
| streamOptions: { | ||
| includeUsage: true, // Include token usage in the final chunk | ||
| }, | ||
| }); | ||
|
|
||
| console.log('Streaming response:'); | ||
| let fullContent = ''; | ||
|
|
||
| // The SDK returns an async iterable that you can iterate with for-await-of | ||
| for await (const chunk of stream) { | ||
| // Each chunk contains partial data | ||
| if (chunk.choices?.[0]?.delta?.content) { | ||
| const content = chunk.choices[0].delta.content; | ||
| process.stdout.write(content); // Write without newline to see real-time streaming | ||
| fullContent += content; | ||
| } | ||
|
|
||
| // Usage stats are included in the final chunk when streamOptions.includeUsage is true | ||
| if (chunk.usage) { | ||
| console.log('\n\nStream usage:', chunk.usage); | ||
| } | ||
| } | ||
|
|
||
| console.log('\n\nFull response:', fullContent); | ||
| console.log(); | ||
| } | ||
|
|
||
| async function main() { | ||
| try { | ||
| // Demonstrate both streaming and non-streaming modes | ||
| await nonStreamingExample(); | ||
| await streamingExample(); | ||
| } catch (error) { | ||
| console.error('Error:', error); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| main(); | ||
|
Comment on lines
+83
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would pick 1 pattern -- either try/catch or main().catch -- generally prefer just naked call or have an utils to wrap the main function |
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would love to pull this out into a function