| 29 | let history = []; |
| 30 | |
| 31 | async function chat(userMessage) { |
| 32 | history.push({ role: "user", content: userMessage }); |
| 33 | try { |
| 34 | const stream = await client.chat.completions.create({ |
| 35 | model, |
| 36 | messages: history, |
| 37 | stream: true, |
| 38 | }); |
| 39 | let reply = ""; |
| 40 | for await (const chunk of stream) { |
| 41 | const delta = chunk.choices[0]?.delta?.content || ""; |
| 42 | process.stdout.write(delta); |
| 43 | reply += delta; |
| 44 | } |
| 45 | process.stdout.write("\n"); |
| 46 | history.push({ role: "assistant", content: reply }); |
| 47 | } catch (err) { |
| 48 | history.pop(); |
| 49 | console.error(`error: ${err.message}`); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | async function generateImage(prompt) { |
| 54 | try { |