andrewyng--aisuite
622d190614
Add a dedicated Requesty provider mirroring the existing OpenRouter provider. Requesty (https://router.requesty.ai/v1) is an OpenAI-compatible LLM router, so it reuses the OpenAI client and OpenAICompliantMessageConverter. - Reads REQUESTY_API_KEY (overridable via config) - Fixed base URL https://router.requesty.ai/v1 - Optional HTTP-Referer / X-Title attribution headers via REQUESTY_SITE_URL / REQUESTY_APP_NAME - Uses provider/model naming (e.g. requesty:openai/gpt-4o-mini) - Adds unit tests mirroring the OpenRouter provider tests - Lists Requesty in the README provider summary Signed-off-by: Thibault Jaigu <thibault.jaigu@gmail.com>
50 行
2.1 KiB
Python
50 行
2.1 KiB
Python
import openai
|
|
import os
|
|
from aisuite.provider import Provider, LLMError
|
|
from aisuite.providers.message_converter import OpenAICompliantMessageConverter
|
|
|
|
|
|
class RequestyProvider(Provider):
|
|
def __init__(self, **config):
|
|
"""
|
|
Initialize the Requesty provider with the given configuration.
|
|
Pass the entire configuration dictionary to the OpenAI client constructor.
|
|
"""
|
|
# Ensure API key is provided either in config or via environment variable
|
|
config.setdefault("api_key", os.getenv("REQUESTY_API_KEY"))
|
|
config.setdefault("base_url", "https://router.requesty.ai/v1")
|
|
|
|
# Support optional Requesty attribution headers
|
|
default_headers = config.get("default_headers", {})
|
|
if os.getenv("REQUESTY_SITE_URL") and "HTTP-Referer" not in default_headers:
|
|
default_headers["HTTP-Referer"] = os.getenv("REQUESTY_SITE_URL")
|
|
if os.getenv("REQUESTY_APP_NAME") and "X-Title" not in default_headers:
|
|
default_headers["X-Title"] = os.getenv("REQUESTY_APP_NAME")
|
|
|
|
if default_headers:
|
|
config["default_headers"] = default_headers
|
|
|
|
if not config["api_key"]:
|
|
raise ValueError(
|
|
"Requesty API key is missing. Please provide it in the config or set the REQUESTY_API_KEY environment variable."
|
|
)
|
|
|
|
self.client = openai.OpenAI(**config)
|
|
self.transformer = OpenAICompliantMessageConverter()
|
|
|
|
super().__init__()
|
|
|
|
def chat_completions_create(self, model, messages, **kwargs):
|
|
# Any exception raised by Requesty will be returned to the caller.
|
|
# Maybe we should catch them and raise a custom LLMError.
|
|
try:
|
|
transformed_messages = self.transformer.convert_request(messages)
|
|
response = self.client.chat.completions.create(
|
|
model=model,
|
|
messages=transformed_messages,
|
|
**kwargs, # Pass any additional arguments to the Requesty API
|
|
)
|
|
return response
|
|
except Exception as e:
|
|
raise LLMError(f"An error occurred: {e}")
|