Harrison Chase commited on
Commit
cee6e7a
1 Parent(s): 3c87692
Files changed (1) hide show
  1. chain.py +44 -43
chain.py CHANGED
@@ -17,53 +17,14 @@ from langchain.prompts.example_selector import \
17
  from langchain.vectorstores import FAISS, Weaviate
18
  from pydantic import BaseModel
19
 
20
- WEAVIATE_URL = os.environ["WEAVIATE_URL"]
21
- client = weaviate.Client(
22
- url=WEAVIATE_URL,
23
- additional_headers={"X-OpenAI-Api-Key": os.environ["OPENAI_API_KEY"]},
24
- )
25
-
26
- _eg_template = """## Example:
27
-
28
- Chat History:
29
- {chat_history}
30
- Follow Up Input: {question}
31
- Standalone question: {answer}"""
32
- _eg_prompt = PromptTemplate(
33
- template=_eg_template,
34
- input_variables=["chat_history", "question", "answer"],
35
- )
36
-
37
-
38
- _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 LangChain."""
39
- _suffix = """## Example:
40
-
41
- Chat History:
42
- {chat_history}
43
- Follow Up Input: {question}
44
- Standalone question:"""
45
- eg_store = Weaviate(
46
- client,
47
- "Rephrase",
48
- "content",
49
- attributes=["question", "answer", "chat_history"],
50
- )
51
- example_selector = SemanticSimilarityExampleSelector(vectorstore=eg_store, k=4)
52
- prompt = FewShotPromptTemplate(
53
- prefix=_prefix,
54
- suffix=_suffix,
55
- example_selector=example_selector,
56
- example_prompt=_eg_prompt,
57
- input_variables=["question", "chat_history"],
58
- )
59
- llm = OpenAI(temperature=0, model_name="text-davinci-003")
60
- key_word_extractor = LLMChain(llm=llm, prompt=prompt)
61
 
62
 
63
  class CustomChain(Chain, BaseModel):
64
 
65
  vstore: Weaviate
66
  chain: BaseCombineDocumentsChain
 
67
 
68
  @property
69
  def input_keys(self) -> List[str]:
@@ -77,7 +38,7 @@ class CustomChain(Chain, BaseModel):
77
  question = inputs["question"]
78
  chat_history_str = _get_chat_history(inputs["chat_history"])
79
  if chat_history_str:
80
- new_question = key_word_extractor.run(
81
  question=question, chat_history=chat_history_str
82
  )
83
  else:
@@ -92,6 +53,46 @@ class CustomChain(Chain, BaseModel):
92
 
93
 
94
  def get_new_chain1(vectorstore) -> Chain:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  EXAMPLE_PROMPT = PromptTemplate(
97
  template=">Example:\nContent:\n---------\n{page_content}\n----------\nSource: {source}",
@@ -115,7 +116,7 @@ Answer in Markdown:"""
115
  prompt=PROMPT,
116
  document_prompt=EXAMPLE_PROMPT,
117
  )
118
- return CustomChain(chain=doc_chain, vstore=vectorstore)
119
 
120
 
121
  def _get_chat_history(chat_history: List[Tuple[str, str]]):
 
17
  from langchain.vectorstores import FAISS, Weaviate
18
  from pydantic import BaseModel
19
 
20
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
 
23
  class CustomChain(Chain, BaseModel):
24
 
25
  vstore: Weaviate
26
  chain: BaseCombineDocumentsChain
27
+ key_word_extractor: Chain
28
 
29
  @property
30
  def input_keys(self) -> List[str]:
 
38
  question = inputs["question"]
39
  chat_history_str = _get_chat_history(inputs["chat_history"])
40
  if chat_history_str:
41
+ new_question = self.key_word_extractor.run(
42
  question=question, chat_history=chat_history_str
43
  )
44
  else:
 
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:
65
+ {chat_history}
66
+ Follow Up Input: {question}
67
+ Standalone question: {answer}"""
68
+ _eg_prompt = PromptTemplate(
69
+ template=_eg_template,
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 LangChain."""
74
+ _suffix = """## Example:
75
+
76
+ Chat History:
77
+ {chat_history}
78
+ Follow Up Input: {question}
79
+ Standalone question:"""
80
+ eg_store = Weaviate(
81
+ client,
82
+ "Rephrase",
83
+ "content",
84
+ attributes=["question", "answer", "chat_history"],
85
+ )
86
+ example_selector = SemanticSimilarityExampleSelector(vectorstore=eg_store, k=4)
87
+ prompt = FewShotPromptTemplate(
88
+ prefix=_prefix,
89
+ suffix=_suffix,
90
+ example_selector=example_selector,
91
+ example_prompt=_eg_prompt,
92
+ input_variables=["question", "chat_history"],
93
+ )
94
+ llm = OpenAI(temperature=0, model_name="text-davinci-003")
95
+ key_word_extractor = LLMChain(llm=llm, prompt=prompt)
96
 
97
  EXAMPLE_PROMPT = PromptTemplate(
98
  template=">Example:\nContent:\n---------\n{page_content}\n----------\nSource: {source}",
 
116
  prompt=PROMPT,
117
  document_prompt=EXAMPLE_PROMPT,
118
  )
119
+ return CustomChain(chain=doc_chain, vstore=vectorstore, key_word_extractor=key_word_extractor)
120
 
121
 
122
  def _get_chat_history(chat_history: List[Tuple[str, str]]):