diff --git a/web-ui.py b/web-ui.py index 509f628..91d0475 100644 --- a/web-ui.py +++ b/web-ui.py @@ -1,5 +1,12 @@ +from __future__ import annotations + +import copy import random +from typing import Iterable + import gradio as gr +from gradio.themes.base import Base +from gradio.themes.utils import colors, fonts, sizes __import__('pysqlite3') import sys @@ -39,7 +46,7 @@ few_shot_prompt = FewShotChatMessagePromptTemplate( final_prompt = ChatPromptTemplate.from_messages( [ ("system", - "You are a lawyer specialized in French Labour laws and promting Unions. You want to help but not misguide the user. If you don't know the answer, just say that you don't know. Don't hesitate to reformulate and think about the issues. Cite law articles as much as possible."), + "You are a lawyer specialized in French Labour laws that promotes Unions and regulation authorities. You want to help but not misguide the user. If you don't know the answer, just say that you don't know. Don't hesitate to reformulate and think about the issues. Cite law articles as much as possible."), few_shot_prompt, ("human", """ Question: {question} @@ -53,17 +60,49 @@ Answer: llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.33) rag_chain = ( - {"context": retriever, "question": RunnablePassthrough(), "input" : RunnablePassthrough()} + {"context": retriever, "question": RunnablePassthrough(), "input": RunnablePassthrough()} | final_prompt | llm ) -def random_response(message, history): - res = rag_chain.invoke(message) + +def merge_history(message: str, reversed_history: list) -> str: + max_words = 1499 + merged_message = [] + if len(reversed_history) == 0: + return message + merged_message.extend( + f"Ma question est: {message}. L'historique de la discussion est ".split()) + + for msg,response in reversed_history: + if len(merged_message) >= max_words: + break + words = msg.split(" ") + merged_message.extend(words[0:(max_words - len(merged_message))]) + merged_message.append("") + return " ".join(merged_message) + + +def llm_response(message, history): + reversed_history = copy.deepcopy(history) + reversed_history.reverse() + merged_message = merge_history(message, reversed_history) + res = rag_chain.invoke(merged_message) return res.content -demo = gr.ChatInterface(random_response) +demo = gr.ChatInterface( + llm_response, + examples=[ + "J'ai été embauchée en Contrat à Durée Déterminée d'une durée de 3 mois pour un poste de vendeuse dans un supermarché. J'ai déjà fait 2 semaines mais le poste ne me convient pas. Comment rompre ma période d’essai?", + "Je suis dans la fonction publique territoriale, plus précisément agent de maîtrise pour une commune. Est-ce que mon employeur peut modifier mes dates de départ en congé alors qu'elles étaient définies depuis plus d'un mois? " + ], + theme=gr.themes.Soft(), + analytics_enabled=False +) if __name__ == "__main__": - demo.launch() + demo.launch( + show_error=True, + + )