From 54c495992f9b3ff2ac6c5b5c4402da2b249248b8 Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Wed, 27 Nov 2024 09:53:51 +0000 Subject: [PATCH 1/4] Adding OpenRouter as a new model provider. --- .../docs/ai-gateway/providers/open-router.mdx | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/content/docs/ai-gateway/providers/open-router.mdx diff --git a/src/content/docs/ai-gateway/providers/open-router.mdx b/src/content/docs/ai-gateway/providers/open-router.mdx new file mode 100644 index 000000000000000..5ac9c0a3f0b3b71 --- /dev/null +++ b/src/content/docs/ai-gateway/providers/open-router.mdx @@ -0,0 +1,66 @@ +--- +title: OpenRouter +pcx_content_type: get-started +sidebar: + badge: + text: Beta +--- + +[OpenRouter](https://openrouter.ai/) is a platform that provides a unified interface for accessing and using large language models (LLMs). + +## Endpoint + +```txt +https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openrouter +``` + +## URL structure + +When making requests to [OpenRouter](https://openrouter.ai/), replace `https://openrouter.ai/api/v1/chat/completions` in the URL you are currently using with `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openrouter`. + +## Examples + +### cURL + +```bash title="Request" +curl -X POST https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY/openrouter/v1/chat/completions \ + --header 'content-type: application/json' \ + --header 'Authorization: Bearer OPENROUTER_TOKEN' \ + --data '{ + "model": "openai/gpt-3.5-turbo", + "messages": [ + { + "role": "user", + "content": "What is Cloudflare?" + } + ] +}' + +``` + +### OpenAI SDK with JavaScript + +If you are using the OpenAI SDK with JavaScript, you can set your endpoint like this: + +```js title="JavaScript" +import OpenAI from 'openai'; + +const openai = new OpenAI({ + apiKey: env.OPENROUTER_TOKEN, + baseURL: "https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY/openrouter" +}); + +try { + const chatCompletion = await openai.chat.completions.create({ + model: "openai/gpt-3.5-turbo", + messages: [{ role: "user", content: "What is Cloudflare?" }] + }); + + const response = chatCompletion.choices[0].message; + + return new Response(JSON.stringify(response)); +} catch (e) { + return new Response(e); +} + +``` \ No newline at end of file From abd0436f9e8cf77b396c329967243987ebfe24c5 Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Wed, 27 Nov 2024 15:27:21 +0000 Subject: [PATCH 2/4] Adding H3s to examples. --- src/content/docs/ai-gateway/providers/anthropic.mdx | 2 ++ src/content/docs/ai-gateway/providers/azureopenai.mdx | 2 ++ src/content/docs/ai-gateway/providers/bedrock.mdx | 4 +++- src/content/docs/ai-gateway/providers/cohere.mdx | 2 ++ .../docs/ai-gateway/providers/google-ai-studio.mdx | 2 +- src/content/docs/ai-gateway/providers/grok.mdx | 8 ++++++++ src/content/docs/ai-gateway/providers/groq.mdx | 2 ++ src/content/docs/ai-gateway/providers/huggingface.mdx | 2 ++ src/content/docs/ai-gateway/providers/mistral.mdx | 2 +- src/content/docs/ai-gateway/providers/open-router.mdx | 2 +- src/content/docs/ai-gateway/providers/openai.mdx | 4 +++- src/content/docs/ai-gateway/providers/perplexity.mdx | 4 +++- 12 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/content/docs/ai-gateway/providers/anthropic.mdx b/src/content/docs/ai-gateway/providers/anthropic.mdx index 77f9e74a182a88b..9a138e95a5dee96 100644 --- a/src/content/docs/ai-gateway/providers/anthropic.mdx +++ b/src/content/docs/ai-gateway/providers/anthropic.mdx @@ -27,6 +27,8 @@ curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/anthropic/v1 }' ``` +### Use Anthropic SDK with JavaScript + If you are using the `@anthropic-ai/sdk`, you can set your endpoint like this: ```js title="JavaScript" diff --git a/src/content/docs/ai-gateway/providers/azureopenai.mdx b/src/content/docs/ai-gateway/providers/azureopenai.mdx index bbc2f4e0e7d00fc..2605f8e3a9c929f 100644 --- a/src/content/docs/ai-gateway/providers/azureopenai.mdx +++ b/src/content/docs/ai-gateway/providers/azureopenai.mdx @@ -41,6 +41,8 @@ curl 'https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/azure-opena }' ``` +### Use `openai-node` with JavaScript + If you are using the `openai-node` library, you can set your endpoint like this: ```js title="JavaScript" diff --git a/src/content/docs/ai-gateway/providers/bedrock.mdx b/src/content/docs/ai-gateway/providers/bedrock.mdx index 74b6db60d449bbe..cc9999a6485054d 100644 --- a/src/content/docs/ai-gateway/providers/bedrock.mdx +++ b/src/content/docs/ai-gateway/providers/bedrock.mdx @@ -20,7 +20,9 @@ When making requests to Amazon Bedrock, replace `https://bedrock-runtime.us-east With Bedrock, you will need to sign the URL before you make requests to AI Gateway. You can try using the [`aws4fetch`](https://github.com/mhart/aws4fetch) SDK. -## Example: +## Example + +### Use `aws4fetch1` SDK with TypeScript ```typescript import { AwsClient } from "aws4fetch"; diff --git a/src/content/docs/ai-gateway/providers/cohere.mdx b/src/content/docs/ai-gateway/providers/cohere.mdx index 4ef9e1fcf1d730a..35612c28d41b063 100644 --- a/src/content/docs/ai-gateway/providers/cohere.mdx +++ b/src/content/docs/ai-gateway/providers/cohere.mdx @@ -31,6 +31,8 @@ curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/cohere/v1/ch }' ``` +### Use Cohere SDK with Python + If using the [`cohere-python-sdk`](https://github.com/cohere-ai/cohere-python), set your endpoint like this: ```js title="Python" diff --git a/src/content/docs/ai-gateway/providers/google-ai-studio.mdx b/src/content/docs/ai-gateway/providers/google-ai-studio.mdx index 6d1cfeffcb89389..9688604107b2594 100644 --- a/src/content/docs/ai-gateway/providers/google-ai-studio.mdx +++ b/src/content/docs/ai-gateway/providers/google-ai-studio.mdx @@ -51,7 +51,7 @@ curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_name}/google-ai }' ``` -### JavaScript +### Use `@google/generative-ai` with JavaScript If you are using the `@google/generative-ai` package, you can set your endpoint like this: diff --git a/src/content/docs/ai-gateway/providers/grok.mdx b/src/content/docs/ai-gateway/providers/grok.mdx index 97ebf08f88eb468..23705811fb846e1 100644 --- a/src/content/docs/ai-gateway/providers/grok.mdx +++ b/src/content/docs/ai-gateway/providers/grok.mdx @@ -37,6 +37,8 @@ curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok/v1/chat }' ``` +### Use OpenAI SDK with JavaScript + If you are using the OpenAI SDK with JavaScript, you can set your endpoint like this: ```js title="JavaScript" @@ -66,6 +68,8 @@ const completion = await openai.chat.completions.create({ console.log(completion.choices[0].message); ``` +### Use OpenAI SDK with Python + If you are using the OpenAI SDK with Python, you can set your endpoint like this: ```python title="Python" @@ -89,6 +93,8 @@ completion = client.chat.completions.create( print(completion.choices[0].message) ``` +### Use Anthropic SDK with JavaScript + If you are using the Anthropic SDK with JavaScript, you can set your endpoint like this: ```js title="JavaScript" @@ -116,6 +122,8 @@ const msg = await anthropic.messages.create({ console.log(msg); ``` +### Use Anthropic SDK with Python + If you are using the Anthropic SDK with Python, you can set your endpoint like this: ```python title="Python" diff --git a/src/content/docs/ai-gateway/providers/groq.mdx b/src/content/docs/ai-gateway/providers/groq.mdx index af0133b91117106..12468f8400fd1ce 100644 --- a/src/content/docs/ai-gateway/providers/groq.mdx +++ b/src/content/docs/ai-gateway/providers/groq.mdx @@ -32,6 +32,8 @@ curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/groq/chat/co }' ``` +### Use Groq SDK with JavaScript + If using the [`groq-sdk`](https://www.npmjs.com/package/groq-sdk), set your endpoint like this: ```js title="JavaScript" diff --git a/src/content/docs/ai-gateway/providers/huggingface.mdx b/src/content/docs/ai-gateway/providers/huggingface.mdx index fc0407b88f1c4fd..b70c0cab1795b5a 100644 --- a/src/content/docs/ai-gateway/providers/huggingface.mdx +++ b/src/content/docs/ai-gateway/providers/huggingface.mdx @@ -26,6 +26,8 @@ curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/huggingface/ }' ``` +### Use HuggingFace.js library with JavaScript + If you are using the HuggingFace.js library, you can set your inference endpoint like this: ```js title="JavaScript" diff --git a/src/content/docs/ai-gateway/providers/mistral.mdx b/src/content/docs/ai-gateway/providers/mistral.mdx index 1c90f28509140c8..8e3a80d477e6418 100644 --- a/src/content/docs/ai-gateway/providers/mistral.mdx +++ b/src/content/docs/ai-gateway/providers/mistral.mdx @@ -50,7 +50,7 @@ curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/mist }' ``` -### JavaScript +### Use `@mistralai/mistralai` package with JavaScript If you are using the `@mistralai/mistralai` package, you can set your endpoint like this: diff --git a/src/content/docs/ai-gateway/providers/open-router.mdx b/src/content/docs/ai-gateway/providers/open-router.mdx index 5ac9c0a3f0b3b71..83af082da10d526 100644 --- a/src/content/docs/ai-gateway/providers/open-router.mdx +++ b/src/content/docs/ai-gateway/providers/open-router.mdx @@ -38,7 +38,7 @@ curl -X POST https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY/openrouter ``` -### OpenAI SDK with JavaScript +### Use OpenAI SDK with JavaScript If you are using the OpenAI SDK with JavaScript, you can set your endpoint like this: diff --git a/src/content/docs/ai-gateway/providers/openai.mdx b/src/content/docs/ai-gateway/providers/openai.mdx index 7956271545e2e34..816a7590fb21761 100644 --- a/src/content/docs/ai-gateway/providers/openai.mdx +++ b/src/content/docs/ai-gateway/providers/openai.mdx @@ -33,7 +33,9 @@ curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/ ' ``` -If you’re using a library like openai-node, set the `baseURL` to your OpenAI endpoint like this: +### Use OpenAI SDK with JavaScript + +If you are using a library like openai-node, set the `baseURL` to your OpenAI endpoint like this: ```js title="JavaScript" import OpenAI from "openai"; diff --git a/src/content/docs/ai-gateway/providers/perplexity.mdx b/src/content/docs/ai-gateway/providers/perplexity.mdx index f866810499ac315..11716ca8e804a5b 100644 --- a/src/content/docs/ai-gateway/providers/perplexity.mdx +++ b/src/content/docs/ai-gateway/providers/perplexity.mdx @@ -29,7 +29,9 @@ curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/perplexity-a }' ``` -Perplexity doesn't have their own SDK, but they have compatibility with the OpenAI SDK. You can use the OpenAI SDK to make a Perplexity call through AI Gateway as follows: +### Use Perplexity through OpenAI SDK with JavaScript + +Perplexity does not have their own SDK, but they have compatibility with the OpenAI SDK. You can use the OpenAI SDK to make a Perplexity call through AI Gateway as follows: ```js title="JavaScript" import OpenAI from "openai"; From 0c54b1396312b989537208ecefb3366e8af505c8 Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Wed, 27 Nov 2024 15:30:48 +0000 Subject: [PATCH 3/4] Hiding redundant Overview page. --- src/content/docs/ai-gateway/providers/index.mdx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/content/docs/ai-gateway/providers/index.mdx b/src/content/docs/ai-gateway/providers/index.mdx index 412e48db8480755..aa20bdc7e8c68b4 100644 --- a/src/content/docs/ai-gateway/providers/index.mdx +++ b/src/content/docs/ai-gateway/providers/index.mdx @@ -2,6 +2,8 @@ pcx_content_type: navigation title: Model providers sidebar: + group: + hideIndex: true order: 3 --- From 3d1a608597d8ecec922fccd77b3549ae9d16a38c Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Wed, 27 Nov 2024 15:54:14 +0000 Subject: [PATCH 4/4] Adding "cURL" as H3s where applicable. --- src/content/docs/ai-gateway/providers/anthropic.mdx | 2 ++ src/content/docs/ai-gateway/providers/azureopenai.mdx | 2 ++ src/content/docs/ai-gateway/providers/bedrock.mdx | 2 +- src/content/docs/ai-gateway/providers/cohere.mdx | 2 ++ src/content/docs/ai-gateway/providers/groq.mdx | 2 ++ src/content/docs/ai-gateway/providers/huggingface.mdx | 2 ++ src/content/docs/ai-gateway/providers/openai.mdx | 2 ++ src/content/docs/ai-gateway/providers/perplexity.mdx | 2 ++ src/content/docs/ai-gateway/providers/replicate.mdx | 2 ++ src/content/docs/ai-gateway/providers/vertex.mdx | 2 ++ 10 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/content/docs/ai-gateway/providers/anthropic.mdx b/src/content/docs/ai-gateway/providers/anthropic.mdx index 9a138e95a5dee96..a09d250672ab914 100644 --- a/src/content/docs/ai-gateway/providers/anthropic.mdx +++ b/src/content/docs/ai-gateway/providers/anthropic.mdx @@ -13,6 +13,8 @@ https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/anthropic` ## Examples +### cURL + ```bash title="Example fetch request" curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/anthropic/v1/messages \ --header 'x-api-key: {anthropic_api_key}' \ diff --git a/src/content/docs/ai-gateway/providers/azureopenai.mdx b/src/content/docs/ai-gateway/providers/azureopenai.mdx index 2605f8e3a9c929f..57cbd5adf9c2188 100644 --- a/src/content/docs/ai-gateway/providers/azureopenai.mdx +++ b/src/content/docs/ai-gateway/providers/azureopenai.mdx @@ -27,6 +27,8 @@ Your new base URL will use the data above in this structure: `https://gateway.ai ## Examples +### cURL + ```bash title="Example fetch request" curl 'https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/azure-openai/{resource_name}/{deployment_name}/chat/completions?api-version=2023-05-15' \ --header 'Content-Type: application/json' \ diff --git a/src/content/docs/ai-gateway/providers/bedrock.mdx b/src/content/docs/ai-gateway/providers/bedrock.mdx index cc9999a6485054d..fea8019538883c8 100644 --- a/src/content/docs/ai-gateway/providers/bedrock.mdx +++ b/src/content/docs/ai-gateway/providers/bedrock.mdx @@ -20,7 +20,7 @@ When making requests to Amazon Bedrock, replace `https://bedrock-runtime.us-east With Bedrock, you will need to sign the URL before you make requests to AI Gateway. You can try using the [`aws4fetch`](https://github.com/mhart/aws4fetch) SDK. -## Example +## Examples ### Use `aws4fetch1` SDK with TypeScript diff --git a/src/content/docs/ai-gateway/providers/cohere.mdx b/src/content/docs/ai-gateway/providers/cohere.mdx index 35612c28d41b063..46dd59aea1d4518 100644 --- a/src/content/docs/ai-gateway/providers/cohere.mdx +++ b/src/content/docs/ai-gateway/providers/cohere.mdx @@ -17,6 +17,8 @@ When making requests to [Cohere](https://cohere.com/), replace `https://api.cohe ## Examples +### cURL + ```bash title="Request" curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/cohere/v1/chat \ --header 'Authorization: Token {cohere_api_token}' \ diff --git a/src/content/docs/ai-gateway/providers/groq.mdx b/src/content/docs/ai-gateway/providers/groq.mdx index 12468f8400fd1ce..e9b918e36f9410d 100644 --- a/src/content/docs/ai-gateway/providers/groq.mdx +++ b/src/content/docs/ai-gateway/providers/groq.mdx @@ -17,6 +17,8 @@ When making requests to [Groq](https://groq.com/), replace `https://api.groq.com ## Examples +### cURL + ```bash title="Example fetch request" curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/groq/chat/completions \ --header 'Authorization: Bearer {groq_api_key}' \ diff --git a/src/content/docs/ai-gateway/providers/huggingface.mdx b/src/content/docs/ai-gateway/providers/huggingface.mdx index b70c0cab1795b5a..07d17184f6395d6 100644 --- a/src/content/docs/ai-gateway/providers/huggingface.mdx +++ b/src/content/docs/ai-gateway/providers/huggingface.mdx @@ -17,6 +17,8 @@ When making requests to HuggingFace Inference API, replace `https://api-inferenc ## Examples +### cURL + ```bash title="Request" curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/huggingface/bigcode/starcoder \ --header 'Authorization: Bearer {hf_api_token}' \ diff --git a/src/content/docs/ai-gateway/providers/openai.mdx b/src/content/docs/ai-gateway/providers/openai.mdx index 816a7590fb21761..1b4a6c11ec75680 100644 --- a/src/content/docs/ai-gateway/providers/openai.mdx +++ b/src/content/docs/ai-gateway/providers/openai.mdx @@ -17,6 +17,8 @@ When making requests to OpenAI, replace `https://api.openai.com/v1` in the URL y ## Examples +### cURL + ```bash title="Request" curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \ --header 'Authorization: Bearer {openai_token}' \ diff --git a/src/content/docs/ai-gateway/providers/perplexity.mdx b/src/content/docs/ai-gateway/providers/perplexity.mdx index 11716ca8e804a5b..b9d1f644c0deca3 100644 --- a/src/content/docs/ai-gateway/providers/perplexity.mdx +++ b/src/content/docs/ai-gateway/providers/perplexity.mdx @@ -13,6 +13,8 @@ https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/perplexity-ai ## Examples +### cURL + ```bash title="Example fetch request" curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/perplexity-ai/chat/completions \ --header 'accept: application/json' \ diff --git a/src/content/docs/ai-gateway/providers/replicate.mdx b/src/content/docs/ai-gateway/providers/replicate.mdx index 22a0f7d1da4222b..290cd8b3b882c18 100644 --- a/src/content/docs/ai-gateway/providers/replicate.mdx +++ b/src/content/docs/ai-gateway/providers/replicate.mdx @@ -17,6 +17,8 @@ When making requests to Replicate, replace `https://api.replicate.com/v1` in the ## Example +### cURL + ```bash title="Request" curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/replicate/predictions \ --header 'Authorization: Token {replicate_api_token}' \ diff --git a/src/content/docs/ai-gateway/providers/vertex.mdx b/src/content/docs/ai-gateway/providers/vertex.mdx index 96c102cef9f464d..6995414d44ba022 100644 --- a/src/content/docs/ai-gateway/providers/vertex.mdx +++ b/src/content/docs/ai-gateway/providers/vertex.mdx @@ -35,6 +35,8 @@ So your final URL will come together as: `https://gateway.ai.cloudflare.com/v1/{ ## Example +### cURL + ```bash title="Example fetch request" curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/google-vertex-ai/v1/projects/{project_name}/locations/{region}/publishers/google/models/gemini-1.0-pro-001:generateContent" \ -H "Authorization: Bearer {vertex_api_key}" \