Skip to content

Commit 7feabec

Browse files
author
Preetam Joshi
committed
Fixed the simple aimon client's handling of batch requests.
1 parent 09395f5 commit 7feabec

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/aimon_rely_client/simple_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,4 @@ def detect(self, data_to_send: List[Dict[str, Any]]):
163163
if len(response.json()) == 0 or 'error' in response.json() or 'error' in response.json()[0]:
164164
raise Exception(
165165
f"Received an error in the response: {response if len(response.json()) == 0 else response.json()}")
166-
return response.json()[0]
166+
return response.json()

src/aimon_rely_client/simple_client_test.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,66 @@ def test_valid_data_valid_response(self):
1313
config = Config({'hallucination': 'default'})
1414
client = SimpleAimonRelyClient(api_key=API_KEY, config=config)
1515
data_to_send = [{"context": "This is the context", "generated_text": "This is the context"}]
16-
response = client.detect(data_to_send)
16+
response = client.detect(data_to_send)[0]
1717
assert "hallucination" in response
1818
assert "is_hallucinated" in response['hallucination']
1919
assert response['hallucination']["is_hallucinated"] == "False"
2020
assert "score" in response['hallucination']
2121
assert "sentences" in response['hallucination']
2222
assert len(response['hallucination']["sentences"]) == 1
23+
assert "This is the context" in response['hallucination']["sentences"][0]["text"]
24+
25+
def test_valid_batch_data_valid_response(self):
26+
config = Config({'hallucination': 'default'})
27+
client = SimpleAimonRelyClient(api_key=API_KEY, config=config)
28+
data_to_send = [{"context": "This is the context", "generated_text": "This is the context"}, {"context": "This is the second context", "generated_text": "This is the second context"}]
29+
response = client.detect(data_to_send)
30+
for i in range(2):
31+
assert "hallucination" in response[i]
32+
assert "is_hallucinated" in response[i]['hallucination']
33+
assert response[i]['hallucination']["is_hallucinated"] == "False"
34+
assert "score" in response[i]['hallucination']
35+
assert "sentences" in response[i]['hallucination']
36+
assert len(response[i]['hallucination']["sentences"]) == 1
37+
assert "This is the context" in response[0]['hallucination']["sentences"][0]["text"]
38+
assert "This is the second context" in response[1]['hallucination']["sentences"][0]["text"]
2339

2440
# Sends an HTTP POST request to the Aimon Rely Hallucination Detection API with a single dict object containing a valid "context" and "generated_text" but with a very short text and receives a valid response
2541
def test_short_text_valid_response(self):
2642
config = Config({'hallucination': 'default'})
2743
client = SimpleAimonRelyClient(api_key=API_KEY, config=config)
28-
short_text = "a"
44+
short_text = "Yes"
2945
data_to_send = [{"context": "This is the context", "generated_text": short_text}]
30-
response = client.detect(data_to_send)
46+
response = client.detect(data_to_send)[0]
3147
assert "hallucination" in response
3248
assert "is_hallucinated" in response['hallucination']
33-
assert response['hallucination']["is_hallucinated"] == "True"
3449
assert "score" in response['hallucination']
50+
assert response['hallucination']["score"] >= 0.48
3551
assert "sentences" in response['hallucination']
3652
assert len(response['hallucination']["sentences"]) == 1
53+
assert "Yes" in response['hallucination']["sentences"][0]["text"]
3754

3855
# Sends an HTTP POST request to the Aimon Rely Hallucination Detection API with a single dict object containing a valid "context" and "generated_text" but with a text containing special characters and receives a valid response
3956
def test_special_characters_valid_response(self):
4057
client = SimpleAimonRelyClient(api_key=API_KEY)
4158
special_text = "!@#$%^&*()_+"
4259
data_to_send = [{"context": "This is the context", "generated_text": special_text}]
43-
response = client.detect(data_to_send)
60+
response = client.detect(data_to_send)[0]
4461
assert "hallucination" in response
4562
assert "is_hallucinated" in response['hallucination']
4663
assert response['hallucination']["is_hallucinated"] == "True"
4764
assert "score" in response['hallucination']
65+
assert response['hallucination']["score"] >= 0.5
4866
assert "sentences" in response['hallucination']
49-
assert len(response['hallucination']["sentences"]) == 12
67+
assert len(response['hallucination']["sentences"]) == 1
5068

5169
def test_valid_data_valid_response_conciseness(self):
5270
config = Config({'conciseness': 'default'})
5371
client = SimpleAimonRelyClient(api_key=API_KEY, config=config)
5472
data_to_send = [{
5573
"context": "the abc have reported that those who receive centrelink payments made up half of radio rental's income last year. Centrelink payments themselves were up 20%.",
5674
"generated_text": "those who receive centrelink payments made up half of radio rental's income last year. The Centrelink payments were 20% up."}]
57-
response = client.detect(data_to_send)
75+
response = client.detect(data_to_send)[0]
5876
print(response["quality_metrics"])
5977
assert "quality_metrics" in response
6078
assert "results" in response["quality_metrics"]
@@ -69,7 +87,7 @@ def test_valid_data_valid_response_completeness(self):
6987
data_to_send = [{
7088
"context": "the abc have reported that those who receive centrelink payments made up half of radio rental's income last year. Centrelink payments themselves were up 20%.",
7189
"generated_text": "those who receive centrelink payments made up half of radio rental's income last year. The Centrelink payments were 20% up."}]
72-
response = client.detect(data_to_send)
90+
response = client.detect(data_to_send)[0]
7391
assert "quality_metrics" in response
7492
print(response["quality_metrics"])
7593
assert "results" in response["quality_metrics"]
@@ -83,7 +101,7 @@ def test_valid_data_valid_response_conciseness_completeness(self):
83101
client = SimpleAimonRelyClient(api_key=API_KEY, config=config)
84102
data_to_send = [{"context": "the abc have reported that those who receive centrelink payments made up half of radio rental's income last year. Centrelink payments themselves were up 20%.",
85103
"generated_text": "those who receive centrelink payments made up half of radio rental's income last year. The Centrelink payments were 20% up."}]
86-
response = client.detect(data_to_send)
104+
response = client.detect(data_to_send)[0]
87105
assert "quality_metrics" in response
88106
print(response["quality_metrics"])
89107
assert "results" in response["quality_metrics"]

0 commit comments

Comments
 (0)