Skip to content

Commit

Permalink
支持通过温度和指令控制使用的搜索模式
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinlic committed Apr 12, 2024
1 parent 331fc66 commit bd1cea5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 26 deletions.
74 changes: 55 additions & 19 deletions src/api/controllers/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import logger from "@/lib/logger.ts";
import util from "@/lib/util.ts";

// 模型名称
const MODEL_NAME = "concise";
const MODEL_NAME = "detail";
// 最大重试次数
const MAX_RETRY_COUNT = 0;
// 重试延迟
Expand Down Expand Up @@ -89,7 +89,7 @@ function generateCookie(token: string) {
*
* @param token 认证Token
*/
async function createConversation(model: string, name: string, token: string) {
async function createConversation(name: string, token: string) {
const metaToken = await acquireMetaToken(token);
const result = await axios.post(
"https://metaso.cn/api/session",
Expand Down Expand Up @@ -130,22 +130,24 @@ async function createCompletion(
model = MODEL_NAME,
messages: any[],
token: string,
useSearch = true,
tempature = 0.6,
retryCount = 0
) {
return (async () => {
logger.info(messages);

if (!["concise", "detail", "research"].includes(model)) model = MODEL_NAME;

// 创建会话
const convId = await createConversation(model, "新会话", token);
const convId = await createConversation("新会话", token);

// 请求流
const metaToken = await acquireMetaToken(token);
const content = messagesPrepare(messages);
const {
model: _model,
content
} = messagesPrepare(model, messages, tempature);

const result = await axios.get(
`https://metaso.cn/api/searchV2?sessionId=${convId}&question=${content}&lang=zh&mode=${model}&is-mini-webview=0&token=${metaToken}`,
`https://metaso.cn/api/searchV2?sessionId=${convId}&question=${content}&lang=zh&mode=${_model}&is-mini-webview=0&token=${metaToken}`,
{
headers: {
Cookie: generateCookie(token),
Expand Down Expand Up @@ -177,7 +179,7 @@ async function createCompletion(
model,
messages,
token,
useSearch,
tempature,
retryCount + 1
);
})();
Expand All @@ -199,22 +201,23 @@ async function createCompletionStream(
model = MODEL_NAME,
messages: any[],
token: string,
useSearch = true,
tempature = 0.6,
retryCount = 0
) {
return (async () => {
logger.info(messages);

if (!["concise", "detail", "research"].includes(model)) model = MODEL_NAME;

// 创建会话
const convId = await createConversation(model, "新会话", token);
const convId = await createConversation("新会话", token);

// 请求流
const metaToken = await acquireMetaToken(token);
const content = messagesPrepare(messages);
const {
model: _model,
content
} = messagesPrepare(model, messages, tempature);
const result = await axios.get(
`https://metaso.cn/api/searchV2?sessionId=${convId}&question=${content}&lang=zh&mode=${model}&is-mini-webview=0&token=${metaToken}`,
`https://metaso.cn/api/searchV2?sessionId=${convId}&question=${content}&lang=zh&mode=${_model}&is-mini-webview=0&token=${metaToken}`,
{
headers: {
Cookie: generateCookie(token),
Expand Down Expand Up @@ -244,7 +247,7 @@ async function createCompletionStream(
model,
messages,
token,
useSearch,
tempature,
retryCount + 1
);
})();
Expand All @@ -258,12 +261,45 @@ async function createCompletionStream(
*
* @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
*/
function messagesPrepare(messages: any[]) {
function messagesPrepare(model: string, messages: any[], tempature: number) {
let latestMessage = messages[messages.length - 1];
if(!latestMessage)
throw new APIException(EX.API_TEST);
logger.info("\n搜索内容:\n" + latestMessage.content);
return encodeURIComponent(latestMessage.content);
let content = latestMessage.content;
// 如果模型名称未遵守预设则检查指令是否存在,如果都没有再以温度为准
if (!["concise", "detail", "research"].includes(model)) {
if(content.indexOf('简洁搜索') != -1) {
model = "concise";
content = content.replace(/简洁搜索[:|:]?/g, '');
}
else if(content.indexOf('深入搜索') != -1) {
model = "detail";
content = content.replace(/深入搜索[:|:]?/g, '');
}
else if(content.indexOf('研究搜索') != -1) {
model = "research";
content = content.replace(/研究搜索[:|:]?/g, '');
}
else {
if(tempature < 0.4)
model = "concise";
else if(tempature >= 0.4 && tempature < 0.7)
model = "detail";
else if(tempature >= 0.7)
model = "research";
else
model = MODEL_NAME;
}
}
logger.info(`\n选用模式:${({
'concise': '简洁',
'detail': '深入',
'research': '研究'
})[model]}\n搜索内容:${content}`);
return {
model,
content: encodeURIComponent(content)
};
}

/**
Expand Down
15 changes: 8 additions & 7 deletions src/api/routes/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ export default {

'/completions': async (request: Request) => {
request
.validate('body.model', v => _.isUndefined(v) || _.isString(v))
.validate('body.messages', _.isArray)
.validate('body.tempature', v => _.isUndefined(v) || _.isNumber(v))
.validate('headers.authorization', _.isString)
// refresh_token切分
// token切分
const tokens = chat.tokenSplit(request.headers.authorization);
// 随机挑选一个refresh_token
// 随机挑选一个token
const token = _.sample(tokens);
const model = request.body.model;
const messages = request.body.messages;
if (request.body.stream) {
const stream = await chat.createCompletionStream(model, messages, token, request.body.use_search);
const { model, messages, stream, tempature } = request.body;;
if (stream) {
const stream = await chat.createCompletionStream(model, messages, token, tempature);
return new Response(stream, {
type: "text/event-stream"
});
}
else
return await chat.createCompletion(model, messages, token, request.body.use_search);
return await chat.createCompletion(model, messages, token, tempature);
}

}
Expand Down

0 comments on commit bd1cea5

Please sign in to comment.