Skip to content

Commit

Permalink
refact(toolbox): 剧情翻译全部调用 Claude
Browse files Browse the repository at this point in the history
  • Loading branch information
mark9804 committed Jun 11, 2024
1 parent 101eafc commit 318b9e0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@
<n-button @click="handleFormalizePunctuation" type="info">
规范符号
</n-button>
<n-button @click="translateHandle(true)" type="info"
<!-- <n-button @click="translateHandle(true)" type="info"
>重新翻译</n-button
>
> -->
<n-button
@click="handleLLMTranslateRequest"
@click="handleLLMTranslateRequest(2)"
type="info"
quaternary
:loading="llmLoading"
Expand Down Expand Up @@ -245,20 +245,21 @@ const translateHandle = (force = false) => {
const text = currentText.value
?.replaceAll("#n", "[#n]")
?.replaceAll(/\[.*?\]/g, "");
translate(
text,
translateHash[config.getLanguage],
translateHash[config.getTargetLang]
)
.then(res => {
config.setTmpMachineTranslate(
currentText.value,
halfToFull((res.translation || [])[0] ?? "")
);
})
.catch(err => {
console.log(err);
});
handleLLMTranslateRequest(0)
// translate(
// text,
// translateHash[config.getLanguage],
// translateHash[config.getTargetLang]
// )
// .then(res => {
// config.setTmpMachineTranslate(
// currentText.value,
// halfToFull((res.translation || [])[0] ?? "")
// );
// })
// .catch(err => {
// console.log(err);
// });
}
};
Expand Down Expand Up @@ -322,7 +323,9 @@ let llmLastCalled = 0;
const llmLoading = ref(false);
const studentNames = computed(() => config.getStudentList);
function handleLLMTranslateRequest() {
function handleLLMTranslateRequest(
model: 0 | 1 | 2 | "haiku" | "sonnet" | "opus" = 0
) {
if (config.getSelectLine !== -1) {
if (Date.now() - llmLastCalled < 5000) {
ElMessage({
Expand All @@ -337,7 +340,7 @@ function handleLLMTranslateRequest() {
const text =
mainStore.getScenario.content[config.getSelectLine][config.getLanguage];
getClaudeTranslation(text)
getClaudeTranslation(text, model)
.then((res: ClaudeMessage) => {
const rawText = res.content[0].text ?? "";
const fullWidthText = halfToFull(rawText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ const instance = axios.create({
});

const rag_request = {
// model: "claude-3-haiku-20240307",
model: "claude-3-opus-20240229",
model: "",
max_tokens: 1000,
temperature: 0,
system:
Expand Down Expand Up @@ -107,7 +106,25 @@ const rag_request = {
],
};

function getClaudeTranslation(input: string) {
const models = [
{
modelName: "claude-3-haiku-20240307",
alias: [0, "haiku"],
},
{
modelName: "claude-3-haiku-20240307",
alias: [1, "sonnet"],
},
{
modelName: "claude-3-opus-20240229",
alias: [2, "opus"],
},
];

function getClaudeTranslation(
input: string,
model: 0 | 1 | 2 | "haiku" | "sonnet" | "opus" = 0
) {
// const test = {
// content: [
// {
Expand All @@ -119,6 +136,8 @@ function getClaudeTranslation(input: string) {

// return new Promise(resolve => resolve(test));

rag_request.model = (models.find(el => el.alias.includes(model)) || models[0]).modelName

const error_message = { content: [{ type: "text", text: "" }] };

if (input && input.length < 10) {
Expand All @@ -128,7 +147,7 @@ function getClaudeTranslation(input: string) {
}

if (!ANTHROPIC_TIER1_SECRET || ANTHROPIC_TIER1_SECRET.length === 0) {
error_message.content[0].text = "找不到Anthropic API Key";
error_message.content[0].text = "找不到 Anthropic API Key";
return new Promise(resolve => resolve(error_message));
}

Expand Down

0 comments on commit 318b9e0

Please sign in to comment.