wujingjing
2025-02-10 e50196bff10f0196307b2567ed6c0829eadd8ff6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import type { ChatMessage } from '../types';
 
export async function chat(messageList: ChatMessage[], apiKey: string) {
    const result = await fetch('https://api.openai.com/v1/chat/completions', {
        method: 'post',
        // signal: AbortSignal.timeout(8000),
        // 开启后到达设定时间会中断流式输出
        headers: {
            'Content-Type': 'application/json',
            Authorization: `Bearer ${apiKey}`,
        },
        body: JSON.stringify({
            model: 'gpt-3.5-turbo',
            stream: true,
            messages: messageList,
        }),
    }).catch(error=>{
    throw error
  });
    return result;
}