Files
everyone-can-use-english/enjoy/src/renderer/hooks/use-ai-command.tsx
an-lee 61c76006fd Feat: more settings for ai engine & models (#611)
* may setup gpt ai engine & model

* ai models setting works

* update openai setting

* validate engine setting before save

* fail fast

* clean code

* refactor gpt preset
2024-05-15 15:52:07 +08:00

136 lines
3.2 KiB
TypeScript

import {
AppSettingsProviderContext,
AISettingsProviderContext,
} from "@renderer/context";
import { useContext } from "react";
import {
lookupCommand,
extractStoryCommand,
translateCommand,
analyzeCommand,
punctuateCommand,
summarizeTopicCommand,
} from "@commands";
export const useAiCommand = () => {
const { EnjoyApp, webApi } = useContext(AppSettingsProviderContext);
const { currentEngine } = useContext(AISettingsProviderContext);
const lookupWord = async (params: {
word: string;
context: string;
sourceId?: string;
sourceType?: string;
}) => {
const { context, sourceId, sourceType } = params;
let { word } = params;
word = word.trim();
if (!word) return;
const lookup = await webApi.lookup({
word,
context,
sourceId,
sourceType,
});
if (lookup.meaning) {
return lookup;
}
const modelName =
currentEngine.models.lookup || currentEngine.models.default;
const res = await lookupCommand(
{
word,
context,
meaningOptions: lookup.meaningOptions,
},
{
key: currentEngine.key,
modelName,
baseUrl: currentEngine.baseUrl,
}
);
// Accept result from gpt-3/4 models
if (modelName.match(/^gpt-(3|4)\S*/i) && res.context_translation?.trim()) {
return webApi.updateLookup(lookup.id, {
meaning: res,
sourceId,
sourceType,
});
}
};
const extractStory = async (story: StoryType) => {
const res = await extractStoryCommand(story.content, {
key: currentEngine.key,
modelName:
currentEngine.models.extractStory || currentEngine.models.default,
baseUrl: currentEngine.baseUrl,
});
const { words = [], idioms = [] } = res;
return webApi.extractVocabularyFromStory(story.id, {
words,
idioms,
});
};
const translate = async (
text: string,
cacheKey?: string
): Promise<string> => {
return translateCommand(text, {
key: currentEngine.key,
modelName: currentEngine.models.translate || currentEngine.models.default,
baseUrl: currentEngine.baseUrl,
}).then((res) => {
if (cacheKey) {
EnjoyApp.cacheObjects.set(cacheKey, res);
}
return res;
});
};
const analyzeText = async (text: string, cacheKey?: string) => {
const res = await analyzeCommand(text, {
key: currentEngine.key,
modelName: currentEngine.models.analyze || currentEngine.models.default,
baseUrl: currentEngine.baseUrl,
});
if (cacheKey) {
EnjoyApp.cacheObjects.set(cacheKey, res);
}
return res;
};
const punctuateText = async (text: string) => {
return punctuateCommand(text, {
key: currentEngine.key,
modelName: currentEngine.models.default,
baseUrl: currentEngine.baseUrl,
});
};
const summarizeTopic = async (text: string) => {
return summarizeTopicCommand(text, {
key: currentEngine.key,
modelName: currentEngine.models.default,
baseUrl: currentEngine.baseUrl,
});
};
return {
lookupWord,
extractStory,
translate,
analyzeText,
punctuateText,
summarizeTopic,
};
};