import OpenAI from"openai";
const client = new OpenAI({ baseURL: "https://api.uncensoredstory.top/v1", apiKey: process.env.API_KEY });
const resp = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Draft a villain monologue for my game." }],
});
console.log(resp.choices[0].message.content);
การตอบแบบสตรีมมิง
เปิดใช้งานสตรีมมิงโดยการตั้งค่า stream: true ในคำขอของคุณ API จะส่งคืนสตรีม Server-Sent Events (SSE) ซึ่งส่งโทเคนทันทีที่สร้าง สิ่งนี้ลดความล่าช้าที่รับรู้สำหรับผู้ใช้ที่อ่านเรื่องราวที่สร้าง สตรีมสุดท้ายประกอบด้วยสถิติการใช้โทเคนทั้งหมด ทำให้คุณสามารถอัปเดตยอดคงเหลือในเครื่องของคุณได้อย่างแม่นยำ การสตรีมมีประโยชน์เป็นพิเศษสำหรับแอปพลิเคชัน uncensored chatbot api แบบโต้ตอบซึ่งต้องการการตอบกลับทันที ตรวจสอบให้แน่ใจว่าไคลเอนต์ของคุณจัดการการสิ้นสุดสตรีมอย่างถูกต้องเพื่อหลีกเลี่ยงการเชื่อมต่อค้าง
stream = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Tell the story in second person."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)