added embed msg and stream parser

This commit is contained in:
Kevin Dang
2024-01-25 18:25:19 -08:00
parent aaf734b06c
commit 97acae3d08
3 changed files with 73 additions and 10 deletions

23
src/utils/streamParse.ts Normal file
View File

@@ -0,0 +1,23 @@
import { AxiosResponse } from "axios";
/**
* When running a /api/chat stream, the output needs to be parsed into an array of objects
* @param stream Axios response to from Ollama
*/
export function parseStream(stream: AxiosResponse<any, any>) {
// split string by newline
const keywordObjects: string[] = stream.data.trim().split('\n')
// parse string and load them into objects
const keywordsArray: {
model: string,
created_at: string,
message: {
role: string,
content: string
},
done: boolean
}[] = keywordObjects.map((keywordString) => JSON.parse(keywordString))
return keywordsArray
}