Skip to content

Commit

Permalink
Re-introduce temperature to question answer tool
Browse files Browse the repository at this point in the history
  • Loading branch information
cecheta committed May 17, 2024
1 parent 8530842 commit 036ee72
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
8 changes: 7 additions & 1 deletion code/backend/batch/utilities/helpers/llm_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,17 @@ def get_chat_completion_with_functions(
function_call=function_call,
)

def get_chat_completion(self, messages: list[dict], model: str | None = None):
def get_chat_completion(
self,
messages: list[dict],
model: str | None = None,
temperature: float | None = None,
):
return self.openai_client.chat.completions.create(
model=model or self.llm_model,
messages=messages,
max_tokens=self.llm_max_tokens,
**({"temperature": temperature} if temperature is not None else {})
)

def get_sk_chat_completion_service(self, service_id: str):
Expand Down
4 changes: 3 additions & 1 deletion code/backend/batch/utilities/tools/question_answer_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ def answer_question(self, question: str, chat_history: list[dict], **kwargs):

llm_helper = LLMHelper()

response = llm_helper.get_chat_completion(messages)
response = llm_helper.get_chat_completion(
messages, temperature=float(self.env_helper.AZURE_OPENAI_TEMPERATURE)
)

answer = response.choices[0].message.content
logger.debug(f"Answer: {answer}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ def test_post_makes_correct_call_to_openai_chat_completions_with_documents(
],
"model": app_config.get("AZURE_OPENAI_MODEL"),
"max_tokens": int(app_config.get("AZURE_OPENAI_MAX_TOKENS")),
"temperature": float(app_config.get("AZURE_OPENAI_TEMPERATURE")),
},
headers={
"Accept": "application/json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ def test_post_makes_correct_call_to_openai_chat_completions_in_question_answer_t
],
"model": app_config.get("AZURE_OPENAI_MODEL"),
"max_tokens": int(app_config.get("AZURE_OPENAI_MAX_TOKENS")),
"temperature": float(app_config.get("AZURE_OPENAI_TEMPERATURE")),
},
headers={
"Accept": "application/json",
Expand Down
13 changes: 9 additions & 4 deletions code/tests/utilities/tools/test_question_answer_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def env_helper_mock():
with patch("backend.batch.utilities.tools.question_answer_tool.EnvHelper") as mock:
env_helper = mock.return_value
env_helper.AZURE_OPENAI_SYSTEM_MESSAGE = "mock azure openai system message"
env_helper.AZURE_OPENAI_TEMPERATURE = "0.0"
env_helper.AZURE_SEARCH_TOP_K = 1
env_helper.AZURE_SEARCH_FILTER = "mock filter"
env_helper.AZURE_SEARCH_USE_INTEGRATED_VECTORIZATION = False
Expand Down Expand Up @@ -162,7 +163,8 @@ def test_correct_prompt_with_few_shot_example(llm_helper_mock: MagicMock):
"content": 'Sources: {"retrieved_documents":[{"[doc1]":{"content":"mock content"}},{"[doc2]":{"content":"mock content 2"}}]}, Question: mock question',
"role": "user",
},
]
],
temperature=0.0,
)


Expand All @@ -189,7 +191,8 @@ def test_correct_prompt_without_few_shot_example(
"content": 'Sources: {"retrieved_documents":[{"[doc1]":{"content":"mock content"}},{"[doc2]":{"content":"mock content 2"}}]}, Question: mock question',
"role": "user",
},
]
],
temperature=0.0,
)


Expand Down Expand Up @@ -227,7 +230,8 @@ def test_correct_prompt_with_few_shot_example_and_chat_history(
"content": 'Sources: {"retrieved_documents":[{"[doc1]":{"content":"mock content"}},{"[doc2]":{"content":"mock content 2"}}]}, Question: mock question',
"role": "user",
},
]
],
temperature=0.0,
)


Expand Down Expand Up @@ -256,7 +260,8 @@ def test_non_on_your_data_prompt_correct(
"content": "Sources: [doc1]: mock content\n\n[doc2]: mock content 2, Question: mock question",
"role": "user",
},
]
],
temperature=0.0,
)


Expand Down

0 comments on commit 036ee72

Please sign in to comment.