Spaces:
Sleeping
Sleeping
Download routing_module.py from kadsai/chatbotComo: direct link, hf CLI and curl.
- Browser
- Download file 3.21 kB
-
https://huggingface.co/spaces/kadsai/chatbotComo/resolve/main/routing_module.py
- Command line
-
hf download hf://spaces/kadsai/chatbotComo/routing_module.py
-
curl -L -o routing_module.py https://huggingface.co/spaces/kadsai/chatbotComo/resolve/main/routing_module.py
3.21 kB
| from langsmith import traceable | |
| from langchain.schema import SystemMessage, HumanMessage | |
| from langchain_openai import ChatOpenAI | |
| from langchain.vectorstores import Chroma | |
| from langchain_openai import OpenAIEmbeddings | |
| from evaluator_agent import evaluate_response | |
| import os | |
| # Désactive l'agent de réclamation, ne garde que l'agent FAQ | |
| OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") | |
| embedding_model = OpenAIEmbeddings( | |
| model="text-embedding-3-large", | |
| openai_api_key=OPENAI_API_KEY | |
| ) | |
| def load_collection(domain): | |
| VECTORSTORE_BASE_DIR = "vectorstores" | |
| persist_directory = os.path.join(VECTORSTORE_BASE_DIR, domain.lower()) | |
| vectordb = Chroma( | |
| collection_name="corismoney", | |
| embedding_function=embedding_model, | |
| persist_directory=persist_directory, | |
| ) | |
| return vectordb | |
| def faq_agent(message, history, domain, model_name, max_tokens, temperature, top_p): | |
| # Recherche dans la base vectorielle | |
| collection = load_collection(domain) | |
| query_embedding = embedding_model.embed_query(message) | |
| results_with_scores = collection.similarity_search_by_vector_with_relevance_scores(query_embedding, k=3) | |
| seuil = 0.75 | |
| filtered = [doc for doc, score in results_with_scores if score >= seuil] | |
| if not filtered: | |
| response = ( | |
| "Je n’ai pas trouvé cette information dans mes documents internes. " | |
| "Pour toute autre assistance, merci de contacter notre service client au 3410 " | |
| "ou par email à corisbank@coris-bank.com." | |
| ) | |
| history.append({"role": "assistant", "content": response}) | |
| return history | |
| context = "\n".join([doc.page_content for doc in filtered]) | |
| system_message = ( | |
| "Tu es AxelIA, l'assistant Multilingue de CorisMoney. " | |
| "Voici des documents internes :\n" | |
| f"{context}\n" | |
| "1. Recherche *uniquement* dans ces documents.\n" | |
| "2. Si réponse trouvée, réponds dans la langue de l'utilisateur.\n" | |
| "3. Sinon, redirige vers service client (3410 / corisbank@coris-bank.com)." | |
| ) | |
| messages = [SystemMessage(content=system_message), HumanMessage(content=message)] | |
| chat = ChatOpenAI( | |
| temperature=temperature, | |
| max_tokens=max_tokens, | |
| top_p=top_p, | |
| model=model_name, | |
| openai_api_key=OPENAI_API_KEY | |
| ) | |
| result = chat.invoke(messages) | |
| validated = evaluate_response(result.content, message, context) | |
| history.append({"role": "user", "content": message}) | |
| history.append({"role": "assistant", "content": validated}) | |
| return history | |
| def respond(message, history, system_message, domain, model_name, max_tokens, temperature, top_p): | |
| """ | |
| Désactive complètement l'agent réclamation : toutes les requêtes passent par faq_agent. | |
| """ | |
| if not message.strip(): | |
| return history + [{"role": "assistant", "content": "❌ Message vide."}] | |
| # Appel direct à l'agent FAQ | |
| history = faq_agent(message, history, domain, model_name, max_tokens, temperature, top_p) | |
| # Marque la dernière intention comme FAQ | |
| history.append({"role": "system", "content": "intention:faq"}) | |
| return history | |