/
prompt_templates.py
68 lines (52 loc) 路 2.49 KB
/
prompt_templates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from abc import ABC, abstractmethod
from langchain.prompts import PromptTemplate
from pydantic import BaseModel
class BasePromptTemplate(ABC, BaseModel):
@abstractmethod
def create_template(self) -> PromptTemplate:
pass
class QueryExpansionTemplate(BasePromptTemplate):
prompt: str = """You are an AI language model assistant. Your task is to generate {to_expand_to_n}
different versions of the given user question to retrieve relevant documents from a vector
database. By generating multiple perspectives on the user question, your goal is to help
the user overcome some of the limitations of the distance-based similarity search.
Provide these alternative questions seperated by '{separator}'.
Original question: {question}"""
@property
def separator(self) -> str:
return "#next-question#"
def create_template(self, to_expand_to_n: int) -> PromptTemplate:
return PromptTemplate(
template=self.prompt,
input_variables=["question"],
partial_variables={
"separator": self.separator,
"to_expand_to_n": to_expand_to_n,
},
)
class SelfQueryTemplate(BasePromptTemplate):
prompt: str = """You are an AI language model assistant. Your task is to extract information from a user question.
The required information that needs to be extracted is the user or author id.
Your response should consists of only the extracted id (e.g. 1345256), nothing else.
User question: {question}"""
def create_template(self) -> PromptTemplate:
return PromptTemplate(template=self.prompt, input_variables=["question"])
class RerankingTemplate(BasePromptTemplate):
prompt: str = """You are an AI language model assistant. Your task is to rerank passages related to a query
based on their relevance.
The most relevant passages should be put at the beginning.
You should only pick at max {keep_top_k} passages.
The provided and reranked documents are separated by '{separator}'.
The following are passages related to this query: {question}.
Passages:
{passages}
"""
def create_template(self, keep_top_k: int) -> PromptTemplate:
return PromptTemplate(
template=self.prompt,
input_variables=["question", "passages"],
partial_variables={"keep_top_k": keep_top_k, "separator": self.separator},
)
@property
def separator(self) -> str:
return "\n#next-document#\n"