|
| 1 | +"""LLM query and response steps.""" |
| 2 | + |
| 3 | +import requests |
| 4 | +from behave import then, when |
| 5 | +from behave.runner import Context |
| 6 | + |
| 7 | +DEFAULT_LLM_TIMEOUT = 60 |
| 8 | + |
| 9 | + |
| 10 | +@when('I ask question "{question}"') |
| 11 | +def ask_question(context: Context, question: str) -> None: |
| 12 | + """Call the service REST API endpoint with question.""" |
| 13 | + base = f"http://{context.hostname}:{context.port}" |
| 14 | + path = f"{context.api_prefix}/query".replace("//", "/") |
| 15 | + url = base + path |
| 16 | + data = {"query": question} |
| 17 | + context.response = requests.post(url, json=data, timeout=DEFAULT_LLM_TIMEOUT) |
| 18 | + |
| 19 | + |
| 20 | +@then("The response should have proper LLM response format") |
| 21 | +def check_llm_response_format(context: Context) -> None: |
| 22 | + """Check the format of response from the service with LLM-generated answer.""" |
| 23 | + assert context.response is not None |
| 24 | + response_json = context.response.json() |
| 25 | + assert "conversation_id" in response_json |
| 26 | + assert "response" in response_json |
| 27 | + |
| 28 | + |
| 29 | +@then("The response should not be truncated") |
| 30 | +def check_llm_response_not_truncated(context: Context) -> None: |
| 31 | + """Check that the response from LLM is not truncated.""" |
| 32 | + assert context.response is not None |
| 33 | + response_json = context.response.json() |
| 34 | + assert response_json["truncated"] is False |
| 35 | + |
| 36 | + |
| 37 | +@then("The response should contain following fragments") |
| 38 | +def check_fragments_in_response(context: Context) -> None: |
| 39 | + """Check if the LLM response contain list of fragments.""" |
| 40 | + assert context.response is not None |
| 41 | + response_json = context.response.json() |
| 42 | + response = response_json["response"] |
| 43 | + |
| 44 | + for fragment in context.table: |
| 45 | + expected = fragment["Fragments in LLM response"] |
| 46 | + assert ( |
| 47 | + expected in response |
| 48 | + ), f"Fragment '{expected}' not found in LLM response: '{response}'" |
0 commit comments