Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make preamble in vector query prompt customisable #642

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions griptape/engines/query/base_query_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def query(
*,
rulesets: Optional[list[Ruleset]] = None,
top_n: Optional[int] = None,
preamble: Optional[str] = None,
) -> TextArtifact:
...

Expand Down
6 changes: 6 additions & 0 deletions griptape/engines/query/vector_query_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class VectorQueryEngine(BaseQueryEngine):
)
template_generator: J2 = field(default=Factory(lambda: J2("engines/query/vector_query.j2")), kw_only=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I do like the granularity of being able to override specific bits of the prompt, I'm not sure this change is necessary since users can override template_generator to completely change the prompt.

@andrewfrench @vasinov do you have any suggestions for how we might improve this?


DEFAULT_QUERY_PREAMBLE = "You can answer questions by searching through text segments. Always be truthful. Don't make up facts. Use the below list of text segments to respond to the subsequent query. If the answer cannot be found in the segments, say 'I could not find an answer'."

def query(
self,
query: str,
Expand All @@ -31,6 +33,7 @@ def query(
rulesets: Optional[list[Ruleset]] = None,
metadata: Optional[str] = None,
top_n: Optional[int] = None,
preamble: Optional[str] = None,
) -> TextArtifact:
tokenizer = self.prompt_driver.tokenizer
result = self.vector_store_driver.query(query, top_n, namespace)
Expand All @@ -41,11 +44,13 @@ def query(
]
text_segments = []
message = ""
preamble = preamble if preamble else self.DEFAULT_QUERY_PREAMBLE

for artifact in artifacts:
text_segments.append(artifact.value)

message = self.template_generator.render(
preamble=preamble,
metadata=metadata,
query=query,
text_segments=text_segments,
Expand All @@ -59,6 +64,7 @@ def query(
text_segments.pop()

message = self.template_generator.render(
preamble=preamble,
metadata=metadata,
query=query,
text_segments=text_segments,
Expand Down
7 changes: 6 additions & 1 deletion griptape/tasks/text_query_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ class TextQueryTask(BaseTextInputTask):
loader: TextLoader = field(default=Factory(lambda: TextLoader()), kw_only=True)
namespace: Optional[str] = field(default=None, kw_only=True)
top_n: Optional[int] = field(default=None, kw_only=True)
preamble: Optional[str] = field(default=None, kw_only=True)

def run(self) -> TextArtifact:
return self.query_engine.query(
self.input.to_text(), namespace=self.namespace, rulesets=self.all_rulesets, top_n=self.top_n
self.input.to_text(),
namespace=self.namespace,
rulesets=self.all_rulesets,
top_n=self.top_n,
preamble=self.preamble,
)
2 changes: 1 addition & 1 deletion griptape/templates/engines/query/vector_query.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
You can answer questions by searching through text segments. Always be truthful. Don't make up facts. Use the below list of text segments to respond to the subsequent query. If the answer cannot be found in the segments, say "I could not find an answer."
{{ preamble }}

{% if metadata %}
Metadata: """
Expand Down