-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
202 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'token.js': patch | ||
--- | ||
|
||
Support OpenRouter |
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
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,44 @@ | ||
# Perplexity | ||
|
||
[Get an OpenRouter API key](https://openrouter.ai/settings/keys) | ||
|
||
## Usage | ||
|
||
{% code title=".env" %} | ||
```bash | ||
OPENROUTER_API_KEY= | ||
``` | ||
{% endcode %} | ||
|
||
```typescript | ||
import { TokenJS } from 'token.js' | ||
|
||
// Create the Token.js client | ||
const tokenjs = new TokenJS() | ||
|
||
async function main() { | ||
// Create a model response | ||
const completion = await tokenjs.chat.completions.create({ | ||
// Specify the provider and model | ||
provider: 'openrouter', | ||
model: 'nvidia/nemotron-4-340b-instruct', | ||
// Define your message | ||
messages: [ | ||
{ | ||
role: 'user', | ||
content: 'Hello!', | ||
}, | ||
], | ||
}) | ||
console.log(completion.choices[0]) | ||
} | ||
main() | ||
``` | ||
|
||
## Compatibility | ||
OpenRouter supports more than 180 models from a variety of providers which may have varying feature support. We recommend reviewing the OpenRouter and provider documentation for specific compatibility information. | ||
|
||
## Additional Resources | ||
|
||
* [Supported Models](https://openrouter.ai/models) | ||
* [OpenRouter Documentation](https://openrouter.ai/docs/quick-start) |
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
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,43 @@ | ||
import OpenAI from 'openai' | ||
|
||
import { OpenRouterModel, ProviderCompletionParams } from '../chat/index.js' | ||
import { | ||
CompletionResponse, | ||
StreamCompletionResponse, | ||
} from '../userTypes/index.js' | ||
import { BaseHandler } from './base.js' | ||
import { InputError } from './types.js' | ||
|
||
// Groq is very compatible with OpenAI's API, so we could likely reuse the OpenAI SDK for this handler | ||
// to reducee the bundle size. | ||
export class OpenRouterHandler extends BaseHandler<OpenRouterModel> { | ||
validateInputs(body: ProviderCompletionParams<'openrouter'>): void { | ||
super.validateInputs(body) | ||
} | ||
|
||
async create( | ||
body: ProviderCompletionParams<'openrouter'> | ||
): Promise<CompletionResponse | StreamCompletionResponse> { | ||
this.validateInputs(body) | ||
|
||
console.log('open router') | ||
|
||
const apiKey = this.opts.apiKey ?? process.env.OPENROUTER_API_KEY | ||
const client = new OpenAI({ | ||
apiKey, | ||
baseURL: 'https://openrouter.ai/api/v1', | ||
defaultHeaders: { | ||
'HTTP-Referer': 'docs.tokenjs.ai', | ||
'X-Title': 'Token.js', | ||
}, | ||
}) | ||
|
||
if (apiKey === undefined) { | ||
throw new InputError( | ||
'API key is required for OpenRouter, define OPENROUTER_API_KEY in your environment or specifty the apiKey option.' | ||
) | ||
} | ||
|
||
return client.chat.completions.create(body) | ||
} | ||
} |
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
Oops, something went wrong.