INNER CODE UNIT · Python
category_response_parser
google-marketing-solutions/feedgen · enrichment/src/main.py:99
def category_response_parser(response):
if "candidates" in response and len(response["candidates"][0]) > 0 and "content" in response["candidates"][0] and "parts" in response["candidates"][0]["content"] and len(response["candidates"][0]["content"]["parts"]) > 0:
category = response["candidates"][0]["content"]["parts"][0]["text"]
return category
return "N/A"
# Extract the id from the request
def id_response_parser(request):
for contents in request['contents']:
for parts in contents['parts']:
if parts["text"].startswith("Id: "):
current_id = parts["text"].replace("Id: ", "")
return current_id
return "N/A" # for good practice, but Id is always present in request
df_category[target_column_name] = df_category['response'].apply(category_response_parser)
df_category[id_column_name] = df_category['request'].apply(id_response_parser)