Skip to content

Commit

Permalink
Fix for custom template in OpenAIAnswerGenerator (#4220)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjrl authored Feb 21, 2023
1 parent c4b98fc commit 2bedb80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion haystack/nodes/answer_generator/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def __init__(
else:
# Check for required prompts
required_params = ["context", "query"]
if all(p in prompt_template.prompt_params for p in required_params):
if not all(p in prompt_template.prompt_params for p in required_params):
raise ValueError(
"The OpenAIAnswerGenerator requires a PromptTemplate that has `context` and "
"`query` in its `prompt_params`. Supply a different `prompt_template` or "
Expand Down
21 changes: 21 additions & 0 deletions test/nodes/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from haystack.schema import Document
from haystack.nodes.answer_generator import Seq2SeqGenerator, OpenAIAnswerGenerator
from haystack.pipelines import TranslationWrapperPipeline, GenerativeQAPipeline
from haystack.nodes import PromptTemplate

import logging

Expand Down Expand Up @@ -142,6 +143,26 @@ def test_openai_answer_generator(openai_generator, docs):
assert "Carla" in prediction["answers"][0].answer


@pytest.mark.integration
@pytest.mark.skipif(
not os.environ.get("OPENAI_API_KEY", None),
reason="No OpenAI API key provided. Please export an env var called OPENAI_API_KEY containing the OpenAI API key to run this test.",
)
def test_openai_answer_generator_custom_template(docs):
lfqa_prompt = PromptTemplate(
name="lfqa",
prompt_text="""
Synthesize a comprehensive answer from your knowledge and the following topk most relevant paragraphs and the given question.
\n===\Paragraphs: $context\n===\n$query""",
prompt_params=["context", "query"],
)
node = OpenAIAnswerGenerator(
api_key=os.environ.get("OPENAI_API_KEY", ""), model="text-babbage-001", top_k=1, prompt_template=lfqa_prompt
)
prediction = node.predict(query="Who lives in Berlin?", documents=docs, top_k=1)
assert len(prediction["answers"]) == 1


@pytest.mark.integration
@pytest.mark.skipif(
not os.environ.get("OPENAI_API_KEY", None),
Expand Down

0 comments on commit 2bedb80

Please sign in to comment.