[Python] Why am I not getting the desired output for my LLM Product detail generator [closed]

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo


    I am not getting the desired result as per prompt. this is part of LLM, where some Product details needs to come and it will generate the output in form of Product Name, Short Description, Full Description, Keywords
    ​

    def generate_listing(product_name, short_description):
    """Generate a comprehensive product listing"""
    prompt = f"""You are an expert e-commerce product listing creator and SEO specialist.
    You need to create a complete product listing for a given product.
    Your listing should be suitable for popular platforms like amazon.com, ebay.com
    Make sure to use only the information provided to create an accurate listing.
    Output the listing in the following format:
    **Product Name:** Smart LED TV
    **Short Description:** Experience stunning visuals with this 32-inch Smart LED TV. Enjoy crystal-clear picture quality and seamless connectivity.
    **Full Description:** This Smart LED TV delivers immersive viewing with its vibrant display and advanced features. Stream your favorite content, connect to your devices, and enjoy endless entertainment. With its sleek design and intuitive interface, this TV is a perfect addition to your home entertainment setup.
    **Keywords:** Smart TV, LED TV, 32-inch TV, HD TV, Smart Home
    """

    user_test = f""" You are an expert e-commerce product listing creator and SEO specialist
    The details given should be related to product and should be SEO Optimized and suitable for popular platforms like {reference_sites}
    The details should be limited to Product name, Short Description, Full Description and Keywords
    You need to create a complete product listing for the following product:

    Product name: {product_name}
    Short description: {short_description}
    Full description: Give a full description of the product in 2-3 paragraphs
    Keywords: Give 3-5 keywords that would help this product rank well
    """

    messages_updated = [
    {"role" : "system", "content" : prompt},
    {"role" : "user", "content" : user_test}
    ]
    inputs =tokenizer.apply_chat_template(messages_updated, return_tensors="pt")


    #inputs=tokenizer(prompt, return_tensors="pt")
    outputs = model.generate(inputs,temperature=0.7, max_new_tokens=1024)
    full_response = tokenizer.decode(outputs[0])

    #Extract the assistant's reponse (Product listing details)
    start_index = full_response.find("**Product Name:**")
    end_index = full_response.find("<|eot_id|>", start_index) #find the end marker
    response = full_response[start_index:end_index].strip()


    #response = response
    #response = text_pipeline(prompt, max_new_tokens=1024)[0]["generated_text"]

    fields = ["Product Name:", "Short Description:", "Full Description:", "Keywords:"]
    results = {}

    for field in fields:
    start_index = response.find(f"**{field}**")
    if start_index == -1:
    print(f"{field} not found in the response")
    result[field[:1]] = "Not available"
    continue

    end_index = response.find(f"**{fields[(fields.index(field) + 1) % len(fields)]}**", start_index + len(f"**{field}**"))

    if end_index == -1:
    end_index = len(response) # If next field not found, take till the end

    results[field[:-1]] = response[start_index + len(f"**{field}**"):end_index].strip()

    title = results.get("Product Name", "Not found")
    short_desc = results.get("Short Description", "Not found")
    desc = results.get("Full Description", "Not found")
    keywords = results.get("Keywords", "Not found")

    return title, short_desc, desc, keywords


    generate_listing("tv", "32 inch latest tv")


    OUTPUT The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's attention_mask to obtain reliable results. Setting pad_token_id to eos_token_id:128001 for open-end generation. ('Not found', 'Not found', 'Not found', 'Smart TV, LED TV, 32-inch TV, HD TV, Smart Home')

    Continue reading...

Compartilhe esta Página