in infrastructure/cymbal-toy-store/backend/src/backend/chat_handler.py [0:0]
def _create_response(self, prompts: list[BaseMessage], intent: dict[str, Any],messages: dict[str, Any]) -> dict:
image_url = ""
if intent["shouldDescribeImage"]:
image_prompt = messages[-1]["text"]
image_data=messages[-1]["image_url"]["url"]
prompts[0] = HumanMessage(
content=[
{"type": "text", "text": f"{image_prompt}"},
{
"type": "image_url",
"image_url": {"url": f"{image_data}"},
},
],
)
model_response = self.chat_llm.invoke([prompts[0]])
res_json={}
res_json['content'] = {'type':'text','text':f'{model_response.content}'}
res_json['image_url'] = {'type': "image_url", "image_url": {"url": f"{image_data}"}}
return res_json
if not intent["isOnTopic"]:
prompts[0] = self._gen_system_template("Gently redirect the conversation back to toys and other products in the store. Do not use markdown for output")
elif not intent["shouldRecommendProduct"]:
prompts[0] = self._gen_system_template(f"Need more information - {intent['shouldRecommendProductReasoning']}")
else:
products = self._find_similar_products(intent["summary"], 5)
print(f"The products: {products}")
image_url= products[list(products.keys())[0]]["image_url"]
products_str = "\n".join(str(row) for row in products.values())
prompts[0] = self._gen_system_template(
#With RAG
f"Recommend a suitable product for the user from the below.\n{products_str}\nIn 35 words or less, mention the name (leave out the brand name) and price of the toy, and why the recommended product is a good fit. \nChoose product only from the provided list and suggest only one product from the list.\n Add url for the chosen product to the very end of the responce like: \n {{image_url:https://storage.googleapis.com/gleb-genai-002-cymbal-images-01/1053.jpeg}}\n")
#Without RAG
#f"Recommend a suitable product for the user \nIn 35 words or less, mention the name (leave out the brand name) and price of the toy or product, and why the recommended product is a good fit.\n")
model_response = self.chat_llm.invoke(prompts)
print(f"Here is model response: {model_response.content}")
content,image_url = self._extract_url(model_response.content)
print(f"Here is content: {content}")
print(f"Here is url: {image_url}")
res_json={}
res_json['content'] = {'type':'text','text':f'{content}'}
res_json['image_url'] = {'type': "image_url", "image_url": {"url": f"{image_url}"}}
print(f"Here is model response: {res_json}")
return res_json