-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from henriqueweiand/ai-module
Ai module
- Loading branch information
Showing
26 changed files
with
605 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
DATABASE_URL="postgresql://docker:docker@localhost:5432/project?schema=public" | ||
PORT=3000 | ||
PORT=3000 | ||
OPENAI_API_KEY= |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Debug Nest Framework", | ||
"args": [ | ||
"${workspaceFolder}/src/main.ts" | ||
], | ||
"runtimeArgs": [ | ||
"--nolazy", | ||
"-r", | ||
"ts-node/register", | ||
"-r", | ||
"tsconfig-paths/register" | ||
], | ||
"sourceMaps": true, | ||
"restart": true, | ||
"envFile": "${workspaceFolder}/.env", | ||
"cwd": "${workspaceRoot}", | ||
"console": "integratedTerminal", | ||
"protocol": "inspector" | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -1,7 +1,76 @@ | ||
# nestjs-generate-questions | ||
|
||
- [x] Base library with database and repository | ||
- [x] Module user with one endpoint | ||
- [x] Apply unit-tests for all parts of the library | ||
- [x] Apply unit-tests for user module | ||
- [x] Apply e2e-tests for user module | ||
This repository houses a question generation tool, enabling users to input a text, from which it generates multiple questions for selection, with one of them being the correct answer. | ||
|
||
Common libraries | ||
|
||
- [x] Base library with database and user repository | ||
- [x] unit-tests for all parts of the library | ||
- [x] Questions and answers repositories | ||
- [x] Swagger (http://localhost:3000/api) | ||
|
||
User module | ||
|
||
- [x] Module | ||
- [x] Use-case | ||
- [x] unit-tests | ||
- [x] e2e-tests | ||
|
||
Question and answers module | ||
|
||
- [x] Module | ||
- [x] Use-case | ||
- [x] unit-tests | ||
|
||
To-do | ||
|
||
- [ ] Review unit-test of questions create | ||
- [ ] Review e2e-test of questions create | ||
|
||
### How to use | ||
|
||
1. Configure .env file | ||
2. Create an user by using POST /user | ||
3. Create an question using POST /question | ||
|
||
Example of POST /question response | ||
|
||
``` | ||
{ | ||
"id": "2c9218cd-99c7-49f7-990a-381983017112", | ||
"content": "When Britney Spears' much-hyped autobiography, The Woman in Me, went on sale in October, it instantly became the bestselling book on Amazon, thanks to presales from eager fans. This week it debuted at number one on the New York Times Bestsellers List (memoirs from actors John Stamos and Jada Pinkett Smith also earned spots on the coveted list). In her book, Spears recounts, with a kind of fevered urgency, her rise from small town Louisiana to mega-stardom. Even before she gets to the subject of the infamous 2008 court-ordered conservatorship, which gave Spears's father power over her finances and daily life, the reader feels intimately familiar with the singer. Some of the more salacious details from the book made their way into the media in the weeks before the memoir's publication: the painful at-home abortion endured as her then-partner, the singer and actor Justin Timberlake strummed an acoustic guitar; the night she shaved her head in front of a throng of camera-toting paparazzi; even the menacing hiss of the giant snake she bore aloft at the 2001 MTV Video Music Awards. ", | ||
"createdAt": "2023-11-03T21:17:35.455Z", | ||
"updatedAt": "2023-11-03T21:17:35.455Z", | ||
"authorId": "f82828f7-ba96-443c-b45a-90d4803f4111", | ||
"answers": [ | ||
{ | ||
"id": "e4b227e9-0472-4417-b0a3-78bd7486a02c", | ||
"content": "The Woman in Me debuted at number one on the New York Times Bestsellers List.", | ||
"createdAt": "2023-11-03T21:17:35.465Z", | ||
"updatedAt": "2023-11-03T21:17:35.465Z", | ||
"questionId": "2c9218cd-99c7-49f7-990a-381983017112", | ||
"correct": true | ||
}, | ||
{ | ||
"id": "232f236d-688f-4a28-bc5c-ba942275dbb2", | ||
"content": "The book instantly became the bestselling book on Amazon due to presales from eager fans.", | ||
"createdAt": "2023-11-03T21:17:35.471Z", | ||
"updatedAt": "2023-11-03T21:17:35.471Z", | ||
"questionId": "2c9218cd-99c7-49f7-990a-381983017112", | ||
"correct": false | ||
}, | ||
{ | ||
"id": "06133b15-b6f7-471b-b502-50032772cb85", | ||
"content": "John Stamos and Jada Pinkett Smith also earned spots on the New York Times Bestsellers List with their memoirs.", | ||
"createdAt": "2023-11-03T21:17:35.475Z", | ||
"updatedAt": "2023-11-03T21:17:35.475Z", | ||
"questionId": "2c9218cd-99c7-49f7-990a-381983017112", | ||
"correct": false | ||
} | ||
] | ||
} | ||
``` | ||
|
||
### swagger | ||
|
||
![Preview](https://raw.githubusercontent.com/henriqueweiand/nestjs-generate-questions/main/assets/swagger.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Injectable, InternalServerErrorException, OnModuleInit } from '@nestjs/common'; | ||
import { ChatGPTAPI as IChatGPTAPI, ChatMessage } from 'chatgpt'; | ||
import { EnvService } from '../env'; | ||
import { AIChatGenerator } from './interface/ai-chat-generator'; | ||
|
||
@Injectable() | ||
export class AIChat implements AIChatGenerator, OnModuleInit { | ||
private API: IChatGPTAPI; | ||
|
||
constructor( | ||
private envService: EnvService) { | ||
} | ||
|
||
async onModuleInit() { | ||
const importDynamic = new Function('modulePath', 'return import(modulePath)') | ||
const { ChatGPTAPI } = await importDynamic('chatgpt') | ||
|
||
this.API = new ChatGPTAPI({ | ||
apiKey: this.envService.get('OPENAI_API_KEY'), | ||
}); | ||
} | ||
|
||
async ask(question: string): Promise<ChatMessage | null> { | ||
try { | ||
const response = await this.API.sendMessage(question); | ||
return response; | ||
} catch (e) { | ||
throw new InternalServerErrorException('Was not possible to generate the answers'); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,16 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AIChatGenerator } from './interface/ai-chat-generator'; | ||
import { AIChat } from './ai-chat'; | ||
import { EnvModule } from '../env'; | ||
|
||
@Module({ | ||
imports: [EnvModule], | ||
providers: [ | ||
{ | ||
provide: AIChatGenerator, | ||
useClass: AIChat | ||
}, | ||
], | ||
exports: [AIChatGenerator], | ||
}) | ||
export class AIModule { } |
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,3 @@ | ||
export * from './ai.module'; | ||
export * from './ai-chat'; | ||
export * from './interface/ai-chat-generator'; |
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,5 @@ | ||
import { ChatMessage } from "chatgpt"; | ||
|
||
export abstract class AIChatGenerator { | ||
abstract ask(question: string): Promise<ChatMessage | null> | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './database.module'; | ||
export * from './repository/question.repositoy'; | ||
export * from './repository/answer.repositoy'; | ||
export * from './repository/user.repositoy'; | ||
export * from './prisma/prisma.service'; |
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './database'; | ||
export * from './env'; | ||
export * from './AI'; |
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.