Skip to content

Commit

Permalink
Merge branch 'chatbot/docker-compose-complete' into chatbot/sessions-…
Browse files Browse the repository at this point in the history
…query-by-date
  • Loading branch information
batdevis committed Oct 16, 2024
2 parents 6d11e67 + 91c782a commit ee03467
Show file tree
Hide file tree
Showing 29 changed files with 138 additions and 90 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-mirrors-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextjs-website": patch
---

fix reference in anchorEl for chatbot
6 changes: 6 additions & 0 deletions .changeset/curly-geese-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"gitbook-docs": patch
"nextjs-website": patch
---

Remove warnings from build output
5 changes: 5 additions & 0 deletions .changeset/fuzzy-bags-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextjs-website": patch
---

Remove subtitle from banner link codec
5 changes: 5 additions & 0 deletions .changeset/fuzzy-buttons-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"strapi-cms": minor
---

Remove subtitle attribute from bannerLink component
5 changes: 5 additions & 0 deletions .changeset/orange-beans-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextjs-website": minor
---

Update url mapping for documentation
2 changes: 2 additions & 0 deletions apps/chatbot/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CHB_WEBSITE_URL=...
CHB_REDIS_INDEX_NAME=...
CHB_LLAMAINDEX_INDEX_ID=...
CHB_DOCUMENTATION_DIR=...
CHB_USE_PRESIDIO=...
CHB_GOOGLE_API_KEY=...
CHB_PROVIDER=...
CHB_MODEL_ID=...
Expand All @@ -25,3 +26,4 @@ CHB_ENGINE_USE_ASYNC=True
CHB_ENGINE_USE_STREAMING=...
CHB_QUERY_TABLE_PREFIX=chatbot-local
CHB_DYNAMODB_URL=http://locahost:8080
CHB_USE_PRESIDIO=True
10 changes: 8 additions & 2 deletions apps/chatbot/config/prompts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ qa_prompt_str: |
- the answer must be clear, non-redundant, and have not repeated sentences.
- the answer must not include the query.
- If your answer is based on this retrieved context, include a "Rif" section at the end of the response, listing the titles and filenames from the source nodes used. If no context is used, do not include a reference.
- the answer must be with the same language of the query.
--------------------
Output Examples:
Query: Cos'è il nodo dei pagamenti?
Expand All @@ -38,7 +37,14 @@ qa_prompt_str: |
--------------------
Task:
Given the query: {query_str}
Answer the query according to the `Chatbot Policy` listed above.
Reply to the user following these two steps:
Step 1:
Pay great attention in detail on the query's language and determine if it is formulated in Italian, English, Spanish, French, German, Greek, Croatian, or Slovenian ('yes' or 'no').
Step 2:
If Step 1 returns 'yes': reply always in Italian, regardless of the input language, according to the `Chatbot Policy` listed above.
Otherwise: reply you cannot speak that language and ask for a new query written in an accepted language.
Answer:
Expand Down
1 change: 1 addition & 0 deletions apps/chatbot/docker/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ services:
image: redis/redis-stack:7.2.0-v13
ports:
- "6379:6379"
- "8001:8001"
networks:
- ntw

Expand Down
17 changes: 11 additions & 6 deletions apps/chatbot/src/modules/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
from src.modules.presidio import PresidioPII


AWS_S3_BUCKET = os.getenv("CHB_AWS_S3_BUCKET")
ITALIAN_THRESHOLD = 0.85
NUM_MIN_WORDS_QUERY = 3
NUM_MIN_REFERENCES = 1
USE_PRESIDIO = True if os.getenv("CHB_USE_PRESIDIO", "True") == "True" else False
RESPONSE_TYPE = Union[
Response, StreamingResponse, AsyncStreamingResponse, PydanticResponse
]
Expand All @@ -36,7 +33,9 @@ def __init__(

self.params = params
self.prompts = prompts
self.pii = PresidioPII(config=params["config_presidio"])
if USE_PRESIDIO:
self.pii = PresidioPII(config=params["config_presidio"])

self.model = get_llm()
self.embed_model = get_embed_model()
self.index = load_automerging_index_redis(
Expand Down Expand Up @@ -111,6 +110,9 @@ def _get_response_str(self, engine_response: RESPONSE_TYPE) -> str:
"""
else:
response_str = self._unmask_reference(response_str, nodes)

if "Step 2:" in response_str:
response_str = response_str.split("Step 2:")[1].strip()

return response_str

Expand Down Expand Up @@ -142,7 +144,10 @@ def _unmask_reference(self, response_str: str, nodes) -> str:


def mask_pii(self, message: str) -> str:
return self.pii.mask_pii(message)
if USE_PRESIDIO:
return self.pii.mask_pii(message)
else:
return message


def generate(self, query_str: str) -> str:
Expand Down
28 changes: 12 additions & 16 deletions apps/chatbot/src/modules/presidio.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


# see supported entities by Presidio with their description at: https://microsoft.github.io/presidio/supported_entities/
ENTITIES = [
GLOBAL_ENTITIES = [
"CREDIT_CARD",
"CRYPTO",
"DATE_TIME",
Expand All @@ -23,21 +23,16 @@
"LOCATION",
"PERSON",
"PHONE_NUMBER",
"MEDICAL_LICENSE",
"MEDICAL_LICENSE"
]

IT_ENTITIES = [
"IT_FISCAL_CODE",
"IT_DRIVER_LICENSE",
"IT_VAT_CODE",
"IT_PASSPORT",
"IT_IDENTITY_CARD",
"IT_PHYSICAL_ADDRESS", # this is a custom entity added to the analyzer registry
# "ES_NIF",
# "ES_NIE",
# "US_BANK_NUMBER",
# "US_DRIVER_LICENSE",
# "US_ITIN",
# "US_PASSPORT",
# "US_SSN",
# "UK_NHS"
"IT_PHYSICAL_ADDRESS"
]

ALLOW_LIST = [
Expand Down Expand Up @@ -102,9 +97,10 @@ def __init__(
analyzer_threshold: float = 0.4
):
self.config = config
self.languages = [item["lang_code"] for item in config["models"]]
self.entity_mapping = entity_mapping
self.mapping = mapping
self.entities = entities if entities else ENTITIES
self.entities = entities if entities else GLOBAL_ENTITIES
self.analyzer_threshold = analyzer_threshold

if isinstance(self.config, (Path, str)):
Expand All @@ -117,7 +113,7 @@ def __init__(
self.nlp_engine = nlp_engine
self.analyzer = AnalyzerEngine(
nlp_engine = self.nlp_engine,
supported_languages = ["it", "en"], # "es", "fr", "de"
supported_languages = self.languages,
default_score_threshold = analyzer_threshold
)
self._add_italian_physical_address_entity()
Expand All @@ -136,7 +132,7 @@ def detect_language(self, text: str) -> str:
detected_languages = detect_langs(text)
lang_list = []
for detected_lang in detected_languages:
if detected_lang.lang in ["it", "en", "es", "fr", "de"]:
if detected_lang.lang in self.languages:
lang_list.append(detected_lang.lang)

if not lang_list:
Expand All @@ -145,7 +141,7 @@ def detect_language(self, text: str) -> str:
elif "it" in lang_list:
lang = "it"
else:
lang = "en" # lang_list[0].lang
lang = lang_list[0]
except:
logging.warning("No detected language.")
lang = "it"
Expand All @@ -160,7 +156,7 @@ def detect_pii(self, text: str) -> List[RecognizerResult]:
results = self.analyzer.analyze(
text=text,
language=lang,
entities=self.entities,
entities=self.entities + IT_ENTITIES if lang == "it" else self.entities,
allow_list=ALLOW_LIST
)

Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs-website/src/_contents/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const spaceToPrefixMap = pipe(
/**
* Contains the mapping between the docs.pagopa.it url and the developer portal url.
*/
export const urlReplacesMap: { readonly [url: string]: string } = {
export const staticUrlReplaceMap: { readonly [url: string]: string } = {
// App IO
'https://docs.pagopa.it/io-guida-tecnica-1.3/':
'/app-io/guides/io-guida-tecnica/v1.3',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import ProductLayout, {
import { getGuide, getProductGuidePath } from '@/lib/api';
import { Product } from '@/lib/types/product';
import React from 'react';
import {
gitBookPagesWithTitle,
spaceToPrefixMap,
urlReplacesMap,
} from '@/_contents/products';
import { gitBookPagesWithTitle, spaceToPrefixMap } from '@/_contents/products';
import { ParseContentConfig } from 'gitbook-docs/parseContent';
import { Metadata } from 'next';
import {
Expand All @@ -17,7 +13,7 @@ import {
} from '@/helpers/metadata.helpers';
import GitBookTemplate from '@/components/templates/GitBookTemplate/GitBookTemplate';
import { productPageToBreadcrumbs } from '@/helpers/breadcrumbs.helpers';
import { getGuidesProps } from '@/lib/cmsApi';
import { getGuidesProps, getUrlReplaceMapProps } from '@/lib/cmsApi';
import { generateStructuredDataScripts } from '@/helpers/generateStructuredDataScripts.helpers';
import {
breadcrumbItemByProduct,
Expand Down Expand Up @@ -81,7 +77,7 @@ const Page = async ({ params }: { params: Params }) => {
params?.productSlug,
params?.productGuidePage ?? ['']
);

const urlReplaceMap = await getUrlReplaceMapProps();
const { product, page, guide, version, versions, source, bannerLinks, seo } =
guideProps;
const props: ProductGuidePageProps = {
Expand All @@ -98,7 +94,7 @@ const Page = async ({ params }: { params: Params }) => {
assetsPrefix: source.assetsPrefix,
gitBookPagesWithTitle,
spaceToPrefix: spaceToPrefixMap,
urlReplaces: urlReplacesMap,
urlReplaces: urlReplaceMap,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import {
import { Product } from '@/lib/types/product';
import GitBookContent from '@/components/organisms/GitBookContent/GitBookContent';
import { Box } from '@mui/material';
import {
gitBookPagesWithTitle,
spaceToPrefixMap,
urlReplacesMap,
} from '@/_contents/products';
import { gitBookPagesWithTitle, spaceToPrefixMap } from '@/_contents/products';
import { ParseContentConfig } from 'gitbook-docs/parseContent';
import { Metadata } from 'next';
import {
Expand All @@ -34,6 +30,7 @@ import {
breadcrumbItemByProduct,
productToBreadcrumb,
} from '@/helpers/structuredData.helpers';
import { getUrlReplaceMapProps } from '@/lib/cmsApi';

type Params = {
productSlug: string;
Expand Down Expand Up @@ -124,6 +121,7 @@ const Page = async ({ params }: { params: Params }) => {
);
}

const urlReplaceMap = await getUrlReplaceMapProps();
const tutorialProps = await getStaticTutorial(productSlug, [tutorialPath]);
const { product, page, bannerLinks, source, relatedLinks } = tutorialProps;
const props: ProductTutorialPageProps = {
Expand All @@ -137,7 +135,7 @@ const Page = async ({ params }: { params: Params }) => {
assetsPrefix: source.assetsPrefix,
gitBookPagesWithTitle,
spaceToPrefix: spaceToPrefixMap,
urlReplaces: urlReplacesMap,
urlReplaces: urlReplaceMap,
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import React from 'react';
import {
gitBookPagesWithTitle,
spaceToPrefixMap,
urlReplacesMap,
} from '@/_contents/products';
import { gitBookPagesWithTitle, spaceToPrefixMap } from '@/_contents/products';
import { Metadata } from 'next';
import { makeMetadata } from '@/helpers/metadata.helpers';
import { getSolutionDetail, getSolutionSubPaths } from '@/lib/api';
import GitBookTemplate from '@/components/templates/GitBookTemplate/GitBookTemplate';
import { pageToBreadcrumbs } from '@/helpers/breadcrumbs.helpers';
import { ParseContentConfig } from 'gitbook-docs/parseContent';
import { getSolutionsProps } from '@/lib/cmsApi';
import { getSolutionsProps, getUrlReplaceMapProps } from '@/lib/cmsApi';
import { SolutionTemplateProps } from '@/components/templates/SolutionTemplate/SolutionTemplate';
import { generateStructuredDataScripts } from '@/helpers/generateStructuredDataScripts.helpers';
import { getItemFromPaths } from '@/helpers/structuredData.helpers';
Expand Down Expand Up @@ -64,6 +60,7 @@ const Page = async ({ params }: { params: Params }) => {
params?.solutionSubPathSlugs
);

const urlReplaceMap = await getUrlReplaceMapProps();
if (!solutionProps) {
return null;
}
Expand All @@ -79,7 +76,7 @@ const Page = async ({ params }: { params: Params }) => {
assetsPrefix: source.assetsPrefix,
gitBookPagesWithTitle,
spaceToPrefix: spaceToPrefixMap,
urlReplaces: urlReplacesMap,
urlReplaces: urlReplaceMap,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Link from 'next/link';
import { useTranslations } from 'next-intl';
import { usePathname, useRouter } from 'next/navigation';
import { MobileSiteHeaderStyledTreeItem } from '@/components/molecules/MobileSiteHeader/MobileSiteHeader';
import { ButtonNaked } from '@pagopa/mui-italia';

type MobileUserInfoProps = {
// eslint-disable-next-line functional/no-return-void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,26 @@ const ChatbotLayout = ({
}: ChatbotLayoutProps) => {
const t = useTranslations();
const { palette } = useTheme();
const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(
null
const ref = React.useRef<HTMLElement | undefined>();
const [anchorEl, setAnchorEl] = React.useState<HTMLElement | undefined>(
undefined
);

const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
const handleClick = () => {
setAnchorEl(ref.current);
return null;
};

const handleClose = () => {
setAnchorEl(null);
setAnchorEl(undefined);
};

const open = Boolean(anchorEl);
const id = open ? 'chat-modal' : undefined;

return (
<Box
ref={ref}
sx={{
position: 'fixed',
bottom: { xs: '1rem', md: '2rem' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CodeBlockProps } from 'gitbook-docs/markdoc/schema/code';
import { ReactNode } from 'react';
import CodeBlockPart from '@/components/molecules/CodeBlockPart/CodeBlockPart';
import MermaidDiagram from '@/components/atoms/MermaidDiagram/MermaidDiagram';
import dynamic from 'next/dynamic';

const NoSSRMermaidDiagram = dynamic(
Expand Down
Loading

0 comments on commit ee03467

Please sign in to comment.