feat: add chatStream

This commit is contained in:
熊潇 2025-05-09 19:50:32 +08:00
parent 4a2d3e8d32
commit 2338242018
4 changed files with 15 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import { OpenAI } from 'openai';
import type { BaseChatInterface, ChatMessageComplete, ChatMessage, ChatMessageOptions, BaseChatUsageInterface } from './type.ts';
import type { BaseChatInterface, ChatMessageComplete, ChatMessage, ChatMessageOptions, BaseChatUsageInterface, ChatStream } from './type.ts';
export type BaseChatOptions<T = Record<string, any>> = {
/**
@ -87,7 +87,7 @@ export class BaseChat implements BaseChatInterface, BaseChatUsageInterface {
if (createParams.response_format) {
throw new Error('response_format is not supported in stream mode');
}
return this.openai.chat.completions.create(createParams) as any;
return this.openai.chat.completions.create(createParams) as unknown as ChatStream;
}
/**

View File

@ -1,2 +1,10 @@
import { ChatStream } from './type.ts';
export * from './chat.ts';
export * from './type.ts';
export const readStream = async (chatStream: ChatStream) => {
for await (const chunk of chatStream) {
console.log(chunk);
}
};

View File

@ -1,6 +1,6 @@
import OpenAI from 'openai';
export type ChatMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam ;
export type ChatMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam;
export type ChatMessageOptions = Partial<OpenAI.Chat.Completions.ChatCompletionCreateParams>;
export type ChatMessageComplete = OpenAI.Chat.Completions.ChatCompletion;
export type ChatMessageStream = OpenAI.Chat.Completions.ChatCompletion;
@ -23,3 +23,5 @@ export interface BaseChatUsageInterface {
*/
completion_tokens: number;
}
export type ChatStream = AsyncGenerator<ChatMessageComplete, void, unknown>;

View File

@ -13,10 +13,10 @@ const main = async () => {
console.log(usage);
};
// main();
main();
const mainChat = async () => {
const res = await siliconflow.chat([{ role: 'user', content: 'Hello, world! 1 + 1 equals ?' }]);
console.log(res);
};
mainChat();
// mainChat();