Skip to content

Commit

Permalink
Merge pull request #8 from HumanCompatibleAI/error-handle-score
Browse files Browse the repository at this point in the history
added logging for 404 bad request errors
  • Loading branch information
JACProjec authored Aug 21, 2024
2 parents f493b3a + eab3e14 commit c3a081d
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions perspective_ranker.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,45 @@ async def score(self, attributes, statement, statement_id):
"requestedAttributes": {attr: {} for attr in attributes},
}

response = httpx.post(
f"{PERSPECTIVE_HOST}/v1alpha1/comments:analyze?key={self.api_key}",
json=data,
headers=headers,
).json()

results = []
scorable = True
for attr in attributes:
try:
score = response["attributeScores"][attr]["summaryScore"]["value"]
except KeyError:
score = (
0 # for now, set the score to 0 if it wasn't possible get a score
logger.info(f"Sending request to Perspective API for statement_id: {statement_id}")
logger.debug(f"Request payload: {data}")

try:
async with httpx.AsyncClient() as client:
response = await client.post(
f"{PERSPECTIVE_HOST}/v1alpha1/comments:analyze?key={self.api_key}",
json=data,
headers=headers,
)
scorable = False

results.append((attr, score))

result = self.ScoredStatement(statement, results, statement_id, scorable)

return result

response.raise_for_status()
response_json = response.json()

logger.debug(f"Response for statement_id {statement_id}: {response_json}")

results = []
scorable = True
for attr in attributes:
try:
score = response_json["attributeScores"][attr]["summaryScore"]["value"]
except KeyError:
logger.warning(f"Failed to get score for attribute {attr} in statement_id {statement_id}")
score = 0
scorable = False

results.append((attr, score))

result = self.ScoredStatement(statement, results, statement_id, scorable)
return result

except httpx.HTTPStatusError as e:
logger.error(f"HTTP error occurred for statement_id {statement_id}: {e}")
logger.error(f"Response content: {e.response.text}")
raise

except Exception as e:
logger.error(f"Unexpected error occurred for statement_id {statement_id}: {e}")
raise

async def ranker(self, ranking_request: RankingRequest):
arm_weights = self.arm_selection(ranking_request)
Expand Down

0 comments on commit c3a081d

Please sign in to comment.