Spaces:
Runtime error
Runtime error
Update chain.py
Browse files
chain.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import json
|
| 2 |
import os
|
| 3 |
import pathlib
|
|
|
|
| 4 |
from typing import Dict, List, Tuple
|
| 5 |
|
| 6 |
import weaviate
|
|
@@ -17,12 +18,9 @@ from langchain.prompts.example_selector import \
|
|
| 17 |
from langchain.vectorstores import FAISS, Weaviate
|
| 18 |
from pydantic import BaseModel
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
class CustomChain(Chain, BaseModel):
|
| 24 |
|
| 25 |
-
vstore:
|
| 26 |
chain: BaseCombineDocumentsChain
|
| 27 |
key_word_extractor: Chain
|
| 28 |
|
|
@@ -52,13 +50,7 @@ class CustomChain(Chain, BaseModel):
|
|
| 52 |
return {"answer": answer}
|
| 53 |
|
| 54 |
|
| 55 |
-
def get_new_chain1(vectorstore) -> Chain:
|
| 56 |
-
WEAVIATE_URL = os.environ["WEAVIATE_URL"]
|
| 57 |
-
client = weaviate.Client(
|
| 58 |
-
url=WEAVIATE_URL,
|
| 59 |
-
additional_headers={"X-OpenAI-Api-Key": os.environ["OPENAI_API_KEY"]},
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
_eg_template = """## Example:
|
| 63 |
|
| 64 |
Chat History:
|
|
@@ -70,40 +62,39 @@ def get_new_chain1(vectorstore) -> Chain:
|
|
| 70 |
input_variables=["chat_history", "question", "answer"],
|
| 71 |
)
|
| 72 |
|
| 73 |
-
_prefix = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question. You should assume that the question is related to
|
| 74 |
_suffix = """## Example:
|
| 75 |
|
| 76 |
Chat History:
|
| 77 |
{chat_history}
|
| 78 |
Follow Up Input: {question}
|
| 79 |
Standalone question:"""
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
)
|
| 86 |
-
example_selector = SemanticSimilarityExampleSelector(vectorstore=eg_store, k=4)
|
| 87 |
prompt = FewShotPromptTemplate(
|
| 88 |
prefix=_prefix,
|
| 89 |
suffix=_suffix,
|
| 90 |
-
example_selector=
|
| 91 |
example_prompt=_eg_prompt,
|
| 92 |
input_variables=["question", "chat_history"],
|
| 93 |
)
|
| 94 |
-
|
| 95 |
-
key_word_extractor = LLMChain(llm=
|
| 96 |
|
| 97 |
EXAMPLE_PROMPT = PromptTemplate(
|
| 98 |
template=">Example:\nContent:\n---------\n{page_content}\n----------\nSource: {source}",
|
| 99 |
input_variables=["page_content", "source"],
|
| 100 |
)
|
| 101 |
-
|
| 102 |
-
You are
|
|
|
|
| 103 |
You should only use hyperlinks that are explicitly listed as a source in the context. Do NOT make up a hyperlink that is not listed.
|
| 104 |
If the question includes a request for code, provide a code block directly from the documentation.
|
| 105 |
If you don't know the answer, just say "Hmm, I'm not sure." Don't try to make up an answer.
|
| 106 |
-
If the question is not about
|
| 107 |
Question: {question}
|
| 108 |
=========
|
| 109 |
{context}
|
|
@@ -111,10 +102,11 @@ Question: {question}
|
|
| 111 |
Answer in Markdown:"""
|
| 112 |
PROMPT = PromptTemplate(template=template, input_variables=["question", "context"])
|
| 113 |
doc_chain = load_qa_chain(
|
| 114 |
-
|
| 115 |
chain_type="stuff",
|
| 116 |
prompt=PROMPT,
|
| 117 |
document_prompt=EXAMPLE_PROMPT,
|
|
|
|
| 118 |
)
|
| 119 |
return CustomChain(chain=doc_chain, vstore=vectorstore, key_word_extractor=key_word_extractor)
|
| 120 |
|
|
|
|
| 1 |
import json
|
| 2 |
import os
|
| 3 |
import pathlib
|
| 4 |
+
import pickle
|
| 5 |
from typing import Dict, List, Tuple
|
| 6 |
|
| 7 |
import weaviate
|
|
|
|
| 18 |
from langchain.vectorstores import FAISS, Weaviate
|
| 19 |
from pydantic import BaseModel
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
class CustomChain(Chain, BaseModel):
|
| 22 |
|
| 23 |
+
vstore: FAISS
|
| 24 |
chain: BaseCombineDocumentsChain
|
| 25 |
key_word_extractor: Chain
|
| 26 |
|
|
|
|
| 50 |
return {"answer": answer}
|
| 51 |
|
| 52 |
|
| 53 |
+
def get_new_chain1(vectorstore, rephraser_llm, final_output_llm) -> Chain:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
_eg_template = """## Example:
|
| 55 |
|
| 56 |
Chat History:
|
|
|
|
| 62 |
input_variables=["chat_history", "question", "answer"],
|
| 63 |
)
|
| 64 |
|
| 65 |
+
_prefix = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question. You should assume that the question is related to Hugging Face Code."""
|
| 66 |
_suffix = """## Example:
|
| 67 |
|
| 68 |
Chat History:
|
| 69 |
{chat_history}
|
| 70 |
Follow Up Input: {question}
|
| 71 |
Standalone question:"""
|
| 72 |
+
|
| 73 |
+
#### LOAD VSTORE WITH REPHRASE EXAMPLES
|
| 74 |
+
with open("rephrase_eg.pkl", 'rb') as f:
|
| 75 |
+
rephrase_example_selector = pickle.load(f)
|
| 76 |
+
|
|
|
|
|
|
|
| 77 |
prompt = FewShotPromptTemplate(
|
| 78 |
prefix=_prefix,
|
| 79 |
suffix=_suffix,
|
| 80 |
+
example_selector=rephrase_example_selector,
|
| 81 |
example_prompt=_eg_prompt,
|
| 82 |
input_variables=["question", "chat_history"],
|
| 83 |
)
|
| 84 |
+
|
| 85 |
+
key_word_extractor = LLMChain(llm=rephraser_llm, prompt=prompt)
|
| 86 |
|
| 87 |
EXAMPLE_PROMPT = PromptTemplate(
|
| 88 |
template=">Example:\nContent:\n---------\n{page_content}\n----------\nSource: {source}",
|
| 89 |
input_variables=["page_content", "source"],
|
| 90 |
)
|
| 91 |
+
|
| 92 |
+
template = """You are an AI assistant for the open source transformers library provided by Hugging Face. The documentation is located at https://huggingface.co/docs/transformers.
|
| 93 |
+
You are given the following extracted parts of a long document and a question. Provide a conversational answer with a hyperlink to the documentation. Do NOT add .html to the end of links.
|
| 94 |
You should only use hyperlinks that are explicitly listed as a source in the context. Do NOT make up a hyperlink that is not listed.
|
| 95 |
If the question includes a request for code, provide a code block directly from the documentation.
|
| 96 |
If you don't know the answer, just say "Hmm, I'm not sure." Don't try to make up an answer.
|
| 97 |
+
If the question is not about Hugging Face Transformers, politely inform them that you are tuned to only answer questions about Transformers.
|
| 98 |
Question: {question}
|
| 99 |
=========
|
| 100 |
{context}
|
|
|
|
| 102 |
Answer in Markdown:"""
|
| 103 |
PROMPT = PromptTemplate(template=template, input_variables=["question", "context"])
|
| 104 |
doc_chain = load_qa_chain(
|
| 105 |
+
final_output_llm,
|
| 106 |
chain_type="stuff",
|
| 107 |
prompt=PROMPT,
|
| 108 |
document_prompt=EXAMPLE_PROMPT,
|
| 109 |
+
verbose=True
|
| 110 |
)
|
| 111 |
return CustomChain(chain=doc_chain, vstore=vectorstore, key_word_extractor=key_word_extractor)
|
| 112 |
|