Skip to content

Commit 07927c5

Browse files
committed
Add support for openai-compatible models
1 parent 3a5c41b commit 07927c5

File tree

6 files changed

+16
-4
lines changed

6 files changed

+16
-4
lines changed

.changeset/dry-news-joke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rootapp/dolphin': patch
3+
---
4+
5+
Add support for openai-compatible models

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ Dolphin defines an interface for translators to implement, which is responsible
6969

7070
#### openai
7171

72-
This is a local translator which uses OpenAI API to translate strings. To use this translator, you need to provide the OpenAI API key as an environment variable (`OPENAI_API_KEY`) or pass it to the translator with the `apiKey` option.
72+
This is a local translator which uses OpenAI API to translate strings. To use this translator, you need to provide the OpenAI API key as an environment variable (`OPENAI_API_KEY`) or pass it to the translator with the `apiKey` option. Optionally, you can specify the model to use with the `model` option. By default, it uses `gpt-4o` model.
73+
74+
The translator also supports any openai-comptable models, such as gemini and deepseek. However, the model needs support json response format, for major supported models, please refer to [OpenRouter website](https://openrouter.ai/models?fmt=cards&supported_parameters=response_format&order=top-weekly). To use a custom OpenAI-compatible endpoint, you can provide the `baseUrl` option to the translator.
7375

7476
#### api
7577

@@ -145,7 +147,7 @@ The source language of the strings, which is used to translate from.
145147

146148
Supported translators:
147149

148-
- **openai**: OpenAI API. You need to provide the OpenAI API key as an environment variable (`OPENAI_API_KEY`) or pass it to the translator with the `apiKey` option. By default, it uses `gpt-4o` model, which can be customized with the `model` option.
150+
- **openai**: OpenAI API. You need to provide the OpenAI API key as an environment variable (`OPENAI_API_KEY`) or pass it to the translator with the `apiKey` option. By default, it uses `gpt-4o` model, which can be customized with the `model` option. Optionally, you can provide the `baseUrl` to use a custom OpenAI-compatible endpoint.
149151
- **api**: Dolphin API. You need to provide the `baseUrl` to the API endpoint.
150152

151153
Supported modes:

packages/base/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const DolphinTranslatorConfigSchema = CommonTranslatorConfigSchema.extend({
7272

7373
const OpenAITranslatorConfigSchema = CommonTranslatorConfigSchema.extend({
7474
agent: z.literal('openai'),
75+
baseUrl: z.string().optional(),
7576
apiKey: z.string().optional(),
7677
model: z.string().optional(),
7778
}).merge(LLMTranslatorConfigSchema);

packages/provider/src/openai.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ export class OpenAITranslationProvider implements TranslationProvider {
1010
private openai: OpenAIProvider;
1111
private model: LanguageModel;
1212

13-
constructor(options: { apiKey: string; model?: string }) {
13+
constructor(options: { apiKey: string; baseUrl?: string; model?: string }) {
1414
this.openai = createOpenAI({
15+
baseURL: options.baseUrl,
1516
apiKey: options.apiKey,
1617
compatibility: 'strict', // https://sdk.vercel.ai/providers/ai-sdk-providers/openai#provider-instance
1718
});

packages/translate/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ async function translateStrings(
211211
}
212212
translator = new OpenAITranslator({
213213
apiKey,
214+
baseUrl: config.translator.baseUrl,
214215
model: config.translator.model,
215216
maxRetry: config.translator.maxRetry,
216217
});

packages/translate/src/translator/openai/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ export class OpenAITranslator implements Translator {
2222
constructor({
2323
apiKey,
2424
model,
25+
baseUrl,
2526
maxRetry = 1,
2627
}: {
2728
apiKey: string;
29+
baseUrl?: string;
2830
model?: string;
2931
maxRetry: number;
3032
}) {
@@ -34,7 +36,7 @@ export class OpenAITranslator implements Translator {
3436
totalTokens: 0,
3537
};
3638
this.maxRetry = maxRetry;
37-
this.provider = new OpenAITranslationProvider({ apiKey, model });
39+
this.provider = new OpenAITranslationProvider({ baseUrl, apiKey, model });
3840
}
3941

4042
async config(): Promise<LLMTranslatorConfig> {

0 commit comments

Comments
 (0)