-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
linxiaodong
committed
Sep 23, 2024
1 parent
2bd033a
commit db128ad
Showing
8 changed files
with
340 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import OpenAI from "openai"; | ||
import { renderTemplate } from '../helpers/utils'; | ||
|
||
type OpenAIProvider = { | ||
apiUrl: string; | ||
apiKey: string; | ||
modelName?: string; | ||
prompt?: string; | ||
}; | ||
|
||
export async function translateWithOpenAI( | ||
text: string, | ||
provider: OpenAIProvider, | ||
sourceLanguage: string, | ||
targetLanguage: string | ||
) { | ||
const openai = new OpenAI({ | ||
baseURL: provider.apiUrl, | ||
apiKey: provider.apiKey, | ||
}); | ||
|
||
try { | ||
const systemPrompt = provider.prompt | ||
? renderTemplate(provider.prompt, { sourceLanguage, targetLanguage, content: text }) | ||
: `You are a helpful assistant that translates text from ${sourceLanguage} to ${targetLanguage}.`; | ||
|
||
const userPrompt = `Translate the following text from ${sourceLanguage} to ${targetLanguage}: "${text}"`; | ||
|
||
const completion = await openai.chat.completions.create({ | ||
model: provider.modelName || "gpt-3.5-turbo", | ||
messages: [ | ||
{ role: "system", content: systemPrompt }, | ||
{ role: "user", content: userPrompt } | ||
], | ||
temperature: 0.3, | ||
}); | ||
|
||
return completion?.choices?.[0]?.message?.content?.trim(); | ||
} catch (error) { | ||
console.error('OpenAI translation error:', error); | ||
throw new Error(`OpenAI translation failed: ${error.message}`); | ||
} | ||
} | ||
|
||
export default translateWithOpenAI; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.