|
1 | | -import { |
2 | | - IHttp, |
3 | | - IModify, |
4 | | - IRead, |
5 | | -} from "@rocket.chat/apps-engine/definition/accessors"; |
6 | | -import { |
7 | | - ISlashCommand, |
8 | | - SlashCommandContext, |
9 | | -} from "@rocket.chat/apps-engine/definition/slashcommands"; |
| 1 | +import { IHttp, IModify, IRead } from "@rocket.chat/apps-engine/definition/accessors" |
| 2 | +import { ISlashCommand, SlashCommandContext } from "@rocket.chat/apps-engine/definition/slashcommands" |
10 | 3 |
|
11 | | -import { PromptFactory } from "../core/prompt.factory"; |
12 | | -import { Query } from "../core/query"; |
13 | | -import { Neo4j } from "../core/services/db/neo4j"; |
14 | | -import { MiniLML6 } from "../core/services/embeddings/minilml6"; |
15 | | -import { Llama3_70B } from "../core/services/llm/llama3_70B"; |
16 | | -import { handleCommandResponse } from "../utils/handleCommandResponse"; |
| 4 | +import { PromptFactory } from "../core/prompt.factory" |
| 5 | +import { Query } from "../core/query" |
| 6 | +import { Neo4j } from "../core/services/db/neo4j" |
| 7 | +import { MiniLML6 } from "../core/services/embeddings/minilml6" |
| 8 | +import { Llama3_70B } from "../core/services/llm/llama3_70B" |
| 9 | +import { handleCommandResponse } from "../utils/handleCommandResponse" |
17 | 10 |
|
18 | 11 | export class AskCodeCommand implements ISlashCommand { |
19 | | - public command = "rcc-askcode"; |
20 | | - public i18nParamsExample = ""; |
21 | | - public i18nDescription = ""; |
22 | | - public providesPreview = false; |
| 12 | + public command = "rcc-askcode" |
| 13 | + public i18nParamsExample = "" |
| 14 | + public i18nDescription = "" |
| 15 | + public providesPreview = false |
23 | 16 |
|
24 | | - /** |
25 | | - * Processes the user's query and returns the answer. |
26 | | - * |
27 | | - * @param {IHttp} http - The HTTP object used for making requests. |
28 | | - * @param {string} query - The user's query. |
29 | | - * @returns {Promise<string | null>} A promise that resolves to the response to be given to the user or `null` if no answer or no reference is found. |
30 | | - */ |
31 | | - private async process(http: IHttp, query: string): Promise<string | null> { |
32 | | - const db = new Neo4j(http); |
33 | | - const llm = new Llama3_70B(http); |
34 | | - const embeddingModel = new MiniLML6(http); |
| 17 | + /** |
| 18 | + * Processes the user's query and returns the answer. |
| 19 | + * |
| 20 | + * @param {IHttp} http - The HTTP object used for making requests. |
| 21 | + * @param {string} query - The user's query. |
| 22 | + * @returns {Promise<string | null>} A promise that resolves to the response to be given to the user or `null` if no answer or no reference is found. |
| 23 | + */ |
| 24 | + private async process(http: IHttp, query: string): Promise<string | null> { |
| 25 | + const db = new Neo4j(http) |
| 26 | + const llm = new Llama3_70B(http) |
| 27 | + const embeddingModel = new MiniLML6(http) |
35 | 28 |
|
36 | | - /** |
37 | | - * --------------------------------------------------------------------------------------------- |
38 | | - * STEP 1: |
39 | | - * Extract the possible keywords from the user's query |
40 | | - * --------------------------------------------------------------------------------------------- |
41 | | - */ |
42 | | - const keywords = await Query.getDBKeywordsFromQuery(llm, query); |
43 | | - if (!keywords.length) return null; |
| 29 | + /** |
| 30 | + * --------------------------------------------------------------------------------------------- |
| 31 | + * STEP 1: |
| 32 | + * Extract the possible keywords from the user's query |
| 33 | + * --------------------------------------------------------------------------------------------- |
| 34 | + */ |
| 35 | + const keywords = await Query.getDBKeywordsFromQuery(llm, query) |
| 36 | + if (!keywords.length) return null |
44 | 37 |
|
45 | | - /** |
46 | | - * --------------------------------------------------------------------------------------------- |
47 | | - * STEP 2: |
48 | | - * Query the database to find the nodes names of which are similar to what user has requested |
49 | | - * --------------------------------------------------------------------------------------------- |
50 | | - */ |
51 | | - const results = await Query.getCodeNodesFromKeywords( |
52 | | - db, |
53 | | - embeddingModel, |
54 | | - keywords |
55 | | - ); |
56 | | - if (!results.length) return null; |
| 38 | + /** |
| 39 | + * --------------------------------------------------------------------------------------------- |
| 40 | + * STEP 2: |
| 41 | + * Query the database to find the nodes names of which are similar to what user has requested |
| 42 | + * --------------------------------------------------------------------------------------------- |
| 43 | + */ |
| 44 | + const results = await Query.getCodeNodesFromKeywords(db, embeddingModel, keywords) |
| 45 | + if (!results.length) return null |
57 | 46 |
|
58 | | - /** |
59 | | - * --------------------------------------------------------------------------------------------- |
60 | | - * STEP 3: |
61 | | - * Generate the answer and diagram for the user's query given the nodes data |
62 | | - * --------------------------------------------------------------------------------------------- |
63 | | - */ |
64 | | - const answer = await llm.ask( |
65 | | - PromptFactory.makeAskCodePrompt( |
66 | | - results.map((x) => x.code).join("\n\n"), |
67 | | - query |
68 | | - ) |
69 | | - ); |
70 | | - if (!answer) return null; |
| 47 | + /** |
| 48 | + * --------------------------------------------------------------------------------------------- |
| 49 | + * STEP 3: |
| 50 | + * Generate the answer and diagram for the user's query given the nodes data |
| 51 | + * --------------------------------------------------------------------------------------------- |
| 52 | + */ |
| 53 | + const answer = await llm.ask( |
| 54 | + PromptFactory.makeAskCodePrompt(results.map((x) => x.code).join("\n\n"), query) |
| 55 | + ) |
| 56 | + if (!answer) return null |
71 | 57 |
|
72 | | - return answer; |
73 | | - } |
| 58 | + return answer |
| 59 | + } |
74 | 60 |
|
75 | | - /** |
76 | | - * Executes the AskCodeCommand. |
77 | | - * |
78 | | - * @param context - The SlashCommandContext object. |
79 | | - * @param read - The IRead object. |
80 | | - * @param modify - The IModify object. |
81 | | - * @param http - The IHttp object. |
82 | | - * @returns A Promise that resolves to void. |
83 | | - */ |
84 | | - public async executor( |
85 | | - context: SlashCommandContext, |
86 | | - read: IRead, |
87 | | - modify: IModify, |
88 | | - http: IHttp |
89 | | - ): Promise<void> { |
90 | | - const query = context.getArguments().join(" "); |
91 | | - if (!query) return; |
| 61 | + /** |
| 62 | + * Executes the AskCodeCommand. |
| 63 | + * |
| 64 | + * @param context - The SlashCommandContext object. |
| 65 | + * @param read - The IRead object. |
| 66 | + * @param modify - The IModify object. |
| 67 | + * @param http - The IHttp object. |
| 68 | + * @returns A Promise that resolves to void. |
| 69 | + */ |
| 70 | + public async executor( |
| 71 | + context: SlashCommandContext, |
| 72 | + read: IRead, |
| 73 | + modify: IModify, |
| 74 | + http: IHttp |
| 75 | + ): Promise<void> { |
| 76 | + const query = context.getArguments().join(" ") |
| 77 | + if (!query) return |
92 | 78 |
|
93 | | - const sendEditedMessage = await handleCommandResponse( |
94 | | - query, |
95 | | - context.getSender(), |
96 | | - context.getRoom(), |
97 | | - modify, |
98 | | - this.command |
99 | | - ); |
| 79 | + const sendEditedMessage = await handleCommandResponse( |
| 80 | + query, |
| 81 | + context.getSender(), |
| 82 | + context.getRoom(), |
| 83 | + modify, |
| 84 | + this.command |
| 85 | + ) |
100 | 86 |
|
101 | | - const res = await this.process(http, query); |
102 | | - if (res) { |
103 | | - await sendEditedMessage(res); |
104 | | - } else { |
105 | | - await sendEditedMessage("❌ Unable to process your query"); |
106 | | - } |
107 | | - } |
| 87 | + const res = await this.process(http, query) |
| 88 | + if (res) { |
| 89 | + await sendEditedMessage(res) |
| 90 | + } else { |
| 91 | + await sendEditedMessage("❌ Unable to process your query") |
| 92 | + } |
| 93 | + } |
108 | 94 | } |
0 commit comments