clear
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { BaseChat, BaseChatOptions } from '../core/chat.ts';
|
||||
import { OpenAI } from 'openai';
|
||||
import type { ChatMessage, ChatMessageOptions } from '../core/index.ts';
|
||||
|
||||
export type SiliconFlowOptions = Partial<BaseChatOptions>;
|
||||
@@ -33,7 +32,7 @@ export class SiliconFlow extends BaseChat {
|
||||
async getUsageInfo(): Promise<SiliconFlowUsageResponse> {
|
||||
return this.get('/user/info');
|
||||
}
|
||||
async chat(messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[], options?: ChatMessageOptions) {
|
||||
async chat(messages: ChatMessage[], options?: ChatMessageOptions) {
|
||||
const res = await super.chat(messages, options);
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import type {
|
||||
ChatMessageComplete,
|
||||
ChatMessage,
|
||||
ChatMessageOptions,
|
||||
BaseChatUsageInterface,
|
||||
Usage,
|
||||
ChatStream,
|
||||
EmbeddingMessage,
|
||||
EmbeddingMessageComplete,
|
||||
@@ -37,7 +37,7 @@ export type BaseChatOptions<T = Record<string, any>> = {
|
||||
stream?: boolean;
|
||||
} & T;
|
||||
|
||||
export class BaseChat implements BaseChatInterface, BaseChatUsageInterface {
|
||||
export class BaseChat implements BaseChatInterface, Usage {
|
||||
/**
|
||||
* 默认baseURL
|
||||
*/
|
||||
@@ -168,7 +168,18 @@ export class BaseChat implements BaseChatInterface, BaseChatUsageInterface {
|
||||
|
||||
return stream as unknown as ChatStream;
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单提问接口
|
||||
* @param message
|
||||
* @param options
|
||||
* @returns
|
||||
*/
|
||||
question(message: string, options?: ChatMessageOptions) {
|
||||
const messages: ChatMessage[] = [
|
||||
{ role: 'user', content: message }
|
||||
];
|
||||
return this.chat(messages, options);
|
||||
}
|
||||
/**
|
||||
* 获取聊天使用情况
|
||||
* @returns
|
||||
|
||||
@@ -1,23 +1,176 @@
|
||||
import OpenAI from 'openai';
|
||||
|
||||
export type ChatMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam;
|
||||
export type ChatMessageOptions = Partial<OpenAI.Chat.Completions.ChatCompletionCreateParams> & {
|
||||
export type ChatMessage = {
|
||||
role?: 'user' | 'assistant' | 'system' | 'tool';
|
||||
content: string;
|
||||
}
|
||||
export type ChatMessageOptions = {
|
||||
messages: ChatMessage[];
|
||||
/**
|
||||
* 模型名称
|
||||
*/
|
||||
model?: string;
|
||||
/**
|
||||
* 温度参数,控制随机性 (0-2)
|
||||
* 较高的值如0.8会使输出更随机,较低的值如0.2会使其更集中和确定
|
||||
*/
|
||||
temperature?: number;
|
||||
/**
|
||||
* 核采样参数 (0-1)
|
||||
* 与temperature类似,但使用不同的采样方法
|
||||
*/
|
||||
top_p?: number;
|
||||
/**
|
||||
* 生成的最大令牌数
|
||||
*/
|
||||
max_tokens?: number;
|
||||
/**
|
||||
* 停止序列
|
||||
* 当遇到这些序列时停止生成
|
||||
*/
|
||||
stop?: string | string[];
|
||||
/**
|
||||
* 频率惩罚 (-2.0 到 2.0)
|
||||
* 正值会根据新令牌在文本中的现有频率来惩罚它们
|
||||
*/
|
||||
frequency_penalty?: number;
|
||||
/**
|
||||
* 存在惩罚 (-2.0 到 2.0)
|
||||
* 正值会根据新令牌是否出现在文本中来惩罚它们
|
||||
*/
|
||||
presence_penalty?: number;
|
||||
/**
|
||||
* 流式输出
|
||||
*/
|
||||
stream?: boolean;
|
||||
/**
|
||||
* 是否能够思考
|
||||
* 如果会话是千文,服务器的接口,默认为 true
|
||||
*/
|
||||
enable_thinking?: boolean;
|
||||
};
|
||||
export type ChatMessageComplete = OpenAI.Chat.Completions.ChatCompletion;
|
||||
export type ChatMessageStream = OpenAI.Chat.Completions.ChatCompletion;
|
||||
response_format?: 'text' | 'json' | 'xml' | 'html';
|
||||
|
||||
/**
|
||||
* 工具调用参数
|
||||
*/
|
||||
tool_calls?: any;
|
||||
|
||||
};
|
||||
|
||||
type Choice = {
|
||||
finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter' | 'function_call';
|
||||
index: number;
|
||||
message: {
|
||||
role: 'assistant' | 'tool';
|
||||
content: string;
|
||||
};
|
||||
}
|
||||
|
||||
export type ChatMessageComplete = {
|
||||
/**
|
||||
* 聊天完成的唯一标识符
|
||||
*/
|
||||
id: string;
|
||||
/**
|
||||
* 聊天完成选项列表
|
||||
* 如果 n 大于 1,可以有多个选项
|
||||
*/
|
||||
choices: Array<Choice>;
|
||||
/**
|
||||
* 聊天完成创建时的 Unix 时间戳(秒)
|
||||
*/
|
||||
created: number;
|
||||
/**
|
||||
* 用于聊天完成的模型名称
|
||||
*/
|
||||
model: string;
|
||||
/**
|
||||
* 对象类型,始终为 `chat.completion`
|
||||
*/
|
||||
object: 'chat.completion';
|
||||
/**
|
||||
* 系统指纹
|
||||
* 用于标识后端配置
|
||||
*/
|
||||
system_fingerprint?: string;
|
||||
/**
|
||||
* 完成请求的使用统计信息
|
||||
*/
|
||||
usage?: Usage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向量嵌入请求参数
|
||||
*/
|
||||
export type EmbeddingMessage = {
|
||||
/**
|
||||
* 输入文本或文本数组
|
||||
* 要生成嵌入向量的文本内容
|
||||
*/
|
||||
input: string | string[];
|
||||
/**
|
||||
* 模型名称
|
||||
* 用于生成嵌入向量的模型
|
||||
*/
|
||||
model: string;
|
||||
/**
|
||||
* 编码格式
|
||||
* 返回的嵌入向量编码格式
|
||||
*/
|
||||
encoding_format?: 'float' | 'base64';
|
||||
/**
|
||||
* 维度
|
||||
* 输出嵌入向量的维度(某些模型支持)
|
||||
*/
|
||||
dimensions?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* 单个嵌入向量对象
|
||||
*/
|
||||
export type EmbeddingObject = {
|
||||
/**
|
||||
* 对象类型,始终为 `embedding`
|
||||
*/
|
||||
object: 'embedding';
|
||||
/**
|
||||
* 嵌入向量
|
||||
* 浮点数数组,表示文本的向量表示
|
||||
*/
|
||||
embedding: number[];
|
||||
/**
|
||||
* 索引位置
|
||||
* 该嵌入在输入数组中的位置
|
||||
*/
|
||||
index: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* 向量嵌入响应结果
|
||||
*/
|
||||
export type EmbeddingMessageComplete = {
|
||||
/**
|
||||
* 对象类型,始终为 `list`
|
||||
*/
|
||||
object: 'list';
|
||||
/**
|
||||
* 嵌入向量数据列表
|
||||
*/
|
||||
data: EmbeddingObject[];
|
||||
/**
|
||||
* 使用的模型名称
|
||||
*/
|
||||
model: string;
|
||||
/**
|
||||
* 使用统计信息
|
||||
*/
|
||||
usage: Usage;
|
||||
};
|
||||
|
||||
|
||||
export type EmbeddingMessage = Partial<OpenAI.Embeddings.EmbeddingCreateParams>;
|
||||
export type EmbeddingMessageComplete = OpenAI.Embeddings.CreateEmbeddingResponse;
|
||||
export interface BaseChatInterface {
|
||||
chat(messages: ChatMessage[], options?: ChatMessageOptions): Promise<ChatMessageComplete>;
|
||||
}
|
||||
|
||||
export interface BaseChatUsageInterface {
|
||||
export interface Usage {
|
||||
/**
|
||||
* 提示词令牌
|
||||
*/
|
||||
|
||||
@@ -27,11 +27,11 @@ export class VideoSiliconFlow extends SiliconFlow {
|
||||
console.log('uploadAudioVoice', res);
|
||||
}
|
||||
async audioSpeech() {
|
||||
this.openai.audio.speech.create({
|
||||
model: 'FunAudioLLM/CosyVoice2-0.5B',
|
||||
voice: 'alloy',
|
||||
input: '在一无所知中, 梦里的一天结束了,一个新的轮回便会开始',
|
||||
response_format: 'mp3',
|
||||
});
|
||||
// this.openai.audio.speech.create({
|
||||
// model: 'FunAudioLLM/CosyVoice2-0.5B',
|
||||
// voice: 'alloy',
|
||||
// input: '在一无所知中, 梦里的一天结束了,一个新的轮回便会开始',
|
||||
// response_format: 'mp3',
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user