Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(husky-v0): added directory info for husky context #2070

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions apps/web-api/src/husky/husky-ai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import Handlebars from 'handlebars';
@Injectable()
export class HuskyAiService {
constructor(
private logger: LogService,
private huskyVectorDbService: QdrantVectorDbService,
private huskyCacheDbService: RedisCacheDbService,
private huskyPersistentDbService: MongoPersistantDbService
Expand All @@ -48,11 +47,12 @@ export class HuskyAiService {
// Rephrase the question and get the matching documents to create context
const rephrasedQuestion = await this.getRephrasedQuestionBasedOnHistory(uid, question);
const questionEmbedding = await this.getEmbeddingForText(rephrasedQuestion);
const [matchingDocs, actionDocs] = await Promise.all([
const [nonDirectoryDocs, directoryDocs] = await Promise.all([
this.getMatchingEmbeddingsBySource(source, questionEmbedding),
this.getActionDocs(questionEmbedding),
]);
const context = this.createContextForMatchingDocs(matchingDocs);

const context = this.createContextWithMatchedDocs(nonDirectoryDocs, directoryDocs);

// Handle the case when there is no context
if (context === '') {
Expand All @@ -68,7 +68,7 @@ export class HuskyAiService {

// If Context is valid, then create prompt and stream the response
const chatSummaryFromDb = await this.huskyCacheDbService.get(`${uid}:summary`);
const prompt = this.createPromptForHuskyChat(question, context, chatSummaryFromDb || '', actionDocs);
const prompt = this.createPromptForHuskyChat(question, context, chatSummaryFromDb || '', directoryDocs);
return streamObject({
model: openai(process.env.OPENAI_LLM_MODEL || ''),
schema: HuskyResponseSchema,
Expand Down Expand Up @@ -190,26 +190,37 @@ export class HuskyAiService {
return this.huskyVectorDbService.searchEmbeddings(collection, embedding, limit, true);
}

createContextForMatchingDocs(matchingDocs: any[]) {
const formattedResults: any[] = [];
for (const result of matchingDocs) {
formattedResults.push({
score: result.score,
text: result?.payload?.page_content ?? '',
source: result?.payload?.metadata?.url ?? '',
createContextWithMatchedDocs(nonDirectoryDocs: any[], directoryDocs: any) {
let allDocs: any[] = [];
const actionDocKeys = ['memberDocs', 'teamDocs', 'projectDocs'];

actionDocKeys.forEach((key: string) => {
const docs = [...directoryDocs[key]].map((doc: any) => {
return {
text: doc?.info,
score: doc?.score,
source: doc?.directoryLink,
};
});
}
allDocs = [...allDocs, ...docs];
});

const sortedResults = formattedResults
.sort((a, b) => b?.score - a?.score)
.filter((v) => v.score > 0.4)
.slice(0, 10);
const formattedNonDictoryDocs = [...nonDirectoryDocs].map((doc: any) => {
return {
score: doc.score,
text: doc?.payload?.page_content ?? '',
source: doc?.payload?.metadata?.url ?? '',
};
});

const context = sortedResults
.filter((result) => result?.text?.length > 5)
allDocs = [...allDocs, ...formattedNonDictoryDocs];

return allDocs
.sort((a, b) => b?.score - a?.score)
.filter((v) => v.score > 0.45 && v?.text?.length > 5)
.slice(0, 10)
.map((result) => `${result?.text}${result?.source ? ` (Source:${result?.source})` : ''}`)
.join('\n');
return context;
}

createPromptForHuskyChat(question: string, context: string, chatSummary: string, allDocs: any) {
Expand Down
19 changes: 2 additions & 17 deletions apps/web-api/src/husky/husky.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,12 @@ import { HuskyController } from './husky.controller';
import { RedisCacheDbService } from './db/redis-cache-db.service';
import { QdrantVectorDbService } from './db/qdrant-vector-db.service';
import { MongoPersistantDbService } from './db/mongo-persistant-db.service';
import { Neo4jGraphDbService } from './db/neo4j-graph-db.service';
import { HuskyAiService } from './husky-ai.service';

@Module({
controllers: [HuskyController],
providers: [
HuskyService,
HuskyAiService,
RedisCacheDbService,
QdrantVectorDbService,
MongoPersistantDbService,
Neo4jGraphDbService,
],
providers: [HuskyService, HuskyAiService, RedisCacheDbService, QdrantVectorDbService, MongoPersistantDbService],
imports: [],
exports: [
HuskyService,
HuskyAiService,
RedisCacheDbService,
QdrantVectorDbService,
MongoPersistantDbService,
Neo4jGraphDbService,
],
exports: [HuskyService, HuskyAiService, RedisCacheDbService, QdrantVectorDbService, MongoPersistantDbService],
})
export class HuskyModule {}
Loading