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

Feature/non named votes ai #592

Merged
merged 14 commits into from
Aug 17, 2024
Merged
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
9 changes: 9 additions & 0 deletions bruno/democracy-development/bruno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "1",
"name": "democracy-development",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}
3 changes: 3 additions & 0 deletions bruno/democracy-development/environments/local.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vars {
non-named-votes-ai-token: CHANGE_ME
}
15 changes: 15 additions & 0 deletions bruno/democracy-development/non-named-votes-ai/deleteAll.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
meta {
name: deleteAll
type: http
seq: 1
}

delete {
url: http://localhost:3005/all
body: none
auth: none
}

headers {
authorization: Token {{non-named-votes-ai-token}}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
meta {
name: getBeschlusstext 2
type: http
seq: 3
}

get {
url: http://localhost:3005/beschlusstext?title=Gesetz zu dem Protokoll vom 6. Juli 2023 zur Änderung des Abkommens vom 23. April 2012 zwischen der Bundesrepublik Deutschland und dem Großherzogtum Luxemburg zur Vermeidung der Doppelbesteuerung und Verhinderung der Steuerhinterziehung auf dem Gebiet der Steuern vom Einkommen und vom Vermögen (Deutsch-luxemburgisches Steuerabkommen)&pdfUrl=https://dserver.bundestag.de/btp/20/20131.pdf#P.16409
body: none
auth: none
}

params:query {
title: Gesetz zu dem Protokoll vom 6. Juli 2023 zur Änderung des Abkommens vom 23. April 2012 zwischen der Bundesrepublik Deutschland und dem Großherzogtum Luxemburg zur Vermeidung der Doppelbesteuerung und Verhinderung der Steuerhinterziehung auf dem Gebiet der Steuern vom Einkommen und vom Vermögen (Deutsch-luxemburgisches Steuerabkommen)
pdfUrl: https://dserver.bundestag.de/btp/20/20131.pdf#P.16409
}

headers {
authorization: Token {{non-named-votes-ai-token}}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
meta {
name: getBeschlusstext
type: http
seq: 2
}

get {
url: http://localhost:3005/beschlusstext?title=Kommission zur Reform des Wahlrechts und zur Modernisierung der Parlamentsarbeit&pdfUrl=https://dserver.bundestag.de/btp/20/20020.pdf#P.1464&drucksachen=20/1023
body: none
auth: none
}

params:query {
title: Kommission zur Reform des Wahlrechts und zur Modernisierung der Parlamentsarbeit
pdfUrl: https://dserver.bundestag.de/btp/20/20020.pdf#P.1464
drucksachen: 20/1023
}

headers {
authorization: Token {{non-named-votes-ai-token}}
}
4 changes: 3 additions & 1 deletion bundestag.io/admin/.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ BIO_EDIT_TOKEN="local-token"
AI_SERVER_URL=http://votes-ai.local.democracy-app.de
AI_SERVER_URL_CODEGEN=http://localhost:4003
AI_ACCESS_TOKEN=EMPTY
AI_SIMULATION=true
AI_SIMULATION=true
NON_NAMED_VOTES_AI_SERVER_URL=http://localhost:3005
NON_NAMED_VOTES_AI_SECRET=CHANGE_ME
6 changes: 4 additions & 2 deletions bundestag.io/admin/.env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
CREDENTIALS=user:password
BUNDESTAGIO_SERVER_URL=https://internal.api.bundestag.io
NEXTAUTH_SECRET=secret
BIO_EDIT_TOKEN=""
BIO_EDIT_TOKEN="local-token"
AI_SERVER_URL=http://ai-votes:4003
AI_SERVER_URL_CODEGEN=http://localhost:4003
AI_ACCESS_TOKEN=EMPTY
AI_SIMULATION=true
AI_SIMULATION=true
NON_NAMED_VOTES_AI_SERVER_URL=http://localhost:3005
NON_NAMED_VOTES_AI_SECRET=CHANGE_ME
1 change: 1 addition & 0 deletions bundestag.io/admin/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ yarn-debug.log*
yarn-error.log*

# local env files
.env
.env.local
.env.development.local
.env.test.local
Expand Down
2 changes: 1 addition & 1 deletion bundestag.io/admin/garden.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ kind: Deploy
name: admin
type: kubernetes
description: Deploy the Admin
dependencies: [build.admin, deploy.bundestag-io-api, deploy.votes-ai]
dependencies: [build.admin, deploy.bundestag-io-api, deploy.votes-ai, deploy.non-named-votes-ai]

variables:
BIO_EDIT_TOKEN: ${actions.deploy.bundestag-io-api.var.BIO_EDIT_TOKEN}
Expand Down
2 changes: 2 additions & 0 deletions bundestag.io/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"graphql-tag": "^2.12.6",
"next": "^14.2.3",
"next-auth": "^4.24.7",
"pino": "^9.3.2",
"pino-pretty": "^11.2.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"styled-components": "^6.1.11"
Expand Down
41 changes: 41 additions & 0 deletions bundestag.io/admin/src/components/Procedures/Beschlusstext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { log } from '@/lib/logger';
import { Button, Spin } from 'antd';
import FormItem from 'antd/es/form/FormItem';
import TextArea from 'antd/es/input/TextArea';
import axios from 'axios';
import { useState } from 'react';

type Props = {
pdfUrl: string;
title: string;
drucksachen?: string[];
};

export const Beschlusstext: React.FC<Props> = ({ pdfUrl, title, drucksachen }) => {
const [aiText, setAiText] = useState<string | null>(null);
const [loading, setLoading] = useState(false);

const onGetViaAi = async () => {
log.debug(`request non-named-votes-ai: ${pdfUrl}, ${title}, ${drucksachen}`);
setLoading(true);
const response = await axios.get('/api/non-named-votes-ai/get', {
params: { pdfUrl, title, drucksachen },
});
setAiText(response.data.text);
setLoading(false);
};

return (
<>
<FormItem label="Beschlusstext" name="decisionText" rules={[{ required: true, message: 'Beschlusstext fehlt!' }]}>
<TextArea placeholder="Beschlusstext" rows={3} />
</FormItem>
{!aiText && (
<Button type="dashed" onClick={onGetViaAi} disabled={loading}>
{!loading ? 'get via AI' : <Spin />}
</Button>
)}
{aiText && <div>{aiText}</div>}
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import React from 'react';
import { Form, Input, Button, InputNumber, Radio, Row, Col, notification, Switch } from 'antd';
import { axiosClient } from '../../lib/axios';
import { AiVotes, AiVotesProps } from './AiVotes';
import { Beschlusstext } from './Beschlusstext';

// Ant Design Sub-Elements
const { TextArea } = Input;
const FormItem = Form.Item;

const FRACTIONS = [
Expand All @@ -28,7 +28,7 @@ const FRACTIONS = [
},
];

const VoteResultsForm = ({ data, type, procedureId, period }) => {
const VoteResultsForm = ({ data, type, procedureId, period, lastPlenaryProtocoll, title }) => {
const [form] = Form.useForm();

const onFinish = (values) => {
Expand Down Expand Up @@ -113,7 +113,6 @@ const VoteResultsForm = ({ data, type, procedureId, period }) => {
})),
}}
>
<div>HELLO</div>
<FormItem
label="Abstimmung über"
name="votingDocument"
Expand All @@ -127,9 +126,7 @@ const VoteResultsForm = ({ data, type, procedureId, period }) => {
<FormItem label="Ergebnis umdrehen" name="toggleDecision">
<Switch defaultChecked={data.votingRecommendation === false} />
</FormItem>
<FormItem label="Beschlusstext" name="decisionText" rules={[{ required: true, message: 'Beschlusstext fehlt!' }]}>
<TextArea placeholder="Beschlusstext" rows={3} />
</FormItem>
<Beschlusstext pdfUrl={lastPlenaryProtocoll.findSpotUrl} title={title} />
<FormItem
noStyle
shouldUpdate={(prevValues, currentValues) => prevValues.decisionText !== currentValues.decisionText}
Expand Down
13 changes: 13 additions & 0 deletions bundestag.io/admin/src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pino from 'pino';

const log = pino({
level: process.env.PINO_LOG_LEVEL || 'info',
transport: {
target: 'pino-pretty',
options: {
colorize: true,
},
},
});

export { log };
5 changes: 4 additions & 1 deletion bundestag.io/admin/src/pages/api/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { getServerSession } from 'next-auth/next';
import { authOptions } from '../auth/[...nextauth]';
import { axiosClient } from '../../../lib/axios';
import { config } from '@/lib/config';
import { log } from '@/lib/logger';

const graphql = async (req: NextApiRequest, res: NextApiResponse) => {
log.debug(`api->graphql ${JSON.stringify(req.body.variables)}`);

const session = await getServerSession(req, res, authOptions);
const { query, variables } = req.body;

Expand All @@ -14,7 +17,7 @@ const graphql = async (req: NextApiRequest, res: NextApiResponse) => {

res.status(response.status).send(response.data);
} catch (error) {
console.error(error);
log.error(error);
res.status(500).send("Couldn't fetch data");
}
} else {
Expand Down
34 changes: 34 additions & 0 deletions bundestag.io/admin/src/pages/api/non-named-votes-ai/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getServerSession } from 'next-auth';
import { authOptions } from '../auth/[...nextauth]';
import { log } from '../../../lib/logger';
import axios from 'axios';

const get = async (req, res) => {
if (!process.env.NON_NAMED_VOTES_AI_SERVER_URL) {
throw new Error('NON_NAMED_VOTES_AI_SERVER_URL not set');
}
const session = await getServerSession(req, res, authOptions);
log.debug(session);
if (!!session?.user) {
const { pdfUrl, title, drucksachen } = req.query as { pdfUrl: string; title: string; drucksachen?: string[] };
log.info('getBeschlusstext start');
log.debug(`getBeschlusstext: ${pdfUrl}, ${title}, ${drucksachen}`);

const response = await axios.get(`${process.env.NON_NAMED_VOTES_AI_SERVER_URL}/beschlusstext`, {
params: { pdfUrl, title, drucksachen },
headers: {
Authorization: `Token ${process.env.NON_NAMED_VOTES_AI_SECRET}`,
},
});

log.debug(response.data);

res.status(200).json(response.data);
} else {
throw new Error('not authorized');
}

res.end();
};

export default get;
9 changes: 8 additions & 1 deletion bundestag.io/admin/src/pages/procedure/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,14 @@ const Procedure: React.FC = () => {
</dl>
{!namedVote && <VoteResultTextHelper procedureId={procedureId} importantDocuments={importantDocuments} />}
{!namedVote && (
<VoteResultsForm data={customData.voteResults} type={type} procedureId={procedureId} period={period} />
<VoteResultsForm
data={customData.voteResults}
type={type}
procedureId={procedureId}
period={period}
lastPlenaryProtocoll={plenaryProtocolls?.slice(-1)?.[0]}
title={title}
/>
)}
{namedVote && <VoteResultsFormNamedPoll data={customData.voteResults} procedureId={procedureId} />}
</App>
Expand Down
5 changes: 5 additions & 0 deletions common/bundestagio/src/models/NonNamedVotesAI/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import mongoose from 'mongoose';
import NonNamedVotesAiSchema, { INonNamedVotesAi } from './schema';

export const NonNamedVotesAiModel = mongoose.model<INonNamedVotesAi>('NonNamedVotesAi', NonNamedVotesAiSchema);
export { NonNamedVotesAiSchema, INonNamedVotesAi };
22 changes: 22 additions & 0 deletions common/bundestagio/src/models/NonNamedVotesAI/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Schema, SchemaTimestampsConfig, Document } from 'mongoose';

export interface INonNamedVotesAi extends Document, SchemaTimestampsConfig {
pdfUrl: string;
assistantId?: string;
vectorStoreId?: string;
threadId?: string;
fileId?: string;
}

const NonNamedVotesAiSchema = new Schema<INonNamedVotesAi>(
{
pdfUrl: { type: String, unique: true, index: true, required: true },
assistantId: { type: String },
vectorStoreId: { type: String },
threadId: { type: String },
fileId: { type: String },
},
{ timestamps: true },
);

export default NonNamedVotesAiSchema;
1 change: 1 addition & 0 deletions common/bundestagio/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './NamedPoll';
export * from './Procedure';
export * from './User';
export * from './PlenaryMinute';
export * from './NonNamedVotesAI';
Loading