* ensure AI command model support json response_format * update models list * update tts model list
73 lines
2.1 KiB
TypeScript
73 lines
2.1 KiB
TypeScript
import { ChatOpenAI } from "@langchain/openai";
|
|
import { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
import { z } from "zod";
|
|
import { RESPONSE_JSON_FORMAT_MODELS } from "@/constants";
|
|
|
|
export const extractStoryCommand = async (
|
|
content: string,
|
|
options: {
|
|
key: string;
|
|
modelName?: string;
|
|
temperature?: number;
|
|
baseUrl?: string;
|
|
}
|
|
): Promise<{ words: string[]; idioms: string[] }> => {
|
|
const { key, temperature = 0, baseUrl } = options;
|
|
let { modelName = "gpt-4-turbo-preview" } = options;
|
|
|
|
if (RESPONSE_JSON_FORMAT_MODELS.indexOf(modelName) === -1) {
|
|
modelName = "gpt-4-turbo-preview";
|
|
}
|
|
|
|
const saveExtraction = z.object({
|
|
words: z.array(z.string().describe("extracted word")),
|
|
idioms: z.array(z.string().describe("extracted idiom")),
|
|
});
|
|
|
|
const chatModel = new ChatOpenAI({
|
|
openAIApiKey: key,
|
|
modelName,
|
|
temperature,
|
|
modelKwargs: {
|
|
response_format: {
|
|
type: "json_object",
|
|
},
|
|
},
|
|
configuration: {
|
|
baseURL: baseUrl,
|
|
},
|
|
cache: true,
|
|
verbose: true,
|
|
}).bind({
|
|
tools: [
|
|
{
|
|
type: "function",
|
|
function: {
|
|
name: "save_extraction",
|
|
description: "Save the extracted words and idioms from a text",
|
|
parameters: zodToJsonSchema(saveExtraction),
|
|
},
|
|
},
|
|
],
|
|
});
|
|
|
|
const prompt = ChatPromptTemplate.fromMessages([
|
|
["system", EXTRACT_STORY_PROMPT],
|
|
["human", "{text}"],
|
|
]);
|
|
|
|
const response = await prompt.pipe(chatModel).invoke({
|
|
learning_language: "English",
|
|
text: content,
|
|
});
|
|
|
|
return JSON.parse(
|
|
response.additional_kwargs?.tool_calls?.[0]?.function?.arguments || "{}"
|
|
);
|
|
};
|
|
|
|
const EXTRACT_STORY_PROMPT = `
|
|
I am an {learning_language} beginner and only have a grasp of 500 high-frequency basic words. You are an {learning_language} learning assistant robot, and your task is to analyze the article I provide and extract all the meaningful words and idioms that I may not be familiar with. Specifically, it should include common words used in uncommon ways. Return in JSON format.
|
|
`;
|