feat: let max_tokens configurable (#212)

* feat: let max_tokens configurable

* fix: update ci test case
This commit is contained in:
yuyutaotao 2024-12-26 13:24:21 +08:00 committed by GitHub
parent 7e5b4c9b3b
commit 4a82e9bda9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 16 additions and 3 deletions

View File

@ -28,6 +28,9 @@ export MIDSCENE_OPENAI_INIT_CONFIG_JSON='{"baseURL":"....","defaultHeaders":{"ke
# if you want to use proxy. Midscene uses `socks-proxy-agent` under the hood.
export MIDSCENE_OPENAI_SOCKS_PROXY="socks5://127.0.0.1:1080"
# if you want to specify the max tokens for the model
export OPENAI_MAX_TOKENS=2048
```
## Using Azure OpenAI Service

View File

@ -25,6 +25,9 @@ export MIDSCENE_OPENAI_INIT_CONFIG_JSON='{"baseURL":"....","defaultHeaders":{"ke
# 可选, 如果你想使用代理。Midscene 使用 `socks-proxy-agent` 作为底层库。
export MIDSCENE_OPENAI_SOCKS_PROXY="socks5://127.0.0.1:1080"
# 可选, 如果你想指定模型 max_tokens
export OPENAI_MAX_TOKENS=2048
```
## 使用 Azure OpenAI 服务时的配置

View File

@ -24,6 +24,7 @@ import {
MIDSCENE_USE_AZURE_OPENAI,
OPENAI_API_KEY,
OPENAI_BASE_URL,
OPENAI_MAX_TOKENS,
OPENAI_USE_AZURE,
allAIConfig,
getAIConfig,
@ -149,6 +150,8 @@ export async function call(
const shouldPrintTiming =
typeof getAIConfig(MIDSCENE_DEBUG_AI_PROFILE) === 'string';
const maxTokens = getAIConfig(OPENAI_MAX_TOKENS);
const startTime = Date.now();
const model = getModelName();
let content: string | undefined;
@ -156,7 +159,10 @@ export async function call(
const commonConfig = {
temperature: 0.1,
stream: false,
max_tokens: 3000,
max_tokens:
typeof maxTokens === 'number'
? maxTokens
: Number.parseInt(maxTokens || '2048', 10),
};
if (style === 'openai') {
const result = await completion.create({

View File

@ -10,6 +10,7 @@ export const MIDSCENE_DEBUG_MODE = 'MIDSCENE_DEBUG_MODE';
export const MIDSCENE_OPENAI_SOCKS_PROXY = 'MIDSCENE_OPENAI_SOCKS_PROXY';
export const OPENAI_API_KEY = 'OPENAI_API_KEY';
export const OPENAI_BASE_URL = 'OPENAI_BASE_URL';
export const OPENAI_MAX_TOKENS = 'OPENAI_MAX_TOKENS';
export const MIDSCENE_MODEL_TEXT_ONLY = 'MIDSCENE_MODEL_TEXT_ONLY';
export const MIDSCENE_CACHE = 'MIDSCENE_CACHE';
@ -43,6 +44,7 @@ const allConfigFromEnv = () => {
[OPENAI_BASE_URL]: process.env[OPENAI_BASE_URL] || undefined,
[MIDSCENE_MODEL_TEXT_ONLY]:
process.env[MIDSCENE_MODEL_TEXT_ONLY] || undefined,
[OPENAI_MAX_TOKENS]: process.env[OPENAI_MAX_TOKENS] || undefined,
[OPENAI_USE_AZURE]: process.env[OPENAI_USE_AZURE] || undefined,
[MIDSCENE_CACHE]: process.env[MIDSCENE_CACHE] || undefined,
[MATCH_BY_POSITION]: process.env[MATCH_BY_POSITION] || undefined,

View File

@ -86,7 +86,7 @@ describe(
});
it('search engine', async () => {
const { originPage, reset } = await launchPage('https://www.baidu.com/');
const { originPage, reset } = await launchPage('https://www.bing.com/');
const mid = new PuppeteerAgent(originPage);
await mid.aiAction(
'type "AI 101" in search box, hit Enter, wait 2s, click the second result, wait 4s',
@ -100,7 +100,6 @@ describe(
it('scroll', async () => {
const htmlPath = path.join(__dirname, 'scroll.html');
const { originPage, reset } = await launchPage(`file://${htmlPath}`);
// const { originPage, reset } = await launchPage('https://news.baidu.com/');
const mid = new PuppeteerAgent(originPage);
await mid.aiAction(
'find the "Vertical 2" element, scroll down 200px, find the "Horizontal 2" element, scroll right 100px',