in experiments/google/cloud/ml/applied/attributes/attributes.py [0:0]
def generate_attributes(desc: str, candidates: list[dict]) -> m.AttributeValue:
"""Use an LLM to determine attributes given nearest neighbor candidates
Args:
desc: product description
candidates: list of dicts with the following keys:
attributes: attributes in dict form e.g.
{'color':'green', 'pattern': 'striped'}
description: string describing product
Returns: attributes in dict form e.g. {'color':'green', 'pattern': 'striped'}
"""
prompt = generate_prompt(desc, candidates)
llm_parameters = {
"max_output_tokens": 256,
"temperature": 0.0,
}
response = llm.predict(prompt, **llm_parameters)
res = response.text
if not res:
raise ValueError(
"ERROR: No LLM response returned. This seems to be an intermittent bug"
)
try:
formatted_res = parse_answer(res)
except Exception as e:
logging.error(e)
raise ValueError(f"LLM Response: {res} is not in the expected format")
return m.dict_to_attribute_values(formatted_res)