-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
187 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...-ai/lang-chain/lang-chain.service.spec.ts → ...rvice/langchain/langchain.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
apps/yggdrasil-core-engine/src/app/llm-ai/service/llmai/llmai.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { LLMAIService } from './llmai.service'; | ||
import { AsgardLoggerSupplement } from '@asgard-hub/nest-winston'; | ||
import { LangChainService } from '../langchain/langchain.service'; | ||
|
||
describe('LlmaiService', () => { | ||
let service: LLMAIService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
{ | ||
provide: AsgardLoggerSupplement.LOGGER_HELPER_SERVICE, | ||
useValue: {}, | ||
}, | ||
{ | ||
provide: LangChainService, | ||
useValue: {}, | ||
}, | ||
LLMAIService, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<LLMAIService>(LLMAIService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
apps/yggdrasil-core-engine/src/app/llm-ai/service/llmai/llmai.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { FunctionSelection } from '@asgard-hub/utils'; | ||
import { AsgardLogger, AsgardLoggerSupplement } from '@asgard-hub/nest-winston'; | ||
import { LangChainService } from '../langchain/langchain.service'; | ||
import { ChatOptions } from '../../../controllers/llm-ai/llm-ai.controller'; | ||
import { TranslateToEnglishPrompt } from '../../prompt/translate-to-english.prompt'; | ||
import { Subject, of, switchMap } from 'rxjs'; | ||
import { randomUUID } from 'crypto'; | ||
|
||
export interface GrpcResponse<T = unknown> { | ||
id: string; | ||
timestamp: number; | ||
uuid: string; | ||
event: string; | ||
data: string | T; | ||
} | ||
|
||
@Injectable() | ||
export class LLMAIService { | ||
@Inject(AsgardLoggerSupplement.LOGGER_HELPER_SERVICE) | ||
private readonly asgardLogger: AsgardLogger; | ||
@Inject() | ||
private readonly langChainService: LangChainService; | ||
chat(data: ChatOptions, subject?: Subject<GrpcResponse>) { | ||
return of(this.promptSelect(data.key)).pipe( | ||
switchMap((prompt) => { | ||
subject?.next({ | ||
id: 'prompt', | ||
timestamp: Date.now(), | ||
uuid: randomUUID(), | ||
event: 'notification', | ||
data: prompt?.name ?? FunctionSelection.generalChat, | ||
}); | ||
return of(prompt); | ||
}), | ||
switchMap(async (type) => { | ||
if (!type) { | ||
return await this.langChainService.getGeneralChatResponse( | ||
data.userInput | ||
); | ||
} else { | ||
return await this.langChainService.getFunctionChainResponse( | ||
data.userInput, | ||
type | ||
); | ||
} | ||
}), | ||
switchMap((response) => { | ||
subject?.next({ | ||
id: 'response', | ||
timestamp: Date.now(), | ||
uuid: randomUUID(), | ||
event: 'message', | ||
data: JSON.stringify(response), | ||
}); | ||
this.asgardLogger.debug(response); | ||
return of(subject.complete()); | ||
}) | ||
); | ||
} | ||
|
||
private promptSelect(key: FunctionSelection) { | ||
switch (key) { | ||
case FunctionSelection.generalChat: | ||
return undefined; | ||
case FunctionSelection.anyTranslateToEnglish: | ||
return TranslateToEnglishPrompt; | ||
default: | ||
return undefined; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.