-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent.ts
More file actions
50 lines (45 loc) · 1.39 KB
/
agent.ts
File metadata and controls
50 lines (45 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { loadQARefineChain, RefineDocumentsChain } from "npm:langchain/chains";
import { ChatOpenAI } from "npm:langchain/chat_models/openai";
import { Document } from "npm:langchain/document";
import { loadDiffFromGit } from "./diffloader.ts";
import { DEFAULT_PROMPT } from "./prompt.ts";
export class CommitMessageAgent {
private model: ChatOpenAI;
private chain: RefineDocumentsChain;
private prompt: string;
constructor(
modelName: string,
temperature: number,
maxTokens: number,
prompt?: string,
) {
this.model = new ChatOpenAI({
modelName: modelName,
temperature: temperature,
maxTokens: maxTokens,
});
this.chain = loadQARefineChain(this.model);
this.prompt = prompt
? Deno.readTextFileSync(prompt).toString()
: DEFAULT_PROMPT;
}
public async generateCommitMessages(
diffDocuments: Document[],
): Promise<string[]> {
const response = await this.chain.call({
question: this.prompt,
input_documents: diffDocuments,
});
return response.output_text.split("\n").map(this.trimMessage);
}
public async thinkCommitMessages() {
const diffDocuments = await loadDiffFromGit();
if (diffDocuments.length === 0) {
return [];
}
return await this.generateCommitMessages(diffDocuments);
}
private trimMessage(message: string): string {
return message.trim().replace(/^-+\s*/, "");
}
}