Skip to content

Commit b9a805f

Browse files
committed
Minor changes
2 parents 8ead1fd + 4d2915c commit b9a805f

File tree

16 files changed

+195
-210
lines changed

16 files changed

+195
-210
lines changed

ai-assistant/src/commands/AskCodeCommand.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class AskCodeCommand implements ISlashCommand {
2121
* @param {string} query - The user's query.
2222
* @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.
2323
*/
24-
private async process(http: IHttp, query: string): Promise<string | null> {
24+
private async process(http: IHttp, query: string): Promise<string> {
2525
const db = new Neo4j(http)
2626
const llm = new Llama3_70B(http)
2727
const embeddingModel = new MiniLML6(http)
@@ -33,7 +33,7 @@ export class AskCodeCommand implements ISlashCommand {
3333
* ---------------------------------------------------------------------------------------------
3434
*/
3535
const keywords = await Query.getDBKeywordsFromQuery(llm, query)
36-
if (!keywords.length) return null
36+
if (!keywords.length) return "I'm sorry, I couldn't understand your query. Please try again."
3737

3838
/**
3939
* ---------------------------------------------------------------------------------------------
@@ -42,7 +42,7 @@ export class AskCodeCommand implements ISlashCommand {
4242
* ---------------------------------------------------------------------------------------------
4343
*/
4444
const results = await Query.getCodeNodesFromKeywords(db, embeddingModel, keywords)
45-
if (!results.length) return null
45+
if (!results.length) return "I'm sorry, I couldn't find any code related to your query."
4646

4747
/**
4848
* ---------------------------------------------------------------------------------------------
@@ -53,7 +53,7 @@ export class AskCodeCommand implements ISlashCommand {
5353
const answer = await llm.ask(
5454
PromptFactory.makeAskCodePrompt(results.map((x) => x.code).join("\n\n"), query)
5555
)
56-
if (!answer) return null
56+
if (!answer) return "I'm sorry, I'm having trouble connecting to the server. Please try again later."
5757

5858
return answer
5959
}
@@ -85,10 +85,6 @@ export class AskCodeCommand implements ISlashCommand {
8585
)
8686

8787
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-
}
88+
await sendEditedMessage(res)
9389
}
9490
}

ai-assistant/src/commands/AskDocsCommand.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class AskDocsCommand implements ISlashCommand {
2121
* @param {string} query - The user's query.
2222
* @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.
2323
*/
24-
private async process(http: IHttp, query: string): Promise<string | null> {
24+
private async process(http: IHttp, query: string): Promise<string> {
2525
const db = new Neo4j(http)
2626
const llm = new Llama3_70B(http)
2727
const embeddingModel = new MiniLML6(http)
@@ -33,7 +33,7 @@ export class AskDocsCommand implements ISlashCommand {
3333
* ---------------------------------------------------------------------------------------------
3434
*/
3535
const results = await Query.getDocsNodesFromQuery(db, embeddingModel, query)
36-
if (!results.length) return null
36+
if (!results.length) return "I'm sorry, I couldn't find any documentation related to your query."
3737

3838
/**
3939
* ---------------------------------------------------------------------------------------------
@@ -45,7 +45,7 @@ export class AskDocsCommand implements ISlashCommand {
4545
const answer = await llm.ask(
4646
PromptFactory.makeAskDocsPrompt(results.map((x) => x.content).join("\n\n"), uniqueSources, query)
4747
)
48-
if (!answer) return null
48+
if (!answer) return "I'm sorry, I'm having trouble connecting to the server. Please try again later."
4949

5050
return answer
5151
}
@@ -68,10 +68,6 @@ export class AskDocsCommand implements ISlashCommand {
6868
)
6969

7070
const res = await this.process(http, query)
71-
if (res) {
72-
await sendEditedMessage(res)
73-
} else {
74-
await sendEditedMessage("❌ Unable to process your query")
75-
}
71+
await sendEditedMessage(res)
7672
}
7773
}

ai-assistant/src/commands/DiagramCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ export class DiagramCommand implements ISlashCommand {
4747
const diagram = await llm.ask(
4848
PromptFactory.makeDiagramPrompt(results.map((x) => x.code).join("\n\n"), query)
4949
)
50+
console.log(diagram)
5051
if (!diagram) return null
5152

52-
// @ts-ignore
5353
const diagramContent = diagram
5454
.replace("```mermaid", "")
5555
.replace("```", "")

ai-assistant/src/commands/DocumentCommand.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class DocumentCommand implements ISlashCommand {
2323
private async process(
2424
http: IHttp,
2525
query: string
26-
): Promise<{ jsDoc: string; explanation: string | null } | null> {
26+
): Promise<{ jsDoc: string; explanation: string | null } | string> {
2727
const db = new Neo4j(http)
2828
const llm = new Llama3_70B(http)
2929
const embeddingModel = new MiniLML6(http)
@@ -35,7 +35,7 @@ export class DocumentCommand implements ISlashCommand {
3535
* ---------------------------------------------------------------------------------------------
3636
*/
3737
const keywords = await Query.getDBKeywordsFromQuery(llm, query)
38-
if (!keywords.length) return null
38+
if (!keywords.length) return "I'm sorry, I couldn't understand your query. Please try again."
3939

4040
/**
4141
* ---------------------------------------------------------------------------------------------
@@ -44,7 +44,7 @@ export class DocumentCommand implements ISlashCommand {
4444
* ---------------------------------------------------------------------------------------------
4545
*/
4646
const codeNodes = await Query.getCodeNodesFromKeywords(db, embeddingModel, keywords)
47-
if (!codeNodes.length) return null
47+
if (!codeNodes.length) return "I'm sorry, I couldn't find any code related to your query."
4848

4949
/**
5050
* ---------------------------------------------------------------------------------------------
@@ -53,7 +53,7 @@ export class DocumentCommand implements ISlashCommand {
5353
* ---------------------------------------------------------------------------------------------
5454
*/
5555
const result = await llm.ask(PromptFactory.makeDocumentPrompt(JSON.stringify(codeNodes), query))
56-
if (!result) return null
56+
if (!result) return "I'm sorry, I couldn't generate documentation for your query."
5757

5858
//@ts-ignore
5959
const jsDoc = result.split("<JSDOC_START>")[1].split("<JSDOC_END>")[0].trim()
@@ -93,10 +93,10 @@ export class DocumentCommand implements ISlashCommand {
9393
)
9494

9595
let res = await this.process(http, query)
96-
if (res) {
97-
await sendEditedMessage(`${res.jsDoc}\n\n${res.explanation}`)
96+
if (typeof res === "string") {
97+
await sendEditedMessage(res)
9898
} else {
99-
await sendEditedMessage("❌ No references found!")
99+
await sendEditedMessage(`${res.jsDoc}\n\n${res.explanation}`)
100100
}
101101
}
102102
}

ai-assistant/src/commands/TranslateCommand.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class TranslateCommand implements ISlashCommand {
2121
* @param {string} targetEntity - The target entity for translation.
2222
* @returns {Promise<string | null>} A promise that resolves to the translated result or null if no translation is found.
2323
*/
24-
private async process(http: IHttp, targetLanguage: string, targetEntity: string): Promise<string | null> {
24+
private async process(http: IHttp, targetLanguage: string, targetEntity: string): Promise<string> {
2525
const db = new Neo4j(http)
2626
const llm = new Llama3_70B(http)
2727
const embeddingModel = new MiniLML6(http)
@@ -33,7 +33,7 @@ export class TranslateCommand implements ISlashCommand {
3333
* ---------------------------------------------------------------------------------------------
3434
*/
3535
const codeNodes = await Query.getCodeNodesFromKeywords(db, embeddingModel, [targetEntity])
36-
if (!codeNodes.length) return null
36+
if (!codeNodes.length) return "I'm sorry, I couldn't find any code related to your query."
3737

3838
/**
3939
* ---------------------------------------------------------------------------------------------
@@ -48,7 +48,7 @@ export class TranslateCommand implements ISlashCommand {
4848
targetLanguage
4949
)
5050
)
51-
if (!res) return null
51+
if (!res) return "I'm sorry, I'm having trouble connecting to the server. Please try again later."
5252

5353
return res
5454
}
@@ -85,10 +85,6 @@ export class TranslateCommand implements ISlashCommand {
8585
)
8686

8787
const res = await this.process(http, targetEntity, targetLanguage)
88-
if (res) {
89-
await sendEditedMessage(res)
90-
} else {
91-
await sendEditedMessage("❌ Translation failed")
92-
}
88+
await sendEditedMessage(res)
9389
}
9490
}

ai-assistant/src/commands/WhyUsedCommand.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Query } from "../core/query"
77
import { MiniLML6 } from "../core/services/embeddings/minilml6"
88
import { Llama3_70B } from "../core/services/llm/llama3_70B"
99
import { handleCommandResponse } from "../utils/handleCommandResponse"
10+
import { renderDiagramToBase64URI } from "../core/diagram"
1011

1112
export class WhyUsedCommand implements ISlashCommand {
1213
public command = "rcc-whyused"
@@ -24,10 +25,13 @@ export class WhyUsedCommand implements ISlashCommand {
2425
private async process(
2526
http: IHttp,
2627
query: string
27-
): Promise<{
28-
explanation: string
29-
diagram: string
30-
} | null> {
28+
): Promise<
29+
| {
30+
explanation: string
31+
diagram: string
32+
}
33+
| string
34+
> {
3135
const db = new Neo4j(http)
3236
const llm = new Llama3_70B(http)
3337
const embeddingModel = new MiniLML6(http)
@@ -39,7 +43,7 @@ export class WhyUsedCommand implements ISlashCommand {
3943
* ---------------------------------------------------------------------------------------------
4044
*/
4145
const keywords = await Query.getDBKeywordsFromQuery(llm, query)
42-
if (!keywords.length) return null
46+
if (!keywords.length) return "I'm sorry, I couldn't understand your query. Please try again."
4347

4448
/**
4549
* ---------------------------------------------------------------------------------------------
@@ -48,7 +52,7 @@ export class WhyUsedCommand implements ISlashCommand {
4852
* ---------------------------------------------------------------------------------------------
4953
*/
5054
const codeNodes = await Query.getCodeNodesFromKeywords(db, embeddingModel, keywords)
51-
if (!codeNodes.length) return null
55+
if (!codeNodes.length) return "I'm sorry, I couldn't find any code related to your query."
5256

5357
/**
5458
* ---------------------------------------------------------------------------------------------
@@ -59,7 +63,7 @@ export class WhyUsedCommand implements ISlashCommand {
5963
const result = await llm.ask(
6064
PromptFactory.makeWhyUsedPrompt(codeNodes.map((x) => x.code).join("\n\n"), query)
6165
)
62-
if (!result) return null
66+
if (!result) return "I'm sorry, I couldn't find any references for your query."
6367

6468
const explanation = result.split("<EXPLANATION>")[1].split("</EXPLANATION>")[0].trim()
6569
const diagram = result.split("<DIAGRAM>")[1].split("</DIAGRAM>")[0].trim()
@@ -71,17 +75,12 @@ export class WhyUsedCommand implements ISlashCommand {
7175
* ---------------------------------------------------------------------------------------------
7276
*/
7377
const data = { explanation, diagram: "" }
74-
// TODO:
75-
// if (diagram) {
76-
// const parsedDiagram = diagram
77-
// .replace("```mermaid", "")
78-
// .replace("```", "")
79-
// .trim();
80-
// writeFileSync("output.txt", parsedDiagram);
81-
// try {
82-
// // data.diagram = await renderDiagramToBase64URI(parsedDiagram);
83-
// } catch {}
84-
// }
78+
if (diagram) {
79+
const parsedDiagram = diagram.replace("```mermaid", "").replace("```", "").trim()
80+
try {
81+
data.diagram = await renderDiagramToBase64URI(http, parsedDiagram)
82+
} catch {}
83+
}
8584

8685
return data
8786
}
@@ -113,11 +112,10 @@ export class WhyUsedCommand implements ISlashCommand {
113112
)
114113

115114
const res = await this.process(http, query)
116-
if (!res) {
117-
await sendEditedMessage("❌ No references found!")
115+
if (typeof res === "string") {
116+
await sendEditedMessage(res)
118117
return
119118
}
120-
121119
await sendEditedMessage(res.explanation, [res.diagram!])
122120
}
123121
}

ai-assistant/src/core/query.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ export namespace Query {
2727
const result = await db.run(
2828
`
2929
CALL db.index.vector.queryNodes("${indexName}", 2, $vector)
30-
YIELD node, score
31-
WHERE score >= ${threshold}
32-
WITH node, score
33-
OPTIONAL MATCH (node)-[r]->(relatedNode)
34-
RETURN node, COLLECT(relatedNode) AS relatedNodes, score
35-
ORDER BY score DESC
30+
YIELD node, score
31+
WHERE score >= ${threshold}
32+
WITH node, score
33+
OPTIONAL MATCH (node)-[r]->(relatedNode)
34+
RETURN node, COLLECT(relatedNode) AS relatedNodes, score
35+
ORDER BY score DESC
3636
`,
3737
{ vector }
3838
)
@@ -67,13 +67,16 @@ export namespace Query {
6767
keywords: string[]
6868
): Promise<DBNode[]> {
6969
const results: DBNode[] = []
70-
for (const keyword of keywords) {
71-
const queryVector = await embeddingModel.generate(keyword)
72-
if (!queryVector) continue
7370

74-
const result = await getDBNodesFromVectorQuery(db, "nameEmbeddings", queryVector, 0.85)
75-
results.push(...result)
76-
}
71+
try {
72+
for (const keyword of keywords) {
73+
const queryVector = await embeddingModel.generate(keyword)
74+
if (!queryVector) continue
75+
76+
const result = await getDBNodesFromVectorQuery(db, "nameEmbeddings", queryVector, 0.85)
77+
results.push(...result)
78+
}
79+
} catch {}
7780

7881
return results
7982
}

ai-assistant/src/core/services/db/neo4j.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ export class Neo4j implements IDB {
3333
// password: string
3434
) {
3535
this.http = http
36+
3637
this.baseUrl = "http://neo4j:7474"
3738
this.username = "neo4j"
3839
this.password = "strongpasswordsafe123"
39-
// this.baseUrl = "http://44.192.104.170:7474";
40-
// this.username = "neo4j";
41-
// this.password = "individuals-societies-wools";
40+
41+
// this.baseUrl = "http://44.192.104.170:7474"
42+
// this.username = "neo4j"
43+
// this.password = "individuals-societies-wools"
4244
}
4345

4446
/**

0 commit comments

Comments
 (0)