Skip to content

Commit e920bd3

Browse files
lilacquanru
andauthored
refactor(core): replace @langchain/core with native template literals (#1459)
* refactor(core): replace @langchain/core with native template literals * chore(core): fix lint formatting in inspect.ts --------- Co-authored-by: quanruzhuoxiu <quanruzhuoxiu@gmail.com>
1 parent 5b061ab commit e920bd3

File tree

7 files changed

+30
-116
lines changed

7 files changed

+30
-116
lines changed

packages/core/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
"dependencies": {
7575
"@anthropic-ai/sdk": "0.33.1",
7676
"@azure/identity": "4.5.0",
77-
"@langchain/core": "0.3.26",
7877
"@midscene/recorder": "workspace:*",
7978
"@midscene/shared": "workspace:*",
8079
"@ui-tars/action-parser": "1.2.3",

packages/core/src/ai-model/inspect.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export async function AiLocateElement<
145145
targetElementDescription,
146146
'cannot find the target element description',
147147
);
148-
const userInstructionPrompt = await findElementPrompt.format({
148+
const userInstructionPrompt = findElementPrompt({
149149
pageDescription: description,
150150
targetElementDescription: extraTextFromUserPrompt(targetElementDescription),
151151
});
@@ -307,7 +307,7 @@ export async function AiLocateSection(options: {
307307
const { screenshotBase64 } = context;
308308

309309
const systemPrompt = systemPromptToLocateSection(vlMode);
310-
const sectionLocatorInstructionText = await sectionLocatorInstruction.format({
310+
const sectionLocatorInstructionText = sectionLocatorInstruction({
311311
sectionDescription: extraTextFromUserPrompt(sectionDescription),
312312
});
313313
const msgs: AIArgs = [
@@ -433,10 +433,7 @@ export async function AiExtractElementInfo<
433433
vlMode,
434434
});
435435

436-
const extractDataPromptText = await extractDataQueryPrompt(
437-
description,
438-
dataQuery,
439-
);
436+
const extractDataPromptText = extractDataQueryPrompt(description, dataQuery);
440437

441438
const userContent: ChatCompletionUserMessageParam['content'] = [];
442439

packages/core/src/ai-model/prompt/extraction.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { PromptTemplate } from '@langchain/core/prompts';
21
import type { ResponseFormatJSONSchema } from 'openai/resources/index';
32

43
export function systemPromptToExtract() {
@@ -87,7 +86,7 @@ By viewing the screenshot and page contents, you can extract the following data:
8786
`;
8887
}
8988

90-
export const extractDataQueryPrompt = async (
89+
export const extractDataQueryPrompt = (
9190
pageDescription: string,
9291
dataQuery: string | Record<string, string>,
9392
) => {
@@ -97,23 +96,16 @@ export const extractDataQueryPrompt = async (
9796
} else {
9897
dataQueryText = JSON.stringify(dataQuery, null, 2);
9998
}
100-
const extractDataPrompt = new PromptTemplate({
101-
template: `
99+
100+
return `
102101
<PageDescription>
103-
{pageDescription}
102+
${pageDescription}
104103
</PageDescription>
105104
106105
<DATA_DEMAND>
107-
{dataQuery}
106+
${dataQueryText}
108107
</DATA_DEMAND>
109-
`,
110-
inputVariables: ['pageDescription', 'dataQuery'],
111-
});
112-
113-
return await extractDataPrompt.format({
114-
pageDescription,
115-
dataQuery: dataQueryText,
116-
});
108+
`;
117109
};
118110

119111
export const extractDataSchema: ResponseFormatJSONSchema = {

packages/core/src/ai-model/prompt/llm-locator.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { PromptTemplate } from '@langchain/core/prompts';
21
import type { TVlModeTypes } from '@midscene/shared/env';
32
import type { ResponseFormatJSONSchema } from 'openai/resources/index';
43
import { bboxDescription } from './common';
@@ -254,14 +253,17 @@ export const locatorSchema: ResponseFormatJSONSchema = {
254253
},
255254
};
256255

257-
export const findElementPrompt = new PromptTemplate({
258-
template: `
256+
export const findElementPrompt = ({
257+
pageDescription,
258+
targetElementDescription,
259+
}: {
260+
pageDescription: string;
261+
targetElementDescription: string;
262+
}) => `
259263
Here is the item user want to find:
260264
=====================================
261-
{targetElementDescription}
265+
${targetElementDescription}
262266
=====================================
263267
264-
{pageDescription}
265-
`,
266-
inputVariables: ['pageDescription', 'targetElementDescription'],
267-
});
268+
${pageDescription}
269+
`;

packages/core/src/ai-model/prompt/llm-section-locator.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { PromptTemplate } from '@langchain/core/prompts';
21
import type { TVlModeTypes } from '@midscene/shared/env';
32
import { bboxDescription } from './common';
43

@@ -35,11 +34,12 @@ the return value should be like this:
3534
`;
3635
}
3736

38-
export const sectionLocatorInstruction = new PromptTemplate({
39-
template: `Here is the target element user interested in:
37+
export const sectionLocatorInstruction = ({
38+
sectionDescription,
39+
}: {
40+
sectionDescription: string;
41+
}) => `Here is the target element user interested in:
4042
<targetDescription>
41-
{sectionDescription}
43+
${sectionDescription}
4244
</targetDescription>
43-
`,
44-
inputVariables: ['sectionDescription'],
45-
});
45+
`;

packages/core/tests/unit-test/prompt/prompt.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,16 @@ describe('extract element', () => {
189189
expect(prompt).toMatchSnapshot();
190190
});
191191

192-
it('extract element by extractDataPrompt', async () => {
193-
const prompt = await extractDataQueryPrompt(
192+
it('extract element by extractDataPrompt', () => {
193+
const prompt = extractDataQueryPrompt(
194194
'todo title, string',
195195
'todo title, string',
196196
);
197197
expect(prompt).toMatchSnapshot();
198198
});
199199

200-
it('extract element by extractDataPrompt - object', async () => {
201-
const prompt = await extractDataQueryPrompt('todo title, string', {
200+
it('extract element by extractDataPrompt - object', () => {
201+
const prompt = extractDataQueryPrompt('todo title, string', {
202202
foo: 'an array indicates the foo',
203203
});
204204
expect(prompt).toMatchSnapshot();

pnpm-lock.yaml

Lines changed: 0 additions & 76 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)