Skip to content

Commit

Permalink
fix mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vemonet committed Sep 13, 2023
1 parent c37c351 commit 7771687
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 30 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions chat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions config/chat-conversation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions config/chat-falcon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/configure-conversation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/configure-documents-qa.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
4 changes: 3 additions & 1 deletion src/libre_chat/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 9 additions & 9 deletions src/libre_chat/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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",
# )
3 changes: 2 additions & 1 deletion src/libre_chat/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
49 changes: 30 additions & 19 deletions src/libre_chat/ui.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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 []

Expand Down Expand Up @@ -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()
2 changes: 2 additions & 0 deletions tests/config/additional-prop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit 7771687

Please sign in to comment.