generated from tailored/app-template
	feat: add chatStream
This commit is contained in:
		@@ -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;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -23,3 +23,5 @@ export interface BaseChatUsageInterface {
 | 
			
		||||
   */
 | 
			
		||||
  completion_tokens: number;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export type ChatStream = AsyncGenerator<ChatMessageComplete, void, unknown>;
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user