Home > Back-end >  TypeError: unsupported operand type(s) for : 'set' and 'set' returning multiple
TypeError: unsupported operand type(s) for : 'set' and 'set' returning multiple

Time:02-10

I'm kinda beginner using python from javascript, and wanting to return param vals together into an api... I've been trying to use though it give's me error

"TypeError: unsupported operand type(s) for : 'set' and 'set'"

How do correcting this?

def generate_ad(product_type: str, product_name: str, platform: str, audience: str) -> str:
    # Load your API key from an environment variable or secret management service
    product_type = f'Write a creative ad for {product_type} '
    product_name = f'named {product_name}'
    platform = f'to run on {platform} '
    audience = f'aimed at {audience}:'
    
    enriched_prompt = product_type   product_name   platform   audience

test API call:

from fastapi import FastAPI
from riplir import generate_ad


app = FastAPI()


@app.get("/generate_snippet")
async def creative_ad_api(product_type: str, product_name: str , platform: str, audience: str):
    snippet = generate_ad({product_type}   {product_name}   {platform}   {audience})
    
    return {
        "message": snippet,
        }

# uvicorn riplir_api:app --reload

CodePudding user response:

Either the generate_ad function input is a string, which you can build using f-string:

@app.get("/generate_snippet")
async def creative_ad_api(product_type: str,
                          product_name: str,
                          platform: str,
                          audience: str):
    snippet = generate_ad(f'{product_type}   {product_name}   {platform}   {audience}')

    return {
    "message": snippet,
    }

Or the input is a set, which can be built from the 4 string parameters:

@app.get("/generate_snippet")
async def creative_ad_api(product_type: str,
                          product_name: str,
                          platform: str,
                          audience: str):
    snippet = generate_ad(set(product_type, product_name, platform, audience))

    return {
    "message": snippet,
    }

Or simply, you have 4 inputs:

@app.get("/generate_snippet")
async def creative_ad_api(product_type: str,
                          product_name: str,
                          platform: str,
                          audience: str):
    snippet = generate_ad(product_type, product_name, platform, audience)

    return {
    "message": snippet,
    }
  • Related