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

Escape quotes for with_bm25/with_hybrid/with_generate #422

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions test/gql/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def test_getbuilder_with_additional_props(additional_props: AdditionalProperties
),
("other query", [], 'bm25:{query: "other query"}'),
("other query", None, 'bm25:{query: "other query"}'),
('what is an "airport"', None, 'bm25:{query: "what is an \\"airport\\""}'),
("what is an 'airport'", None, """bm25:{query: "what is an 'airport'"}"""),
],
)
def test_bm25(query: str, properties: List[str], expected: str):
Expand Down Expand Up @@ -108,6 +110,8 @@ def test_get_references(property_name: str, in_class: str, properties: List[str]
'hybrid:{query: "query", vector: [1, 2, 3], alpha: 0.5}',
),
("query", None, None, None, None, 'hybrid:{query: "query"}'),
('query "query2"', None, None, None, None, 'hybrid:{query: "query \\"query2\\""}'),
("query 'query2'", None, None, None, None, """hybrid:{query: "query 'query2'"}"""),
("query", None, None, ["prop1"], None, 'hybrid:{query: "query", properties: ["prop1"]}'),
(
"query",
Expand Down Expand Up @@ -164,12 +168,36 @@ def test_hybrid(
None,
"""generate(singleResult:{prompt:"What is the meaning of life?"}){error singleResult} """,
),
(
'What is the meaning of "life"?',
None,
None,
"""generate(singleResult:{prompt:"What is the meaning of \\"life\\"?"}){error singleResult} """,
),
(
"What is the meaning of 'life'?",
None,
None,
"""generate(singleResult:{prompt:"What is the meaning of 'life'?"}){error singleResult} """,
),
(
None,
"Explain why these magazines or newspapers are about finance",
None,
"""generate(groupedResult:{task:"Explain why these magazines or newspapers are about finance"}){error groupedResult} """,
),
(
None,
'Explain why these magazines or newspapers are about "finance"',
None,
"""generate(groupedResult:{task:"Explain why these magazines or newspapers are about \\"finance\\""}){error groupedResult} """,
),
(
None,
"Explain why these magazines or newspapers are about 'finance'",
None,
"""generate(groupedResult:{task:"Explain why these magazines or newspapers are about 'finance'"}){error groupedResult} """,
),
(
"What is the meaning of life?",
"Explain why these magazines or newspapers are about finance",
Expand Down
10 changes: 6 additions & 4 deletions weaviate/gql/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BM25:
properties: Optional[List[str]]

def __str__(self) -> str:
ret = f'query: "{util.strip_newlines(self.query)}"'
ret = f"query: {dumps(util.strip_newlines(self.query))}"
if self.properties is not None and len(self.properties) > 0:
props = '","'.join(self.properties)
ret += f', properties: ["{props}"]'
Expand All @@ -59,7 +59,7 @@ class Hybrid:
fusion_type: Optional[HybridFusion]

def __str__(self) -> str:
ret = f'query: "{util.strip_newlines(self.query)}"'
ret = f"query: {dumps(util.strip_newlines(self.query))}"
if self.vector is not None:
ret += f", vector: {self.vector}"
if self.alpha is not None:
Expand Down Expand Up @@ -1130,14 +1130,16 @@ def with_generate(
task_and_prompt = ""
if single_prompt is not None:
results.append("singleResult")
task_and_prompt += f'singleResult:{{prompt:"{util.strip_newlines(single_prompt)}"}}'
task_and_prompt += (
f"singleResult:{{prompt:{dumps(util.strip_newlines(single_prompt))}}}"
)
if grouped_task is not None or (
grouped_properties is not None and len(grouped_properties) > 0
):
results.append("groupedResult")
args = []
if grouped_task is not None:
args.append(f'task:"{util.strip_newlines(grouped_task)}"')
args.append(f"task:{dumps(util.strip_newlines(grouped_task))}")
if grouped_properties is not None and len(grouped_properties) > 0:
props = '","'.join(grouped_properties)
args.append(f'properties:["{props}"]')
Expand Down