我現在想將Workers AI的api在第三方web ui上使用,我想部屬可以將Workers AI的api接口轉化為Openai api格式的base…
- User
我現在想將Workers AI的api在第三方web ui上使用,我想部屬可以將Workers AI的api接口轉化為Openai api格式的base url,之後我只要在類似lobehub這樣的web ui上,填入base url和api和模型名字就能使用,我應該怎麼寫這個網站代碼呢? 註解:網站我會部屬在Cloudflare Workers上。 註解:這是Workers AI相容於Openai的文件說明
Use AI Gateway for analytics, caching, and security on requests to Workers AI. REST API
To interact with a REST API, update the URL used for your request:
Previous: https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model_id} New: https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/workers-ai/{model_id}For these parameters:
{account_id} is your Cloudflare account ID. {gateway_id} refers to the name of your existing AI Gateway. {model_id} refers to the model ID of the Workers AI model.Examples
First, generate an API token with Workers AI Read access and use it in your request. Request to Workers AI llama model
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/workers-ai/@cf/meta/llama-3.1-8b-instruct
--header 'Authorization: Bearer {cf_api_token}'
--header 'Content-Type: application/json'
--data '{"prompt": "What is Cloudflare?"}'Request to Workers AI text classification model
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/workers-ai/@cf/huggingface/distilbert-sst-2-int8
--header 'Authorization: Bearer {cf_api_token}'
--header 'Content-Type: application/json'
--data '{ "text": "Cloudflare docs are amazing!" }'OpenAI compatible endpoints
Workers AI supports OpenAI compatible endpoints for text generation (/v1/chat/completions) and text embedding models (/v1/embeddings). This allows you to use the same code as you would for your OpenAI commands, but swap in Workers AI easily.
Request to OpenAI compatible endpoint
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/workers-ai/v1/chat/completions
--header 'Authorization: Bearer {cf_api_token}'
--header 'Content-Type: application/json'
--data '{ "model": "@cf/meta/llama-3.1-8b-instruct", "messages": [ { "role": "user", "content": "What is Cloudflare?" } ] } 'Worker
To include an AI Gateway within your Worker, add the gateway as an object in your Workers AI request.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.