|
31 | 31 | "deepseek", |
32 | 32 | "fireworks", |
33 | 33 | "ollama", |
| 34 | + "openrouter", |
34 | 35 | "xai", |
35 | 36 | "litellm", |
36 | 37 | ] |
@@ -946,6 +947,60 @@ def from_provider( |
946 | 947 | ) |
947 | 948 | raise |
948 | 949 |
|
| 950 | + elif provider == "openrouter": |
| 951 | + try: |
| 952 | + import openai |
| 953 | + from instructor import from_openai |
| 954 | + import os |
| 955 | + |
| 956 | + # Get API key from kwargs or environment |
| 957 | + api_key = api_key or os.environ.get("OPENROUTER_API_KEY") |
| 958 | + |
| 959 | + if not api_key: |
| 960 | + from .core.exceptions import ConfigurationError |
| 961 | + |
| 962 | + raise ConfigurationError( |
| 963 | + "OPENROUTER_API_KEY is not set. " |
| 964 | + "Set it with `export OPENROUTER_API_KEY=<your-api-key>` or pass it as kwarg api_key=<your-api-key>" |
| 965 | + ) |
| 966 | + |
| 967 | + # OpenRouter uses OpenAI-compatible API |
| 968 | + base_url = kwargs.pop("base_url", "https://openrouter.ai/api/v1") |
| 969 | + |
| 970 | + client = ( |
| 971 | + openai.AsyncOpenAI(api_key=api_key, base_url=base_url) |
| 972 | + if async_client |
| 973 | + else openai.OpenAI(api_key=api_key, base_url=base_url) |
| 974 | + ) |
| 975 | + |
| 976 | + result = from_openai( |
| 977 | + client, |
| 978 | + model=model_name, |
| 979 | + mode=mode if mode else instructor.Mode.TOOLS, |
| 980 | + **kwargs, |
| 981 | + ) |
| 982 | + logger.info( |
| 983 | + "Client initialized", |
| 984 | + extra={**provider_info, "status": "success"}, |
| 985 | + ) |
| 986 | + return result |
| 987 | + except ImportError: |
| 988 | + from .core.exceptions import ConfigurationError |
| 989 | + |
| 990 | + raise ConfigurationError( |
| 991 | + "The openai package is required to use the OpenRouter provider. " |
| 992 | + "Install it with `pip install openai`." |
| 993 | + ) from None |
| 994 | + except Exception as e: |
| 995 | + logger.error( |
| 996 | + "Error initializing %s client: %s", |
| 997 | + provider, |
| 998 | + e, |
| 999 | + exc_info=True, |
| 1000 | + extra={**provider_info, "status": "error"}, |
| 1001 | + ) |
| 1002 | + raise |
| 1003 | + |
949 | 1004 | elif provider == "litellm": |
950 | 1005 | try: |
951 | 1006 | from litellm import completion, acompletion |
|
0 commit comments