CoolFace
Apppublic

ExpertPrompters/AskIDF

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
chat.py47 linesDownload Raw Back to root
1from langchain.llms.base import get_prompts2from sqlalchemy import label3import streamlit as st4from typing import Callable5 6 7 8RESPONSE_LABEL = 'chat_response'9PROMPT_LABEL = 'chat_prompt'10 11class Chat:12 13    def __init__(self): 14        if RESPONSE_LABEL not in st.session_state:15            st.session_state[RESPONSE_LABEL] = []16 17        if PROMPT_LABEL not in st.session_state:18            st.session_state[PROMPT_LABEL] = []19 20    def process(self, process_prompt: Callable, *args):21        """22        process_prompt(promt: str, *args) -> tuple(Any, Callable)23            callback to process the chat promt, it takes the promt for input24            and returns a tuple with the response and a render callback25        """26 27        # Render history28        messages = zip(st.session_state[PROMPT_LABEL], st.session_state[RESPONSE_LABEL])29        for prompt, (response, on_render) in list(messages)[::-1]:30            with st.chat_message("user"):31                st.write(prompt)32            with st.chat_message("assistant"):33                on_render(response)34 35        # Compute prompt36        if prompt:= st.chat_input("Ask IDF Anything"):37            st.session_state[PROMPT_LABEL].append(prompt)38            (response, on_render) = process_prompt(prompt, *args)39            st.session_state[RESPONSE_LABEL].append((response, on_render))40 41            with st.chat_message("user"):42                st.write(prompt)43 44            with st.chat_message("assistant"):45                on_render(response)46 47