SimpleChatTC: Extract streamed field - assume only 1f at any time
Update response_extract_stream to check for which field is being currently streamed ie is it normal content or tool call func name or tool call func args and then return the field name and extracted value. Previously it was always assumed that only normal content will be returned. Currently it is assumed that the server will only stream one of the 3 supported fields at any time and not more than one of them at the same time. TODO: Have to also add logic to extract the reasoning field later, ie wrt gen ai models which give out their thinking. Have updated append_response to expect both the key and the value wrt the latestResponse object, which it will be manipualted. Previously it was always assumed that content is what will be got and inturn appended.
This commit is contained in:
parent
bfe7ef69fa
commit
63430dc9f7
|
|
@ -152,10 +152,10 @@ class SimpleChat {
|
|||
/**
|
||||
* Collate the latest response from the server/ai-model, as it is becoming available.
|
||||
* This is mainly useful for the stream mode.
|
||||
* @param {string} content
|
||||
* @param {{key: string, value: string}} resp
|
||||
*/
|
||||
append_response(content) {
|
||||
this.latestResponse.content += content;
|
||||
append_response(resp) {
|
||||
this.latestResponse[resp.key] += resp.value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -311,10 +311,25 @@ class SimpleChat {
|
|||
* @param {string} apiEP
|
||||
*/
|
||||
response_extract_stream(respBody, apiEP) {
|
||||
let key = "content"
|
||||
let assistant = "";
|
||||
if (apiEP == ApiEP.Type.Chat) {
|
||||
if (respBody["choices"][0]["finish_reason"] !== "stop") {
|
||||
if (respBody["choices"][0]["finish_reason"] !== null) {
|
||||
if (respBody["choices"][0]["delta"]["content"] !== undefined) {
|
||||
assistant = respBody["choices"][0]["delta"]["content"];
|
||||
} else {
|
||||
if (respBody["choices"][0]["delta"]["tool_calls"] !== undefined) {
|
||||
if (respBody["choices"][0]["delta"]["tool_calls"][0]["function"]["name"] !== undefined) {
|
||||
key = "toolname";
|
||||
assistant = respBody["choices"][0]["delta"]["tool_calls"][0]["function"]["name"];
|
||||
} else {
|
||||
if (respBody["choices"][0]["delta"]["tool_calls"][0]["function"]["arguments"] !== undefined) {
|
||||
key = "toolargs";
|
||||
assistant = respBody["choices"][0]["delta"]["tool_calls"][0]["function"]["arguments"];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
|
|
@ -323,7 +338,7 @@ class SimpleChat {
|
|||
assistant = respBody["content"];
|
||||
}
|
||||
}
|
||||
return assistant;
|
||||
return { key: key, value: assistant };
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue