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

Aletheia Chat bot Copilot #1187

Merged
merged 25 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
268d95d
Create chat bot user interface to create an interactivity between the…
pepermao May 6, 2024
7e6ec73
Draft Chat Bot BackEnd:
caneppelevitor May 7, 2024
f7118f3
Create fetch tool that connects with the automated-fact-checking API
pepermao May 7, 2024
8f407f2
Integrate client side and server side
pepermao May 7, 2024
65dc921
Refactoring:
caneppelevitor May 7, 2024
3de4acb
Utilized the automated fact checking service function to make the api…
pepermao May 7, 2024
0875568
Refining Agent:
caneppelevitor May 8, 2024
c56dc06
Improved copilot chat bot user interface
pepermao May 7, 2024
524224f
Properly passing context to agentChat
caneppelevitor May 8, 2024
c0f53b0
- Created implment report button that apply the ai report into your own
pepermao May 9, 2024
0269afe
Refined agent responses with context
caneppelevitor May 9, 2024
6120e61
- Improved automated fact-checking api error handling
pepermao May 10, 2024
c7d831e
- Typescript improvements
pepermao May 10, 2024
2f70f0b
rename get positions file
pepermao May 10, 2024
133565f
Ensuring to pass date to the chat-bot conversation and readded langua…
caneppelevitor May 10, 2024
4d20180
- Used the agents api from the config file
pepermao May 10, 2024
e655df4
add warning that copilot chat bot is only visible in full page report
pepermao May 11, 2024
d76e3b1
Add feature flag in the copilot warning
pepermao May 11, 2024
e085d94
Fixed sonar cloud issues
pepermao May 11, 2024
8cfdd05
Removed optionals from tool schema and preventing the AI agent to ski…
caneppelevitor May 11, 2024
ddfe0f6
Removed console log, added guard verification on copilot endpoint and…
caneppelevitor May 11, 2024
7c0689b
Improve logging and debugging
thesocialdev May 11, 2024
8f8fb62
Merge branch 'mvp-copilot-chat-bot' of github.com:AletheiaFact/alethe…
thesocialdev May 11, 2024
214d32c
fixed cypress test
pepermao May 12, 2024
daca6b9
Add AGENTS_API_URL to the deployment script
thesocialdev May 13, 2024
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
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ AWS_ACCESS_KEY_ID=test
AWS_SECRET_ACCESS_KEY=test
AWS_ENDPOINT=http://localhost:4566

MONGO_MIGRATION_URL=
MONGO_MIGRATION_URL=

OPENAI_API_KEY=
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified .yarn/install-state.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
cors: '*'
debug: true
websocketUrl: ws://localhost:5001
agentsUrl: http://localhost:8000
automatedFactCheckingAPIUrl: AGENTS_API_URL
thesocialdev marked this conversation as resolved.
Show resolved Hide resolved
#override_public_routes: true
recaptcha_secret: RECAPTCHA_SECRET
recaptcha_sitekey: 6Lc2BtYUAAAAAOUBI-9r1sDJUIfG2nt6C43noOXh
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
"@dhaiwat10/react-link-preview": "^1.9.1",
"@emotion/react": "^11.10.4",
"@emotion/styled": "^11.10.4",
"@langchain/community": "^0.0.54",
"@langchain/openai": "^0.0.28",
"@mui/icons-material": "^5.10.9",
"@mui/material": "^5.10.13",
"@mui/x-data-grid": "^5.17.11",
Expand Down Expand Up @@ -102,6 +104,7 @@
"@typescript-eslint/eslint-plugin": "^4.29.0",
"@xstate/react": "^3.0.0",
"accept-language-parser": "^1.5.0",
"ai": "^3.1.1",
"antd": "^4.18.5",
"aws-sdk": "^2.1154.0",
"axios": "^1.5.0",
Expand All @@ -124,6 +127,7 @@
"jotai": "^1.9.2",
"jotai-xstate": "^0.3.0",
"js-cookie": "^3.0.1",
"langchain": "^0.1.36",
"lottie-web": "^5.10.1",
"md5": "^2.3.0",
"migrate-mongo-ts": "^1.1.4",
Expand Down
9 changes: 1 addition & 8 deletions public/locales/en/claimReviewForm.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,5 @@
"rejectionCommentLabel": "Rejection comment",
"rejectionCommentPlaceholder": "Describe what needs to be changed",
"loginButton": "Login to continue",
"notReviewed": "Not reviewed",
"addAgentReview": "Add AI review",
"agentInputText": "Fact-check the following claim: {{sentence}}, provide the answer in English.",
"addAgentReviewModalTitle": "Generating fact-checking report",
"agentLoadingThoughts": "Thinking...",
"agentFinishedThoughts": "Finished.",
"agentFinishedReport": "Report generated",
"submitAgentReview": "Submit generated report"
"notReviewed": "Not reviewed"
}
14 changes: 14 additions & 0 deletions public/locales/en/copilotChatBot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"chatBotGreetings": "Hello, I am the Aletheia fact-checker assistant.",
"Assistant": "Assistant",
"You": "You",
"suggestionHeader": "How can I help you ?",
"suggestion1": "I need help with my report",
"suggestion2": "List questions to be answered",
"inputPlaceholder": "Message aletheia assistant",
"addFactCheckingReportButton": "Add fact-check in my report.",
"footer": "Aletheia assistant can make mistakes. Consider checking important information.",
"agentLoadingThoughts": "Thinking",
"rateQuestion": "How would you rate this conversation ?",
"copilotWarning": "Assistant visible only in full page."
}
9 changes: 1 addition & 8 deletions public/locales/pt/claimReviewForm.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,5 @@
"rejectionCommentLabel": "Comentário de rejeição",
"rejectionCommentPlaceholder": "Descreva o que precisa ser alterado",
"loginButton": "Faça login para continuar",
"notReviewed": "Não revisado",
"addAgentReview": "Adicionar checagem por IA",
"agentInputText": "Faça a checagem da seguinte afirmação: {{sentence}} forneça a resposta em português",
"addAgentReviewModalTitle": "Gerando relatório de checagem",
"agentLoadingThoughts": "Pensando...",
"agentFinishedThoughts": "Finalizado.",
"agentFinishedReport": "Relatório gerado",
"submitAgentReview": "Enviar relatório gerado"
"notReviewed": "Não revisado"
}
14 changes: 14 additions & 0 deletions public/locales/pt/copilotChatBot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"chatBotGreetings": "Olá, eu sou checador de fatos assistente da Aletheia.",
"Assistant": "Assistente",
"You": "Você",
"suggestionHeader": "Como posso te ajudar ?",
"suggestion1": "Preciso de ajuda com minha checagem",
"suggestion2": "Liste perguntas a serem respondidas",
"inputPlaceholder": "Escreva para o assistente da aletheia",
"addFactCheckingReportButton": "Adicionar checagem no meu relatorio.",
"footer": "Assistente Aletheia pode cometer erros. Considere checar informações importantes.",
"agentLoadingThoughts": "Pensando",
"rateQuestion": "Como você avaliaria esta conversa ?",
"copilotWarning": "Assistente apenas disponivel na página completa."
}
2 changes: 2 additions & 0 deletions server/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { CommentModule } from "./claim-review-task/comment/comment.module";
import { NameSpaceModule } from "./auth/name-space/name-space.module";
import { NameSpaceGuard } from "./auth/name-space/name-space.guard";
import { AutomatedFactCheckingModule } from "./automated-fact-checking/automated-fact-checking.module";
import { CopilotChatModule } from "./copilot/copilot-chat.module";

@Module({})
export class AppModule implements NestModule {
Expand Down Expand Up @@ -109,6 +110,7 @@ export class AppModule implements NestModule {
CommentModule,
NameSpaceModule,
AutomatedFactCheckingModule,
CopilotChatModule,
];
if (options.feature_flag) {
imports.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export class AutomatedFactCheckingController {
@ApiTags("automated-fact-checking")
@Post("api/ai-fact-checking")
@Header("Cache-Control", "no-cache")
async create(@Body() { sentence }: CreateAutomatedFactCheckingDTO) {
return this.automatedFactCheckingService.getResponseFromAgents(
sentence
);
async create(@Body() { claim, context }: CreateAutomatedFactCheckingDTO) {
return this.automatedFactCheckingService.getResponseFromAgents({
claim,
context,
});
}
}
60 changes: 24 additions & 36 deletions server/automated-fact-checking/automated-fact-checking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ import { ConfigService } from "@nestjs/config";
export class AutomatedFactCheckingService {
constructor(private configService: ConfigService) {}

async getResponseFromAgents(sentence: string = ""): Promise<object> {
async getResponseFromAgents(data): Promise<{ stream: string; json: any }> {
try {
const url = `${this.configService.get<string>("agentsUrl")}/stream`;
const params = {
input: {
messages: [
{
content: sentence,
type: "human",
role: "human",
},
],
sender: "Supervisor",
claim: data.claim,
context: data.context,
language: "Portuguese",
messages: [],
questions: [],
can_be_fact_checked: false,
},
};

const response = await fetch(url, {
method: "POST",
body: JSON.stringify(params),
headers: {
"Content-Type": "application/json",
},
});
const response = await fetch(
`${this.configService.get<string>(
"automatedFactCheckingAPIUrl"
)}/stream`,
{
method: "POST",
body: JSON.stringify(params),
headers: {
"Content-Type": "application/json",
},
}
);

let reader = response.body.getReader();

Expand All @@ -47,26 +48,13 @@ export class AutomatedFactCheckingService {
.map((line) => JSON.parse(line.substring(5)))
.reduce((acc, data) => ({ ...acc, ...data }), {});

jsonEvents.__end__.messages = this.convertMessageContentsToJSON(
jsonEvents.__end__.messages = JSON.parse(
jsonEvents.__end__.messages
);
return jsonEvents.__end__;
} catch (error) {
return {
error: "Error in data fetching process",
};
}
}

convertMessageContentsToJSON(messages) {
return messages
.map(({ content }) => {
try {
return JSON.parse(content);
} catch (error) {
return undefined;
}
})
.filter((message) => message !== undefined);
return { stream: streamResponse, json: jsonEvents.__end__ };
} catch (e) {
throw new Error("Error while doing the fact-checking");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { IsString } from "class-validator";
import { IsObject, IsString } from "class-validator";

export class CreateAutomatedFactCheckingDTO {
@IsString()
sentence: string;
claim: string;

@IsObject()
context: object;
}
8 changes: 4 additions & 4 deletions server/claim-review-task/claim-review-task.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class ClaimReviewController {
public async personalityList(@Req() req: Request, @Res() res: Response) {
const parsedUrl = parse(req.url, true);
const enableCollaborativeEditor = this.isEnableCollaborativeEditor();
const enableAgentReview = this.isEnableAgentReview();
const enableCopilotChatBot = this.isEnableCopilotChatBot();
const enableEditorAnnotations = this.isEnableEditorAnnotations();

await this.viewService.getNextServer().render(
Expand All @@ -174,7 +174,7 @@ export class ClaimReviewController {
sitekey: this.configService.get<string>("recaptcha_sitekey"),
enableCollaborativeEditor,
enableEditorAnnotations,
enableAgentReview,
enableCopilotChatBot,
websocketUrl: this.configService.get<string>("websocketUrl"),
nameSpace: req.params.namespace,
})
Expand All @@ -189,10 +189,10 @@ export class ClaimReviewController {
: false;
}

private isEnableAgentReview() {
private isEnableCopilotChatBot() {
const config = this.configService.get<string>("feature_flag");

return config ? this.unleash.isEnabled("agent_review") : false;
return config ? this.unleash.isEnabled("copilot_chat_bot") : false;
}

private isEnableEditorAnnotations() {
Expand Down
20 changes: 10 additions & 10 deletions server/claim/claim.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export class ClaimController {
);

const enableCollaborativeEditor = this.isEnableCollaborativeEditor();
const enableAgentReview = this.isEnableAgentReview();
const enableCopilotChatBot = this.isEnableCopilotChatBot();
const enableEditorAnnotations = this.isEnableEditorAnnotations();

hideDescriptions[TargetModel.Claim] =
Expand Down Expand Up @@ -353,7 +353,7 @@ export class ClaimController {
hideDescriptions,
enableCollaborativeEditor,
enableEditorAnnotations,
enableAgentReview,
enableCopilotChatBot,
websocketUrl: this.configService.get<string>("websocketUrl"),
nameSpace: req.params.namespace,
})
Expand Down Expand Up @@ -534,7 +534,7 @@ export class ClaimController {
namespace as NameSpaceEnum
);
const enableCollaborativeEditor = this.isEnableCollaborativeEditor();
const enableAgentReview = this.isEnableAgentReview();
const enableCopilotChatBot = this.isEnableCopilotChatBot();
const enableEditorAnnotations = this.isEnableEditorAnnotations();

if (claim.personalities.length > 0) {
Expand All @@ -557,7 +557,7 @@ export class ClaimController {
sitekey: this.configService.get<string>("recaptcha_sitekey"),
enableCollaborativeEditor,
enableEditorAnnotations,
enableAgentReview,
enableCopilotChatBot,
websocketUrl: this.configService.get<string>("websocketUrl"),
nameSpace: namespace,
})
Expand All @@ -577,7 +577,7 @@ export class ClaimController {
const parsedUrl = parse(req.url, true);

const enableCollaborativeEditor = this.isEnableCollaborativeEditor();
const enableAgentReview = this.isEnableAgentReview();
const enableCopilotChatBot = this.isEnableCopilotChatBot();
const enableEditorAnnotations = this.isEnableEditorAnnotations();

const personality =
Expand Down Expand Up @@ -610,7 +610,7 @@ export class ClaimController {
sitekey: this.configService.get<string>("recaptcha_sitekey"),
enableCollaborativeEditor,
enableEditorAnnotations,
enableAgentReview,
enableCopilotChatBot,
websocketUrl: this.configService.get<string>("websocketUrl"),
hideDescriptions,
nameSpace: namespace,
Expand All @@ -637,7 +637,7 @@ export class ClaimController {
);

const enableCollaborativeEditor = this.isEnableCollaborativeEditor();
const enableAgentReview = this.isEnableAgentReview();
const enableCopilotChatBot = this.isEnableCopilotChatBot();
const enableEditorAnnotations = this.isEnableEditorAnnotations();

const claim = await this.claimService.getByPersonalityIdAndClaimSlug(
Expand All @@ -655,7 +655,7 @@ export class ClaimController {
claim,
enableCollaborativeEditor,
enableEditorAnnotations,
enableAgentReview,
enableCopilotChatBot: enableCopilotChatBot,
websocketUrl: this.configService.get<string>("websocketUrl"),
nameSpace: namespace,
})
Expand Down Expand Up @@ -797,10 +797,10 @@ export class ClaimController {
: false;
}

private isEnableAgentReview() {
private isEnableCopilotChatBot() {
const config = this.configService.get<string>("feature_flag");

return config ? this.unleash.isEnabled("agent_review") : false;
return config ? this.unleash.isEnabled("copilot_chat_bot") : false;
}

private isEnableEditorAnnotations() {
Expand Down
42 changes: 42 additions & 0 deletions server/copilot/copilot-chat.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Controller for Langchain Chat operations.
*
* Handles HTTP requests for context-aware chat interactions
* in the Langchain application. This controller is responsible for
* validating incoming request data and orchestrating chat interactions through the LangchainChatService.
* It supports endpoints for initiating context-aware chat
* and ensuring a versatile chat service experience.
*
* @class LangchainChatController
*
* @method contextAwareChat - Initiates a context-aware chat interaction. Accepts POST requests with a ContextAwareMessagesDto to manage chat context.
* Leverages LangchainChatService for processing.
* @param {ContextAwareMessagesDto} contextAwareMessagesDto - DTO for managing chat context.
* @returns Contextual chat response from the LangchainChatService.
*
* This controller uses decorators to define routes and their configurations, ensuring proper request handling and response formatting. It also integrates file upload handling for PDF documents, enabling document-context chat functionalities.
*/

import { Body, Controller, Post, Req } from "@nestjs/common";
import { CopilotChatService } from "./copilot-chat.service";
import { ContextAwareMessagesDto } from "./dtos/context-aware-messages.dto";

@Controller()
export class CopilotChatController {
constructor(private readonly copilotChatService: CopilotChatService) {}

@Post("api/agent-chat")
thesocialdev marked this conversation as resolved.
Show resolved Hide resolved
async agentChat(
@Body() contextAwareMessagesDto: ContextAwareMessagesDto,
@Req() req
) {
try {
return await this.copilotChatService.agentChat(
contextAwareMessagesDto,
req.language
);
} catch (e) {
throw new Error(e);
}
}
}
12 changes: 12 additions & 0 deletions server/copilot/copilot-chat.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from "@nestjs/common";
import { CopilotChatService } from "./copilot-chat.service";
import { CopilotChatController } from "./copilot-chat.controller";
import { AutomatedFactCheckingModule } from "../automated-fact-checking/automated-fact-checking.module";
import { EditorParseModule } from "../editor-parse/editor-parse.module";

@Module({
imports: [AutomatedFactCheckingModule, EditorParseModule],
controllers: [CopilotChatController],
providers: [CopilotChatService],
})
export class CopilotChatModule {}
Loading
Loading