From 7771687b116877d175004f01f5cb5bbf9ebc7454 Mon Sep 17 00:00:00 2001 From: Vincent Emonet Date: Wed, 13 Sep 2023 22:11:41 +0200 Subject: [PATCH] fix mypy errors --- README.md | 2 ++ chat.yml | 2 ++ config/chat-conversation.yml | 2 ++ config/chat-falcon.yml | 2 ++ docs/docs/configure-conversation.md | 2 ++ docs/docs/configure-documents-qa.md | 2 ++ src/libre_chat/conf.py | 4 ++- src/libre_chat/endpoint.py | 18 +++++------ src/libre_chat/llm.py | 3 +- src/libre_chat/ui.py | 49 ++++++++++++++++++----------- tests/config/additional-prop.yml | 2 ++ 11 files changed, 58 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 322e1f5..b0d96bf 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,8 @@ info: version: "0.1.0" description: | Open source and free chatbot powered by [LangChain](https://python.langchain.com) and [Llama 2](https://ai.meta.com/llama) [7B](https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML) + + See also: [๐Ÿ“ก API](/docs) | [๐Ÿ–ฅ๏ธ Alternative UI](/ui) examples: - What is the capital of the Netherlands? - Which drugs are approved by the FDA to mitigate Alzheimer symptoms? diff --git a/chat.yml b/chat.yml index c6a2496..a3d39d8 100644 --- a/chat.yml +++ b/chat.yml @@ -21,6 +21,8 @@ info: version: "0.1.0" description: | Open source and free chatbot powered by [LangChain](https://python.langchain.com) and [Llama 2](https://ai.meta.com/llama) [7B](https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML) + + See also: [๐Ÿ“ก API](/docs) | [๐Ÿ–ฅ๏ธ Alternative UI](/ui) examples: - What is the capital of the Netherlands? - Which drugs are approved by the FDA to mitigate Alzheimer symptoms? diff --git a/config/chat-conversation.yml b/config/chat-conversation.yml index d0b4896..8249f94 100644 --- a/config/chat-conversation.yml +++ b/config/chat-conversation.yml @@ -21,6 +21,8 @@ info: version: "0.1.0" description: | Open source and free chatbot powered by [LangChain](https://python.langchain.com) and [Llama 2](https://ai.meta.com/llama) [7B](https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML) + + See also: [๐Ÿ“ก API](/docs) | [๐Ÿ–ฅ๏ธ Alternative UI](/ui) examples: - What is the capital of the Netherlands? - Which drugs are approved by the FDA to mitigate Alzheimer symptoms? diff --git a/config/chat-falcon.yml b/config/chat-falcon.yml index aa11ee0..d3d63d1 100644 --- a/config/chat-falcon.yml +++ b/config/chat-falcon.yml @@ -21,6 +21,8 @@ info: version: "0.1.0" description: | Open source and free chatbot powered by [LangChain](https://python.langchain.com) and [Llama 2](https://ai.meta.com/llama) [7B](https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML) + + See also: [๐Ÿ“ก API](/docs) | [๐Ÿ–ฅ๏ธ Alternative UI](/ui) examples: - What is the capital of the Netherlands? - Which drugs are approved by the FDA to mitigate Alzheimer symptoms? diff --git a/docs/docs/configure-conversation.md b/docs/docs/configure-conversation.md index f07a6e0..c929c63 100644 --- a/docs/docs/configure-conversation.md +++ b/docs/docs/configure-conversation.md @@ -25,6 +25,8 @@ info: version: "0.1.0" description: | Open source and free chatbot powered by [LangChain](https://python.langchain.com) and [Llama 2](https://ai.meta.com/llama) [7B](https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML) + + See also: [๐Ÿ“ก API](/docs) | [๐Ÿ–ฅ๏ธ Alternative UI](/ui) examples: - What is the capital of the Netherlands? - Which drugs are approved by the FDA to mitigate Alzheimer symptoms? diff --git a/docs/docs/configure-documents-qa.md b/docs/docs/configure-documents-qa.md index 7a9f49d..6254b59 100644 --- a/docs/docs/configure-documents-qa.md +++ b/docs/docs/configure-documents-qa.md @@ -115,6 +115,8 @@ info: version: "0.1.0" description: | Open source and free chatbot powered by [LangChain](https://python.langchain.com) and [Llama 2](https://ai.meta.com/llama) [7B](https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML) + + See also: [๐Ÿ“ก API](/docs) | [๐Ÿ–ฅ๏ธ Alternative UI](/ui) examples: - What is the capital of the Netherlands? - Which drugs are approved by the FDA to mitigate Alzheimer symptoms? diff --git a/src/libre_chat/conf.py b/src/libre_chat/conf.py index a6cf83a..150870a 100644 --- a/src/libre_chat/conf.py +++ b/src/libre_chat/conf.py @@ -28,7 +28,9 @@ class SettingsInfo(BaseConf): ] title: str = "Libre Chat" version: str = "0.1.0" - description: str = """Open source and free chatbot powered by [LangChain](https://python.langchain.com) and [Llama 2](https://ai.meta.com/llama) [7B](https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML)""" + description: str = """Open source and free chatbot powered by [LangChain](https://python.langchain.com) and [Llama 2](https://ai.meta.com/llama) [7B](https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML) + + See also: [๐Ÿ“ก API](/docs) | [๐Ÿ–ฅ๏ธ Alternative UI](/ui)""" public_url: str = "https://your-endpoint-url" repository_url: str = "https://github.com/vemonet/libre-chat" favicon: str = "https://raw.github.com/vemonet/libre-chat/main/docs/docs/assets/logo.png" diff --git a/src/libre_chat/endpoint.py b/src/libre_chat/endpoint.py index 7957058..843f32b 100644 --- a/src/libre_chat/endpoint.py +++ b/src/libre_chat/endpoint.py @@ -84,15 +84,6 @@ async def add_process_time_header(request: Request, call_next: Any) -> Response: name="static", ) - # TODO: Try with svelte https://github.com/OriginalStefikO/fastapi-svelte-starter/blob/main/main.py - # self.mount( - # "/", - # StaticFiles( - # directory=pkg_resources.resource_filename("libre_chat", "webapp"), html=True - # ), - # name="static", - # ) - # TODO: not found when used after installing from pip, cf. https://github.com/vemonet/libre-chat/issues/2 templates = Jinja2Templates( directory=pkg_resources.resource_filename("libre_chat", "templates") @@ -131,3 +122,12 @@ def admin_ui(request: Request) -> Any: ) self = gr.mount_gradio_app(self, gradio_app(self.llm), path="/") + + # TODO: Try with svelte https://github.com/OriginalStefikO/fastapi-svelte-starter/blob/main/main.py + # self.mount( + # "/", + # StaticFiles( + # directory=pkg_resources.resource_filename("libre_chat", "webapp"), html=True + # ), + # name="static", + # ) diff --git a/src/libre_chat/llm.py b/src/libre_chat/llm.py index ceb6826..3f1c0b4 100644 --- a/src/libre_chat/llm.py +++ b/src/libre_chat/llm.py @@ -274,7 +274,8 @@ def query( res["source_documents"][i]["metadata"]["source"] ) else: - # NOTE: initializing the LLM at every call to prevent the conversation to take up lot of memory after some time + # Not using vectostore, generic conversation + # NOTE: initializing the LLM and conversation at every call to prevent the conversation to take up lot of memory after some time # And enable to customize the instructions prompt and temperature for each query # Memory is handled at the gradio level if not memory: diff --git a/src/libre_chat/ui.py b/src/libre_chat/ui.py index 53eb412..5fd1ca1 100644 --- a/src/libre_chat/ui.py +++ b/src/libre_chat/ui.py @@ -1,8 +1,8 @@ """Module: Gradio web UI for LangChain chatbot""" -from collections.abc import Generator +from collections.abc import Iterator from queue import Empty, Queue from threading import Thread -from typing import Any +from typing import Any, List, Tuple import gradio as gr from langchain.callbacks.base import BaseCallbackHandler @@ -25,7 +25,7 @@ # https://huggingface.co/spaces/HuggingFaceH4/falcon-chat-demo-for-blog/blob/main/app.py # https://huggingface.co/spaces/HuggingFaceH4/falcon-chat-demo-for-blog def gradio_app(llm: Llm) -> Any: - def chat_accordion(): + def chat_accordion() -> Tuple[float, int]: with gr.Accordion("Parameters", open=False): temperature = gr.Slider( minimum=0, @@ -47,9 +47,11 @@ def chat_accordion(): sources_list = [] - def stream(input_text, memory, instructions, temperature, max_new_tokens) -> Generator: + def stream( + input_text: str, memory: Any, instructions: str, temperature: float, max_new_tokens: int + ) -> Iterator[Tuple[object, str]]: # Create a Queue - q = Queue() + q: Queue[object] = Queue() job_done = object() # Create a function to call - this will run in a thread @@ -79,19 +81,19 @@ def task() -> None: next_token = q.get(True, timeout=1) if next_token is job_done: break - content += next_token + content += str(next_token) yield next_token, content except Empty: continue - def vote(data: gr.LikeData): + def vote(data: gr.LikeData) -> None: # TODO: save votes somewhere if data.liked: print("You upvoted this response: " + data.value) else: print("You downvoted this response: " + data.value) - def on_select(evt: gr.SelectData): + def on_select(evt: gr.SelectData) -> str: msg_index = evt.index[0] if msg_index < len(sources_list): sources_str = f"## ๐Ÿ—ƒ๏ธ Sources\nFor message nยฐ{msg_index}\n" @@ -100,6 +102,7 @@ def on_select(evt: gr.SelectData): f'### ๐Ÿ“„ {source["metadata"]["filename"]}\n{source["page_content"]}\n\n' ) return sources_str + return "" # return f"You selected ใ€{evt.value}ใ€‘ at ใ€{evt.index}ใ€‘ from ใ€{evt.target}ใ€‘" # gray https://www.gradio.app/guides/theming-guide#core-colors @@ -121,9 +124,9 @@ def on_select(evt: gr.SelectData): with gr.Row(): submit_button = gr.Button("๐Ÿ“ฉ Submit question", variant="primary") - retry_button = gr.Button("โ™ป๏ธ Retry last turn") - delete_turn_button = gr.Button("๐Ÿงฝ Delete last turn") - clear_chat_button = gr.Button("๐Ÿงน Delete all history", variant="stop") + retry_button = gr.Button("โ™ป๏ธ Retry last question") + delete_turn_button = gr.Button("๐Ÿงฝ Delete last question") + clear_chat_button = gr.Button("๐Ÿงน Delete history", variant="stop") # clear = gr.ClearButton([msg, chatbot]) gr.Examples( @@ -151,8 +154,12 @@ def on_select(evt: gr.SelectData): memory = ConversationBufferMemory(ai_prefix="AI Assistant") def run_chat( - message: str, chat_history, instructions: str, temperature: float, max_new_tokens: float - ): + message: str, + chat_history: List[List[str]], + instructions: str, + temperature: float, + max_new_tokens: int, + ) -> Iterator[List[List[str]]]: if not message or (message == RETRY_COMMAND and len(chat_history) == 0): yield chat_history return @@ -175,19 +182,23 @@ def run_chat( # memory.chat_memory.add_ai_message(chat_history[-1][1]) # print(memory.chat_memory.messages) - def delete_last_turn(chat_history): + def delete_last_turn(chat_history: List[List[str]]) -> Any: if chat_history: chat_history.pop(-1) return {chatbot: gr.update(value=chat_history)} def run_retry( - message: str, chat_history, instructions: str, temperature: float, max_new_tokens: float - ): + message: str, + chat_history: List[List[str]], + instructions: str, + temperature: float, + max_new_tokens: int, + ) -> Iterator[List[List[str]]]: yield from run_chat( RETRY_COMMAND, chat_history, instructions, temperature, max_new_tokens ) - def clear_chat(): + def clear_chat() -> List[str]: ConversationBufferMemory(ai_prefix="AI Assistant") return [] @@ -229,11 +240,11 @@ def clear_chat(): class StreamGradioCallback(BaseCallbackHandler): """Callback handler for streaming LLM responses to a queue.""" - def __init__(self, q): + def __init__(self, q: Queue[object]): self.q = q def on_llm_new_token(self, token: str, **kwargs: Any) -> None: self.q.put(token) - def on_llm_end(self, *args, **kwargs: Any) -> None: + def on_llm_end(self, *args: Any, **kwargs: Any) -> bool: return self.q.empty() diff --git a/tests/config/additional-prop.yml b/tests/config/additional-prop.yml index 4a5f935..7cbd378 100644 --- a/tests/config/additional-prop.yml +++ b/tests/config/additional-prop.yml @@ -36,6 +36,8 @@ info: version: "0.1.0" description: | Open source and free chatbot powered by [LangChain](https://python.langchain.com) and [Llama 2](https://ai.meta.com/llama) [7B](https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML) + + See also: [๐Ÿ“ก API](/docs) | [๐Ÿ–ฅ๏ธ Alternative UI](/ui) examples: - What is the capital of the Netherlands? - Which drugs are approved by the FDA to mitigate Alzheimer symptoms?