Advanced
Extending Polyglot
Learn how to extend Polyglot with custom providers and middleware.
Understanding Polyglot’s architecture makes it easier to extend the library to support new providers or add new functionality.
Adding a New LLM Provider
To add support for a new LLM provider, you need to implement several components:
- Message Format Adapter: Implements
CanMapMessages
to convert Polyglot’s message format to the provider’s format - Body Format Adapter: Implements
CanMapRequestBody
to structure the request body according to the provider’s API - Request Adapter: Implements
ProviderRequestAdapter
to build HTTP requests for the provider - Response Adapter: Implements
ProviderResponseAdapter
to parse responses from the provider - Usage Format Adapter: Implements
CanMapUsage
to extract token usage information
Then, you need to modify the InferenceDriverFactory
to create the appropriate driver for your provider:
Finally, add your provider to the make
method’s match statement.
Adding a New Embeddings Provider
Similarly, to add a new embeddings provider, implement the CanVectorize
interface:
Then, modify the Embeddings
class to create your driver:
Adding Custom Middleware
You can extend Polyglot’s HTTP layer by creating custom middleware:
Then, add your middleware to the HTTP client: