mirror of
https://github.com/numexai/numex-sdk.git
synced 2026-09-25 13:32:15 +00:00
Sohbette messages dizisini message/history alanlarina da cevir; arama parametresini belgele
This commit is contained in:
parent
0d263a3f94
commit
81f80e1722
|
|
@ -1,9 +1,27 @@
|
||||||
|
// Numex /chat uç noktası { message, history } bekler; OpenAI tarzı { messages } da
|
||||||
|
// verilebilsin diye son kullanıcı mesajını ve önceki mesajları bu alanlara da kopyalarız.
|
||||||
|
function withMessageFields(params = {}) {
|
||||||
|
if (params.message || !Array.isArray(params.messages) || !params.messages.length) return params;
|
||||||
|
let lastUser = -1;
|
||||||
|
for (let i = params.messages.length - 1; i >= 0; i--) {
|
||||||
|
if (params.messages[i] && params.messages[i].role === 'user') { lastUser = i; break; }
|
||||||
|
}
|
||||||
|
if (lastUser === -1) return params;
|
||||||
|
const content = params.messages[lastUser].content;
|
||||||
|
const message = typeof content === 'string'
|
||||||
|
? content
|
||||||
|
: Array.isArray(content) ? content.map((c) => (c && c.text) || '').join('') : '';
|
||||||
|
const history = params.history || params.messages.slice(0, lastUser);
|
||||||
|
return { ...params, message, history };
|
||||||
|
}
|
||||||
|
|
||||||
class ChatCompletions {
|
class ChatCompletions {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(params) {
|
async create(params) {
|
||||||
|
params = withMessageFields(params);
|
||||||
// If stream is requested, use the appropriate streaming endpoint/method
|
// If stream is requested, use the appropriate streaming endpoint/method
|
||||||
if (params.stream) {
|
if (params.stream) {
|
||||||
return this.client.post('/chat/stream', params, { stream: true });
|
return this.client.post('/chat/stream', params, { stream: true });
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user