### Install from Source Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/10-quick-start.md Clone the repository and install the library in editable mode. ```bash git clone https://github.com/fredguth/langextract-openrouter.git cd langextract-openrouter pip install -e . ``` -------------------------------- ### Install LangExtract openrouter Provider from Source Source: https://github.com/fredguth/langextract-openrouter/blob/main/README.md Install the provider in development mode using pip. ```bash pip install -e . ``` -------------------------------- ### Install LangExtract openrouter Provider from PyPI Source: https://github.com/fredguth/langextract-openrouter/blob/main/README.md Install the provider from the Python Package Index using pip. ```bash pip install langextract-openrouter ``` -------------------------------- ### Setuptools Entry Point for OpenRouter Provider Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/01-overview.md This configuration snippet defines the entry point for the OpenRouter provider within the project's setup. It allows LangExtract to discover and load the 'openrouterLanguageModel' class when plugins are loaded. ```ini [project.entry-points."langextract.providers"] openrouter = "langextract_openrouter.provider:openrouterLanguageModel" ``` -------------------------------- ### Basic Usage with LangExtract Framework Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/10-quick-start.md Utilize the LangExtract framework for structured data extraction with examples. ```python import langextract as lx # Load plugins lx.providers.load_plugins_once() # Use the extract function result = lx.extract( text="John Smith works at Google in Mountain View.", model_id="openrouter/google/gemini-2.5-flash", prompt_description="Extract person name, company, and location", examples=[ { "text": "Alice works at Microsoft in Seattle.", "extraction": { "person": "Alice", "company": "Microsoft", "location": "Seattle" } } ] ) print(result) ``` -------------------------------- ### Configuration via LangExtract Framework Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/03-configuration.md Integrate the Openrouter provider using the LangExtract factory. This allows for a more structured configuration, especially when managing multiple models or complex setups. ```python import langextract as lx from langextract import factory lx.providers.load_plugins_once() config = factory.ModelConfig( model_id="openrouter/google/gemini-2.5-flash", provider_kwargs={ "api_key": "sk-or-v1-...", "temperature": 0.7, "max_tokens": 500 } ) model = factory.create_model(config) ``` -------------------------------- ### OpenAI Client Streaming Example Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/08-openai-integration.md This snippet demonstrates how to use the OpenAI client to stream responses. Note that the current OpenRouter integration does not support this feature. ```python with client.chat.completions.create( model=..., messages=..., stream=True ) as stream: for text in stream.text_stream: print(text, end="", flush=True) ``` -------------------------------- ### Advanced Inference with LangExtract Framework Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/02-api-reference.md Shows how to use the OpenRouter provider within the LangExtract framework for complex extraction tasks, including specifying a `prompt_description` and providing few-shot `examples`. ```python import langextract as lx # The provider is automatically registered and available # when the plugin is installed and loaded lx.providers.load_plugins_once() # LangExtract factory uses the provider automatically result = lx.extract( text="John Smith works at Google in Mountain View, CA.", model_id="openrouter/google/gemini-2.5-flash", prompt_description="Extract person name, company, and location", examples=[ { "text": "Alice works at Microsoft in Seattle.", "extraction": {"person": "Alice", "company": "Microsoft", "location": "Seattle"} } ] ) ``` -------------------------------- ### Network Timeout Error Log Example Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/05-errors.md Example log message for a network connection timeout. ```text ERROR:langextract_openrouter.provider:Error calling OpenRouter completion for model google/gemini-2.5-flash: HTTPError('Connection timeout') ``` -------------------------------- ### Authentication Error Log Example Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/05-errors.md Example log message for an authentication failure (401 Unauthorized). ```text ERROR:langextract_openrouter.provider:Error calling OpenRouter completion for model google/gemini-2.5-flash: 401 Client error '401 Unauthorized' for url ... ``` -------------------------------- ### Invalid Model Error Log Example Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/05-errors.md Example log message for requesting a non-existent model (404 Not Found). ```text ERROR:langextract_openrouter.provider:Error calling OpenRouter completion for model invalid-model: 404 Client error '404 Not Found' for url ... ``` -------------------------------- ### Rate Limit Error Log Example Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/05-errors.md Example log message for hitting API rate limits (429 Too Many Requests). ```text ERROR:langextract_openrouter.provider:Error calling OpenRouter completion for model google/gemini-2.5-flash: 429 Client error '429 Too Many Requests' for url ... ``` -------------------------------- ### Batch Inference Resilience Example Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/05-errors.md Demonstrates how batch inference continues even if individual prompts fail. Results will contain entries with a score of 0.0 for failed prompts. ```python from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel(model_id="openrouter/google/gemini-2.5-flash") prompts = [ "Valid prompt 1", "Valid prompt 2", "Valid prompt 3" ] # Even if one fails, iteration continues results = list(provider.infer(prompts)) # results will have 3 entries, some with score=1.0, some with score=0.0 for i, result_list in enumerate(results): score = result_list[0].score if result_list else 0.0 print(f"Prompt {i}: score={score}") ``` -------------------------------- ### BaseLanguageModel Usage Example Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/04-types.md Shows how to instantiate and use the infer() method of a concrete implementation of BaseLanguageModel, such as openrouterLanguageModel. The infer() method yields lists of ScoredOutput objects. ```python from langextract_openrouter import openrouterLanguageModel # openrouterLanguageModel inherits from BaseLanguageModel provider = openrouterLanguageModel(model_id="openrouter/google/gemini-2.5-flash") # The infer() method is inherited interface, implemented by the provider for results in provider.infer(["prompt 1", "prompt 2"]): print(results) # List of ScoredOutput objects ``` -------------------------------- ### OpenAI ChatCompletion Client Example Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/04-types.md Illustrates how to use the OpenAI client to make a chat completion request, which is internally used by the provider. The response content is accessed via result.choices[0].message.content. ```python from openai import OpenAI client = OpenAI(api_key="...", base_url="https://openrouter.ai/api/v1") result = client.chat.completions.create( model="google/gemini-2.5-flash", messages=[{"role": "user", "content": "Hello"}] ) # result.choices[0].message.content contains the response text print(result.choices[0].message.content) ``` -------------------------------- ### Handling OpenAI API Errors in Python Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/08-openai-integration.md Provides an example of how to catch specific OpenAI API exceptions like AuthenticationError, RateLimitError, and general APIStatusError. Ensure necessary exceptions are imported. ```python from openai import APIStatusError, AuthenticationError, RateLimitError try: result = client.chat.completions.create(...) except AuthenticationError as e: print("Invalid API key") except RateLimitError as e: print("Rate limited; retry later") except APIStatusError as e: print(f"API error: {e.status_code}") ``` -------------------------------- ### Handle Rate Limiting Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/10-quick-start.md If you receive a 'Rate Limit Exceeded' error, implement a retry mechanism with a delay. This example waits for 60 seconds before retrying the inference. ```python import time results = list(provider.infer(["test"])) if results[0][0].score == 0.0: print("Rate limited; waiting...") time.sleep(60) results = list(provider.infer(["test"]))) ``` -------------------------------- ### Use Valid Model IDs with OpenRouter Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/05-errors.md To avoid errors, always use a model ID that is known to exist on OpenRouter and is accessible with your API key. This example shows how to instantiate the provider with a valid model. ```python from langextract_openrouter import openrouterLanguageModel # Use a known-valid model ID valid_models = [ "openrouter/google/gemini-2.5-flash", "openrouter/openai/gpt-4.1", "openrouter/anthropic/claude-3-opus" ] provider = openrouterLanguageModel(model_id=valid_models[0]) ``` -------------------------------- ### Handle Network Errors with OpenRouter Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/05-errors.md Network issues like connection timeouts or DNS failures can occur when trying to reach the OpenRouter API. This example shows how to enable error logging to view details and includes a basic try-except block to catch and report these exceptions. ```python from langextract_openrouter import openrouterLanguageModel import logging # Enable error logging to see network details logging.basicConfig(level=logging.ERROR) provider = openrouterLanguageModel(model_id="openrouter/google/gemini-2.5-flash") try: results = list(provider.infer(["test"])) except Exception as e: print(f"Network error: {e}") # Retry or use fallback ``` -------------------------------- ### Package Initialization (__init__.py) Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/09-source-code-reference.md Defines the package entry point and exports the main provider class. Includes package version information. ```python """LangExtract provider plugin for openrouter.""" from langextract_openrouter.provider import openrouterLanguageModel __all__ = ['openrouterLanguageModel'] __version__ = "0.1.0" ``` -------------------------------- ### Provider Registration Entry Point Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/INDEX.md Shows the setuptools entry point for registering the OpenRouter provider as a LangExtract plugin. This is used for package initialization. ```text langextract.providers → openrouter → langextract_openrouter.provider:openrouterLanguageModel ``` -------------------------------- ### Setuptools Package Discovery Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/09-source-code-reference.md Configures setuptools to find and include the 'langextract_openrouter' package during the build process. ```toml where = [". ``` ```toml include = ["langextract_openrouter*"] ``` -------------------------------- ### Initializing with Common OpenAI Parameters Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/08-openai-integration.md Shows how to initialize the openrouterLanguageModel with common OpenAI API parameters such as temperature, max_tokens, and top_p. ```python provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash", api_key="sk-or-v1-ப்படாத", temperature=0.7, max_tokens=500, top_p=0.9 ) ``` -------------------------------- ### Initialize and Use Multiple Models Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/10-quick-start.md Instantiate different language models for varying task complexities. Use fast models for simple tasks and powerful models for complex ones. ```python from langextract_openrouter import openrouterLanguageModel # Fast model for simple tasks fast_provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash" ) # Powerful model for complex tasks powerful_provider = openrouterLanguageModel( model_id="openrouter/anthropic/claude-3-opus" ) # Route based on task complexity simple_result = list(fast_provider.infer(["Simple task"]))[0][0].output complex_result = list(powerful_provider.infer(["Complex task"]))[0][0].output ``` -------------------------------- ### Register OpenRouter Provider Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/09-source-code-reference.md Decorator used to register the `openrouterLanguageModel` class with the langextract provider registry. It matches model IDs starting with 'openrouter'. ```python @lx.providers.registry.register(r'^openrouter', priority=10) class openrouterLanguageModel(lx.inference.BaseLanguageModel): ``` -------------------------------- ### Plugin Discovery with LangExtract Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/07-provider-interface.md Loads LangExtract provider plugins by scanning setuptools entry points. This process automatically registers discovered providers. ```python import langextract as lx # LangExtract discovers entry points lx.providers.load_plugins_once() ``` -------------------------------- ### Performing Inference with a Provider Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/07-provider-interface.md Executes the inference process using an instantiated provider. The 'infer' method is called with a batch of prompts to get results. ```python results = provider.infer(batch_prompts) ``` -------------------------------- ### openrouterLanguageModel Initialization and Inference Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/README.md Demonstrates how to initialize the openrouterLanguageModel provider with a specific model and API key, and how to run inference on a batch of prompts. ```APIDOC ## openrouterLanguageModel ### Description Initializes the provider with a model and API key, and runs inference. ### Class `openrouterLanguageModel` ### Initialization Example ```python from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash", api_key="sk-or-v1-...", temperature=0.7, max_tokens=500 ) ``` ### Infer Method #### Signature `infer(batch_prompts, **kwargs)` #### Purpose Run inference on prompts. #### Request Example ```python # Run inference results = list(provider.infer(["Your prompt here"])) output = results[0][0].output ``` ### Environment Variable - `OPENROUTER_API_KEY` — API key for authentication ``` -------------------------------- ### Initializing with Model-Specific Parameters Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/08-openai-integration.md Illustrates initializing the openrouterLanguageModel with model-specific parameters like top_k and repetition_penalty, which may not be supported by all models. ```python provider = openrouterLanguageModel( model_id="openrouter/meta-llama/llama-2-70b-chat", api_key="sk-or-v1-ப்படாத", top_k=40, # Some models support top_k repetition_penalty=1.1 # Some models support repetition_penalty ) ``` -------------------------------- ### Build System Requirements Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/09-source-code-reference.md Specifies the build system requirements for the project, including setuptools and wheel. ```toml requires = ["setuptools>=61.0", "wheel"] build-backend = "setuptools.build_meta" ``` -------------------------------- ### Define Provider Entry Point in pyproject.toml Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/07-provider-interface.md Defines the entry point for the 'openrouter' provider in LangExtract. This configuration tells LangExtract where to find the provider class. ```toml [project.entry-points."langextract.providers"] openrouter = "langextract_openrouter.provider:openrouterLanguageModel" ``` -------------------------------- ### Initialize Provider with API Key Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/10-quick-start.md Initialize the openrouterLanguageModel with the model ID and API key. ```python from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash", api_key="sk-or-v1-your-key-here" ) ``` -------------------------------- ### Configuration with Inference Parameters Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/03-configuration.md Configure the provider with specific inference parameters like temperature and max_tokens. Adjust these to control the model's output creativity and length. ```python from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel( model_id="openrouter/anthropic/claude-3-opus", api_key="sk-or-v1-...", temperature=0.5, max_tokens=1000, top_p=0.95 ) ``` -------------------------------- ### Register and Use OpenRouter Model with LangExtract Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/02-api-reference.md Import the necessary classes and load plugins to enable OpenRouter models. Then, create a model instance using the factory with a model ID prefixed by 'openrouter'. ```python import langextract as lx from langextract_openrouter import openrouterLanguageModel lx.providers.load_plugins_once() # Now any model_id starting with 'openrouter' will use this provider model = lx.factory.create_model( lx.factory.ModelConfig(model_id="openrouter/google/gemini-2.5-flash") ) ``` -------------------------------- ### Basic Inference with OpenRouter Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/02-api-reference.md Demonstrates how to perform single inference calls using the `infer` method with a list of prompts. The results are collected into a list of lists of `ScoredOutput` objects. ```python import langextract as lx from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash" ) # Single inference call prompts = [ "Extract all entities from: John works at Google in Mountain View.", "Summarize: The quick brown fox jumps over the lazy dog." ] results = list(provider.infer(prompts)) # results is a list of lists of ScoredOutput objects # results[0] = [ScoredOutput(score=1.0, output="...")] # results[1] = [ScoredOutput(score=1.0, output="...")] for i, result_list in enumerate(results): if result_list and result_list[0].score > 0: print(f"Prompt {i}: {result_list[0].output}") ``` -------------------------------- ### OpenAI Chat Completion Messages Format Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/08-openai-integration.md Format the messages for the chat completion API call. This example shows a simple user message, adhering to the standard OpenAI role and content structure. ```python messages = [ { "role": "user", "content": str(prompt) } ] ``` -------------------------------- ### Instantiate openrouterLanguageModel with Additional Parameters Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/02-api-reference.md Instantiate the provider with a model ID, API key, and additional inference parameters such as temperature and max_tokens. These parameters are forwarded to the underlying OpenAI chat completions API. ```python provider = openrouterLanguageModel( model_id="openrouter/anthropic/claude-3-opus", api_key="sk-or-v1-...", temperature=0.7, max_tokens=500 ) ``` -------------------------------- ### Initialize openrouterLanguageModel Provider Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/README.md Instantiate the main provider class with a specific model ID, API key, and inference parameters. The API key can also be set via the OPENROUTER_API_KEY environment variable. ```python from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash", api_key="sk-or-v1-...", temperature=0.7, max_tokens=500 ) # Run inference results = list(provider.infer(["Your prompt here"])) output = results[0][0].output ``` -------------------------------- ### Instantiate openrouterLanguageModel using Environment Variable Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/02-api-reference.md Instantiate the provider by relying on the OPENROUTER_API_KEY environment variable. This is useful for keeping API keys out of source code. The model ID can be in various formats, and the 'openrouter/' prefix is handled automatically. ```python import os os.environ['OPENROUTER_API_KEY'] = 'sk-or-v1-...' provider = openrouterLanguageModel( model_id="openai/gpt-4.1" ) ``` -------------------------------- ### Provider API Key Check Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/03-configuration.md Illustrates how the provider checks for the API key, prioritizing the constructor argument and falling back to the environment variable. ```python self.api_key = api_key or os.environ.get('OPENROUTER_API_KEY') ``` -------------------------------- ### Provide API Key for Initialization Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/05-errors.md Set the OPENROUTER_API_KEY environment variable or pass the api_key directly to the constructor to avoid initialization errors. ```python import os # Set before importing provider os.environ['OPENROUTER_API_KEY'] = 'sk-or-v1-...' from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel(model_id="openrouter/google/gemini-2.5-flash") ``` ```python from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash", api_key="sk-or-v1-..." ) ``` -------------------------------- ### LangExtract Provider Entry Point Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/09-source-code-reference.md Registers the 'openrouter' provider with the LangExtract plugin system. ```toml openrouter = "langextract_openrouter.provider:openrouterLanguageModel" ``` -------------------------------- ### Publish LangExtract Plugin to PyPI Source: https://github.com/fredguth/langextract-openrouter/blob/main/README.md Upload the built package to the Python Package Index using twine. ```bash twine upload dist/* ``` -------------------------------- ### Load Plugins and Resolve Models with Multiple Providers Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/07-provider-interface.md Load plugins once to make all registered providers available. The registry then routes model IDs to the appropriate provider based on their registered patterns and priorities. ```python import langextract as lx from langextract_openrouter import openrouterLanguageModel lx.providers.load_plugins_once() # Registry now has multiple providers: # - openrouterLanguageModel (pattern: ^openrouter) # - Other providers (from other plugins) # Each model ID is routed to the appropriate provider lx.providers.registry.resolve("openrouter/google/gemini") # → openrouterLanguageModel lx.providers.registry.resolve("some-other/model") # → SomeOtherProvider ``` -------------------------------- ### Load Plugins and Resolve Provider Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/07-provider-interface.md Loads LangExtract plugins and demonstrates how to resolve a provider for a given model ID. The registry uses the defined patterns and priorities to select the appropriate provider. ```python import langextract as lx lx.providers.load_plugins_once() # "openrouter/google/gemini-2.5-flash" matches ^openrouter provider_class = lx.providers.registry.resolve("openrouter/google/gemini-2.5-flash") # Returns: openrouterLanguageModel # "google/gemini-2.5-flash" does NOT match ^openrouter # If no other provider matches, returns None or raises ``` -------------------------------- ### Equivalent Imports for OpenRouter Language Model Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/07-provider-interface.md Demonstrates two equivalent ways to import the openrouterLanguageModel, either directly from the package or from its specific provider module. ```python # Both imports are equivalent from langextract_openrouter import openrouterLanguageModel from langextract_openrouter.provider import openrouterLanguageModel ``` -------------------------------- ### Initialize OpenAI Client with OpenRouter Base URL Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/03-configuration.md This snippet shows how to initialize the OpenAI client, specifying the custom base URL for the OpenRouter API. ```python self.client = OpenAI(api_key=self.api_key, base_url="https://openrouter.ai/api/v1") ``` -------------------------------- ### Create Model via Factory Pattern Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/06-usage-patterns.md Utilize the LangExtract factory pattern to create model instances by providing a configuration object that specifies the model ID and provider-specific arguments like API key and temperature. ```python import langextract as lx from langextract import factory lx.providers.load_plugins_once() # Create model via factory config = factory.ModelConfig( model_id="openrouter/google/gemini-2.5-flash", provider_kwargs={ "api_key": "sk-or-v1-…", "temperature": 0.5 } ) model = factory.create_model(config) # Use model for inference results = list(model.infer(["Test prompt"])) ``` -------------------------------- ### Initialize with Environment Variable Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/06-usage-patterns.md Set the OPENROUTER_API_KEY environment variable before creating an instance of openrouterLanguageModel. This is useful for managing API keys securely. ```python import os from langextract_openrouter import openrouterLanguageModel # Set API key once os.environ['OPENROUTER_API_KEY'] = 'sk-or-v1-your-key' # Create provider instances as needed provider = openrouterLanguageModel(model_id="openrouter/google/gemini-2.5-flash") ``` -------------------------------- ### Create Model using LangExtract Factory Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/07-provider-interface.md Instantiates a language model provider using the LangExtract factory. Configure the model ID and any provider-specific keyword arguments for inference. ```python from langextract import factory config = factory.ModelConfig( model_id="openrouter/google/gemini-2.5-flash", provider_kwargs={"temperature": 0.5} ) model = factory.create_model(config) ``` -------------------------------- ### Initialize OpenRouter Provider Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/09-source-code-reference.md Initializes the OpenRouter provider, setting up the model ID, API key (from argument or environment variable), and the OpenAI client with the OpenRouter base URL. ```python def __init__(self, model_id: str, api_key: str = None, **kwargs): """Initialize the openrouter provider. Args: model_id: The model identifier. api_key: API key for authentication. **kwargs: Additional provider-specific parameters. """ super().__init__() if model_id.startswith("openrouter/"): self.model_id = model_id[11:] # Remove 'openrouter/' prefix else: self.model_id = model_id self.original_model_id = model_id self.api_key = api_key or os.environ.get('OPENROUTER_API_KEY') self.client = OpenAI(api_key=self.api_key, base_url="https://openrouter.ai/api/v1") self._extra_kwargs = kwargs logging.info(f'Initialized OpenRouter provider for model: {self.model_id}') ``` -------------------------------- ### Build LangExtract Plugin Package Source: https://github.com/fredguth/langextract-openrouter/blob/main/README.md Build the distribution package for the plugin using the build module. ```bash python -m build ``` -------------------------------- ### Instantiate openrouterLanguageModel with API Key Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/02-api-reference.md Directly instantiate the provider with your OpenRouter API key and a specific model ID. Ensure the model ID is correctly formatted. ```python import langextract as lx from langextract_openrouter import openrouterLanguageModel # Direct instantiation with explicit API key provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash", api_key="sk-or-v1-..." ) ``` -------------------------------- ### Run LangExtract Plugin Tests Source: https://github.com/fredguth/langextract-openrouter/blob/main/README.md Execute the plugin's tests using the provided Python script. ```bash python test_plugin.py ``` -------------------------------- ### Create a Reusable Inference Service Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/06-usage-patterns.md Encapsulate the `openrouterLanguageModel` instance within a class to create a reusable service for processing batches of prompts. This pattern promotes code organization and simplifies repeated inference tasks. ```python from langextract_openrouter import openrouterLanguageModel class InferenceService: def __init__(self, model_id, api_key): self.provider = openrouterLanguageModel( model_id=model_id, api_key=api_key ) def process_batch(self, prompts): results = list(self.provider.infer(prompts)) return [ r[0].output if r[0].score == 1.0 else None for r in results ] # Usage service = InferenceService( model_id="openrouter/google/gemini-2.5-flash", api_key="sk-or-v1-..." ) outputs = service.process_batch([ "Prompt 1", "Prompt 2", "Prompt 3" ]) ``` -------------------------------- ### Instantiate openrouterLanguageModel with Model ID Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/03-configuration.md Instantiate the openrouterLanguageModel by providing the required model_id. The model_id can include or omit the 'openrouter/' prefix. ```python provider = openrouterLanguageModel(model_id="openrouter/google/gemini-2.5-flash") ``` -------------------------------- ### Configure Inference Parameters Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/10-quick-start.md Customize inference behavior by setting parameters like `temperature`, `max_tokens`, and `top_p`. Provide your API key during initialization. ```python from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel( model_id="openrouter/openai/gpt-4.1", api_key="sk-or-v1-...", temperature=0.5, # Lower = more deterministic max_tokens=100, # Limit response length top_p=0.9 # Nucleus sampling ) results = list(provider.infer(["Your prompt here"])). ``` -------------------------------- ### Key Parameters Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/INDEX.md Details on the important parameters used when initializing or calling methods of the `openrouterLanguageModel` class. ```APIDOC ## Parameters for openrouterLanguageModel ### `model_id` - **Type**: `str` - **Required**: Yes - **Description**: The identifier for the language model to use (e.g., `"openrouter/google/gemini-2.5-flash"`). ### `api_key` - **Type**: `str` - **Required**: No - **Default**: `OPENROUTER_API_KEY` environment variable - **Description**: Your OpenRouter API key for authentication. ### `temperature` - **Type**: `float` - **Required**: No - **Default**: Provider default - **Description**: Controls the randomness of the output. Values typically range from 0.0 to 2.0. ### `max_tokens` - **Type**: `int` - **Required**: No - **Default**: Provider default - **Description**: The maximum number of tokens to generate in the response. ``` -------------------------------- ### openrouterLanguageModel Constructor Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/02-api-reference.md Initializes the OpenRouter language model provider. It requires a model ID and optionally accepts an API key and other provider-specific parameters. ```APIDOC ## `openrouterLanguageModel` Constructor ### Description Initializes the `openrouterLanguageModel` provider, enabling interaction with OpenRouter's API for language model inference. It handles model ID formatting and API key management, falling back to environment variables if the key is not explicitly provided. ### Signature ```python def __init__(self, model_id: str, api_key: str = None, **kwargs) ``` ### Parameters #### Path Parameters * None #### Query Parameters * None #### Request Body * None ### Parameters * **`model_id`** (str) - Required - The model identifier. Can be prefixed with `openrouter/` (which is auto-stripped) or in the format `provider/model-name`. * **`api_key`** (str) - Optional - Defaults to `None`. Your OpenRouter API key. If not provided, it attempts to read from the `OPENROUTER_API_KEY` environment variable. An error will be raised if neither is available. * **`**kwargs`** (dict) - Optional - Defaults to `{}`. Additional provider-specific parameters that are forwarded to the OpenAI chat completions API. Examples include `temperature`, `max_tokens`, and `top_p`. ### Request Example ```python import langextract as lx from langextract_openrouter import openrouterLanguageModel # Direct instantiation with explicit API key provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash", api_key="sk-or-v1-..." ) # Or rely on OPENROUTER_API_KEY environment variable import os os.environ['OPENROUTER_API_KEY'] = 'sk-or-v1-...' provider = openrouterLanguageModel( model_id="openai/gpt-4.1" ) # With additional inference parameters provider = openrouterLanguageModel( model_id="openrouter/anthropic/claude-3-opus", api_key="sk-or-v1-...", temperature=0.7, max_tokens=500 ) ``` ### Response #### Success Response * The constructor does not return a value but initializes the provider instance. #### Response Example * N/A (Constructor) ``` -------------------------------- ### Dynamically Select Models Based on Task Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/06-usage-patterns.md Use a factory function to create different language model providers based on predefined model types like 'fast', 'balanced', or 'powerful'. This allows for flexible model selection tailored to specific task requirements. ```python from langextract_openrouter import openrouterLanguageModel def create_provider(model_type="fast"): models = { "fast": "openrouter/google/gemini-2.5-flash", "balanced": "openrouter/openai/gpt-4", "powerful": "openrouter/anthropic/claude-3-opus" } return openrouterLanguageModel( model_id=models.get(model_type, models["fast"]), api_key="sk-or-v1-..." ) # Use different models for different tasks fast_provider = create_provider("fast") powerful_provider = create_provider("powerful") fast_results = list(fast_provider.infer(["Quick task"])) powerful_results = list(powerful_provider.infer(["Complex task"])) ``` -------------------------------- ### Provider Instantiation Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/07-provider-interface.md Instantiates a provider class obtained from the registry. The model ID and any additional keyword arguments are passed to the provider's constructor. ```python provider_class = lx.providers.registry.resolve(model_id) provider = provider_class(model_id=model_id, **kwargs) ``` -------------------------------- ### Automatic Provider Discovery with LangExtract Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/06-usage-patterns.md Load all available plugins, including OpenRouter, to enable LangExtract to automatically discover and use OpenRouter models for extraction tasks. This requires plugins to be loaded once. ```python import langextract as lx from langextract_openrouter import openrouterLanguageModel # Load all plugins (including openrouter) lx.providers.load_plugins_once() # Now LangExtract can use openrouter models automatically result = lx.extract( text="John Smith works at Microsoft in Seattle.", model_id="openrouter/google/gemini-2.5-flash", prompt_description="Extract person name, company, and location", examples=[ { "text": "Alice works at Google in Mountain View.", "extraction": { "person": "Alice", "company": "Google", "location": "Mountain View" } } ] ) print(result) ``` -------------------------------- ### Import Paths for LangExtract and OpenAI Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/04-types.md Provides essential import statements for using the openrouterLanguageModel, LangExtract base types, and the OpenAI client. ```python # Main provider class from langextract_openrouter import openrouterLanguageModel from langextract_openrouter.provider import openrouterLanguageModel # LangExtract base types (for reference) from langextract.inference import BaseLanguageModel, ScoredOutput from langextract.providers import registry from langextract import factory # OpenAI client from openai import OpenAI ``` -------------------------------- ### Configure Multiple Inference Parameters Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/06-usage-patterns.md Combine various inference parameters such as temperature, max_tokens, top_p, and frequency_penalty to fine-tune model behavior for specific generation tasks. ```python from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel( model_id="openrouter/anthropic/claude-3-opus", api_key="sk-or-v1-…", temperature=0.7, max_tokens=500, top_p=0.9, frequency_penalty=0.5 ) results = list(provider.infer(["Generate creative text: A robot discovers..."])) ``` -------------------------------- ### Supported Model ID Formats Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/INDEX.md Demonstrates the two equivalent formats for specifying model IDs with the OpenRouter provider: with and without the 'openrouter/' prefix. ```python # With prefix model_id = "openrouter/google/gemini-2.5-flash" # Without prefix model_id = "google/gemini-2.5-flash" ``` -------------------------------- ### Handle Empty or None Prompts Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/05-errors.md Ensure all prompts are strings before passing them to the infer method. This prevents errors from None or objects without a string representation. ```python from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel(model_id="openrouter/google/gemini-2.5-flash") # Ensure all prompts are strings or have proper __str__ methods prompts = [str(p) for p in ["prompt1", 2, "prompt3"]] # Convert all to strings results = list(provider.infer(prompts)) ``` -------------------------------- ### Automatic Provider Discovery with LangExtract Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/07-provider-interface.md Shows how LangExtract can automatically discover and use the correct provider for model inference. Ensure plugins are loaded once before calling extraction methods. ```python import langextract as lx lx.providers.load_plugins_once() # lx.extract automatically finds the right provider result = lx.extract( text="John works at Google.", model_id="openrouter/google/gemini-2.5-flash", ... ) ``` -------------------------------- ### Set API Key via Environment Variable Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/10-quick-start.md Ensure your OPENROUTER_API_KEY environment variable is set. If it's missing, export it with your key. ```bash # Check environment variable echo $OPENROUTER_API_KEY # Set it if missing export OPENROUTER_API_KEY=sk-or-v1-your-key ``` -------------------------------- ### Minimal Openrouter Configuration Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/03-configuration.md Set the OPENROUTER_API_KEY environment variable for minimal configuration. This is useful when you don't want to pass the API key directly in the code. ```python import os from langextract_openrouter import openrouterLanguageModel os.environ['OPENROUTER_API_KEY'] = 'sk-or-v1-...' provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash" ) ``` -------------------------------- ### Project Metadata Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/09-source-code-reference.md Defines core project metadata such as name, version, description, Python version compatibility, license, and runtime dependencies. ```toml name = "langextract-openrouter" version = "0.1.1" description = "LangExtract provider plugin for openrouter" readme = "README.md" requires-python = ">=3.10" license = { text = "Apache-2.0" } dependencies = [ "langextract>=1.0.0", "openai>=1.101.0", ] ``` -------------------------------- ### Set Logging Levels Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/03-configuration.md Configure the root logger to INFO and the provider logger to DEBUG. This allows for detailed debugging information to be captured. ```python import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger('langextract_openrouter.provider') logger.setLevel(logging.DEBUG) # Enable DEBUG logs if available ``` -------------------------------- ### Manage API Key from Environment Variables Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/06-usage-patterns.md Load your OpenRouter API key from environment variables or a `.env` file using `python-dotenv` for secure key management. Ensure the `OPENROUTER_API_KEY` environment variable is set before running. ```python import os from dotenv import load_dotenv from langextract_openrouter import openrouterLanguageModel # Load from environment or .env file load_dotenv() api_key = os.environ.get('OPENROUTER_API_KEY') if not api_key: raise ValueError("OPENROUTER_API_KEY not set") provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash", api_key=api_key ) ``` -------------------------------- ### Imports for Test Plugin Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/09-source-code-reference.md Imports necessary modules for testing the OpenRouter provider with LangExtract. ```python import re import sys import langextract as lx from langextract.providers import registry from langextract_openrouter import openrouterLanguageModel ``` -------------------------------- ### Set API Key via Environment Variable Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/10-quick-start.md Set the OPENROUTER_API_KEY environment variable for authentication. ```bash export OPENROUTER_API_KEY=sk-or-v1-your-key-here ``` ```python import os os.environ['OPENROUTER_API_KEY'] = 'sk-or-v1-...' ``` -------------------------------- ### Consuming the Generator from infer() Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/04-types.md Demonstrates how to iterate over the generator returned by the infer() method, processing results as they are yielded, or collecting all results into a list. ```python from typing import Generator from langextract.inference import ScoredOutput from typing import List def infer(self, batch_prompts, **kwargs) -> Generator[List[ScoredOutput], None, None]: """Yields lists of ScoredOutput objects.""" ... provider = openrouterLanguageModel(model_id="openrouter/google/gemini-2.5-flash") # Consuming the generator for result_list in provider.infer(["prompt1", "prompt2"]): # result_list is List[ScoredOutput] print(result_list) # Or collect all results all_results = list(provider.infer(["prompt1", "prompt2"])) # all_results is List[List[ScoredOutput]] ``` -------------------------------- ### Direct Import of openrouterLanguageModel from provider.py Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/01-overview.md This Python code shows the direct import path for the 'openrouterLanguageModel' class from its definition file, 'provider.py'. This is equivalent to the package-level re-export. ```python from langextract_openrouter.provider import openrouterLanguageModel ``` -------------------------------- ### Re-exporting openrouterLanguageModel in __init__.py Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/01-overview.md This Python code demonstrates how the 'openrouterLanguageModel' class is re-exported at the package level in '__init__.py' for easier access. ```python from langextract_openrouter import openrouterLanguageModel ``` -------------------------------- ### Logging Integration Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/06-usage-patterns.md Integrate logging to monitor provider initialization, API calls, and errors. This pattern is crucial for debugging and understanding the library's behavior during operation. ```python import logging from langextract_openrouter import openrouterLanguageModel # Configure logging to see provider errors and info logging.basicConfig( level=logging.INFO, format='%(name)s - %(levelname)s - %(message)s' ) provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash", api_key="sk-or-v1-..." ) # Now logs will show initialization, API calls, and errors results = list(provider.infer(["Test"])) ``` -------------------------------- ### Set OPENROUTER_API_KEY Environment Variable Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/03-configuration.md Set the OPENROUTER_API_KEY environment variable to authenticate with OpenRouter. This is required if the API key is not passed directly to the constructor. ```bash export OPENROUTER_API_KEY=sk-or-v1-your-key-here ``` -------------------------------- ### Set OpenRouter API Key Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/INDEX.md Set your OpenRouter API key as an environment variable. This is required for authentication. ```bash export OPENROUTER_API_KEY=sk-or-v1-your-key ``` -------------------------------- ### OpenRouter Provider Imports Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/09-source-code-reference.md Imports necessary modules for the OpenRouter provider, including logging, environment variable access, langextract, and the OpenAI client. ```python import logging import os import langextract as lx from openai import OpenAI ``` -------------------------------- ### Perform Batch Inference Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/10-quick-start.md Use this to process multiple prompts efficiently in a single call. Ensure the `openrouterLanguageModel` is initialized with the desired model. ```python from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash" ) prompts = [ "Summarize: AI is rapidly changing the world.", "Extract entities: Alice works at Microsoft.", "Translate to Spanish: Hello world." ] results = list(provider.infer(prompts)) for i, result in enumerate(results): print(f"Prompt {i}: {result[0].output}") ``` -------------------------------- ### Test Provider Registration and Inference Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/07-provider-interface.md This Python script tests the registration of an OpenRouter provider, its instantiation with a specific model ID, and the functionality of its infer() method. It ensures the provider is discoverable and correctly handles inference requests. ```python import langextract as lx from langextract.providers import registry lx.providers.load_plugins_once() # Test registration provider_class = registry.resolve("openrouter/google/gemini-2.5-flash") assert provider_class.__name__ == "openrouterLanguageModel" # Test instantiation provider = provider_class(model_id="openrouter/google/gemini-2.5-flash") # Test inference results = list(provider.infer(["test"])) assert len(results) == 1 ``` -------------------------------- ### Set API Key in Constructor Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/10-quick-start.md Alternatively, pass your API key directly to the openrouterLanguageModel constructor. ```python provider = openrouterLanguageModel( model_id="...", api_key="sk-or-v1-your-key" ) ``` -------------------------------- ### Explicit API Key Configuration Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/03-configuration.md Provide the API key directly to the openrouterLanguageModel constructor. Use this when environment variables are not suitable or for specific overrides. ```python from langextract_openrouter import openrouterLanguageModel provider = openrouterLanguageModel( model_id="openrouter/openai/gpt-4.1", api_key="sk-or-v1-your-key" ) ``` -------------------------------- ### Control Temperature for Deterministic Output Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/06-usage-patterns.md Set a lower temperature value to achieve more deterministic and predictable model outputs. This is useful when consistency is prioritized over creativity. ```python from langextract_openrouter import openrouterLanguageModel # Lower temperature for deterministic output provider = openrouterLanguageModel( model_id="openrouter/google/gemini-2.5-flash", api_key="sk-or-v1-…", temperature=0.2 # More deterministic ) results = list(provider.infer(["Extract dates from: The event is on June 24, 2026."])) ``` -------------------------------- ### OpenRouter Provider Infer Method Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/09-source-code-reference.md Runs inference on a batch of prompts using the OpenAI client configured for OpenRouter. Handles API calls and yields results or empty output on error. ```python def infer(self, batch_prompts, **kwargs): """Run inference on a batch of prompts. Args: batch_prompts: List of prompts to process. **kwargs: Additional inference parameters. Yields: Lists of ScoredOutput objects, one per prompt. """ for prompt in batch_prompts: try: logging.info(f'Calling OpenRouter completion for model {self.model_id}') result = self.client.chat.completions.create( model=self.model_id, messages=[{"role": "user", "content": str(prompt)}], **self._extra_kwargs ) yield [lx.inference.ScoredOutput(score=1.0, output=result.choices[0].message.content)] except Exception as e: logging.error(f'Error calling OpenRouter completion for model {self.model_id}: {e}') yield [lx.inference.ScoredOutput(score=0.0, output='')] # Return empty output on error ``` -------------------------------- ### Implementing Streaming for OpenRouter Source: https://github.com/fredguth/langextract-openrouter/blob/main/_autodocs/08-openai-integration.md This function shows how to modify the inference process to enable streaming responses from the OpenAI client when using OpenRouter. It iterates through prompts, streams the response, and yields the full output. ```python def infer(self, batch_prompts, **kwargs): for prompt in batch_prompts: try: outputs = [] with self.client.chat.completions.create( model=self.model_id, messages=[{"role": "user", "content": str(prompt)}], stream=True, **self._extra_kwargs ) as stream: for text in stream.text_stream: outputs.append(text) full_output = "".join(outputs) yield [lx.inference.ScoredOutput(score=1.0, output=full_output)] except Exception as e: logging.error(...) yield [lx.inference.ScoredOutput(score=0.0, output='')] ```