-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #564 from janhq/feat/cli-chat
[WIP] feat: add CLI for chat
- Loading branch information
Showing
7 changed files
with
215 additions
and
49 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import { Model } from '../models/model.interface'; | ||
import { Extension } from './extension.abstract'; | ||
|
||
export abstract class EngineExtension extends Extension { | ||
abstract provider: string; | ||
abstract inference(completion: any, req: any, res: any): void; | ||
abstract loadModel(loadModel: any): Promise<void>; | ||
abstract unloadModel(modelId: string): Promise<void>; | ||
|
||
abstract inference(completion: any, req: any, stream: any, res?: any): void; | ||
|
||
async loadModel(model: Model): Promise<void> {} | ||
|
||
async unloadModel(modelId: string): Promise<void> {} | ||
} |
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
74 changes: 65 additions & 9 deletions
74
cortex-js/src/infrastructure/commanders/inference.command.ts
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 |
---|---|---|
@@ -1,25 +1,81 @@ | ||
import { ChatUsecases } from '@/usecases/chat/chat.usecases'; | ||
import { CommandRunner, SubCommand } from 'nest-commander'; | ||
import { CreateChatCompletionDto } from '../dtos/chat/create-chat-completion.dto'; | ||
import { ChatCompletionRole } from '@/domain/models/message.interface'; | ||
import { stdout } from 'process'; | ||
import * as readline from 'node:readline/promises'; | ||
import { ChatStreamEvent } from '@/domain/abstracts/oai.abstract'; | ||
import { ChatCompletionMessage } from '../dtos/chat/chat-completion-message.dto'; | ||
|
||
@SubCommand({ name: 'chat' }) | ||
export class InferenceCommand extends CommandRunner { | ||
constructor() { | ||
exitClause = 'exit()'; | ||
userIndicator = '>> '; | ||
exitMessage = 'Bye!'; | ||
|
||
constructor(private readonly chatUsecases: ChatUsecases) { | ||
super(); | ||
} | ||
|
||
async run(_input: string[]): Promise<void> { | ||
const lineByLine = require('readline'); | ||
const lbl = lineByLine.createInterface({ | ||
async run(): Promise<void> { | ||
console.log(`Inorder to exit, type '${this.exitClause}'.`); | ||
const messages: ChatCompletionMessage[] = []; | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
prompt: this.userIndicator, | ||
}); | ||
rl.prompt(); | ||
|
||
rl.on('close', () => { | ||
console.log(this.exitMessage); | ||
process.exit(0); | ||
}); | ||
lbl.on('line', (userInput: string) => { | ||
if (userInput.trim() === 'exit()') { | ||
lbl.close(); | ||
|
||
rl.on('line', (userInput: string) => { | ||
if (userInput.trim() === this.exitClause) { | ||
rl.close(); | ||
return; | ||
} | ||
|
||
console.log('Result:', userInput); | ||
console.log('Enter another equation or type "exit()" to quit.'); | ||
messages.push({ | ||
content: userInput, | ||
role: ChatCompletionRole.User, | ||
}); | ||
|
||
const chatDto: CreateChatCompletionDto = { | ||
messages, | ||
model: 'TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF', | ||
stream: true, | ||
max_tokens: 2048, | ||
stop: [], | ||
frequency_penalty: 0.7, | ||
presence_penalty: 0.7, | ||
temperature: 0.7, | ||
top_p: 0.7, | ||
}; | ||
|
||
let llmFullResponse = ''; | ||
const writableStream = new WritableStream<ChatStreamEvent>({ | ||
write(chunk) { | ||
if (chunk.type === 'data') { | ||
stdout.write(chunk.data ?? ''); | ||
llmFullResponse += chunk.data ?? ''; | ||
} else if (chunk.type === 'error') { | ||
console.log('Error!!'); | ||
} else { | ||
messages.push({ | ||
content: llmFullResponse, | ||
role: ChatCompletionRole.Assistant, | ||
}); | ||
llmFullResponse = ''; | ||
console.log('\n'); | ||
} | ||
}, | ||
}); | ||
|
||
this.chatUsecases.createChatCompletions(chatDto, {}, writableStream); | ||
}); | ||
} | ||
} |
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