Spaces:
Sleeping
Sleeping
jonathanjordan21
commited on
Commit
•
6a8f373
1
Parent(s):
f3c827b
Update custom_llm.py
Browse files- custom_llm.py +18 -2
custom_llm.py
CHANGED
@@ -19,6 +19,14 @@ from langchain_community.document_loaders import PyMuPDFLoader
|
|
19 |
import os
|
20 |
from langchain.embeddings import HuggingFaceEmbeddings
|
21 |
from langchain.vectorstores import FAISS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
|
24 |
|
@@ -26,8 +34,12 @@ def custom_chain_with_history(llm, memory):
|
|
26 |
|
27 |
prompt = PromptTemplate.from_template("""You are a helpful, respectful, and honest assistant. Always answer as helpfully as possible while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
28 |
|
29 |
-
|
30 |
|
|
|
|
|
|
|
|
|
31 |
|
32 |
{chat_history}
|
33 |
### User: {question}
|
@@ -44,7 +56,11 @@ def custom_chain_with_history(llm, memory):
|
|
44 |
print(len(docs))
|
45 |
return "\n".join([f"{i+1}. {d.page_content}" for i,d in enumerate(docs)])
|
46 |
|
47 |
-
return {
|
|
|
|
|
|
|
|
|
48 |
|
49 |
|
50 |
class CustomLLM(LLM):
|
|
|
19 |
import os
|
20 |
from langchain.embeddings import HuggingFaceEmbeddings
|
21 |
from langchain.vectorstores import FAISS
|
22 |
+
import datasets
|
23 |
+
|
24 |
+
|
25 |
+
def create_vectorstore():
|
26 |
+
data = load_datasets("ruslanmv/ai-medical-chatbot", split='train')
|
27 |
+
emb_model = HuggingFaceEmbeddings(model_name='sentence-transformers/paraphrase-multilingual-mpnet-base-v2', encode_kwargs={'normalize_embeddings': True})
|
28 |
+
faiss = FAISS.from_texts(data['Doctor'], emb_model)
|
29 |
+
return faiss
|
30 |
|
31 |
|
32 |
|
|
|
34 |
|
35 |
prompt = PromptTemplate.from_template("""You are a helpful, respectful, and honest assistant. Always answer as helpfully as possible while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
36 |
|
37 |
+
You have the access to the following context information:
|
38 |
|
39 |
+
{context}
|
40 |
+
|
41 |
+
|
42 |
+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
43 |
|
44 |
{chat_history}
|
45 |
### User: {question}
|
|
|
56 |
print(len(docs))
|
57 |
return "\n".join([f"{i+1}. {d.page_content}" for i,d in enumerate(docs)])
|
58 |
|
59 |
+
return {
|
60 |
+
"chat_history":lambda x:prompt_memory(x['memory']),
|
61 |
+
"question":lambda x:x['question'],
|
62 |
+
"context": itemgetter("question") | create_vectorstore().as_retriever(search_type="similarity", search_kwargs={"k": 6}) | format_docs
|
63 |
+
} | prompt | llm
|
64 |
|
65 |
|
66 |
class CustomLLM(LLM):
|