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6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
from langchain_core.globals import set_llm_cache
|
| 10 |
+
from langchain_core.caches import InMemoryCache
|
| 11 |
+
|
| 12 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 13 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 14 |
+
|
| 15 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 16 |
+
from langchain_community.vectorstores import Weaviate
|
| 17 |
+
from langchain_community.vectorstores import FAISS
|
| 18 |
+
from langchain_groq import ChatGroq
|
| 19 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 20 |
+
|
| 21 |
+
from langchain_classic.chains.combine_documents import create_stuff_documents_chain
|
| 22 |
+
from langchain_classic.chains import create_retrieval_chain
|
| 23 |
+
|
| 24 |
+
from langchain_core.runnables.history import RunnableWithMessageHistory
|
| 25 |
+
from langchain_community.chat_message_histories import ChatMessageHistory
|
| 26 |
+
from langchain_core.chat_history import BaseChatMessageHistory
|
| 27 |
+
|
| 28 |
+
#================== CONFIG==================
|
| 29 |
+
|
| 30 |
+
load_dotenv()
|
| 31 |
+
|
| 32 |
+
set_llm_cache(InMemoryCache())
|
| 33 |
+
|
| 34 |
+
api_key=os.environ["GROQ_API_KEY"]
|
| 35 |
+
print("api chargée:" if api_key else "y'a probleme!!")
|
| 36 |
+
|
| 37 |
+
#========== charger et decouper documents=================
|
| 38 |
+
|
| 39 |
+
urls=[
|
| 40 |
+
"https://fr.wikipedia.org/wiki/Intelligence_artificielle",
|
| 41 |
+
"https://fr.wikipedia.org/wiki/Apprentissage_automatique"
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
loader = WebBaseLoader(urls)
|
| 45 |
+
docs =loader.load()
|
| 46 |
+
|
| 47 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size= 1000, chunk_overlap=200)
|
| 48 |
+
chunks= splitter.split_documents(docs)
|
| 49 |
+
|
| 50 |
+
#============embeding et indexation vers faiss_db================
|
| 51 |
+
|
| 52 |
+
embeddings= HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 53 |
+
|
| 54 |
+
faiss_db=FAISS.from_documents(
|
| 55 |
+
documents=chunks,
|
| 56 |
+
embedding=embeddings
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
retriever=faiss_db.as_retriever(search_type="similarity", search_kwargs={"k":3})
|
| 60 |
+
|
| 61 |
+
#=============== LLM et Prompt=================
|
| 62 |
+
|
| 63 |
+
llm = ChatGroq(
|
| 64 |
+
model="llama-3.3-70b-versatile",
|
| 65 |
+
temperature=0.0,
|
| 66 |
+
max_tokens=1200
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
prompt=ChatPromptTemplate.from_template(
|
| 70 |
+
"""
|
| 71 |
+
Tu es un assistant expert en IA, reponds clairement aux questions.Si tu ne connais pas n'invente pas.
|
| 72 |
+
Garde le ton amical.
|
| 73 |
+
|
| 74 |
+
Historique :
|
| 75 |
+
{{chat_history}}
|
| 76 |
+
|
| 77 |
+
Contexte:
|
| 78 |
+
{context}
|
| 79 |
+
|
| 80 |
+
Question:{input}
|
| 81 |
+
|
| 82 |
+
"""
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
#============= CHAINE DE RECUPERATION=======
|
| 86 |
+
|
| 87 |
+
stuff_chain= create_stuff_documents_chain(llm, prompt)
|
| 88 |
+
rag_chain=create_retrieval_chain(retriever, stuff_chain)
|
| 89 |
+
|
| 90 |
+
import gradio as gr
|
| 91 |
+
|
| 92 |
+
store = {}
|
| 93 |
+
|
| 94 |
+
def get_session_history(session_id):
|
| 95 |
+
if session_id not in store:
|
| 96 |
+
store[session_id] = ChatMessageHistory()
|
| 97 |
+
return store[session_id]
|
| 98 |
+
|
| 99 |
+
# ====== CHAIN AVEC MÉMOIRE ======
|
| 100 |
+
convers_chain = RunnableWithMessageHistory(
|
| 101 |
+
rag_chain,
|
| 102 |
+
get_session_history,
|
| 103 |
+
input_messages_key="input",
|
| 104 |
+
history_messages_key="chat_history"
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
# ====== FONCTION CHAT ======
|
| 108 |
+
def chat_fn(message, session_id):
|
| 109 |
+
|
| 110 |
+
result = convers_chain.invoke(
|
| 111 |
+
{"input": message},
|
| 112 |
+
config={"configurable": {"session_id": session_id}}
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
#IMPORTANT : clé correcte = "answer"
|
| 116 |
+
return result.get("answer", str(result))
|
| 117 |
+
|
| 118 |
+
# ====== GRADIO ======
|
| 119 |
+
with gr.Blocks(theme= gr.themes.Soft(
|
| 120 |
+
primary_hue="yellow",
|
| 121 |
+
secondary_hue="gray",
|
| 122 |
+
neutral_hue="blue")) as demo:
|
| 123 |
+
|
| 124 |
+
gr.Markdown("🙋SUPER CHATTER👌")
|
| 125 |
+
|
| 126 |
+
session_id = gr.State(str(uuid.uuid4()))
|
| 127 |
+
|
| 128 |
+
chatbot = gr.Chatbot(height=800) # version simple (tuples)
|
| 129 |
+
msg = gr.Textbox( label="❓",placeholder="Posez une question")
|
| 130 |
+
|
| 131 |
+
def respond(message, chat_history, session_id):
|
| 132 |
+
|
| 133 |
+
answer = chat_fn(message, session_id)
|
| 134 |
+
|
| 135 |
+
chat_history.append((message, answer))
|
| 136 |
+
|
| 137 |
+
return "", chat_history, session_id
|
| 138 |
+
|
| 139 |
+
msg.submit(
|
| 140 |
+
respond,
|
| 141 |
+
inputs=[msg, chatbot, session_id],
|
| 142 |
+
outputs=[msg, chatbot, session_id]
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# ===================LANCEMENT ===============
|
| 146 |
+
|
| 147 |
+
demo.launch(share=True, debug=True)
|