Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
revolunet committed Dec 12, 2024
1 parent 796aea9 commit b90bc40
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 241 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"is-ci": "^3.0.1",
"next": "15.1.0",
"nuqs": "^2.2.3",
"p-all": "^5.0.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-dropzone": "^14.3.5",
Expand Down
156 changes: 156 additions & 0 deletions src/lib/albert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { useEffect, useState } from "react";

const ALBERT_API_KEY = process.env.ALBERT_API_KEY;
const API_URL = "/api/albert"; //https://albert.api.etalab.gouv.fr";
const LANGUAGE_MODEL = "AgentPublic/llama3-instruct-8b"; // see https://albert.api.etalab.gouv.fr/v1/models
const EMBEDDING_MODEL = "BAAI/bge-m3";

export const albertApi = ({
path,
method = "POST",
body,
}: {
path: string;
method?: "POST" | "GET";
body?: string;
}) =>
fetch(`${API_URL}/v1${path}`, {
method,
headers: {
// Authorization: `Bearer ${ALBERT_API_KEY}`,
"Content-Type": "application/json",
},
body,
}).then((r) => r.json());

type AlbertCollection = {
id: string;
name: string;
type: "public" | "private";
model: "string"; // "BAAI/bge-m3";
user: string;
description: string;
created_at: number;
documents: null | number;
};

export const useAlbertCollections = () => {
const [collections, setCollections] = useState<AlbertCollection[]>([]);
const loadCollections = async () => {
const collections = await albertApi({
path: "/collections",
method: "GET",
});
return collections;
};
useEffect(() => {
reloadCollections();
}, []);

const reloadCollections = () => {
loadCollections().then((res) => {
setCollections(res.data);
});
};
return { collections, reloadCollections };
};

export const createCollection = ({ name, model = EMBEDDING_MODEL }) =>
fetch(`${API_URL}/v1/collections`, {
method: "POST",
headers: {
Authorization: `Bearer ${ALBERT_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ name, model }),
})
.then((r) => r.json())
.then((d) => {
console.log(d);
return d;
})
.then((d) => d.id);

export const addFileToCollection = async ({ file, fileName, collectionId }) => {
const formData = new FormData();
formData.append("file", file, fileName);
formData.append("request", JSON.stringify({ collection: collectionId }));
return fetch(`${API_URL}/v1/files`, {
method: "POST",
headers: {
Authorization: `Bearer ${ALBERT_API_KEY}`,
//"Content-Type": "multipart/form-data",
},
body: formData,
}).then(async (r) => {
//console.log(r);
if (r.status !== 200) {
console.log("Cannot upload document", r.statusText);
return {
detail: r.statusText,
};
}
if (r.statusText === "OK") {
let json = {};
try {
json = await r.json();
} catch (e) {}
if (json && json.detail) {
console.log("Cannot upload document", json.detail);
return {
detail: json.detail,
};
}
return json;
}
return {
detail: "plop",
};
});
};

export const getSearch = ({
collections,
query,
}: {
collections: string[];
query: string;
}) => {
console.log({ url: `${API_URL}/v1/search`, query });
return fetch(`${API_URL}/v1/search`, {
cache: "no-cache",
method: "POST",
headers: {
Authorization: `Bearer ${ALBERT_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ collections, k: 6, prompt: query }),
})
.then((r) => {
console.log(r);
return r.json();
})
.catch((r) => {
console.error(r);
throw r;
});
};

export const getPromptWithRagResults = ({
results,
input,
}) => `Réponds à la question suivante au format markdown sans mettre de titre et en te basant sur le contexte fourni uniquement.
## Question: ${input}
## Contexte
${results.data
.map(
(hit) => `${hit.chunk.metadata.title} ${hit.chunk.metadata.document_name}
${hit.chunk.content}
`
)
.join("\n")}
`;
42 changes: 6 additions & 36 deletions src/pages/a-propos.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,14 @@ import Head from "next/head";
import { Alert } from "@codegouvfr/react-dsfr/Alert";

<Head>
<title>Titre article | template</title>
<title>Démo albert-docs - À propos</title>
</Head>

# Page au format Markdown(X)
# albert-docs demo

C'est du texte, que l'on peut éditer facilement et enrichir avec des composants. [voir la source de cette page](https://github.com/betagouv/template/blob/main/src/pages/article.mdx?plain=1)
Cette application permet d'utiliser l'[API Albert](https://github.com/etalab-ia/albert-api) pour uploader des document et effectuer des recherches dessus.

## Du texte
## Références

🚀 Ensemble, nous pouvons rendre le web meilleur pour tous et toutes !

## Un composant du DSFR

[Voir tous les composants de @codegouv/react-dsfr](https://components.react-dsfr.codegouv.studio/)

<Alert
severity="warning"
title="Alerte"
description="Ceci est le contenu de l'alerte"
/>

## Un tableau

| Tables | Are | Cool |
| -------- | --- | ------- |
| col 1 is | 👌 | $1600 |
| col 2 is | 🆗 | $12 |
| col 3 is | 🙀 | **$42** |

## Une image

![artwork](https://beta.gouv.fr/assets/home/design.svg)

## Citation

> exemple de citation
## Liens

- [Lien interne](/dsfr)
- [Lien externe](https://beta.gouv.fr)
- repo Albert: https://github.com/etalab-ia/albert-api
- doc API Albert: https://albert.api.etalab.gouv.fr/documentation
Loading

0 comments on commit b90bc40

Please sign in to comment.