import os import json import bcrypt import chainlit as cl from chainlit.input_widget import TextInput, Select, Switch, Slider from chainlit import user_session from literalai import LiteralClient literal_client = LiteralClient(api_key=os.getenv("LITERAL_API_KEY")) from operator import itemgetter from pinecone import Pinecone from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_community.llms import HuggingFaceEndpoint from langchain.memory import ConversationBufferMemory from langchain.schema import StrOutputParser from langchain.schema.runnable import Runnable from langchain.schema.runnable.config import RunnableConfig from langchain.schema.runnable import Runnable, RunnablePassthrough, RunnableLambda from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_core.prompts import PromptTemplate @cl.password_auth_callback def auth_callback(username: str, password: str): auth = json.loads(os.environ['CHAINLIT_AUTH_LOGIN']) ident = next(d['ident'] for d in auth if d['ident'] == username) pwd = next(d['pwd'] for d in auth if d['ident'] == username) resultLogAdmin = bcrypt.checkpw(username.encode('utf-8'), bcrypt.hashpw(ident.encode('utf-8'), bcrypt.gensalt())) resultPwdAdmin = bcrypt.checkpw(password.encode('utf-8'), bcrypt.hashpw(pwd.encode('utf-8'), bcrypt.gensalt())) resultRole = next(d['role'] for d in auth if d['ident'] == username) if resultLogAdmin and resultPwdAdmin and resultRole == "admindatapcc": return cl.User( identifier=ident + " : đ§âđŒ Admin Datapcc", metadata={"role": "admin", "provider": "credentials"} ) elif resultLogAdmin and resultPwdAdmin and resultRole == "userdatapcc": return cl.User( identifier=ident + " : đ§âđ User Datapcc", metadata={"role": "user", "provider": "credentials"} ) @cl.author_rename def rename(orig_author: str): rename_dict = {"LLMMathChain": "Albert Einstein", "Doc Chain Assistant": "Assistant Reviewstream"} return rename_dict.get(orig_author, orig_author) @cl.set_chat_profiles async def chat_profile(): return [ cl.ChatProfile(name="Reviewstream",markdown_description="RequĂȘter sur les publications de recherche",icon="/public/logo-ofipe.jpg",), cl.ChatProfile(name="Imagestream",markdown_description="RequĂȘter sur un ensemble d'images",icon="./public/logo-ofipe.jpg",), ] @cl.on_chat_start async def start(): await cl.Message(f"> REVIEWSTREAM").send() await cl.Message(f"Nous avons le plaisir de vous accueillir dans l'application de recherche et d'analyse des publications.").send() listPrompts_name = f"Liste des revues de recherche" contentPrompts = """
Hal Archives Ouvertes : Une archive ouverte est un réservoir numérique contenant des documents issus de la recherche scientifique, généralement déposés par leurs auteurs, et permettant au grand public d'y accéder gratuitement et sans contraintes.
Persée : offre un accÚs libre et gratuit à des collections complÚtes de publications scientifiques (revues, livres, actes de colloques, publications en série, sources primaires, etc.) associé à une gamme d'outils de recherche et d'exploitation.
""" prompt_elements = [] prompt_elements.append( cl.Text(content=contentPrompts, name=listPrompts_name, display="side") ) await cl.Message(content="đ " + listPrompts_name, elements=prompt_elements).send() #settings = await cl.ChatSettings( # [ # Select( # id="Model", # label="Publications de recherche", # values=["---", "HAL", "PersĂ©e"], # initial_index=0, # ), # ] #).send() cl.user_session.set("memory", ConversationBufferMemory(return_messages=True)) memory = cl.user_session.get("memory") template = """