🤖 Polyglot is a powerful and flexible TypeScript SDK for interacting with multiple Language Model APIs.
MIT License
Polyglot is a powerful and flexible TypeScript SDK for interacting with multiple Language Model APIs. It provides a unified interface for various LLMs, along with advanced features like streaming responses, caching, middleware support, and error handling.
npm install polyglot-sdk
import { Polyglot, Models, ModelConfig } from "polyglot-sdk";
const polyglot = new Polyglot();
const claudeConfig: ModelConfig = {
apiKey: "your-claude-api-key",
model: "claude-3-5-sonnet-20240620",
};
polyglot.addModel("claude", new Models.ClaudeModel(claudeConfig));
async function getResponse() {
const messages = [{ role: "user", content: "Hello, how are you?" }];
const response = await polyglot.generateResponse("claude", messages);
console.log(response.content);
}
getResponse();
Platform | Supported Models |
---|---|
Claude (Anthropic) | claude-3-5-sonnet-20240620, claude-3-opus-20240229, claude-3-sonnet-20240229, claude-3-haiku-20240307 |
ChatGPT (OpenAI) | gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-3.5-turbo |
Mistral AI | open-mistral-nemo, mistral-small-latest, mistral-medium-latest, mistral-large-latest, codestral-latest |
Google Gemini | gemini-1.5-pro, gemini-1.5-flash, gemini-1.0-pro |
const stream = polyglot.generateStreamingResponse("claude", messages);
stream.on("data", (chunk) => console.log("Received chunk:", chunk.toString()));
stream.on("end", () => console.log("Stream ended"));
polyglot.use(async (messages, next) => {
console.log("Sending messages:", messages);
const response = await next(messages);
console.log("Received response:", response);
return response;
});
Caching is enabled by default. To disable:
const response = await polyglot.generateResponse("claude", messages, {
useCache: false,
});
You can easily switch between different models of the same platform:
const chatgptModel = polyglot.getModel("chatgpt");
chatgptModel.setModel("gpt-4-turbo");
For detailed API reference, please refer to the TypeScript definitions in the types.ts
file.
This project is licensed under the MIT License.