Files
FastGPT/packages/service/core/ai/functions/queryExtension.ts
Archer 34602b25df 4.6.8-alpha (#804)
* perf: redirect request and err log replace

perf: dataset openapi

feat: session

fix: retry input error

feat: 468 doc

sub page

feat: standard sub

perf: rerank tip

perf: rerank tip

perf: api sdk

perf: openapi

sub plan

perf: sub ui

fix: ts

* perf: init log

* fix: variable select

* sub page

* icon

* perf: llm model config

* perf: menu ux

* perf: system store

* perf: publish app name

* fix: init data

* perf: flow edit ux

* fix: value type format and ux

* fix prompt editor default value (#13)

* fix prompt editor default value

* fix prompt editor update when not focus

* add key with variable

---------

Co-authored-by: Archer <545436317@qq.com>

* fix: value type

* doc

* i18n

* import path

* home page

* perf: mongo session running

* fix: ts

* perf: use toast

* perf: flow edit

* perf: sse response

* slider ui

* fetch error

* fix prompt editor rerender when not focus by key defaultvalue (#14)

* perf: prompt editor

* feat: dataset search concat

* perf: doc

* fix:ts

* perf: doc

* fix json editor onblur value (#15)

* faq

* vector model default config

* ipv6

---------

Co-authored-by: heheer <71265218+newfish-cmyk@users.noreply.github.com>
2024-02-01 21:57:41 +08:00

62 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { replaceVariable } from '@fastgpt/global/common/string/tools';
import { getAIApi } from '../config';
const prompt = `
您的任务是生成根据用户问题,从不同角度,生成两个不同版本的问题,以便可以从矢量数据库检索相关文档。例如:
问题: FastGPT如何使用
OUTPUT: ["FastGPT使用教程。","怎么使用FastGPT"]
-------------------
问题: FastGPT如何收费
OUTPUT: ["FastGPT收费标准。","FastGPT是如何计费的"]
-------------------
问题: 怎么FastGPT部署
OUTPUT: ["FastGPT的部署方式。","如何部署FastGPT"]
-------------------
问题 question: {{q}}
OUTPUT:
`;
export const searchQueryExtension = async ({ query, model }: { query: string; model: string }) => {
const ai = getAIApi({
timeout: 480000
});
const result = await ai.chat.completions.create({
model,
temperature: 0,
messages: [
{
role: 'user',
content: replaceVariable(prompt, { q: query })
}
],
stream: false
});
const answer = result.choices?.[0]?.message?.content || '';
if (!answer) {
return {
queries: [query],
model,
inputTokens: 0,
responseTokens: 0
};
}
try {
return {
queries: JSON.parse(answer) as string[],
model,
inputTokens: result.usage?.prompt_tokens || 0,
responseTokens: result.usage?.completion_tokens || 0
};
} catch (error) {
return {
queries: [query],
model,
inputTokens: 0,
responseTokens: 0
};
}
};