Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor ai commands * fix json command * fix extract story command * may summarize topic for audio
- Loading branch information
Showing
18 changed files
with
245 additions
and
232 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,37 @@ | ||
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"; | ||
import { jsonCommand } from "./json.command"; | ||
|
||
export const extractStoryCommand = async ( | ||
content: string, | ||
text: 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" } = options; | ||
|
||
if (RESPONSE_JSON_FORMAT_MODELS.indexOf(modelName) === -1) { | ||
modelName = "gpt-4-turbo"; | ||
} | ||
|
||
const saveExtraction = z.object({ | ||
const schema = 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, | ||
maxRetries: 2, | ||
}).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([ | ||
const prompt = await ChatPromptTemplate.fromMessages([ | ||
["system", EXTRACT_STORY_PROMPT], | ||
["human", "{text}"], | ||
]); | ||
|
||
const response = await prompt.pipe(chatModel).invoke({ | ||
]).format({ | ||
learning_language: "English", | ||
text: content, | ||
text, | ||
}); | ||
|
||
return JSON.parse( | ||
response.additional_kwargs?.tool_calls?.[0]?.function?.arguments || "{}" | ||
); | ||
return jsonCommand(prompt, { ...options, schema }); | ||
}; | ||
|
||
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. | ||
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 like following: | ||
{{ | ||
words: ["word1", "word2", ...], | ||
idiom: ["idiom1", "idiom2", ...] | ||
}} | ||
`; |
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,48 @@ | ||
import { ChatOpenAI } from "@langchain/openai"; | ||
import { RESPONSE_JSON_FORMAT_MODELS } from "@/constants"; | ||
import { zodToJsonSchema } from "zod-to-json-schema"; | ||
|
||
export const jsonCommand = async ( | ||
prompt: string, | ||
options: { | ||
key: string; | ||
modelName?: string; | ||
temperature?: number; | ||
baseUrl?: string; | ||
schema: any; | ||
} | ||
): Promise<any> => { | ||
const { key, temperature = 0, baseUrl, schema } = options; | ||
let { modelName = "gpt-4-turbo" } = options; | ||
|
||
if (RESPONSE_JSON_FORMAT_MODELS.indexOf(modelName) === -1) { | ||
modelName = "gpt-4-turbo"; | ||
} | ||
|
||
const chatModel = new ChatOpenAI({ | ||
openAIApiKey: key, | ||
modelName, | ||
temperature, | ||
modelKwargs: { | ||
response_format: { | ||
type: "json_object", | ||
}, | ||
}, | ||
configuration: { | ||
baseURL: baseUrl, | ||
}, | ||
cache: true, | ||
verbose: true, | ||
maxRetries: 1, | ||
}); | ||
|
||
const structuredOutput = chatModel.withStructuredOutput( | ||
zodToJsonSchema(schema), | ||
{ | ||
method: "jsonMode", | ||
} | ||
); | ||
|
||
const response = await structuredOutput.invoke(prompt); | ||
return response; | ||
}; |
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.