{ "cells": [ { "cell_type": "code", "execution_count": 136, "id": "8c480c69-69bd-4d36-907b-db70f15c6959", "metadata": {}, "outputs": [], "source": [ "from transformers import AutoTokenizer\n", "from sentence_transformers import SentenceTransformer\n", "import os\n", "import chromadb\n", "import re\n", "import html\n", "import copy\n", "from llama_cpp import Llama\n", "import gradio as gr\n", "from IPython.display import Markdown, display" ] }, { "cell_type": "markdown", "id": "3c31df71-9eb1-499c-bbab-c92d4c870e6c", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "# Embedding model and tokenizer" ] }, { "cell_type": "code", "execution_count": 3, "id": "d67fc6ef-6e90-49c0-bf3b-29d0fdaa5300", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/peportier/miniforge3/envs/RAG_ENV/lib/python3.9/site-packages/transformers/utils/generic.py:309: UserWarning: torch.utils._pytree._register_pytree_node is deprecated. Please use torch.utils._pytree.register_pytree_node instead.\n", " _torch_pytree._register_pytree_node(\n" ] } ], "source": [ "#embed_model_name = \"dangvantuan/sentence-camembert-large\"\n", "#embed_model = HuggingFaceEmbedding(model_name=embed_model_name)\n", "\n", "embed_model_name = 'intfloat/multilingual-e5-large'\n", "tokenizer = AutoTokenizer.from_pretrained(embed_model_name)\n", "embed_model = SentenceTransformer(embed_model_name)" ] }, { "cell_type": "markdown", "id": "631ab89d-55f7-4d89-9e82-0d1a09359c79", "metadata": {}, "source": [ "# LLM model" ] }, { "cell_type": "code", "execution_count": 9, "id": "bc970979-82f3-46c4-ab86-4d9bf65acdd6", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "llama_model_loader: loaded meta data with 21 key-value pairs and 291 tensors from /Users/peportier/llm/a/a/zephyr-7b-beta.Q5_K_M.gguf (version GGUF V3 (latest))\n", "llama_model_loader: - tensor 0: token_embd.weight q5_K [ 4096, 32000, 1, 1 ]\n", "llama_model_loader: - tensor 1: blk.0.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 2: blk.0.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 3: blk.0.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 4: blk.0.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 5: blk.0.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 6: blk.0.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 7: blk.0.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 8: blk.0.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 9: blk.0.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 10: blk.1.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 11: blk.1.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 12: blk.1.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 13: blk.1.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 14: blk.1.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 15: blk.1.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 16: blk.1.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 17: blk.1.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 18: blk.1.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 19: blk.2.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 20: blk.2.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 21: blk.2.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 22: blk.2.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 23: blk.2.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 24: blk.2.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 25: blk.2.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 26: blk.2.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 27: blk.2.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 28: blk.3.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 29: blk.3.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 30: blk.3.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 31: blk.3.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 32: blk.3.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 33: blk.3.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 34: blk.3.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 35: blk.3.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 36: blk.3.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 37: blk.4.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 38: blk.4.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 39: blk.4.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 40: blk.4.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 41: blk.4.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 42: blk.4.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 43: blk.4.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 44: blk.4.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 45: blk.4.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 46: blk.5.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 47: blk.5.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 48: blk.5.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 49: blk.5.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 50: blk.5.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 51: blk.5.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 52: blk.5.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 53: blk.5.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 54: blk.5.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 55: blk.6.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 56: blk.6.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 57: blk.6.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 58: blk.6.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 59: blk.6.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 60: blk.6.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 61: blk.6.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 62: blk.6.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 63: blk.6.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 64: blk.7.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 65: blk.7.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 66: blk.7.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 67: blk.7.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 68: blk.7.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 69: blk.7.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 70: blk.7.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 71: blk.7.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 72: blk.7.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 73: blk.8.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 74: blk.8.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 75: blk.8.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 76: blk.8.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 77: blk.10.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 78: blk.10.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 79: blk.10.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 80: blk.10.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 81: blk.10.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 82: blk.10.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 83: blk.10.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 84: blk.10.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 85: blk.10.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 86: blk.11.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 87: blk.11.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 88: blk.11.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 89: blk.11.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 90: blk.11.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 91: blk.11.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 92: blk.11.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 93: blk.11.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 94: blk.11.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 95: blk.12.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 96: blk.12.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 97: blk.12.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 98: blk.12.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 99: blk.12.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 100: blk.12.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 101: blk.8.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 102: blk.8.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 103: blk.8.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 104: blk.8.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 105: blk.8.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 106: blk.9.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 107: blk.9.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 108: blk.9.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 109: blk.9.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 110: blk.9.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 111: blk.9.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 112: blk.9.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 113: blk.9.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 114: blk.9.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 115: blk.12.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 116: blk.12.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 117: blk.12.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 118: blk.13.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 119: blk.13.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 120: blk.13.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 121: blk.13.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 122: blk.13.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 123: blk.13.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 124: blk.13.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 125: blk.13.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 126: blk.13.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 127: blk.14.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 128: blk.14.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 129: blk.14.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 130: blk.14.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 131: blk.14.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 132: blk.14.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 133: blk.14.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 134: blk.14.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 135: blk.14.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 136: blk.15.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 137: blk.15.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 138: blk.15.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 139: blk.15.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 140: blk.15.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 141: blk.15.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 142: blk.15.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 143: blk.15.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 144: blk.15.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 145: blk.16.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 146: blk.16.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 147: blk.16.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 148: blk.16.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 149: blk.16.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 150: blk.16.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 151: blk.16.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 152: blk.16.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 153: blk.16.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 154: blk.17.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 155: blk.17.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 156: blk.17.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 157: blk.17.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 158: blk.17.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 159: blk.17.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 160: blk.17.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 161: blk.17.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 162: blk.17.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 163: blk.18.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 164: blk.18.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 165: blk.18.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 166: blk.18.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 167: blk.18.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 168: blk.18.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 169: blk.18.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 170: blk.18.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 171: blk.18.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 172: blk.19.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 173: blk.19.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 174: blk.19.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 175: blk.19.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 176: blk.19.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 177: blk.19.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 178: blk.19.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 179: blk.19.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 180: blk.19.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 181: blk.20.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 182: blk.20.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 183: blk.20.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 184: blk.20.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 185: blk.20.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 186: blk.20.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 187: blk.20.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 188: blk.20.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 189: blk.20.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 190: blk.21.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 191: blk.21.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 192: blk.21.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 193: blk.21.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 194: blk.21.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 195: blk.21.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 196: blk.21.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 197: blk.21.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 198: blk.21.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 199: blk.22.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 200: blk.22.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 201: blk.22.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 202: blk.22.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 203: blk.22.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 204: blk.22.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 205: blk.22.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 206: blk.22.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 207: blk.22.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 208: blk.23.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 209: blk.23.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 210: blk.23.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 211: blk.23.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 212: blk.23.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 213: blk.23.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 214: blk.23.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 215: blk.23.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 216: blk.23.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 217: blk.24.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 218: blk.24.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 219: blk.24.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 220: blk.24.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 221: blk.24.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 222: blk.24.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 223: blk.24.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 224: blk.24.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 225: blk.24.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 226: blk.25.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 227: blk.25.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 228: blk.25.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 229: blk.25.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 230: blk.25.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 231: blk.25.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 232: blk.25.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 233: blk.25.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 234: blk.25.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 235: blk.26.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 236: blk.26.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 237: blk.26.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 238: blk.26.attn_v.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 239: blk.26.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 240: blk.26.ffn_down.weight q5_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 241: blk.26.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 242: blk.26.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 243: blk.26.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 244: blk.27.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 245: blk.27.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 246: blk.27.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 247: blk.27.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 248: blk.27.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 249: blk.27.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 250: blk.27.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 251: blk.27.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 252: blk.27.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 253: blk.28.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 254: blk.28.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 255: blk.28.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 256: blk.28.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 257: blk.28.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 258: blk.28.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 259: blk.28.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 260: blk.28.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 261: blk.28.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 262: blk.29.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 263: blk.29.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 264: blk.29.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 265: blk.29.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 266: blk.29.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 267: blk.29.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 268: blk.29.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 269: blk.29.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 270: blk.29.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 271: blk.30.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 272: blk.30.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 273: blk.30.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 274: blk.30.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 275: blk.30.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 276: blk.30.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 277: output.weight q6_K [ 4096, 32000, 1, 1 ]\n", "llama_model_loader: - tensor 278: blk.30.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 279: blk.30.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 280: blk.30.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 281: blk.31.attn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 282: blk.31.ffn_down.weight q6_K [ 14336, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 283: blk.31.ffn_gate.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 284: blk.31.ffn_up.weight q5_K [ 4096, 14336, 1, 1 ]\n", "llama_model_loader: - tensor 285: blk.31.ffn_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - tensor 286: blk.31.attn_k.weight q5_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 287: blk.31.attn_output.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 288: blk.31.attn_q.weight q5_K [ 4096, 4096, 1, 1 ]\n", "llama_model_loader: - tensor 289: blk.31.attn_v.weight q6_K [ 4096, 1024, 1, 1 ]\n", "llama_model_loader: - tensor 290: output_norm.weight f32 [ 4096, 1, 1, 1 ]\n", "llama_model_loader: - kv 0: general.architecture str = llama\n", "llama_model_loader: - kv 1: general.name str = huggingfaceh4_zephyr-7b-beta\n", "llama_model_loader: - kv 2: llama.context_length u32 = 32768\n", "llama_model_loader: - kv 3: llama.embedding_length u32 = 4096\n", "llama_model_loader: - kv 4: llama.block_count u32 = 32\n", "llama_model_loader: - kv 5: llama.feed_forward_length u32 = 14336\n", "llama_model_loader: - kv 6: llama.rope.dimension_count u32 = 128\n", "llama_model_loader: - kv 7: llama.attention.head_count u32 = 32\n", "llama_model_loader: - kv 8: llama.attention.head_count_kv u32 = 8\n", "llama_model_loader: - kv 9: llama.attention.layer_norm_rms_epsilon f32 = 0.000010\n", "llama_model_loader: - kv 10: llama.rope.freq_base f32 = 10000.000000\n", "llama_model_loader: - kv 11: general.file_type u32 = 17\n", "llama_model_loader: - kv 12: tokenizer.ggml.model str = llama\n", "llama_model_loader: - kv 13: tokenizer.ggml.tokens arr[str,32000] = [\"\", \"\", \"\", \"<0x00>\", \"<...\n", "llama_model_loader: - kv 14: tokenizer.ggml.scores arr[f32,32000] = [0.000000, 0.000000, 0.000000, 0.0000...\n", "llama_model_loader: - kv 15: tokenizer.ggml.token_type arr[i32,32000] = [2, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, ...\n", "llama_model_loader: - kv 16: tokenizer.ggml.bos_token_id u32 = 1\n", "llama_model_loader: - kv 17: tokenizer.ggml.eos_token_id u32 = 2\n", "llama_model_loader: - kv 18: tokenizer.ggml.unknown_token_id u32 = 0\n", "llama_model_loader: - kv 19: tokenizer.ggml.padding_token_id u32 = 2\n", "llama_model_loader: - kv 20: general.quantization_version u32 = 2\n", "llama_model_loader: - type f32: 65 tensors\n", "llama_model_loader: - type q5_K: 193 tensors\n", "llama_model_loader: - type q6_K: 33 tensors\n", "llm_load_vocab: special tokens definition check successful ( 259/32000 ).\n", "llm_load_print_meta: format = GGUF V3 (latest)\n", "llm_load_print_meta: arch = llama\n", "llm_load_print_meta: vocab type = SPM\n", "llm_load_print_meta: n_vocab = 32000\n", "llm_load_print_meta: n_merges = 0\n", "llm_load_print_meta: n_ctx_train = 32768\n", "llm_load_print_meta: n_embd = 4096\n", "llm_load_print_meta: n_head = 32\n", "llm_load_print_meta: n_head_kv = 8\n", "llm_load_print_meta: n_layer = 32\n", "llm_load_print_meta: n_rot = 128\n", "llm_load_print_meta: n_gqa = 4\n", "llm_load_print_meta: f_norm_eps = 0.0e+00\n", "llm_load_print_meta: f_norm_rms_eps = 1.0e-05\n", "llm_load_print_meta: f_clamp_kqv = 0.0e+00\n", "llm_load_print_meta: f_max_alibi_bias = 0.0e+00\n", "llm_load_print_meta: n_ff = 14336\n", "llm_load_print_meta: rope scaling = linear\n", "llm_load_print_meta: freq_base_train = 10000.0\n", "llm_load_print_meta: freq_scale_train = 1\n", "llm_load_print_meta: n_yarn_orig_ctx = 32768\n", "llm_load_print_meta: rope_finetuned = unknown\n", "llm_load_print_meta: model type = 7B\n", "llm_load_print_meta: model ftype = mostly Q5_K - Medium\n", "llm_load_print_meta: model params = 7.24 B\n", "llm_load_print_meta: model size = 4.78 GiB (5.67 BPW) \n", "llm_load_print_meta: general.name = huggingfaceh4_zephyr-7b-beta\n", "llm_load_print_meta: BOS token = 1 ''\n", "llm_load_print_meta: EOS token = 2 ''\n", "llm_load_print_meta: UNK token = 0 ''\n", "llm_load_print_meta: PAD token = 2 ''\n", "llm_load_print_meta: LF token = 13 '<0x0A>'\n", "llm_load_tensors: ggml ctx size = 0.11 MiB\n", "llm_load_tensors: mem required = 4893.10 MiB\n", "...................................................................................................\n", "llama_new_context_with_model: n_ctx = 4096\n", "llama_new_context_with_model: freq_base = 10000.0\n", "llama_new_context_with_model: freq_scale = 1\n", "llama_new_context_with_model: kv self size = 512.00 MiB\n", "llama_build_graph: non-view tensors processed: 740/740\n", "ggml_metal_init: allocating\n", "ggml_metal_init: found device: Apple M2 Max\n", "ggml_metal_init: picking default device: Apple M2 Max\n", "ggml_metal_init: default.metallib not found, loading from source\n", "ggml_metal_init: loading '/Users/peportier/miniforge3/envs/RAG_ENV/lib/python3.9/site-packages/llama_cpp/ggml-metal.metal'\n", "ggml_metal_init: GPU name: Apple M2 Max\n", "ggml_metal_init: GPU family: MTLGPUFamilyApple8 (1008)\n", "ggml_metal_init: hasUnifiedMemory = true\n", "ggml_metal_init: recommendedMaxWorkingSetSize = 49152.00 MiB\n", "ggml_metal_init: maxTransferRate = built-in GPU\n", "llama_new_context_with_model: compute buffer total size = 291.07 MiB\n", "llama_new_context_with_model: max tensor size = 102.54 MiB\n", "ggml_metal_add_buffer: allocated 'data ' buffer, size = 4893.70 MiB, (10588.06 / 49152.00)\n", "ggml_metal_add_buffer: allocated 'kv ' buffer, size = 512.02 MiB, (11100.08 / 49152.00)\n", "ggml_metal_add_buffer: allocated 'alloc ' buffer, size = 288.02 MiB, (11388.09 / 49152.00)\n", "AVX = 0 | AVX2 = 0 | AVX512 = 0 | AVX512_VBMI = 0 | AVX512_VNNI = 0 | FMA = 0 | NEON = 1 | ARM_FMA = 1 | F16C = 0 | FP16_VA = 1 | WASM_SIMD = 0 | BLAS = 1 | SSE3 = 0 | SSSE3 = 0 | VSX = 0 | \n", "ggml_metal_free: deallocating\n" ] } ], "source": [ "llm = Llama(model_path='/Users/peportier/llm/a/a/zephyr-7b-beta.Q5_K_M.gguf', \n", " n_gpu_layers=1, use_mlock=True, n_ctx=4096)\n", "\n", "system_prompt = \"\"\"\\\n", "Vous fournissez avec soin des réponses précises, factuelles, réfléchies et nuancées, et vous êtes doué pour le raisonnement. \\\n", "Si vous pensez qu'il n'y a peut-être pas de bonne réponse, vous le dites. \\\n", "Ne soyez pas verbeux dans vos réponses, mais donnez des détails et des exemples lorsque cela peut aider à l'explication. \\\n", "Vous rédigez vos réponses en français. \\\n", "\"\"\"\n", "\n", "def format_prompt(question):\n", " prompt = \"\"\n", " prompt = f\"<|system|>\\n {system_prompt.strip()} \\n\"\n", " prompt += f\"<|user|>\\n {question} \\n\"\n", " prompt += f\"<|assistant|>\\n\"\n", " return prompt\n", "\n", "def answer(question):\n", " response = llm(prompt = question,\n", " temperature = 0.1,\n", " mirostat_mode = 2,\n", " max_tokens = -1,\n", " stop = [''])\n", " return response[\"choices\"][0][\"text\"]" ] }, { "cell_type": "markdown", "id": "4b9b8dcd-a371-4f03-b0c6-eb27d56002fe", "metadata": {}, "source": [ "## Test LLM model" ] }, { "cell_type": "code", "execution_count": 11, "id": "460b87be-7778-430a-a167-3c3fd8deaf48", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Le philosophe allemand Georg Wilhelm Friedrich Hegel (1770-1831) a exercé une influence considérable sur la philosophie moderne, particulièrement dans le domaine de l'idéalisme et du marxisme. Voici quelques philosophes qui ont été fortement influencés par Hegel :\n", "\n", "1. Karl Marx (1818-1883) : Le fondateur du marxisme a été profondément inspiré par la philosophie de Hegel, en particulier son concept d'histoire comme processus dialectique. Marx a critiqué et développé l'idée hégélienne de la dialectique pour expliquer le fonctionnement de la société et les lois de l'évolution historique.\n", "\n", "2. Friedrich Engels (1820-1895) : Le collaborateur de Marx a également été influencé par Hegel, en particulier son concept d'histoire comme processus dialectique. Engels a développé cette idée dans son ouvrage \"L'origine de la famille, de la propriété privée et de l'État\" (1884), où il explique comment les relations sociales ont évolué en fonction des conditions historiques.\n", "\n", "3. G.W.F. Hegel lui-même : Certains philosophes ont été influencés par Hegel à tel point qu'ils ont développé leur propre philosophie dans le cadre de l'hégélianisme, une école de pensée qui a été dominante en Allemagne au XIXe siècle. Parmi ces philosophes, on peut citer Arthur Schopenhauer (1788-1860), qui a critiqué et modifié les idées hégéliennes pour développer sa propre philosophie de l'art et du pessimisme, ainsi que Ludwig Feuerbach (1804-1872), qui a développé une critique de la religion et de la philosophie hégélienne.\n", "\n", "4. Jean-Paul Sartre (1905-1980) : Le philosophe existentialiste français a été influencé par Hegel dans son travail sur l'histoire et la dialectique, en particulier dans son ouvrage \"L'existentialisme est un humanisme\" (1946), où il développe une vision de l'histoire comme processus dialectique. Sartre a également critiqué les idées hégéliennes sur le sujet et la conscience, en particulier dans son ouvrage \"L'être et le néant\" (1943).\n", "\n", "5. Slavoj Žižek (né en 1949) : Le philosophe slovène a été influencé par Hegel dans son travail sur la psychanalyse, la politique et la culture populaire, en particulier dans son ouvrage \"Le Sublime Object de l'Idée\" (1981), où il développe une vision dialectique de la culture populaire. Žižek a également critiqué les idées hégéliennes sur le sujet et la conscience, en particulier dans son ouvrage \"Le Pouvoir politique et la Formation du Sujet\" (1976).\n", "\n", "Ces philosophes ont été influencés par Hegel dans des domaines variés de la philosophie moderne, mais ils ont tous reconnu l'importance de ses idées sur l'histoire, la dialectique et le sujet.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n", "llama_print_timings: load time = 575.42 ms\n", "llama_print_timings: sample time = 2695.33 ms / 804 runs ( 3.35 ms per token, 298.29 tokens per second)\n", "llama_print_timings: prompt eval time = 574.33 ms / 165 tokens ( 3.48 ms per token, 287.29 tokens per second)\n", "llama_print_timings: eval time = 22009.23 ms / 803 runs ( 27.41 ms per token, 36.48 tokens per second)\n", "llama_print_timings: total time = 24988.34 ms\n" ] } ], "source": [ "print(answer(format_prompt(\"Quels sont les philosophes les plus influencés par Hegel ?\")))" ] }, { "cell_type": "markdown", "id": "516058c3-c764-4678-8a95-1d234e4c7f6a", "metadata": {}, "source": [ "# Embed a folder to a ChromaDB collection" ] }, { "cell_type": "markdown", "id": "71b71ca4-3e59-4cf9-a43a-2877eccfcf07", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "## Initialize a ChromaDB persistent collection" ] }, { "cell_type": "code", "execution_count": 58, "id": "d16feaee-27b2-4c8a-9f63-bee3a9c5c724", "metadata": {}, "outputs": [], "source": [ "chroma_client = chromadb.PersistentClient(path=\"./chromadb\")\n", "#chroma_client.delete_collection(name=\"cera\")\n", "collection = chroma_client.get_or_create_collection(name=\"cera\")" ] }, { "cell_type": "markdown", "id": "0adb9e64-bc3a-40c7-ab8f-c3b6bf39a15c", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "## Embed the text of a particular web page" ] }, { "cell_type": "code", "execution_count": 43, "id": "1d9f4699-8abc-45cd-a5e0-10ac6d3057f6", "metadata": {}, "outputs": [], "source": [ "def token_length(str):\n", " return len(tokenizer.encode(str, add_special_tokens=False))\n", "\n", "def passage_str(paragraphs, title):\n", " return f\"passage: {title}\\n\" + '\\n'.join(paragraphs)" ] }, { "cell_type": "code", "execution_count": 57, "id": "0e8d1502-3afd-482d-b096-950911ea0ebc", "metadata": {}, "outputs": [], "source": [ "def embed_page(filename, url, title, contents, tags, chroma_collection, embed_model, max_chunk_size=512):\n", " \n", " documents = []\n", " contents_to_embed = [contents]\n", " \n", " while contents_to_embed:\n", " last_item = contents_to_embed.pop()\n", " # (1) For the `multilingual-e5-large` embedding model, \n", " # the string of a document must be prepended with \"passage:\"\n", " # (2) Since the text of a webpage may have to be cut into many documents,\n", " # we always add the title of the webpage at the top of a document\n", " last_item_str = passage_str(last_item, title)\n", " last_item_token_length = token_length(last_item_str)\n", " \n", " if last_item_token_length > max_chunk_size:\n", " # If the text of the webpage, present in file `filename`, \n", " # contains more than `max_chunk_size` tokens, it must be divided \n", " # into multiple documents\n", " if len(last_item) > 1:\n", " # If there are many paragraphs in `last_item`, i.e. the current\n", " # part of the webpage for which an embedding will be made,\n", " # the length of `last_item` can be reduced by dividing its set of\n", " # paragraphs in half\n", " h = len(last_item) // 2\n", " last_item_h1 = last_item[:h]\n", " last_item_h2 = last_item[h:]\n", " contents_to_embed.append(last_item_h1)\n", " contents_to_embed.append(last_item_h2)\n", " else:\n", " # If `last_item` is made of only one long paragraph whose length is\n", " # larger than `chunk_size`, this paragraph will be divided into two parts.\n", " sentences = re.split(r'(?<=[.!?]) +', last_item[0])\n", " \n", " if len(sentences) > 1:\n", " # If there are multiple sentences, try to split into two parts\n", " i = 1\n", " while True:\n", " part1 = ' '.join(sentences[:i])\n", " part2 = ' '.join(sentences[i:])\n", " token_length_part_1 = token_length(passage_str([part1], title))\n", " token_length_part_2 = token_length(passage_str([part2], title))\n", " if (token_length_part_1 <= max_chunk_size and\n", " token_length_part_2 <= max_chunk_size) or \\\n", " token_length_part_1 > max_chunk_size:\n", " break\n", " i += 1\n", " else:\n", " # If there's only one long sentence or no suitable split found, split by words\n", " words = last_item[0].split()\n", " h = len(words) // 2\n", " part1 = ' '.join(words[:h])\n", " part2 = ' '.join(words[h:])\n", " \n", " contents_to_embed.append([part1])\n", " contents_to_embed.append([part2])\n", " else:\n", " documents.append(last_item_str)\n", "\n", " # We want the documents into which a webpage has been divided \n", " # to be in the natural reading order\n", " documents.reverse()\n", " embeddings = embed_model.encode(documents, normalize_embeddings=True)\n", " embeddings = embeddings.tolist()\n", "\n", " # We consider the subpart of an URL as tags describing the webpage\n", " # For example, \n", " # \"https://www.caisse-epargne.fr/rhone-alpes/professionnels/financer-projets-optimiser-tresorerie/\"\n", " # is associated to the tags:\n", " # tags[0] == 'rhone-alpes'\n", " # tags[1] == 'professionnels'\n", " # tags[2] == 'financer-projets-optimiser-tresorerie'\n", " if len(tags) < 2:\n", " category = ''\n", " else:\n", " if tags[0] == 'rhone-alpes':\n", " category = tags[1]\n", " else: category = tags[0]\n", " metadata = {'category': category, 'url': url}\n", " # All the documents corresponding to a same webpage have the same metadata, i.e. URL and category\n", " metadatas = [copy.deepcopy(metadata) for _ in range(len(documents))]\n", "\n", " ids = [filename + '-' + str(i+1) for i in range(len(documents))]\n", "\n", " chroma_collection.add(embeddings=embeddings, documents=documents, metadatas=metadatas, ids=ids)" ] }, { "cell_type": "markdown", "id": "cb3fc271-be0b-4532-978e-8215227fa8fd", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "## Embed all the webpages in a folder" ] }, { "cell_type": "code", "execution_count": 45, "id": "345c43c9-bc88-43c6-a0b4-5919a6893c9a", "metadata": {}, "outputs": [], "source": [ "def embed_folder(folder_path, chroma_collection, embed_model):\n", " for filename in os.listdir(folder_path):\n", " if filename.endswith('.txt'):\n", " file_path = os.path.join(folder_path, filename)\n", " with open(file_path, 'r') as file:\n", " file_contents = file.read()\n", " contents_lst = [str.replace('\\n',' ').replace('\\xa0', ' ') for str in file_contents.split('\\n\\n')]\n", " if len(contents_lst) < 3: # contents_lst[0] is the URL, contents_lst[1] is the title, the rest is the content\n", " continue\n", " url = contents_lst[0]\n", " if '?' in url: # URLs with a '?' corresponds to call to services and have no useful content\n", " continue\n", " title = contents_lst[1]\n", " if not title: # when the title is absent (or empty), the page has no interest\n", " continue\n", " print(f\"{filename} : Start\")\n", " prefix = 'https://www.caisse-epargne.fr/'\n", " suffix = url.replace(prefix, '')\n", " tags = suffix.split('/')\n", " tags = [tag for tag in tags if tag] # remove empty parts\n", " embed_page(filename, url, title, contents_lst[2:], tags, chroma_collection, embed_model)\n", " print(f\"{filename} : Done\")" ] }, { "cell_type": "markdown", "id": "db2c0bd4-c12f-410a-9311-512d3c61a30d", "metadata": {}, "source": [ "## Proceed to the embedding" ] }, { "cell_type": "code", "execution_count": 59, "id": "456a51b2-fae0-4173-9103-deb5a7a8e608", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "255a0eb096.txt : Start\n", "255a0eb096.txt : Done\n", "ce79680ee7.txt : Start\n", "ce79680ee7.txt : Done\n", "565b28a75b.txt : Start\n", "565b28a75b.txt : Done\n", "173fc21d3a.txt : Start\n", "173fc21d3a.txt : Done\n", "b3bd1cf160.txt : Start\n", "b3bd1cf160.txt : Done\n", "f73fb80f59.txt : Start\n", "f73fb80f59.txt : Done\n", "2b45bc13c2.txt : Start\n", "2b45bc13c2.txt : Done\n", "48787daff9.txt : Start\n", "48787daff9.txt : Done\n", "5f21a01035.txt : Start\n", "5f21a01035.txt : Done\n", "aa1030c5fd.txt : Start\n", "aa1030c5fd.txt : Done\n", "a040c90b55.txt : Start\n", "a040c90b55.txt : Done\n", "c3d469cbdb.txt : Start\n", "c3d469cbdb.txt : Done\n", "a6a1d2fea0.txt : Start\n", "a6a1d2fea0.txt : Done\n", "42a2928ef0.txt : Start\n", "42a2928ef0.txt : Done\n", "4eda4de449.txt : Start\n", "4eda4de449.txt : Done\n", "8cce840558.txt : Start\n", "8cce840558.txt : Done\n", "4a06529f5f.txt : Start\n", "4a06529f5f.txt : Done\n", "898d33ba09.txt : Start\n", "898d33ba09.txt : Done\n", "518b61af48.txt : Start\n", "518b61af48.txt : Done\n", "585b794776.txt : Start\n", "585b794776.txt : Done\n", "be98b7bc33.txt : Start\n", "be98b7bc33.txt : Done\n", "2e77392d24.txt : Start\n", "2e77392d24.txt : Done\n", "2db068af5c.txt : Start\n", "2db068af5c.txt : Done\n", "e6642ab0c4.txt : Start\n", "e6642ab0c4.txt : Done\n", "29e0f10548.txt : Start\n", "29e0f10548.txt : Done\n", "daa79fb76c.txt : Start\n", "daa79fb76c.txt : Done\n", "ba68c7b5b1.txt : Start\n", "ba68c7b5b1.txt : Done\n", "e9d58e832a.txt : Start\n", "e9d58e832a.txt : Done\n", "d950f8cfcf.txt : Start\n", "d950f8cfcf.txt : Done\n", "63fe8d8ecf.txt : Start\n", "63fe8d8ecf.txt : Done\n", "33337c3b38.txt : Start\n", "33337c3b38.txt : Done\n", "bd4b626414.txt : Start\n", "bd4b626414.txt : Done\n", "4c0e6dbaa6.txt : Start\n", "4c0e6dbaa6.txt : Done\n", "55262bf6bd.txt : Start\n", "55262bf6bd.txt : Done\n", "10edea0734.txt : Start\n", "10edea0734.txt : Done\n", "6035d44943.txt : Start\n", "6035d44943.txt : Done\n", "0480b7e99c.txt : Start\n", "0480b7e99c.txt : Done\n", "13e8a0bf51.txt : Start\n", "13e8a0bf51.txt : Done\n", "f03e1574c2.txt : Start\n", "f03e1574c2.txt : Done\n", "d5069cd6b3.txt : Start\n", "d5069cd6b3.txt : Done\n", "743fc26faf.txt : Start\n", "743fc26faf.txt : Done\n", "a164829b41.txt : Start\n", "a164829b41.txt : Done\n", "167def23bd.txt : Start\n", "167def23bd.txt : Done\n", "ac97457567.txt : Start\n", "ac97457567.txt : Done\n", "99bab73424.txt : Start\n", "99bab73424.txt : Done\n", "8c445da076.txt : Start\n", "8c445da076.txt : Done\n", "5f103512e6.txt : Start\n", "5f103512e6.txt : Done\n", "68b67c1aed.txt : Start\n", "68b67c1aed.txt : Done\n", "7a083e9237.txt : Start\n", "7a083e9237.txt : Done\n", "5883f2daa0.txt : Start\n", "5883f2daa0.txt : Done\n", "4aac6081e0.txt : Start\n", "4aac6081e0.txt : Done\n", "f74f2366a0.txt : Start\n", "f74f2366a0.txt : Done\n", "5a62906c0c.txt : Start\n", "5a62906c0c.txt : Done\n", "f66629c8b6.txt : Start\n", "f66629c8b6.txt : Done\n", "f6badfbd1a.txt : Start\n", "f6badfbd1a.txt : Done\n", "02c2716018.txt : Start\n", "02c2716018.txt : Done\n", "a3d470b7a1.txt : Start\n", "a3d470b7a1.txt : Done\n", "fe7a283831.txt : Start\n", "fe7a283831.txt : Done\n", "d850e8fd61.txt : Start\n", "d850e8fd61.txt : Done\n", "60465f4946.txt : Start\n", "60465f4946.txt : Done\n", "055634c516.txt : Start\n", "055634c516.txt : Done\n", "3241ba4473.txt : Start\n", "3241ba4473.txt : Done\n", "b236716617.txt : Start\n", "b236716617.txt : Done\n", "83b03c7962.txt : Start\n", "83b03c7962.txt : Done\n", "4bcb190e00.txt : Start\n", "4bcb190e00.txt : Done\n", "cd53afe27d.txt : Start\n", "cd53afe27d.txt : Done\n", "a4217688fc.txt : Start\n", "a4217688fc.txt : Done\n", "f8fbe32801.txt : Start\n", "f8fbe32801.txt : Done\n", "46b1703611.txt : Start\n", "46b1703611.txt : Done\n", "e5c158a765.txt : Start\n", "e5c158a765.txt : Done\n", "ee3c97b958.txt : Start\n", "ee3c97b958.txt : Done\n", "d482034404.txt : Start\n", "d482034404.txt : Done\n", "ad509b796a.txt : Start\n", "ad509b796a.txt : Done\n", "79e77efef1.txt : Start\n", "79e77efef1.txt : Done\n", "76675b7013.txt : Start\n", "76675b7013.txt : Done\n", "2f0acaea22.txt : Start\n", "2f0acaea22.txt : Done\n", "4c10109965.txt : Start\n", "4c10109965.txt : Done\n", "f311309f86.txt : Start\n", "f311309f86.txt : Done\n", "b176019d5a.txt : Start\n", "b176019d5a.txt : Done\n", "351e959cf7.txt : Start\n", "351e959cf7.txt : Done\n", "65d86a3b7f.txt : Start\n", "65d86a3b7f.txt : Done\n", "4bdc40e75b.txt : Start\n", "4bdc40e75b.txt : Done\n", "a31b7de525.txt : Start\n", "a31b7de525.txt : Done\n", "b7e3354b7b.txt : Start\n", "b7e3354b7b.txt : Done\n", "0b5be6abc0.txt : Start\n", "0b5be6abc0.txt : Done\n", "37a7d20803.txt : Start\n", "37a7d20803.txt : Done\n", "8ec5dbe18a.txt : Start\n", "8ec5dbe18a.txt : Done\n", "3bb79284a3.txt : Start\n", "3bb79284a3.txt : Done\n", "a6c4cc430f.txt : Start\n", "a6c4cc430f.txt : Done\n", "215d3fc7da.txt : Start\n", "215d3fc7da.txt : Done\n", "c776277507.txt : Start\n", "c776277507.txt : Done\n", "7526cecc15.txt : Start\n", "7526cecc15.txt : Done\n", "624a63445d.txt : Start\n", "624a63445d.txt : Done\n", "e9e060442f.txt : Start\n", "e9e060442f.txt : Done\n", "347dd2758c.txt : Start\n", "347dd2758c.txt : Done\n", "c37a229373.txt : Start\n", "c37a229373.txt : Done\n", "6f1b7077d4.txt : Start\n", "6f1b7077d4.txt : Done\n", "35d30e0ade.txt : Start\n", "35d30e0ade.txt : Done\n", "80bbbcbf2c.txt : Start\n", "80bbbcbf2c.txt : Done\n", "7ba26ad0e8.txt : Start\n", "7ba26ad0e8.txt : Done\n", "182588fbbf.txt : Start\n", "182588fbbf.txt : Done\n", "de94e4b189.txt : Start\n", "de94e4b189.txt : Done\n", "e45b0e5b68.txt : Start\n", "e45b0e5b68.txt : Done\n", "e2638b3436.txt : Start\n", "e2638b3436.txt : Done\n", "dd0a2ca5ac.txt : Start\n", "dd0a2ca5ac.txt : Done\n", "2e078d988f.txt : Start\n", "2e078d988f.txt : Done\n", "23746976e8.txt : Start\n", "23746976e8.txt : Done\n", "b4ab821d94.txt : Start\n", "b4ab821d94.txt : Done\n", "25dcc77e00.txt : Start\n", "25dcc77e00.txt : Done\n", "c7eb6cf9c8.txt : Start\n", "c7eb6cf9c8.txt : Done\n", "16e59d18b0.txt : Start\n", "16e59d18b0.txt : Done\n", "364b4d5147.txt : Start\n", "364b4d5147.txt : Done\n", "faf51f605e.txt : Start\n", "faf51f605e.txt : Done\n", "84d61f33dc.txt : Start\n", "84d61f33dc.txt : Done\n", "4c578fd78c.txt : Start\n", "4c578fd78c.txt : Done\n", "489cc63745.txt : Start\n", "489cc63745.txt : Done\n", "acdf6c334e.txt : Start\n", "acdf6c334e.txt : Done\n", "d720fc241d.txt : Start\n", "d720fc241d.txt : Done\n", "9c16ef30f1.txt : Start\n", "9c16ef30f1.txt : Done\n", "2426f559ec.txt : Start\n", "2426f559ec.txt : Done\n", "a0cabc58ba.txt : Start\n", "a0cabc58ba.txt : Done\n", "aabc3549f4.txt : Start\n", "aabc3549f4.txt : Done\n", "3bbb946fb3.txt : Start\n", "3bbb946fb3.txt : Done\n", "4a5736d002.txt : Start\n", "4a5736d002.txt : Done\n", "b986d4224b.txt : Start\n", "b986d4224b.txt : Done\n", "acd10eaae3.txt : Start\n", "acd10eaae3.txt : Done\n", "6bac4f4e5a.txt : Start\n", "6bac4f4e5a.txt : Done\n", "edea3bec7b.txt : Start\n", "edea3bec7b.txt : Done\n", "6611e528c9.txt : Start\n", "6611e528c9.txt : Done\n", "54ce3f1081.txt : Start\n", "54ce3f1081.txt : Done\n", "1843d92a2e.txt : Start\n", "1843d92a2e.txt : Done\n", "69f0b5362f.txt : Start\n", "69f0b5362f.txt : Done\n", "2ac98f70a3.txt : Start\n", "2ac98f70a3.txt : Done\n", "4d4eef5de1.txt : Start\n", "4d4eef5de1.txt : Done\n", "3598605354.txt : Start\n", "3598605354.txt : Done\n", "8d9da55793.txt : Start\n", "8d9da55793.txt : Done\n", "90efeedace.txt : Start\n", "90efeedace.txt : Done\n", "b222a935a2.txt : Start\n", "b222a935a2.txt : Done\n", "9a54cdc2d6.txt : Start\n", "9a54cdc2d6.txt : Done\n", "7d9e416887.txt : Start\n", "7d9e416887.txt : Done\n", "a2f809e07a.txt : Start\n", "a2f809e07a.txt : Done\n", "f82f526635.txt : Start\n", "f82f526635.txt : Done\n", "49fc042883.txt : Start\n", "49fc042883.txt : Done\n", "401b54bb3a.txt : Start\n", "401b54bb3a.txt : Done\n", "e88e322e77.txt : Start\n", "e88e322e77.txt : Done\n", "9ff584c6a9.txt : Start\n", "9ff584c6a9.txt : Done\n", "77dd96cdb0.txt : Start\n", "77dd96cdb0.txt : Done\n", "d49e768bef.txt : Start\n", "d49e768bef.txt : Done\n", "c145d2aa96.txt : Start\n", "c145d2aa96.txt : Done\n", "618b26d15d.txt : Start\n", "618b26d15d.txt : Done\n", "d609cf2c68.txt : Start\n", "d609cf2c68.txt : Done\n", "3d159cbe89.txt : Start\n", "3d159cbe89.txt : Done\n", "e53a699f5b.txt : Start\n", "e53a699f5b.txt : Done\n", "7bee3a53d3.txt : Start\n", "7bee3a53d3.txt : Done\n", "40e0072e9e.txt : Start\n", "40e0072e9e.txt : Done\n", "55da73699b.txt : Start\n", "55da73699b.txt : Done\n", "e60e8a6463.txt : Start\n", "e60e8a6463.txt : Done\n", "5e0583f23a.txt : Start\n", "5e0583f23a.txt : Done\n", "b2be33ebf5.txt : Start\n", "b2be33ebf5.txt : Done\n", "ad5d201dda.txt : Start\n", "ad5d201dda.txt : Done\n", "d48b16c825.txt : Start\n", "d48b16c825.txt : Done\n", "2a518c5c8a.txt : Start\n", "2a518c5c8a.txt : Done\n", "88346ffb46.txt : Start\n", "88346ffb46.txt : Done\n", "3f3e46760c.txt : Start\n", "3f3e46760c.txt : Done\n", "dbbeb6c739.txt : Start\n", "dbbeb6c739.txt : Done\n", "8e6a00bdc1.txt : Start\n", "8e6a00bdc1.txt : Done\n", "80a9d4e127.txt : Start\n", "80a9d4e127.txt : Done\n", "833d60cb6c.txt : Start\n", "833d60cb6c.txt : Done\n", "2c55313e35.txt : Start\n", "2c55313e35.txt : Done\n", "cd62a89eed.txt : Start\n", "cd62a89eed.txt : Done\n", "78686c36f9.txt : Start\n", "78686c36f9.txt : Done\n", "e688957c46.txt : Start\n", "e688957c46.txt : Done\n", "7aa9e50a1f.txt : Start\n", "7aa9e50a1f.txt : Done\n", "51e2ac338a.txt : Start\n", "51e2ac338a.txt : Done\n", "51a68d9313.txt : Start\n", "51a68d9313.txt : Done\n", "5fb7a523a7.txt : Start\n", "5fb7a523a7.txt : Done\n", "f49d62acf6.txt : Start\n", "f49d62acf6.txt : Done\n", "3ced86d1db.txt : Start\n", "3ced86d1db.txt : Done\n", "debbca28fa.txt : Start\n", "debbca28fa.txt : Done\n", "bfeaa8b440.txt : Start\n", "bfeaa8b440.txt : Done\n", "9ddab20d94.txt : Start\n", "9ddab20d94.txt : Done\n", "66df15f567.txt : Start\n", "66df15f567.txt : Done\n", "f42bf02416.txt : Start\n", "f42bf02416.txt : Done\n", "c8234b7b50.txt : Start\n", "c8234b7b50.txt : Done\n", "4c0aba7cbb.txt : Start\n", "4c0aba7cbb.txt : Done\n", "2e41ac0342.txt : Start\n", "2e41ac0342.txt : Done\n", "00e9b7dba8.txt : Start\n", "00e9b7dba8.txt : Done\n", "3add1ed406.txt : Start\n", "3add1ed406.txt : Done\n", "69f188b16a.txt : Start\n", "69f188b16a.txt : Done\n", "73b277c156.txt : Start\n", "73b277c156.txt : Done\n", "d2ad8952b8.txt : Start\n", "d2ad8952b8.txt : Done\n", "f77eddd823.txt : Start\n", "f77eddd823.txt : Done\n", "07852c3122.txt : Start\n", "07852c3122.txt : Done\n", "966c432839.txt : Start\n", "966c432839.txt : Done\n", "8ced9fefee.txt : Start\n", "8ced9fefee.txt : Done\n", "06c152d200.txt : Start\n", "06c152d200.txt : Done\n", "f11b7b8fce.txt : Start\n", "f11b7b8fce.txt : Done\n", "09b3f765cf.txt : Start\n", "09b3f765cf.txt : Done\n", "861738ff97.txt : Start\n", "861738ff97.txt : Done\n", "c98c007358.txt : Start\n", "c98c007358.txt : Done\n", "1e209c2f7c.txt : Start\n", "1e209c2f7c.txt : Done\n", "34dc461327.txt : Start\n", "34dc461327.txt : Done\n", "592a315fc4.txt : Start\n", "592a315fc4.txt : Done\n", "f8f9a61e79.txt : Start\n", "f8f9a61e79.txt : Done\n", "49f945e30f.txt : Start\n", "49f945e30f.txt : Done\n", "b39e06064b.txt : Start\n", "b39e06064b.txt : Done\n", "6b44932499.txt : Start\n", "6b44932499.txt : Done\n", "3bb5f27823.txt : Start\n", "3bb5f27823.txt : Done\n", "a54dfc4265.txt : Start\n", "a54dfc4265.txt : Done\n", "3bbe30b18a.txt : Start\n", "3bbe30b18a.txt : Done\n", "27582f5949.txt : Start\n", "27582f5949.txt : Done\n", "ac82a34e3e.txt : Start\n", "ac82a34e3e.txt : Done\n", "cbd0706bd5.txt : Start\n", "cbd0706bd5.txt : Done\n", "d38e72ab4a.txt : Start\n", "d38e72ab4a.txt : Done\n", "d6b0c46314.txt : Start\n", "d6b0c46314.txt : Done\n", "7549112ce8.txt : Start\n", "7549112ce8.txt : Done\n", "c64375ea25.txt : Start\n", "c64375ea25.txt : Done\n", "5242939ba7.txt : Start\n", "5242939ba7.txt : Done\n", "51e1e6a825.txt : Start\n", "51e1e6a825.txt : Done\n", "7cde74e885.txt : Start\n", "7cde74e885.txt : Done\n", "d8adaab329.txt : Start\n", "d8adaab329.txt : Done\n", "79f444033c.txt : Start\n", "79f444033c.txt : Done\n", "75bef15d20.txt : Start\n", "75bef15d20.txt : Done\n", "ab8dbcdc59.txt : Start\n", "ab8dbcdc59.txt : Done\n", "ed1f98efc6.txt : Start\n", "ed1f98efc6.txt : Done\n", "a20a7a7bb1.txt : Start\n", "a20a7a7bb1.txt : Done\n", "c3d7990cb5.txt : Start\n", "c3d7990cb5.txt : Done\n", "ba4dbfd475.txt : Start\n", "ba4dbfd475.txt : Done\n", "059eb08b87.txt : Start\n", "059eb08b87.txt : Done\n", "df20818635.txt : Start\n", "df20818635.txt : Done\n", "4eff143f3f.txt : Start\n", "4eff143f3f.txt : Done\n", "eff4b7ddd6.txt : Start\n", "eff4b7ddd6.txt : Done\n", "0e81a328c1.txt : Start\n", "0e81a328c1.txt : Done\n", "75b4d7d9a7.txt : Start\n", "75b4d7d9a7.txt : Done\n", "5e2ba7946b.txt : Start\n", "5e2ba7946b.txt : Done\n", "d0043c604c.txt : Start\n", "d0043c604c.txt : Done\n", "e7c857ad2b.txt : Start\n", "e7c857ad2b.txt : Done\n", "a8c6752cce.txt : Start\n", "a8c6752cce.txt : Done\n", "165517dfe7.txt : Start\n", "165517dfe7.txt : Done\n", "61d25d4835.txt : Start\n", "61d25d4835.txt : Done\n", "9e3c26ac16.txt : Start\n", "9e3c26ac16.txt : Done\n", "e6bb195473.txt : Start\n", "e6bb195473.txt : Done\n", "836c1b074b.txt : Start\n", "836c1b074b.txt : Done\n", "83e2969710.txt : Start\n", "83e2969710.txt : Done\n", "644186a433.txt : Start\n", "644186a433.txt : Done\n", "a75aa7df01.txt : Start\n", "a75aa7df01.txt : Done\n", "a0398ee914.txt : Start\n", "a0398ee914.txt : Done\n", "bc2a847131.txt : Start\n", "bc2a847131.txt : Done\n", "7868bec686.txt : Start\n", "7868bec686.txt : Done\n", "63f1d8dabd.txt : Start\n", "63f1d8dabd.txt : Done\n", "ce669e66f8.txt : Start\n", "ce669e66f8.txt : Done\n", "792190c4e9.txt : Start\n", "792190c4e9.txt : Done\n", "29c3723901.txt : Start\n", "29c3723901.txt : Done\n", "8ae6d05877.txt : Start\n", "8ae6d05877.txt : Done\n", "dd05acb3cb.txt : Start\n", "dd05acb3cb.txt : Done\n", "a817449f48.txt : Start\n", "a817449f48.txt : Done\n", "967cdecccf.txt : Start\n", "967cdecccf.txt : Done\n", "4ebd32b3b4.txt : Start\n", "4ebd32b3b4.txt : Done\n", "a60556b6b3.txt : Start\n", "a60556b6b3.txt : Done\n", "a719b96889.txt : Start\n", "a719b96889.txt : Done\n", "c079c36809.txt : Start\n", "c079c36809.txt : Done\n", "c0a4d8856e.txt : Start\n", "c0a4d8856e.txt : Done\n", "e707711e05.txt : Start\n", "e707711e05.txt : Done\n", "f7217535d5.txt : Start\n", "f7217535d5.txt : Done\n", "09bb9ce67b.txt : Start\n", "09bb9ce67b.txt : Done\n", "c6e4320cce.txt : Start\n", "c6e4320cce.txt : Done\n", "1255f41d67.txt : Start\n", "1255f41d67.txt : Done\n", "d84a0d7a60.txt : Start\n", "d84a0d7a60.txt : Done\n", "e912b95fb7.txt : Start\n", "e912b95fb7.txt : Done\n", "6c91560cb6.txt : Start\n", "6c91560cb6.txt : Done\n", "23d4b1a861.txt : Start\n", "23d4b1a861.txt : Done\n", "56fcef62ef.txt : Start\n", "56fcef62ef.txt : Done\n", "8ef40bc843.txt : Start\n", "8ef40bc843.txt : Done\n", "ee1f44ee32.txt : Start\n", "ee1f44ee32.txt : Done\n", "6667c30343.txt : Start\n", "6667c30343.txt : Done\n", "a6472532fd.txt : Start\n", "a6472532fd.txt : Done\n", "d47b684de9.txt : Start\n", "d47b684de9.txt : Done\n", "a6e9d84a26.txt : Start\n", "a6e9d84a26.txt : Done\n", "8cb1148ce2.txt : Start\n", "8cb1148ce2.txt : Done\n", "2041450bbc.txt : Start\n", "2041450bbc.txt : Done\n", "c9b741cf85.txt : Start\n", "c9b741cf85.txt : Done\n", "3b494f8454.txt : Start\n", "3b494f8454.txt : Done\n", "7b58b43097.txt : Start\n", "7b58b43097.txt : Done\n", "f6d921ced8.txt : Start\n", "f6d921ced8.txt : Done\n", "a774f5dcb0.txt : Start\n", "a774f5dcb0.txt : Done\n", "a607314ff3.txt : Start\n", "a607314ff3.txt : Done\n", "47d6b05426.txt : Start\n", "47d6b05426.txt : Done\n", "7f501b252e.txt : Start\n", "7f501b252e.txt : Done\n", "6d23b1599b.txt : Start\n", "6d23b1599b.txt : Done\n", "e854f24006.txt : Start\n", "e854f24006.txt : Done\n", "02b3aa0475.txt : Start\n", "02b3aa0475.txt : Done\n", "0bb6c71f04.txt : Start\n", "0bb6c71f04.txt : Done\n", "b23bb1811f.txt : Start\n", "b23bb1811f.txt : Done\n", "d7e4d2661b.txt : Start\n", "d7e4d2661b.txt : Done\n", "d1b7f85297.txt : Start\n", "d1b7f85297.txt : Done\n", "5007221a16.txt : Start\n", "5007221a16.txt : Done\n", "e3e5d4095f.txt : Start\n", "e3e5d4095f.txt : Done\n", "6a5a514a6d.txt : Start\n", "6a5a514a6d.txt : Done\n", "b679419d52.txt : Start\n", "b679419d52.txt : Done\n", "beb8106365.txt : Start\n", "beb8106365.txt : Done\n", "56b5faeff6.txt : Start\n", "56b5faeff6.txt : Done\n", "f4e750afc4.txt : Start\n", "f4e750afc4.txt : Done\n", "13880701f6.txt : Start\n", "13880701f6.txt : Done\n", "28c6c56435.txt : Start\n", "28c6c56435.txt : Done\n", "318852640c.txt : Start\n", "318852640c.txt : Done\n", "062719aca5.txt : Start\n", "062719aca5.txt : Done\n", "0e46adf00f.txt : Start\n", "0e46adf00f.txt : Done\n", "6507087bb8.txt : Start\n", "6507087bb8.txt : Done\n", "f72f737ba9.txt : Start\n", "f72f737ba9.txt : Done\n", "ad1b4fddea.txt : Start\n", "ad1b4fddea.txt : Done\n", "2c8100c054.txt : Start\n", "2c8100c054.txt : Done\n", "a21f6dd6e2.txt : Start\n", "a21f6dd6e2.txt : Done\n", "c98558432b.txt : Start\n", "c98558432b.txt : Done\n", "25594d754c.txt : Start\n", "25594d754c.txt : Done\n", "f378d7a627.txt : Start\n", "f378d7a627.txt : Done\n", "00de2ed323.txt : Start\n", "00de2ed323.txt : Done\n", "c548857fc6.txt : Start\n", "c548857fc6.txt : Done\n", "fda6960842.txt : Start\n", "fda6960842.txt : Done\n", "3dbfdeb28e.txt : Start\n", "3dbfdeb28e.txt : Done\n", "667b92c747.txt : Start\n", "667b92c747.txt : Done\n", "2f45fbeb96.txt : Start\n", "2f45fbeb96.txt : Done\n", "ea8ade1ec9.txt : Start\n", "ea8ade1ec9.txt : Done\n", "2500693c8c.txt : Start\n", "2500693c8c.txt : Done\n", "b3ec5aec10.txt : Start\n", "b3ec5aec10.txt : Done\n", "c7c60ec498.txt : Start\n", "c7c60ec498.txt : Done\n", "2639b3f232.txt : Start\n", "2639b3f232.txt : Done\n", "05cebd07f6.txt : Start\n", "05cebd07f6.txt : Done\n", "2c70d7a211.txt : Start\n", "2c70d7a211.txt : Done\n", "c482ae0d6a.txt : Start\n", "c482ae0d6a.txt : Done\n", "c8b2e1ea64.txt : Start\n", "c8b2e1ea64.txt : Done\n", "cc6023fd33.txt : Start\n", "cc6023fd33.txt : Done\n", "66dcfdc952.txt : Start\n", "66dcfdc952.txt : Done\n", "db3997fa66.txt : Start\n", "db3997fa66.txt : Done\n", "5cc73e5eb8.txt : Start\n", "5cc73e5eb8.txt : Done\n", "e2c63447a3.txt : Start\n", "e2c63447a3.txt : Done\n", "bf9981f1c7.txt : Start\n", "bf9981f1c7.txt : Done\n", "f9c1d63857.txt : Start\n", "f9c1d63857.txt : Done\n", "f5ad27d7c0.txt : Start\n", "f5ad27d7c0.txt : Done\n", "9c6cf67aa0.txt : Start\n", "9c6cf67aa0.txt : Done\n", "2a6ed256ac.txt : Start\n", "2a6ed256ac.txt : Done\n", "a0bc734be1.txt : Start\n", "a0bc734be1.txt : Done\n", "84aa61e2cd.txt : Start\n", "84aa61e2cd.txt : Done\n", "94a849a3dd.txt : Start\n", "94a849a3dd.txt : Done\n", "d17d48de9f.txt : Start\n", "d17d48de9f.txt : Done\n", "441b6dd9fb.txt : Start\n", "441b6dd9fb.txt : Done\n", "81ef81f563.txt : Start\n", "81ef81f563.txt : Done\n", "d5be4d3e85.txt : Start\n", "d5be4d3e85.txt : Done\n", "a968ac3598.txt : Start\n", "a968ac3598.txt : Done\n", "bd56123a22.txt : Start\n", "bd56123a22.txt : Done\n", "f93308201d.txt : Start\n", "f93308201d.txt : Done\n", "0b8e6baade.txt : Start\n", "0b8e6baade.txt : Done\n", "5ef2ba0d6f.txt : Start\n", "5ef2ba0d6f.txt : Done\n", "f07322d8df.txt : Start\n", "f07322d8df.txt : Done\n", "ef44110d87.txt : Start\n", "ef44110d87.txt : Done\n", "cf19743995.txt : Start\n", "cf19743995.txt : Done\n", "c073cfa358.txt : Start\n", "c073cfa358.txt : Done\n", "5f9c4f1f1f.txt : Start\n", "5f9c4f1f1f.txt : Done\n", "4de1c37ee8.txt : Start\n", "4de1c37ee8.txt : Done\n", "694551449e.txt : Start\n", "694551449e.txt : Done\n", "4adf02d48f.txt : Start\n", "4adf02d48f.txt : Done\n", "75ed4241e1.txt : Start\n", "75ed4241e1.txt : Done\n", "e73d5574d3.txt : Start\n", "e73d5574d3.txt : Done\n", "7e75ecd1e8.txt : Start\n", "7e75ecd1e8.txt : Done\n", "de31d88f0f.txt : Start\n", "de31d88f0f.txt : Done\n", "a7b7f0cf8b.txt : Start\n", "a7b7f0cf8b.txt : Done\n", "385444771f.txt : Start\n", "385444771f.txt : Done\n", "55022ab079.txt : Start\n", "55022ab079.txt : Done\n", "849689b4a1.txt : Start\n", "849689b4a1.txt : Done\n", "b278eed9ba.txt : Start\n", "b278eed9ba.txt : Done\n", "9e3e6ecdc1.txt : Start\n", "9e3e6ecdc1.txt : Done\n", "e37b2c1048.txt : Start\n", "e37b2c1048.txt : Done\n", "b366762650.txt : Start\n", "b366762650.txt : Done\n", "3c25273538.txt : Start\n", "3c25273538.txt : Done\n", "00c24abbbd.txt : Start\n", "00c24abbbd.txt : Done\n", "a6320d9006.txt : Start\n", "a6320d9006.txt : Done\n", "56a7a9450b.txt : Start\n", "56a7a9450b.txt : Done\n", "73f0b14dc0.txt : Start\n", "73f0b14dc0.txt : Done\n", "940cb1ea4b.txt : Start\n", "940cb1ea4b.txt : Done\n", "753aefd89c.txt : Start\n", "753aefd89c.txt : Done\n", "b56d0efe5f.txt : Start\n", "b56d0efe5f.txt : Done\n", "a6575b6354.txt : Start\n", "a6575b6354.txt : Done\n", "ff15e2aa90.txt : Start\n", "ff15e2aa90.txt : Done\n", "4aeb967bdb.txt : Start\n", "4aeb967bdb.txt : Done\n", "e8e3f5c3da.txt : Start\n", "e8e3f5c3da.txt : Done\n", "84fd5c6c82.txt : Start\n", "84fd5c6c82.txt : Done\n", "ba21aab5a7.txt : Start\n", "ba21aab5a7.txt : Done\n", "0eb135a63b.txt : Start\n", "0eb135a63b.txt : Done\n", "01f4ec6a93.txt : Start\n", "01f4ec6a93.txt : Done\n", "c96f15eb18.txt : Start\n", "c96f15eb18.txt : Done\n", "d36c2f7f4a.txt : Start\n", "d36c2f7f4a.txt : Done\n", "474efbd959.txt : Start\n", "474efbd959.txt : Done\n", "191dd3cd6e.txt : Start\n", "191dd3cd6e.txt : Done\n", "586213807b.txt : Start\n", "586213807b.txt : Done\n", "abc07266f1.txt : Start\n", "abc07266f1.txt : Done\n", "4dc74ef251.txt : Start\n", "4dc74ef251.txt : Done\n", "4c0f76583e.txt : Start\n", "4c0f76583e.txt : Done\n", "dafc4b7fbb.txt : Start\n", "dafc4b7fbb.txt : Done\n", "6123ccc4f8.txt : Start\n", "6123ccc4f8.txt : Done\n", "b4ecf8ff62.txt : Start\n", "b4ecf8ff62.txt : Done\n", "b1a1fbb48b.txt : Start\n", "b1a1fbb48b.txt : Done\n", "436afdfd4e.txt : Start\n", "436afdfd4e.txt : Done\n", "5edd73f58b.txt : Start\n", "5edd73f58b.txt : Done\n", "e495483bdd.txt : Start\n", "e495483bdd.txt : Done\n" ] } ], "source": [ "embed_folder('docs/cera2', collection, embed_model)" ] }, { "cell_type": "markdown", "id": "227ce12e-f33e-485d-a3d2-0e131279776d", "metadata": {}, "source": [ "# Query the ChromaDB collection" ] }, { "cell_type": "code", "execution_count": 114, "id": "8a81c2c1-4d94-47f2-8998-f65e01505629", "metadata": {}, "outputs": [], "source": [ "def query_collection(query, n_results=3):\n", " query = 'query: ' + query\n", " query_embedding = embed_model.encode(query, normalize_embeddings=True)\n", " query_embedding = query_embedding.tolist()\n", " results = collection.query(\n", " query_embeddings=[query_embedding],\n", " n_results=n_results,\n", " )\n", " return results" ] }, { "cell_type": "code", "execution_count": 401, "id": "5ccd0b6d-32d8-497e-aca6-38f84c8e1f4d", "metadata": {}, "outputs": [], "source": [ "query = \"Comment la Caisse d'Epargne Rhône-Alpes peut-elle aider une entreprise qui rencontre des problèmes de trésorerie ?\"\n", "query_results = query_collection(query)" ] }, { "cell_type": "markdown", "id": "13153d19-b9d5-482c-82e7-8eca1fad5bcd", "metadata": {}, "source": [ "# RAG prompt" ] }, { "cell_type": "code", "execution_count": 402, "id": "1550331f-4fca-43ec-80ba-2b8d3ed6a37c", "metadata": {}, "outputs": [], "source": [ "def format_passages(query_results):\n", " result = []\n", " for i in range(len(query_results[\"documents\"][0])):\n", " passage = query_results[\"documents\"][0][i]\n", " url = query_results[\"metadatas\"][0][i][\"url\"]\n", " category = query_results[\"metadatas\"][0][i][\"category\"]\n", " lines = passage.split('\\n')\n", " if lines[0].startswith('passage: '):\n", " lines[0] = lines[0].replace('passage: ', '')\n", " lines.insert(0, \"###\")\n", " lines.insert(1, f\"Passage {i+1}\")\n", " lines.insert(2, \"Titre :\")\n", " lines.insert(4, \"\")\n", " lines.insert(5, \"Catégorie :\")\n", " lines.insert(6, category)\n", " lines.insert(7, \"\")\n", " lines.insert(8, \"URL :\")\n", " lines.insert(9, url)\n", " lines.insert(10, \"\")\n", " lines.insert(11, \"Contenu : \")\n", " lines += ['']\n", " result += lines\n", " result = '\\n'.join(result)\n", " return result\n", " " ] }, { "cell_type": "code", "execution_count": 417, "id": "b70c180e-9929-4c5f-9499-aadedd55cf3f", "metadata": {}, "outputs": [], "source": [ "rag_system_prompt = \"\"\"\n", "Vous êtes un assistant IA qui répond à la question posée par l'utilisateur en utilisant un contexte répertorié ci-dessous dans la rubrique Contexte.\n", "Le contexte est formé de passages exraits du site web commercial de la Caisse d'Epargne Rhône-Alpes, une banque française régionale.\n", "Votre réponse ne doit pas mentionner des informations déjà présentes dans l'historique de la conversation qui est répertorié ci-dessous dans la rubrique Historique.\n", "Vous fournissez avec soin des réponses précises, factuelles, réfléchies et nuancées, et vous êtes doué pour le raisonnement.\n", "Toutes les informations factuelles que vous utilisez pour répondre proviennent exclusivement du contexte.\n", "Si vous ne pouvez pas répondre à la question sur la base des éléments du contexte, dites simplement que vous ne savez pas, n'essayez pas d'inventer une réponse.\n", "Vos réponses doivent être brèves.\n", "Vous rédigez vos réponses en français au format markdown sous forme d'une liste d'au plus 7 éléments.\n", "Voici le format que doit prendre votre réponse :\n", "```\n", "1. Elément de réponse. (Passage 1)\n", "2. Elément de réponse. (Passage 1)\n", "3. Elément de réponse. (Passage 2)\n", "4. ...\n", "```\n", "\n", "----------------------------------------\n", "Historique :\n", "{history}\n", "----------------------------------------\n", "Contexte :\n", "{context}\n", "\"\"\"\n", "\n", "def format_rag_prompt(query, context=\"\", history=\"\"):\n", " global chat_history\n", " \n", " user_query = f\"Question de l'utilisateur : \\n{query}\\n\\n\"\n", " assistant_answer = f\"Réponse de l'assistant : \\n 1. \"\n", " chat_history.append({'user': user_query, 'assistant': assistant_answer})\n", "\n", " system_prompt = rag_system_prompt.format(history=history, context=context)\n", " \n", " prompt = \"\"\n", " prompt = f\"<|system|>\\n{system_prompt.strip()} \\n\\n\"\n", " prompt += f\"<|user|>\\n{query} \\n\\n\"\n", " prompt += f\"<|assistant|>\\n Voici des éléments de réponse : \\n 1. \"\n", " \n", " return prompt" ] }, { "cell_type": "code", "execution_count": 418, "id": "58ef118f-345b-46b5-a844-0e82bb4efe53", "metadata": {}, "outputs": [], "source": [ "def answer_rag_prompt(query, query_results):\n", " global chat_history\n", " \n", " query_context = format_passages(query_results)\n", "\n", " history = \"\"\n", " for i in reversed(range(len(chat_history))):\n", " history += chat_history[i][\"user\"]\n", " history += chat_history[i][\"assistant\"]\n", " history += \"\\n\\n\"\n", " \n", " prompt = format_rag_prompt(query, query_context, history)\n", " \n", " ans = answer(prompt)\n", " chat_history[-1]['assistant'] += ans\n", " ans = '1. ' + ans\n", " \n", " return ans" ] }, { "cell_type": "code", "execution_count": 419, "id": "cf9f7e2d-616e-46fb-a1c4-9fa2e3bf01ea", "metadata": {}, "outputs": [], "source": [ "chat_history = []" ] }, { "cell_type": "code", "execution_count": 420, "id": "c58035d9-02c4-4da2-91aa-b745690a268f", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Llama.generate: prefix-match hit\n", "\n", "llama_print_timings: load time = 575.42 ms\n", "llama_print_timings: sample time = 7686.78 ms / 2270 runs ( 3.39 ms per token, 295.31 tokens per second)\n", "llama_print_timings: prompt eval time = 4638.92 ms / 1729 tokens ( 2.68 ms per token, 372.72 tokens per second)\n", "llama_print_timings: eval time = 80992.02 ms / 2269 runs ( 35.70 ms per token, 28.02 tokens per second)\n", "llama_print_timings: total time = 93681.23 ms\n" ] } ], "source": [ "ans = answer_rag_prompt(query, query_results)" ] }, { "cell_type": "code", "execution_count": 421, "id": "f11c9e3d-5544-4d0c-a3e0-9e395826d948", "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "1. La Caisse d'Epargne Rhône Alpes propose un ensemble de solutions pour optimiser votre trésorerie sans attendre le règlement de vos factures clients, appelé l'affacturage. (Passage 1)\n", " 2. Vous pouvez également accéder à leur simulateur et obtenir une préconisation sur le mode de financement le plus adapté à votre usage ainsi que de nombreuses informations sur la fiscalité automobile. (Passage 1)\n", " 3. À taux fixe ou variable, les prêts classiques vous accompagnent dans le développement de votre entreprise. (Passage 1)\n", " 4. La Caisse d'Epargne Rhône Alpes vous propose également un financement adapté pour vos projets de création, reprise ou développement de votre activité professionnelle avec un ensemble d'offres et de conseils pour vous accompagner dans le financement de vos projets. (Passage 1)\n", " 5. En outre, la Caisse d'Epargne Rhône Alpes vous accompagne dans votre transition énergétique avec des offres sur mesure en fonction de vos besoins. (Passage 1)\n", " 6. Vous pouvez également rencontrer un conseiller en ligne, à l'agence ou par téléphone à l'horaire de votre choix pour obtenir des solutions adaptées à votre situation financière compliquée. (Passage 2)\n", " 7. La Caisse d'Epargne Rhône Alpes considère que c’est l’une de ses responsabilités sociétales d’être une banque inclusive et engagée pour être utile à nos clients, en proposant un dispositif d’écoute et d’accueil des clients en situation de fragilité, du fait d’un handicap ou de difficultés financières, permettant d’adapter ses services à leurs besoins spécifiques et de maintenir en toute situation une écoute attentive et des solutions personnalisées. (Passage 2)\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(Markdown(ans))" ] }, { "cell_type": "code", "execution_count": 221, "id": "18258b92-2161-44fd-b5a9-2ae575202d20", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1889" ] }, "execution_count": 221, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# len(llm.tokenize(format_rag_prompt_init(query, query_results).encode(encoding='utf-8')))" ] }, { "cell_type": "code", "execution_count": 422, "id": "95600c84-52cf-4bb7-b12a-845aa202653d", "metadata": {}, "outputs": [], "source": [ "# reformulate query\n", "\n", "query_reformulate_system_prompt = \"\"\"\\\n", "Vous êtes un interprète conversationnel pour une conversation entre un utilisateur et \\\n", "un assistant IA spécialiste des produits et services de la Caisse d'Epargne Rhône-Alpes, \\\n", "une banque régionale française. \\n \\\n", "L'utilisateur vous posera une question sans contexte. \\\n", "Vous devez reformuler la question pour prendre en compte le contexte de la conversation. \\n \\\n", "Vous devez supposer que la question est liée aux produits et services de la Caisse d'Epargne Rhône-Alpes. \\n \\\n", "Vous devez également consulter l'historique de la conversation ci-dessous lorsque vous reformulez la question. \\\n", "Par exemple, vous remplacerez les pronoms par les noms les plus probables dans l'historique de la conversation. \\n \\\n", "Lorsque vous reformulez la question, accordez plus d'importance à la dernière question et \\\n", "à la dernière réponse dans l'historique des conversations. \\n \\\n", "L'historique des conversations est présenté dans l'ordre chronologique inverse, \\\n", "de sorte que l'échange le plus récent se trouve en haut de la page. \\n \\\n", "Répondez en seulement une phrase avec la question reformulée. \\n\\n \\\n", "\\\n", "Historique de la conversation : \\n\\n \\\n", "\"\"\"\n", "\n", "def format_prompt_reformulate_query(query):\n", "\n", " system_prompt = query_reformulate_system_prompt\n", "\n", " for i in reversed(range(len(chat_history))):\n", " system_prompt += chat_history[i][\"user\"]\n", " system_prompt += chat_history[i][\"assistant\"]\n", " \n", " prompt = \"\"\n", " prompt = f\"<|system|>\\n{system_prompt.strip()} \\n\\n\"\n", " prompt += f\"<|user|>\\nPeux-tu reformuler la question suivante : \\n \\\"{query}\\\" \\n\\n\"\n", " prompt += f\"<|assistant|> Question reformulée : \\n\\\"\"\n", " \n", " return prompt" ] }, { "cell_type": "code", "execution_count": 423, "id": "45ed5601-d7c2-43f5-8436-1b97289ed066", "metadata": {}, "outputs": [], "source": [ "query2 = \"Peux-tu m'en dire plus au sujet de l'affacturage ?\"\n", "# print(format_prompt_reformulate_query(query2))" ] }, { "cell_type": "code", "execution_count": 424, "id": "69a51002-bb8b-4a6e-8356-7f2cbe83b2f3", "metadata": {}, "outputs": [], "source": [ "def answer_reformulate_query(query):\n", " \n", " prompt = format_prompt_reformulate_query(query)\n", " \n", " ans = answer(prompt)\n", "\n", " if ans.endswith('\"'):\n", " ans = ans[:-1]\n", " \n", " return ans" ] }, { "cell_type": "code", "execution_count": 425, "id": "2ffb55e8-05ef-4c31-90a8-e9d6f62be17c", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Llama.generate: prefix-match hit\n", "\n", "llama_print_timings: load time = 575.42 ms\n", "llama_print_timings: sample time = 194.11 ms / 57 runs ( 3.41 ms per token, 293.65 tokens per second)\n", "llama_print_timings: prompt eval time = 2261.49 ms / 923 tokens ( 2.45 ms per token, 408.14 tokens per second)\n", "llama_print_timings: eval time = 1376.69 ms / 56 runs ( 24.58 ms per token, 40.68 tokens per second)\n", "llama_print_timings: total time = 3800.97 ms\n" ] } ], "source": [ "query2_reformulated = answer_reformulate_query(query2)" ] }, { "cell_type": "code", "execution_count": 426, "id": "687b9ae7-ef51-40c2-813c-88dbb84d43d0", "metadata": {}, "outputs": [], "source": [ "query2_results = query_collection(query2_reformulated)" ] }, { "cell_type": "code", "execution_count": 413, "id": "6d363524-83a1-446f-ad15-08f3f2c2e4e2", "metadata": {}, "outputs": [], "source": [ "# TEST\n", "\n", "query2_context = format_passages(query2_results)\n", "\n", "history = \"\"\n", "for i in reversed(range(len(chat_history))):\n", " history += chat_history[i][\"user\"]\n", " history += chat_history[i][\"assistant\"]\n", " history += \"\\n-----\\n\"\n", "\n", "prompt = format_rag_prompt(query2_reformulated, query2_context, history)" ] }, { "cell_type": "code", "execution_count": 414, "id": "d86bd9e4-0974-49a5-af92-5b2e87471de2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<|system|>\n", "Vous êtes un assistant IA qui répond à la question posée par l'utilisateur en utilisant un contexte répertorié ci-dessous dans la rubrique Contexte.\n", "Le contexte est formé de passages exraits du site web commercial de la Caisse d'Epargne Rhône-Alpes, une banque française régionale.\n", "Pour répondre, tenez également compte de l'historique de la conversation qui est répertorié ci-dessous dans la rubrique Historique.\n", "Votre réponse ne doit pas introduire des informations déjà présentes dans l'historique.\n", "Vous recevez une pénalité de $100 à chaque fois que votre réponse répète une information déjà présente dans l'historique.\n", "Vous fournissez avec soin des réponses précises, factuelles, réfléchies et nuancées, et vous êtes doué pour le raisonnement.\n", "Toutes les informations factuelles que vous utilisez pour répondre proviennent exclusivement du contexte.\n", "Si vous ne pouvez pas répondre à la question sur la base des éléments du contexte, dites simplement que vous ne savez pas, n'essayez pas d'inventer une réponse.\n", "Vos réponses doivent être brèves.\n", "Vous rédigez vos réponses en français au format markdown sous forme d'une liste.\n", "Voici le format que doit prendre votre réponse :\n", "```\n", "1. Elément de réponse. (Passage 1)\n", "2. Elément de réponse. (Passage 1)\n", "3. Elément de réponse. (Passage 2)\n", "4. ...\n", "```\n", "\n", "----------------------------------------\n", "Historique :\n", "Question de l'utilisateur : \n", "Comment la Caisse d'Epargne Rhône-Alpes peut-elle aider une entreprise qui rencontre des problèmes de trésorerie ?\n", "\n", "Réponse de l'assistant : \n", " 1. La Caisse d'Epargne Rhône Alpes propose un ensemble de solutions pour optimiser votre trésorerie sans attendre le règlement de vos factures clients, appelé l'affacturage. (Passage 1)\n", " 2. Vous pouvez également accéder à leur simulateur et obtenir une préconisation sur le mode de financement le plus adapté à votre usage ainsi que de nombreuses informations sur la fiscalité automobile. (Passage 1)\n", " 3. À taux fixe ou variable, les prêts classiques vous accompagnent dans le développement de votre entreprise. (Passage 1)\n", " 4. De plus, avec l'affacturage, vous pouvez optimiser votre trésorerie sans attendre le règlement de vos factures clients. (Passage 1)\n", " 5. La Caisse d'Epargne Rhône Alpes vous propose également un financement adapté pour votre projet de création, de reprise ou d’investissement (acquisition de nouveaux locaux, renouvellement de vos équipements, ...) grâce à des offres et des conseils pour vous accompagner dans le financement de vos projets. (Passage 1)\n", " 6. Enfin, la Caisse d'Epargne Rhône Alpes vous accompagne dans votre transition énergétique avec des offres sur mesure en fonction de vos besoins. (Passage 1)\n", "\n", " Les éléments ci-dessus montrent que la Caisse d'Epargne Rhône Alpes propose plusieurs solutions pour optimiser votre trésorerie et vous aider dans le financement de vos projets, ce qui peut être utile pour une entreprise qui rencontre des problèmes de trésorerie.\n", "-----\n", "\n", "----------------------------------------\n", "Contexte :\n", "###\n", "Passage 1\n", "Titre :\n", "Financer vos projets & optimiser votre trésorerie\n", "\n", "Catégorie :\n", "professionnels\n", "\n", "URL :\n", "https://www.caisse-epargne.fr/rhone-alpes/professionnels/financer-projets-optimiser-tresorerie/\n", "\n", "Contenu : \n", "Vous avez un projet de création, de reprise ou d’investissement (acquisition de nouveaux locaux, renouvellement de vos équipements, ...) ou vous souhaitez assouplir votre trésorerie ? La Caisse d’Epargne Rhône Alpes vous propose un ensemble de solutions pour un financement adapté.\n", "À taux fixe ou variable, les prêts classiques vous accompagnent dans le développement de votre entreprise.\n", "Accédez à notre simulateur et obtenez une préconisation sur le mode de financement le plus adapté à votre usage ainsi que de nombreuses informations sur la fiscalité automobile.\n", "Avec l’affacturage, optimisez votre trésorerie sans attendre le règlement de vos factures clients.\n", "Vous souhaitez\n", "Financer votre projet\n", "Découvrez un ensemble d’offres et de conseils pour vous accompagner dans le financement de vos projets de création, reprise ou développement de votre activité professionnelle.\n", "Prêt Décollage Pro\n", "Un soutien financier complémentaire aux porteurs de projets accompagnés.\n", "Crédit-Bail Mobilier\n", "Une solution pour financer votre équipement à 100% sans immobiliser vos capitaux.\n", "Financer votre transition énergétique\n", "La Caisse d’Epargne vous accompagne dans votre transition énergétique avec des offres sur mesure en fonction de vos besoins.\n", "Acquérir un véhicule professionnel\n", "Gagnez en mobilité et changez rapidement de véhicule grâce aux offres de financement de la Caisse d’Epargne.\n", "Location Longue Durée avec MyCarLease\n", "Besoin d’une voiture, découvrez le service sur-mesure Location Longue Durée. Des prestations adaptées à vos besoins.\n", "Financement de votre équipement\n", "\n", "###\n", "Passage 2\n", "Titre :\n", "Garanties Assurances et Assistance des cartes Visa\n", "\n", "Catégorie :\n", "comptes-cartes\n", "\n", "URL :\n", "https://www.caisse-epargne.fr/rhone-alpes/comptes-cartes/garanties-assurances-et-assistances-des-cartes-visa/\n", "\n", "Contenu : \n", "Caisse d'Epargne\n", "Rhône Alpes\n", "Prendre rendez-vous en ligne\n", "En agence ou par téléphone à l'horaire de votre choix\n", "Vous êtes déjà client ?\n", "Connectez-vous sur votre espace personnel pour consulter les coordonnées de votre conseiller.\n", "\n", "###\n", "Passage 3\n", "Titre :\n", "Formule Famille\n", "\n", "Catégorie :\n", "comptes-cartes\n", "\n", "URL :\n", "https://www.caisse-epargne.fr/rhone-alpes/comptes-cartes/formule-famille/\n", "\n", "Contenu : \n", "Caisse d'Epargne\n", "Rhône Alpes\n", "Prendre rendez-vous en ligne\n", "En agence ou par téléphone à l'horaire de votre choix\n", "Vous êtes déjà client ?\n", "Connectez-vous sur votre espace personnel pour consulter les coordonnées de votre conseiller. \n", "\n", "<|user|>\n", "Pouvez-vous me présenter les solutions d'affacturage proposées par la Caisse d'Epargne Rhône Alpes pour optimiser ma trésorerie sans attendre le règlement de mes factures clients? \n", "\n", "<|assistant|>\n", " Voici des éléments de réponse : \n", " 1. \n" ] } ], "source": [ "print(prompt)" ] }, { "cell_type": "code", "execution_count": 427, "id": "adc6f9e9-4410-4b30-bd60-319e5a463930", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Llama.generate: prefix-match hit\n", "\n", "llama_print_timings: load time = 575.42 ms\n", "llama_print_timings: sample time = 1201.36 ms / 353 runs ( 3.40 ms per token, 293.83 tokens per second)\n", "llama_print_timings: prompt eval time = 13189.84 ms / 3730 tokens ( 3.54 ms per token, 282.79 tokens per second)\n", "llama_print_timings: eval time = 13559.00 ms / 352 runs ( 38.52 ms per token, 25.96 tokens per second)\n", "llama_print_timings: total time = 27777.04 ms\n" ] } ], "source": [ "ans = answer_rag_prompt(query2_reformulated, query2_results)" ] }, { "cell_type": "code", "execution_count": 428, "id": "1321be63-3f13-49f2-9530-a825204eec96", "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "1. La Caisse d'Epargne Rhône Alpes propose l'affacturage, une solution pour optimiser votre trésorerie sans attendre le règlement de vos factures clients. (Passage 1)\n", " 2. Avec l'affacturage, vous pouvez obtenir un financement adapté à votre situation financière compliquée et éviter les problèmes de trésorerie liés au délai d'attente des paiements de vos clients. (Passage 1)\n", " 3. Cette solution permet également de préserver votre image auprès de vos fournisseurs en vous donnant la possibilité de régler leurs factures à l'horaire convenu, même si les paiements de vos clients sont retardés. (Passage 1)\n", " 4. L'affacturage est une solution courante utilisée par de nombreuses entreprises pour optimiser leur trésorerie et gérer efficacement leurs flux de trésorerie. (Passage 1)\n", " 5. La Caisse d'Epargne Rhône Alpes vous propose également un simulateur pour obtenir une préconisation sur le mode de financement le plus adapté à votre usage ainsi que de nombreuses informations sur la fiscalité automobile. (Passage 1)\n", " 6. N'hésitez pas à contacter votre conseiller en ligne, à l'agence ou par téléphone pour obtenir des solutions adaptées à votre situation financière" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(Markdown(ans))" ] }, { "cell_type": "markdown", "id": "85ac55aa-7645-4253-a790-1c3237c21749", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "# Generate a HTML representation of the tree structure of the webages by categories" ] }, { "cell_type": "markdown", "id": "7e063507-a367-428b-b887-e65a9fc5b7d3", "metadata": {}, "source": [ "## Create the tree structure of the tags (i.e., subparts of the URLs)" ] }, { "cell_type": "code", "execution_count": null, "id": "d98bc715-64c3-4a0e-8bd8-feab61980ea1", "metadata": {}, "outputs": [], "source": [ "tags = {}\n", "\n", "txt_folder = 'docs/cera2'\n", "\n", "for filename in os.listdir(txt_folder):\n", " if filename.endswith('.txt'):\n", " file_path = os.path.join(txt_folder, filename)\n", " with open(file_path, 'r') as file:\n", " file_contents = file.read()\n", " contents_lst = [str.replace('\\n',' ').replace('\\xa0', ' ') for str in file_contents.split('\\n\\n')]\n", " url = contents_lst[0]\n", " if '?' in url: # URLs with a '?' corresponds to call to services and have no useful content\n", " continue\n", " title = contents_lst[1]\n", " if not title: # when the title is absent (or empty), the page has no interest\n", " continue\n", " prefix = 'https://www.caisse-epargne.fr/'\n", " suffix = url.replace(prefix, '')\n", " parts = suffix.split('/')\n", " parts = [part for part in parts if part] # remove empty parts\n", " dic = tags\n", " for i, part in enumerate(parts):\n", " if part in dic:\n", " dic[part]['nb'] = dic[part]['nb'] + 1\n", " else:\n", " dic[part] = {'nb': 1, 'sub': {}}\n", " if i == len(parts)-1: # last part of an url\n", " dic[part]['file'] = file_path\n", " \n", " dic = dic[part]['sub']" ] }, { "cell_type": "markdown", "id": "33d2e9a2-435c-4547-aa01-277bdd1cb71b", "metadata": {}, "source": [ "## Transform to HTML the tree structure of the tags" ] }, { "cell_type": "code", "execution_count": null, "id": "402169b6-16ca-4aa7-a63f-669edcb84d05", "metadata": {}, "outputs": [], "source": [ "def read_file_content(file_path):\n", " try:\n", " with open(file_path, 'r') as file:\n", " return html.escape(file.read())\n", " except IOError:\n", " return \"File not found or unable to read.\"\n", "\n", "def dict_to_html(d):\n", " html = '
    '\n", " for key, value in d.items():\n", " file_path = value.get('file', '')\n", " if 'sub' in value and value['sub']:\n", " html += f'
  • {key} (nb: {value[\"nb\"]})
  • '\n", " html += dict_to_html(value['sub'])\n", " else:\n", " content = read_file_content(file_path) if file_path else ''\n", " html += f'
  • {key} (nb: {value[\"nb\"]})
  • '\n", " html += f'
    {content}
    '\n", " html += '
'\n", " return html" ] }, { "cell_type": "markdown", "id": "34878d5e-a27b-4915-a8a7-02978f656f3c", "metadata": {}, "source": [ "## Add styling and javascript navigation to the HTML tree of the tags" ] }, { "cell_type": "code", "execution_count": null, "id": "b3fbdced-18c9-438e-93a3-79f8c15fc146", "metadata": {}, "outputs": [], "source": [ "def save_html_file(content, filename):\n", " html_template = f\"\"\"\n", " \n", " \n", " \n", " \n", " \n", " Website Hierarchy\n", " \n", " \n", " \n", " {content}\n", " \n", " \n", " \n", " \"\"\"\n", " with open(filename, 'w') as file:\n", " file.write(html_template)" ] }, { "cell_type": "markdown", "id": "6b8bc70a-08c6-4e17-8922-20e173863695", "metadata": {}, "source": [ "## Generate the HTML representation of the tree of tags" ] }, { "cell_type": "code", "execution_count": null, "id": "419f16c2-916e-41a0-9427-c71365799218", "metadata": {}, "outputs": [], "source": [ "html_content = dict_to_html(tags)\n", "save_html_file(html_content, 'cera_hierarchy.html')" ] }, { "cell_type": "code", "execution_count": null, "id": "f473843b-1cca-4471-877f-12e5bf1de9f0", "metadata": {}, "outputs": [], "source": [ "query = \"query: En tant que professionnel, comment mieux gérer sa trésorerie ?\"\n", "query_embedding = embed_model.encode(query, normalize_embeddings=True)\n", "query_embedding = query_embedding.tolist()\n", "collection.query(\n", " query_embeddings=query_embedding,\n", " n_results=10,\n", " where={\"category\": \"professionnels\"},\n", ")" ] }, { "cell_type": "markdown", "id": "14c72389-df64-42f3-89fd-1378fe555438", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "# Chat interface" ] }, { "cell_type": "markdown", "id": "f778d03b-f7a1-4996-b813-0505f81f497b", "metadata": {}, "source": [ "## Generate an answer taking into account the history of interactions" ] }, { "cell_type": "code", "execution_count": null, "id": "6abd6080-4545-4621-9753-b4a0ad677ba0", "metadata": {}, "outputs": [], "source": [ "history_trace = []" ] }, { "cell_type": "code", "execution_count": null, "id": "51f1b769-cafe-449c-9223-b930d97d38b4", "metadata": {}, "outputs": [], "source": [ "def generate_text(message, history):\n", " history_trace = history\n", " temp = \"\"\n", " prompt = f\"<|system|>\\n{system_prompt}\\n\"\n", " for interaction in history:\n", " prompt += \"<|user|>\\n\" + str(interaction[0]) + \" \\n \"\n", " prompt += \"<|assistant|>\\n\" + str(interaction[1]) + \" \\n\"\n", "\n", " prompt += \"<|user|>\\n\" + str(message) + \" \\n <|assistant|>\\n \"\n", "\n", " output = llm(\n", " prompt = prompt,\n", " temperature=0.1,\n", " mirostat_mode = 2,\n", " max_tokens=-1,\n", " stop=[''],\n", " stream=True,\n", " )\n", " for out in output:\n", " stream = copy.deepcopy(out)\n", " temp += stream[\"choices\"][0][\"text\"]\n", " yield temp" ] }, { "cell_type": "code", "execution_count": null, "id": "1989ed61-5f75-4922-a3c7-3b17ff9d07f0", "metadata": {}, "outputs": [], "source": [ "prompt = \"Connais-tu des répliques dites par Charlie Munger lors des rencontres annuels organisées par Berkshire Hathaway ?\"\n", "prompt_encoded = prompt.encode(encoding='utf-8')\n", "len(llm.tokenize(prompt_encoded))" ] }, { "cell_type": "markdown", "id": "439401f1-2548-43af-8f4b-7c1c582c2220", "metadata": { "jp-MarkdownHeadingCollapsed": true }, "source": [ "## Gradio interface" ] }, { "cell_type": "code", "execution_count": null, "id": "6833e184-c2e0-4af5-ba4d-40a86acfce3b", "metadata": {}, "outputs": [], "source": [ "demo = gr.ChatInterface(\n", " generate_text,\n", " title=\"CERA\",\n", " description=\"CERA RAG\",\n", " examples=[\"Qui sont les philosophes les plus influencés par Hegel ?\"],\n", " cache_examples=False,\n", " retry_btn=None,\n", " undo_btn=\"Delete Previous\",\n", " clear_btn=\"Clear\",\n", ")\n", "demo.queue()\n", "demo.launch()" ] }, { "cell_type": "code", "execution_count": null, "id": "c96c3f00-9309-4b19-9a77-6d8a80a9ceea", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "28b195b3-0898-4eea-bf07-7272c54e7a3d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "RAG_ENV", "language": "python", "name": "rag_env" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.18" } }, "nbformat": 4, "nbformat_minor": 5 }