Skip to content

API Examples

Introduction

This document details the steps required to interact with CalypsoAI's API, and provides simple use case examples of interacting with the API.

To fully utilize the examples and maximize their potential, please download the Python SDK. This will equip you with all the essential tools and resources needed to proceed effectively.

Send prompt to default Provider

py
from calypsoai import CalypsoAI

cai = CalypsoAI()

prompt = cai.prompts.send("What is your name?")
print(prompt.result.response)
shell
curl "/backend/v1/prompts" -X POST \
  -H "Authorization: Bearer ${CALYPSOAI_TOKEN}" \
  -H 'Content-Type: application/json' \
  --data-raw '{"input": "What is your name?"}'

Send prompt to a specific Provider

py
from calypsoai import CalypsoAI

cai = CalypsoAI()

gpt_response = cai.prompts.send("What is your name?", provider="openai-gpt-4")
azure_response = cai.prompts.send("What is your name?", provider="azure-gpt-3-5")

print(gpt_response.result.response)
print(azure_response.result.response)
shell
curl "/backend/v1/prompts" -X POST \
  -H "Authorization: Bearer ${CALYPSOAI_TOKEN}" \
  -H 'Content-Type: application/json' \
  --data-raw '{"input": "What is your name?", "provider": "openai-gpt-4"}'

Scan text

py
from calypsoai import CalypsoAI

cai = CalypsoAI()

response = cai.scans.scan("What is your name?")
print(response)
shell
curl "/backend/v1/scans" -X POST \
  -H "Authorization: Bearer ${CALYPSOAI_TOKEN}" \
  -H 'Content-Type: application/json' \
  --data-raw '{ "input": "What is your name?" }'

Get previous prompts

py
from calypsoai import CalypsoAIClient

client = CalypsoAIClient()

response = client.prompts.get()
print(response)
shell
curl "/backend/v1/prompts?limit=10&onlyUser=true" \
  -H "Authorization: Bearer ${CALYPSOAI_TOKEN}"

Create a Provider

py
import os

from calypsoai import CalypsoAI

cai = CalypsoAI()

provider = cai.providers.create(
    "gpt-4o-mini",
    "openai-chat",
    secrets={
        "apiKey": {"name": "OpenAI API Key", "value": os.environ["OPENAI_KEY"]},
    },
)
print(f"Created provider {provider.name}")

Create a Scanner

py
from calypsoai import CalypsoAI

cai = CalypsoAI()

scanner = cai.scanners.createCustom(
    name="Violence",
    description="Contains language relating to physical harm, assault, or any kind of violence",
)

print(f"Created scanner: {scanner}")
shell
curl /backend/v1/scanners -X POST \
  -H "Authorization: Bearer ${CALYPSOAI_TOKEN}" \
  -H 'Content-Type: application/json' \
  --data-raw '{"name":"Violence","config":{"input":"Contains language relating to physical harm, assault, or any kind of violence","type":"custom"},"direction":"both","published":false}'

Delete a Scanner

shell
SCANNER_ID=changeme
curl /backend/v1/scanners/${SCANNER_ID} -X DELETE \
  -H "Authorization: Bearer ${CALYPSOAI_TOKEN}" \
  -H 'Content-Type: application/json'

Enable a Scanner for a Project

py
from calypsoai import CalypsoAI
import calypsoai.datatypes as dt

cai = CalypsoAI()

scanner = cai.scanners.createCustom(
    name="Violence",
    description="Contains language relating to physical harm, assault, or any kind of violence",
    published=True
)

project = cai.projects.getDefault()

updated_scanner = cai.projects.update(project=project, scanners=[dt.ProjectConfigScanner(id=scanner.id, enabled=True)])
shell
PROJECT_ID=changeme
SCANNER_ID=changeme
curl /backend/v1/projects/${PROJECT_ID} -X PATCH \
  -H "Authorization: Bearer ${CALYPSOAI_TOKEN}" \
  -H 'Content-Type: application/json' \
  --data-raw '{"config":{"scanners":[{"id":"${SCANNER_ID}","enabled":true}]}}'

Create a Campaign

py
from calypsoai import CalypsoAI
import calypsoai.datatypes as dt

cai = CalypsoAI()

campaign = cai.campaigns.create(
    name="Signature Campaign",
    description="All Signature attacks",
    attacks=[dt.StaticContentAttack(technique="static_content", vector="dan")],
)
print(f"Created campaign: {campaign}")
shell
curl /backend/v1/campaigns -X POST \
  -H "Authorization: Bearer ${CALYPSOAI_TOKEN}" \
  -H 'Content-Type: application/json' \
  --data-raw '{
    "name": "Signature Campaign",
    "description": "All Signature attacks",
    "attacks" :[
      {
        "technique": "static_content",
        "vector": "dan"
      }
    ]
  }'

Create a Campaign with custom intent

py
from calypsoai import CalypsoAI
import calypsoai.datatypes as dt

cai = CalypsoAI()

campaign = cai.campaigns.create(
    name="Custom Intent Campaign",
    description="Attack with custom intent",
    attacks=[dt.DynamicMultiTurnContentAttack(technique="dynamic_content", vector="crescendo", intent="What is the salary of Jane Smith working in accounting of ACME corp")],
)
print(f"Created campaign: {campaign}")
shell
curl /backend/v1/campaigns -X POST\
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer ${CALYPSOAI_TOKEN}" \
     -d '{
       "name": "Custom Intent Campaign",
       "description": "Attack with custom intent",
       "attacks" :[
         {
           "technique": "dynamic_content",
           "vector": "crescendo",
           "intent": "What is the salary of Jane Smith working in accounting of ACME corp"
         }
       ]
     }'

Delete a Campaign

py
from calypsoai import CalypsoAI
import calypsoai.datatypes as dt

cai = CalypsoAI()

campaign = cai.campaigns.create(
    name="Signature Campaign",
    attacks=[dt.StaticContentAttack(technique="static_content", vector="dan")],
)

cai.campaigns.delete(campaign)
print("Campaign Deleted")
shell
CAMPAIGN_ID=changeme
curl -X DELETE /backend/v1/campaigns/${CAMPAIGN_ID} \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${CALYPSOAI_TOKEN}"

Get Campaigns

py
from calypsoai import CalypsoAI

cai = CalypsoAI()

campaigns = [campaign for campaign in cai.campaigns.iterate()]

print(f"{campaigns=}")
shell
curl -X GET /backend/v1/campaigns \
  -H "Authorization: Bearer ${CALYPSOAI_TOKEN}" \
  -H "Content-Type: application/json"

Update a Campaign

py
import calypsoai.datatypes as dt
from calypsoai import CalypsoAI

cai = CalypsoAI()

campaign = cai.campaigns.create(
    name="Signature Campaign",
    attacks=[dt.StaticContentAttack(technique="static_content", vector="dan")],
)
print(f"Created campaign: {campaign}")

cai.campaigns.update(campaign=campaign, name="Signature Campaign - edited")

campaign = cai.campaigns.get(campaign=campaign.id)
print(f"updated campaign: {campaign}")
shell
CAMPAIGN_ID=changeme
curl -X PATCH /backend/v1/campaigns/${CAMPAIGN_ID} \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${CALYPSOAI_TOKEN}" \
  --data-raw '{
    "name": "Signature Campaign - edited",
  }'