Discussions

Ask a Question
Back to All

Agent creation failed.

I am using python function to create agent and seeing authentication failed and also failed to create agent. But same API key is working to create talk.

def create_did_agent(summary, pdf_name, avatar_url=None):
print("Creating D-ID agent...")

# Use default avatar if none is provided
if not avatar_url:
    avatar_url = "https://d-id-public-bucket.s3.us-west-2.amazonaws.com/alice.jpg"

# Prepare agent info
agent_name = f"PDF_Agent_{os.path.splitext(os.path.basename(pdf_name))[0]}"
agent_data = {
    "name": agent_name,
    "avatar_image_url": avatar_url,
    "expertise": "PDF document analysis and summarization",
    "voice_id": "Sara",  # Default voice
    "voice_provider": "microsoft",
    "system_prompt": f"You are an AI assistant specialized in discussing the content of a PDF document. Here's a summary of the document: {summary}",
    "first_message": "Hello! I'm your PDF document assistant. I can answer questions about the document you uploaded. What would you like to know?",
    "rate_limit": 50  # Adjust as needed
}

headers = {
    "Authorization": f"Basic {DID_API_KEY}",
    "Content-Type": "application/json"
}

# Create the agent
try:
    response = requests.post(DID_AGENTS_URL, json=agent_data, headers=headers)
    response.raise_for_status()
    agent_info = response.json()
    print(f"Agent created successfully with ID: {agent_info.get('id')}")
    return agent_info
except requests.exceptions.HTTPError as e:
    if response.status_code == 401:
        print("Authentication failed. Please check your API key.")
    else:
        print(f"API request failed: {e}")
        print(f"Response: {response.text}")
    return None
except requests.exceptions.RequestException as e:
    print(f"API request failed: {e}")
    return None

def downloadavatar(avatar_url, output_dir, agent_id):
print(f"Downloading avatar from {avatar_url}...")
try:
# Parse URL to get filename
parsed_url = urlparse(avatar_url)
filename = os.path.basename(parsed_url.path)
if not filename:
filename = f"avatar
{agent_id}.jpg"

    # Download the image
    response = requests.get(avatar_url, stream=True)
    response.raise_for_status()
    
    # Save the image
    avatar_path = os.path.join(output_dir, filename)
    with open(avatar_path, 'wb') as f:
        response.raw.decode_content = True
        shutil.copyfileobj(response.raw, f)
    
    print(f"Avatar downloaded to {avatar_path}")
    return avatar_path
except Exception as e:
    print(f"Failed to download avatar: {e}")
    return None