Spaces:
Sleeping
Sleeping
h3110Fr13nd
commited on
Commit
·
79340f2
1
Parent(s):
42990eb
Use Hugchat for llm inference
Browse files
.gitignore
CHANGED
|
@@ -4,4 +4,5 @@ pragetx_chroma
|
|
| 4 |
temp*
|
| 5 |
.env
|
| 6 |
.venv
|
| 7 |
-
venv
|
|
|
|
|
|
| 4 |
temp*
|
| 5 |
.env
|
| 6 |
.venv
|
| 7 |
+
venv
|
| 8 |
+
usercookies
|
README.md
CHANGED
|
@@ -1,2 +1,21 @@
|
|
| 1 |
# RAG Chatbot
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# RAG Chatbot
|
| 2 |
|
| 3 |
+
## Setup
|
| 4 |
+
|
| 5 |
+
1. Clone the repository:
|
| 6 |
+
```bash
|
| 7 |
+
git clone https://github.com/your-username/repo.git
|
| 8 |
+
```
|
| 9 |
+
|
| 10 |
+
2. Navigate to the project directory:
|
| 11 |
+
```bash
|
| 12 |
+
cd pragetx-chatbot
|
| 13 |
+
```
|
| 14 |
+
|
| 15 |
+
3. Create a `.env` file and add the following environment variables:
|
| 16 |
+
```bash
|
| 17 | |
| 18 |
+
HF_PASS=your-password
|
| 19 |
+
```
|
| 20 |
+
|
| 21 |
+
Now you can run the chatbot and interact with it.
|
main.py
CHANGED
|
@@ -10,24 +10,36 @@ from langchain_core.runnables import RunnablePassthrough
|
|
| 10 |
from langchain_core.documents import Document
|
| 11 |
from langchain_core.prompts import ChatPromptTemplate
|
| 12 |
from langchain_core.output_parsers import StrOutputParser
|
|
|
|
| 13 |
from langchain_community.chat_models import ChatOllama
|
| 14 |
from langchain_chroma import Chroma
|
| 15 |
from hugchat import hugchat
|
| 16 |
from hugchat.login import Login
|
| 17 |
import dotenv
|
|
|
|
|
|
|
| 18 |
|
| 19 |
dotenv.load_dotenv()
|
| 20 |
|
| 21 |
|
| 22 |
class GradioApp:
|
| 23 |
def __init__(self):
|
| 24 |
-
self.llm = ChatOllama(model="phi3:3.8b", base_url="http://localhost:11434", num_gpu=32)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
self.chain = (self.llm | StrOutputParser())
|
| 32 |
|
| 33 |
def user(self,user_message, history):
|
|
|
|
| 10 |
from langchain_core.documents import Document
|
| 11 |
from langchain_core.prompts import ChatPromptTemplate
|
| 12 |
from langchain_core.output_parsers import StrOutputParser
|
| 13 |
+
# from langchain_community.chains import
|
| 14 |
from langchain_community.chat_models import ChatOllama
|
| 15 |
from langchain_chroma import Chroma
|
| 16 |
from hugchat import hugchat
|
| 17 |
from hugchat.login import Login
|
| 18 |
import dotenv
|
| 19 |
+
from utils import HuggingChat
|
| 20 |
+
from langchain import PromptTemplate
|
| 21 |
|
| 22 |
dotenv.load_dotenv()
|
| 23 |
|
| 24 |
|
| 25 |
class GradioApp:
|
| 26 |
def __init__(self):
|
| 27 |
+
# self.llm = ChatOllama(model="phi3:3.8b", base_url="http://localhost:11434", num_gpu=32)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
template = """
|
| 31 |
+
You are a helpful health assistant. These Human will ask you a questions about their pregnancy health.
|
| 32 |
+
Use following piece of context to answer the question.
|
| 33 |
+
If you don't know the answer, just say you don't know.
|
| 34 |
+
Keep the answer within 2 sentences and concise.
|
| 35 |
+
|
| 36 |
+
Context: {context}
|
| 37 |
+
Question: {question}
|
| 38 |
+
Answer:
|
| 39 |
+
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
self.llm = HuggingChat(email = os.getenv("HF_EMAIL") , psw = os.getenv("HF_PASS") )
|
| 43 |
self.chain = (self.llm | StrOutputParser())
|
| 44 |
|
| 45 |
def user(self,user_message, history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
+
import time
|
| 4 |
+
|
| 5 |
+
from typing import Any, List, Mapping, Optional
|
| 6 |
+
|
| 7 |
+
from langchain.callbacks.manager import CallbackManagerForLLMRun
|
| 8 |
+
from langchain.llms.base import LLM
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# THIS IS A CUSTOM LLM WRAPPER Based on hugchat library
|
| 12 |
+
# Reference :
|
| 13 |
+
# - Langchain custom LLM wrapper : https://python.langchain.com/docs/modules/model_io/models/llms/how_to/custom_llm
|
| 14 |
+
# - HugChat library : https://github.com/Soulter/hugging-chat-api
|
| 15 |
+
# - I am Alessandro Ciciarelli the owner of IntelligenzaArtificialeItalia.net , my dream is to democratize AI and make it accessible to everyone.
|
| 16 |
+
|
| 17 |
+
class HuggingChat(LLM):
|
| 18 |
+
|
| 19 |
+
"""HuggingChat LLM wrapper."""
|
| 20 |
+
|
| 21 |
+
chatbot : Optional[hugchat.ChatBot] = None
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
email: Optional[str] = None
|
| 25 |
+
psw: Optional[str] = None
|
| 26 |
+
cookie_path : Optional[str] = None
|
| 27 |
+
|
| 28 |
+
conversation : Optional[str] = None
|
| 29 |
+
model: Optional[int] = 0 # 0 = OpenAssistant/oasst-sft-6-llama-30b-xor , 1 = meta-llama/Llama-2-70b-chat-hf
|
| 30 |
+
|
| 31 |
+
temperature: Optional[float] = 0.9
|
| 32 |
+
top_p: Optional[float] = 0.95
|
| 33 |
+
repetition_penalty: Optional[float] = 1.2
|
| 34 |
+
top_k: Optional[int] = 50
|
| 35 |
+
truncate: Optional[int] = 1024
|
| 36 |
+
watermark: Optional[bool] = False
|
| 37 |
+
max_new_tokens: Optional[int] = 1024
|
| 38 |
+
stop: Optional[list] = ["</s>"]
|
| 39 |
+
return_full_text: Optional[bool] = False
|
| 40 |
+
stream_resp: Optional[bool] = True
|
| 41 |
+
use_cache: Optional[bool] = False
|
| 42 |
+
is_retry: Optional[bool] = False
|
| 43 |
+
retry_count: Optional[int] = 5
|
| 44 |
+
|
| 45 |
+
avg_response_time: float = 0.0
|
| 46 |
+
log : Optional[bool] = False
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@property
|
| 50 |
+
def _llm_type(self) -> str:
|
| 51 |
+
return "🤗CUSTOM LLM WRAPPER Based on hugging-chat-api library"
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def create_chatbot(self) -> None:
|
| 55 |
+
if not any([self.email, self.psw, self.cookie_path]):
|
| 56 |
+
raise ValueError("email, psw, or cookie_path is required.")
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
if self.email and self.psw:
|
| 60 |
+
# Create a ChatBot using email and psw
|
| 61 |
+
from hugchat.login import Login
|
| 62 |
+
start_time = time.time()
|
| 63 |
+
sign = Login(self.email, self.psw)
|
| 64 |
+
cookies = sign.login()
|
| 65 |
+
end_time = time.time()
|
| 66 |
+
if self.log : print(f"\n[LOG] Login successfull in {round(end_time - start_time)} seconds")
|
| 67 |
+
else:
|
| 68 |
+
# Create a ChatBot using cookie_path
|
| 69 |
+
cookies = self.cookie_path and hugchat.ChatBot(cookie_path=self.cookie_path)
|
| 70 |
+
|
| 71 |
+
self.chatbot = cookies.get_dict() and hugchat.ChatBot(cookies=cookies.get_dict())
|
| 72 |
+
if self.log : print(f"[LOG] LLM WRAPPER created successfully")
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
raise ValueError("LogIn failed. Please check your credentials or cookie_path. " + str(e))
|
| 76 |
+
|
| 77 |
+
# Setup ChatBot info
|
| 78 |
+
self.chatbot.switch_llm(self.model)
|
| 79 |
+
if self.log : print(f"[LOG] LLM WRAPPER switched to model { 'OpenAssistant/oasst-sft-6-llama-30b-xor' if self.model == 0 else 'meta-llama/Llama-2-70b-chat-hf'}")
|
| 80 |
+
|
| 81 |
+
self.conversation = self.conversation or self.chatbot.new_conversation()
|
| 82 |
+
self.chatbot.change_conversation(self.conversation)
|
| 83 |
+
if self.log : print(f"[LOG] LLM WRAPPER changed conversation to {self.conversation}\n")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def _call(
|
| 88 |
+
self,
|
| 89 |
+
prompt: str,
|
| 90 |
+
stop: Optional[List[str]] = None,
|
| 91 |
+
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
| 92 |
+
**kwargs: Any,
|
| 93 |
+
) -> str:
|
| 94 |
+
if stop:
|
| 95 |
+
raise ValueError("stop kwargs are not permitted.")
|
| 96 |
+
|
| 97 |
+
self.create_chatbot() if not self.chatbot else None
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
if self.log : print(f"[LOG] LLM WRAPPER called with prompt: {prompt}")
|
| 101 |
+
start_time = time.time()
|
| 102 |
+
resp = self.chatbot.chat(
|
| 103 |
+
prompt,
|
| 104 |
+
temperature=self.temperature,
|
| 105 |
+
top_p=self.top_p,
|
| 106 |
+
repetition_penalty=self.repetition_penalty,
|
| 107 |
+
top_k=self.top_k,
|
| 108 |
+
truncate=self.truncate,
|
| 109 |
+
watermark=self.watermark,
|
| 110 |
+
max_new_tokens=self.max_new_tokens,
|
| 111 |
+
stop=self.stop,
|
| 112 |
+
return_full_text=self.return_full_text,
|
| 113 |
+
stream=self.stream_resp,
|
| 114 |
+
use_cache=self.use_cache,
|
| 115 |
+
is_retry=self.is_retry,
|
| 116 |
+
retry_count=self.retry_count,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
end_time = time.time()
|
| 120 |
+
|
| 121 |
+
self.avg_response_time = (self.avg_response_time + (end_time - start_time)) / 2 if self.avg_response_time else end_time - start_time
|
| 122 |
+
|
| 123 |
+
if self.log : print(f"[LOG] LLM WRAPPER response time: {round(end_time - start_time)} seconds")
|
| 124 |
+
if self.log : print(f"[LOG] LLM WRAPPER avg response time: {round(self.avg_response_time)} seconds")
|
| 125 |
+
if self.log : print(f"[LOG] LLM WRAPPER response: {resp}\n\n")
|
| 126 |
+
|
| 127 |
+
return str(resp)
|
| 128 |
+
|
| 129 |
+
except Exception as e:
|
| 130 |
+
raise ValueError("ChatBot failed, please check your parameters. " + str(e))
|
| 131 |
+
|
| 132 |
+
@property
|
| 133 |
+
def _identifying_params(self) -> Mapping[str, Any]:
|
| 134 |
+
"""Get the identifying parameters."""
|
| 135 |
+
parms = {
|
| 136 |
+
"model": "HuggingChat",
|
| 137 |
+
"temperature": self.temperature,
|
| 138 |
+
"top_p": self.top_p,
|
| 139 |
+
"repetition_penalty": self.repetition_penalty,
|
| 140 |
+
"top_k": self.top_k,
|
| 141 |
+
"truncate": self.truncate,
|
| 142 |
+
"watermark": self.watermark,
|
| 143 |
+
"max_new_tokens": self.max_new_tokens,
|
| 144 |
+
"stop": self.stop,
|
| 145 |
+
"return_full_text": self.return_full_text,
|
| 146 |
+
"stream": self.stream_resp,
|
| 147 |
+
"use_cache": self.use_cache,
|
| 148 |
+
"is_retry": self.is_retry,
|
| 149 |
+
"retry_count": self.retry_count,
|
| 150 |
+
"avg_response_time": self.avg_response_time,
|
| 151 |
+
}
|
| 152 |
+
return parms
|
| 153 |
+
|
| 154 |
+
@property
|
| 155 |
+
def _get_avg_response_time(self) -> float:
|
| 156 |
+
"""Get the average response time."""
|
| 157 |
+
return self.avg_response_time
|
| 158 |
+
|