Skip to content

Commit

Permalink
Feat/run template (#26)
Browse files Browse the repository at this point in the history
* feat/template-utils-and-variable-utils(packages/qllm-cli/src/utils): add new utility modules

* feat/run-template(packages/qllm-cli/src): add run command to execute templates

* feat(packages/qllm-cli): update dependencies and package version

* feat/run-template(packages/qllm-cli/src/utils): improve variable prompt experience

* feat/run-template(packages/qllm-cli/src/commands): add spinner update and start for template execution

* update

* update

* feat(packages/qllm-cli): add mime-types types and improve io-manager

The commit message should be:

feat(packages/qllm-cli): add mime-types types and improve io-manager

* feat(packages/qllm-cli/src/utils/io-manager): Refactor IOManager class with DisplayManager, InputManager, and SpinnerManager

* update

* feat(packages/qllm-cli): update dependencies and add configuration management

* feat(packages/qllm-lib): update package.json version and template variable extraction
  • Loading branch information
raphaelmansuy authored Aug 26, 2024
1 parent 62db19f commit 3290f3e
Show file tree
Hide file tree
Showing 28 changed files with 941 additions and 528 deletions.
7 changes: 5 additions & 2 deletions packages/qllm-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"license": "Apache-2.0",
"devDependencies": {
"@types/copy-paste": "^1.1.33",

"@types/jest": "^29.5.12",
"@types/mime-types": "^2.1.4",
"@types/node": "^22.5.0",
"@types/prompts": "^2.4.9",
"@types/screenshot-desktop": "^1.12.3",
Expand All @@ -58,18 +60,19 @@
"typescript": "^5.5.4"
},
"dependencies": {
"@types/node": "^22.5.0",
"@npmcli/fs": "^3.1.1",
"@types/node": "^22.5.0",
"cli-table3": "^0.6.5",
"commander": "^12.1.0",
"console-table-printer": "^2.12.1",
"copy-paste": "^1.5.3",
"gradient-string": "^2.0.2",
"jimp": "^0.22.12",
"kleur": "^4.1.5",
"lru-cache": "^11.0.0",
"nanospinner": "^1.1.0",
"prompts": "^2.4.2",
"qllm-lib": "1.8.5",
"qllm-lib": "^1.8.6",
"readline": "^1.3.0",
"screenshot-desktop": "^1.15.0",
"table": "^6.8.2",
Expand Down
25 changes: 13 additions & 12 deletions packages/qllm-cli/src/chat/chat-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// packages/qllm-cli/src/chat/chat-config.ts
import fs from "fs/promises";
import path from "path";
import os from "os";
Expand Down Expand Up @@ -63,79 +62,81 @@ export class ChatConfig {
key: K,
value: ChatConfigType[K]
): void {
this.config[key] = value;
const partialConfig = { [key]: value };
const validatedConfig = ChatConfigSchema.partial().parse(partialConfig);
this.config[key] = validatedConfig[key] as ChatConfigType[K];
}

public getProvider(): string | undefined {
return this.config.provider;
}

public setProvider(provider: string): void {
this.config.provider = provider;
this.set("provider", provider);
}

public getModel(): string | undefined {
return this.config.model;
}

public setModel(model: string): void {
this.config.model = model;
this.set("model", model);
}

public getTemperature(): number | undefined {
return this.config.temperature;
}

public setTemperature(temperature: number): void {
this.config.temperature = temperature;
this.set("temperature", temperature);
}

public getMaxTokens(): number | undefined {
return this.config.maxTokens;
}

public setMaxTokens(maxTokens: number): void {
this.config.maxTokens = maxTokens;
this.set("maxTokens", maxTokens);
}

public getTopP(): number | undefined {
return this.config.topP;
}

public setTopP(topP: number): void {
this.config.topP = topP;
this.set("topP", topP);
}

public getFrequencyPenalty(): number | undefined {
return this.config.frequencyPenalty;
}

public setFrequencyPenalty(frequencyPenalty: number): void {
this.config.frequencyPenalty = frequencyPenalty;
this.set("frequencyPenalty", frequencyPenalty);
}

public getPresencePenalty(): number | undefined {
return this.config.presencePenalty;
}

public setPresencePenalty(presencePenalty: number): void {
this.config.presencePenalty = presencePenalty;
this.set("presencePenalty", presencePenalty);
}

public getStopSequence(): string[] | undefined {
return this.config.stopSequence;
}

public setStopSequence(stopSequence: string[]): void {
this.config.stopSequence = stopSequence;
this.set("stopSequence", stopSequence);
}

public getCurrentConversationId(): string | undefined {
return this.config.currentConversationId;
}

public setCurrentConversationId(conversationId: string | undefined): void {
this.config.currentConversationId = conversationId;
this.set("currentConversationId", conversationId);
}

public async initialize(): Promise<void> {
Expand All @@ -147,4 +148,4 @@ export class ChatConfig {
}
}

export const chatConfig = ChatConfig.getInstance();
export const chatConfig = ChatConfig.getInstance();
18 changes: 9 additions & 9 deletions packages/qllm-cli/src/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { createConversationManager, getLLMProvider } from "qllm-lib";
import { ChatConfig } from "./chat-config";
import { MessageHandler } from "./message-handler";
import { CommandProcessor } from "./command-processor";
import { IOManager } from "./io-manager";
import { IOManager } from "../utils/io-manager";
import { ConfigManager } from "./config-manager";
import { output } from "../utils/output";
import { ioManager} from "../utils/io-manager";
import ImageManager from "./image-manager";

export class Chat {
Expand Down Expand Up @@ -40,11 +40,11 @@ export class Chat {
await this.config.initialize();
this.configManager.setProvider(this.providerName);
this.configManager.setModel(this.modelName);
output.success(
ioManager.displaySuccess(
`Chat initialized with ${this.providerName} provider and ${this.modelName} model.`
);
} catch (error) {
output.error(`Failed to initialize chat: ${(error as Error).message}`);
ioManager.displayError(`Failed to initialize chat: ${(error as Error).message}`);
process.exit(1);
}
}
Expand All @@ -53,10 +53,10 @@ export class Chat {
await this.initialize();
const conversation = await this.conversationManager.createConversation();
this.conversationId = conversation.id;
output.info(
ioManager.displayInfo(
"Chat session started. Type your messages or use special commands."
);
output.info("Type /help for available commands.");
ioManager.displayInfo("Type /help for available commands.");
this.promptUser();
}

Expand Down Expand Up @@ -86,7 +86,7 @@ export class Chat {
try {
const [command, ...args] = input.trim().split(/\s+/);
if (!command) {
output.error("No command provided");
ioManager.displayError("No command provided");
return;
}
const cleanCommand = command.substring(1).toLowerCase();
Expand All @@ -100,7 +100,7 @@ export class Chat {
};
await this.commandProcessor.processCommand(cleanCommand, args, context);
} catch (error) {
output.error(
ioManager.displayError(
"Error processing special command: " +
(error instanceof Error ? error.message : String(error))
);
Expand All @@ -112,7 +112,7 @@ export class Chat {
images: string[]
): Promise<void> {
if (!this.conversationId) {
output.error("No active conversation. Please start a chat first.");
ioManager.displayError("No active conversation. Please start a chat first.");
return;
}
const currentProviderName = this.configManager.getProvider();
Expand Down
2 changes: 1 addition & 1 deletion packages/qllm-cli/src/chat/command-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ConversationManager } from "qllm-lib";
import { ChatConfig } from "./chat-config";
import { ConfigManager } from "./config-manager";
import { IOManager } from "./io-manager";
import { IOManager } from "../utils/io-manager";

import ImageManager from "./image-manager";
import { showHelp } from "./commands/show-help";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandContext } from "../command-processor";
import { IOManager } from "../io-manager";
import { IOManager } from "../../utils/io-manager";
import { ConversationMessage, ChatMessageContent, LLMOptions } from "qllm-lib";

const MESSAGES_PER_PAGE = 5;
Expand Down
19 changes: 11 additions & 8 deletions packages/qllm-cli/src/chat/config-manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// packages/qllm-cli/src/chat/config-manager.ts
import { ChatConfig } from "./chat-config";
import { getLLMProvider } from "qllm-lib";
import { output } from "../utils/output";
import { ioManager } from "../utils/io-manager";
import { DEFAULT_PROVIDER, DEFAULT_MODEL } from "../constants";

export class ConfigManager {
Expand All @@ -11,9 +11,11 @@ export class ConfigManager {
try {
await getLLMProvider(providerName);
this.config.setProvider(providerName);
output.success(`Provider set to: ${providerName}`);
ioManager.displaySuccess(`Provider set to: ${providerName}`);
} catch (error) {
output.error(`Failed to set provider: ${(error as Error).message}`);
ioManager.displayError(
`Failed to set provider: ${(error as Error).message}`
);
}
}

Expand All @@ -27,7 +29,7 @@ export class ConfigManager {

setModel(modelName: string): void {
this.config.setModel(modelName);
output.success(`Model set to: ${modelName}`);
ioManager.displaySuccess(`Model set to: ${modelName}`);
}

getModel(): string {
Expand Down Expand Up @@ -56,11 +58,12 @@ export class ConfigManager {
this.config.setStopSequence(value.split(","));
break;
default:
output.error(`Unknown option: ${option}`);
ioManager.displayError(`Unknown option: ${option}`);
this.showValidOptions();
return;
}
output.success(`Option ${evalOption} set to: ${value}`);

ioManager.displaySuccess(`Option ${evalOption} set to: ${value}`);
}

getAllSettings(): Record<string, any> {
Expand All @@ -77,15 +80,15 @@ export class ConfigManager {
}

private showValidOptions(): void {
output.info("Valid options are:");
ioManager.displayInfo("Valid options are:");
[
"temperature",
"max_tokens",
"top_p",
"frequency_penalty",
"presence_penalty",
"stop_sequence",
].forEach((opt) => output.info(`- ${opt}`));
].forEach((opt) => ioManager.displayInfo(`- ${opt}`));
}

async initialize(): Promise<void> {
Expand Down
18 changes: 9 additions & 9 deletions packages/qllm-cli/src/chat/image-manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// packages/qllm-cli/src/chat/image-manager.ts

import { utils } from "./utils";
import { output } from "../utils/output";
import { ioManager } from "../utils/io-manager";

export class ImageManager {
private images: Set<string>;
Expand All @@ -17,18 +17,18 @@ export class ImageManager {
addImage(image: string): void {
if (utils.isValidUrl(image) || utils.isImageFile(image)) {
this.images.add(image);
output.success(`Image added: ${utils.truncateString(image, 50)}`);
ioManager.displaySuccess(`Image added: ${utils.truncateString(image, 50)}`);
} else {
output.error("Invalid image URL or file path");
ioManager.displayError("Invalid image URL or file path");
}
}

removeImage(image: string): boolean {
const removed = this.images.delete(image);
if (removed) {
output.success(`Image removed: ${utils.truncateString(image, 50)}`);
ioManager.displaySuccess(`Image removed: ${utils.truncateString(image, 50)}`);
} else {
output.warn(`Image not found: ${utils.truncateString(image, 50)}`);
ioManager.displayWarning(`Image not found: ${utils.truncateString(image, 50)}`);
}
return removed;
}
Expand All @@ -48,20 +48,20 @@ export class ImageManager {
clearImages(showOutput: boolean = true): void {
const count = this.images.size;
this.images.clear();
output.success(
ioManager.displaySuccess(
`Cleared ${count} image${count !== 1 ? "s" : ""} from the buffer`
);
}

displayImages(): void {
if (this.images.size === 0) {
output.info("No images in the buffer");
ioManager.displayInfo("No images in the buffer");
return;
}

output.info(`Images in the buffer (${this.images.size}):`);
ioManager.displayInfo(`Images in the buffer (${this.images.size}):`);
this.getImages().forEach((image, index) => {
output.info(`${index + 1}. ${utils.truncateString(image, 70)}`);
ioManager.displayInfo(`${index + 1}. ${utils.truncateString(image, 70)}`);
});
}
}
Expand Down
Loading

0 comments on commit 3290f3e

Please sign in to comment.