Skip to content

Commit dc53f6d

Browse files
committed
feat: Improve promts and extend tests
1 parent ad53724 commit dc53f6d

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/viur/assistant/modules/assistant.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def translate(
206206
"content": (
207207
f"Translate the following text into {CONFIG.language_map.get(language, language)}"
208208
f" ({". ".join(characteristics)})"
209-
f" and only return the translation, keep HTML-tags: {text}\n"
209+
f" and only return the translation, keep HTML-tags (if there are any):\n\n{text}\n"
210210
)
211211
}],
212212
stop=None,
@@ -400,7 +400,7 @@ def openai_create_completion(
400400
message = message["answer"]
401401
except (JSONDecodeError, KeyError):
402402
raise errors.InternalServerError("Got invalid JSON from API")
403-
return message.encode("utf-8").decode() # get rid of unicode codes like \u00ea
403+
return message
404404

405405
def render_text(self, text: str) -> t.Any:
406406
"""

tests/test_translate.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import os
2-
import pytest
31
import requests
42

53
BASE_URL = "http://localhost:8080/json/assistant/translate"
64

75
from utils import session
86

7+
98
def print_response_on_error(response: requests.Response):
109
if response.status_code >= 400:
1110
print(f"\n[HTTP ERROR] {response.status_code} {response.reason}")
1211
print(f"Response body:\n{response.text}\n")
13-
print(f"Response body:\n{response.text}\n")
12+
print(f"Response body:\n{response.json()}\n")
13+
1414

1515
def test_translate_minimal(session):
1616
params = {
@@ -21,6 +21,20 @@ def test_translate_minimal(session):
2121
print_response_on_error(response)
2222
assert response.status_code == 200
2323
assert response.text.strip() # should contain translation
24+
assert response.json().strip() # should be JSON
25+
26+
27+
def test_translate_umlauts(session):
28+
params = {
29+
"text": "flowers bloom in spring.",
30+
"language": "de",
31+
}
32+
response = session.post(BASE_URL, params=params)
33+
print_response_on_error(response)
34+
assert response.status_code == 200
35+
assert response.text.strip() # should contain translation
36+
assert "ü" in response.json()
37+
2438

2539
def test_translate_with_characteristic(session):
2640
params = {
@@ -33,6 +47,7 @@ def test_translate_with_characteristic(session):
3347
assert response.status_code == 200
3448
assert response.text.strip()
3549

50+
3651
def test_translate_with_simplified(session):
3752
params = {
3853
"text": "Hallo Welt!",
@@ -44,6 +59,7 @@ def test_translate_with_simplified(session):
4459
assert response.status_code == 200
4560
assert response.text.strip()
4661

62+
4763
def test_translate_conflicting_simplified_and_characteristic(session):
4864
params = {
4965
"text": "Hallo Welt!",
@@ -55,6 +71,7 @@ def test_translate_conflicting_simplified_and_characteristic(session):
5571
print_response_on_error(response)
5672
assert response.status_code == 400 # BadRequest
5773

74+
5875
def test_translate_missing_parameters(session):
5976
params = {
6077
"language": "en"

0 commit comments

Comments
 (0)