forked from TabbyML/tabby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: cleanup chat api make it message oriented (TabbyML#497)
* refactor: refactor into /chat/completions api * Revert "feat: support request level stop words (TabbyML#492)" This reverts commit 0d6840e. * feat: adjust interface * switch interface in tabby-playground * move to chat/prompt, add unit test * update interface
- Loading branch information
Showing
25 changed files
with
346 additions
and
202 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
type AIStreamCallbacksAndOptions, | ||
createCallbacksTransformer, | ||
createStreamDataTransformer | ||
} from 'ai'; | ||
|
||
const utf8Decoder = new TextDecoder('utf-8'); | ||
|
||
async function processLines( | ||
lines: string[], | ||
controller: ReadableStreamDefaultController<string>, | ||
) { | ||
for (const line of lines) { | ||
const { content } = JSON.parse(line); | ||
controller.enqueue(content); | ||
} | ||
} | ||
|
||
async function readAndProcessLines( | ||
reader: ReadableStreamDefaultReader<Uint8Array>, | ||
controller: ReadableStreamDefaultController<string>, | ||
) { | ||
let segment = ''; | ||
|
||
while (true) { | ||
const { value: chunk, done } = await reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
|
||
segment += utf8Decoder.decode(chunk, { stream: true }); | ||
|
||
const linesArray = segment.split(/\r\n|\n|\r/g); | ||
segment = linesArray.pop() || ''; | ||
|
||
await processLines(linesArray, controller); | ||
} | ||
|
||
if (segment) { | ||
const linesArray = [segment]; | ||
await processLines(linesArray, controller); | ||
} | ||
|
||
controller.close(); | ||
} | ||
|
||
function createParser(res: Response) { | ||
const reader = res.body?.getReader(); | ||
|
||
return new ReadableStream<string>({ | ||
async start(controller): Promise<void> { | ||
if (!reader) { | ||
controller.close(); | ||
return; | ||
} | ||
|
||
await readAndProcessLines(reader, controller); | ||
}, | ||
}); | ||
} | ||
|
||
export function TabbyStream( | ||
reader: Response, | ||
callbacks?: AIStreamCallbacksAndOptions, | ||
): ReadableStream { | ||
return createParser(reader) | ||
.pipeThrough(createCallbacksTransformer(callbacks)) | ||
.pipeThrough( | ||
createStreamDataTransformer(callbacks?.experimental_streamData), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.