feat: add百炼
This commit is contained in:
10
src/provider/chat-adapter/dashscope.ts
Normal file
10
src/provider/chat-adapter/dashscope.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { BaseChat, BaseChatOptions } from '../core/chat.ts';
|
||||
|
||||
export type BailianOptions = Partial<BaseChatOptions>;
|
||||
export class BailianChat extends BaseChat {
|
||||
static BASE_URL = 'https://bailian.aliyuncs.com/compatible-mode/v1/';
|
||||
constructor(options: BailianOptions) {
|
||||
const baseURL = options.baseURL || BailianChat.BASE_URL;
|
||||
super({ ...(options as BaseChatOptions), baseURL: baseURL });
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import { Custom } from './chat-adapter/custom.ts';
|
||||
import { Volces } from './chat-adapter/volces.ts';
|
||||
import { DeepSeek } from './chat-adapter/deepseek.ts';
|
||||
import { ModelScope } from './chat-adapter/model-scope.ts';
|
||||
import { BailianChat } from './chat-adapter/dashscope.ts';
|
||||
|
||||
import { ChatMessage } from './core/type.ts';
|
||||
|
||||
export const OllamaProvider = Ollama;
|
||||
@@ -15,6 +17,7 @@ export const CustomProvider = Custom;
|
||||
export const VolcesProvider = Volces;
|
||||
export const DeepSeekProvider = DeepSeek;
|
||||
export const ModelScopeProvider = ModelScope;
|
||||
export const BailianProvider = BailianChat;
|
||||
|
||||
export const ChatProviderMap = {
|
||||
Ollama: OllamaProvider,
|
||||
@@ -24,6 +27,7 @@ export const ChatProviderMap = {
|
||||
DeepSeek: DeepSeekProvider,
|
||||
ModelScope: ModelScopeProvider,
|
||||
BaseChat: BaseChat,
|
||||
Bailian: BailianProvider,
|
||||
};
|
||||
|
||||
type ProviderManagerConfig = {
|
||||
|
||||
@@ -14,7 +14,7 @@ export type BaseChatOptions<T = Record<string, any>> = {
|
||||
/**
|
||||
* 默认baseURL
|
||||
*/
|
||||
baseURL: string;
|
||||
baseURL?: string;
|
||||
/**
|
||||
* 默认模型
|
||||
*/
|
||||
@@ -32,7 +32,14 @@ export type BaseChatOptions<T = Record<string, any>> = {
|
||||
*/
|
||||
stream?: boolean;
|
||||
} & T;
|
||||
|
||||
export const getIsBrowser = () => {
|
||||
try {
|
||||
// @ts-ignore
|
||||
return IS_BROWSER;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
export class BaseChat implements BaseChatInterface, BaseChatUsageInterface {
|
||||
/**
|
||||
* 默认baseURL
|
||||
@@ -63,7 +70,9 @@ export class BaseChat implements BaseChatInterface, BaseChatUsageInterface {
|
||||
this.baseURL = options.baseURL;
|
||||
this.model = options.model;
|
||||
this.apiKey = options.apiKey;
|
||||
this.isBrowser = options.isBrowser ?? false;
|
||||
// @ts-ignore
|
||||
const DEFAULT_IS_BROWSER = getIsBrowser();
|
||||
this.isBrowser = options.isBrowser ?? DEFAULT_IS_BROWSER;
|
||||
this.openai = new OpenAI({
|
||||
apiKey: this.apiKey,
|
||||
baseURL: this.baseURL,
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import OpenAI from 'openai';
|
||||
|
||||
export type ChatMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam;
|
||||
export type ChatMessageOptions = Partial<OpenAI.Chat.Completions.ChatCompletionCreateParams>;
|
||||
export type ChatMessageOptions = Partial<OpenAI.Chat.Completions.ChatCompletionCreateParams> & {
|
||||
/**
|
||||
* 是否能够思考
|
||||
* 如果会话是千文,服务器的接口,默认为 true
|
||||
*/
|
||||
enable_thinking?: boolean;
|
||||
};
|
||||
export type ChatMessageComplete = OpenAI.Chat.Completions.ChatCompletion;
|
||||
export type ChatMessageStream = OpenAI.Chat.Completions.ChatCompletion;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { KnowledgeBase, KnowledgeOptions } from './knowledge-base.ts';
|
||||
|
||||
export class SiliconFlowKnowledge extends KnowledgeBase {
|
||||
static BASE_URL = 'https://api.siliconflow.com/v1';
|
||||
static BASE_URL = 'https://api.siliconflow.cn/v1';
|
||||
constructor(options: KnowledgeOptions) {
|
||||
super({ ...options, baseURL: options?.baseURL ?? SiliconFlowKnowledge.BASE_URL });
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { SiliconFlow } from '../../../src/provider/chat-adapter/siliconflow.ts';
|
||||
import { SiliconFlowKnowledge } from '../../provider/knowledge-adapter/siliconflow.ts';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
export const siliconflow = new SiliconFlow({
|
||||
apiKey: process.env.SILICONFLOW_API_KEY,
|
||||
model: 'Qwen/Qwen2-7B-Instruct',
|
||||
@@ -7,7 +10,6 @@ export const siliconflow = new SiliconFlow({
|
||||
|
||||
export const knowledge = new SiliconFlowKnowledge({
|
||||
apiKey: process.env.SILICONFLOW_API_KEY,
|
||||
baseURL: SiliconFlow.BASE_URL,
|
||||
model: 'Qwen/Qwen2-7B-Instruct',
|
||||
embeddingModel: 'Pro/BAAI/bge-m3',
|
||||
});
|
||||
|
||||
28
src/test/siliconflow/rerank/fc.ts
Normal file
28
src/test/siliconflow/rerank/fc.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { knowledge } from '../common.ts';
|
||||
import util from 'node:util';
|
||||
|
||||
const fc = [
|
||||
{
|
||||
title: '根据描述查询对应的网页的地址',
|
||||
description: '根据描述查询对应的网页的地址,并返回网页的标题和链接',
|
||||
},
|
||||
{
|
||||
title: '查询我的 draw 应用中的内容 ',
|
||||
description: '根据搜索内容,查询我的 draw 应用中的内容,并返回对应的数据',
|
||||
},
|
||||
];
|
||||
|
||||
const main = async () => {
|
||||
const res = await knowledge.rerank({
|
||||
model: 'BAAI/bge-reranker-v2-m3',
|
||||
query: '查询网页',
|
||||
// query: '网页code-center的地址',
|
||||
documents: fc.map((item) => {
|
||||
return `${item.title}:${item.description}`;
|
||||
}),
|
||||
});
|
||||
console.log(util.inspect(res, { depth: 10, colors: true }));
|
||||
// 8000 tokens 大概1w个字 2万个字符
|
||||
};
|
||||
|
||||
main();
|
||||
12
src/utils/json.ts
Normal file
12
src/utils/json.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* 尝试从字符串中提取JSON对象
|
||||
*/
|
||||
export const getJsonFromString = (str: string) => {
|
||||
try {
|
||||
const jsonMatch = str.match(/```json\s*([\s\S]*?)\s*```/);
|
||||
if (jsonMatch && jsonMatch[1]) {
|
||||
return JSON.parse(jsonMatch[1]);
|
||||
}
|
||||
} catch (error) {}
|
||||
return null;
|
||||
};
|
||||
Reference in New Issue
Block a user