Skip to content

Agentes e Threads

Recomendamos a criação de agentes através da interface da plataforma SipPulse AI, pois ela oferece recursos mais intuitivos e fáceis de gerenciar em comparação com a criação via API. Você pode consultar aqui para obter mais informações sobre como criar e gerenciar agentes.

Após a criação de um agente, é necessário criar uma thread através do endpoint POST /threads para iniciar a interação com ele. Uma thread funciona como um chat com o agente, permitindo que o usuário envie requisições sem se preocupar com o histórico de mensagens, pois este é gerido automaticamente pelo sistema.

Após a criação de uma thread, você pode enviar mensagens para o agente através do endpoint POST /threads/{id}/run e receber respostas em tempo real. As mensagens podem ser enviadas em qualquer ordem e o sistema gerencia o contexto da conversa automaticamente.

Endpoints

POST /threads

Este endpoint cria uma nova thread para interagir com um agente.

Corpo da Requisição

json
{
  "agent_id": "string", // ID do agente a ser utilizado
  "additional_instructions": "string" // (opcional) Instruções adicionais para o agente
}

Exemplo de Requisição

bash
curl -X 'POST' \
  'https://api.sippulse.ai/v1/threads' \
  -H 'Content-Type: application/json' \
  -H 'api-key: $SIPPULSE_API_KEY' \
  -d '{
  "agent_id": "agent_12345",
  "additional_instructions": "Please be concise in your responses."
}'
python
import requests
import json

url = "https://api.sippulse.ai/v1/threads"
headers = {
    "Content-Type": "application/json",
    "api-key": "SIPPULSE_API_KEY"
}
data = {
    "agent_id": "agent_12345",
    "additional_instructions": "Please be concise in your responses."
}

response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.json())
javascript
const url = "https://api.sippulse.ai/v1/threads";
const headers = {
  "Content-Type": "application/json",
  "api-key": "SIPPULSE_API_KEY",
};
const data = {
  agent_id: "agent_12345",
  additional_instructions: "Please be concise in your responses.",
};

fetch(url, {
  method: "POST",
  headers: headers,
  body: JSON.stringify(data),
})
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error("Error:", error));

Exemplo de Resposta

json
{
  "id": "thread_67890",
  "organization_id": "org_12345",
  "user_id": "user_12345",
  "agent_id": "agent_12345",
  "checkpoint": null,
  "history": [],
  "deleted_at": null,
  "created_at": "2023-01-01T00:00:00Z",
  "updated_at": "2023-01-01T00:00:00Z"
}

POST /threads/{id}/run

Este endpoint permite enviar mensagens para a thread e receber respostas do agente.

Parâmetros de Caminho

  • id (obrigatório): O ID da thread.

Corpo da Requisição

json
{
  "role": "string", // O papel da mensagem (ex: user, assistant)
  "content": "string", // O conteúdo da mensagem
  "name": "string", // (opcional) Nome do usuário
  "tool_call_id": "string" // Usado apenas quando a última mensagem do agente for uma tool_call
}

Exemplo de Requisição

bash
curl -X 'POST' \
  'https://api.sippulse.ai/v1/threads/thread_67890/run' \
  -H 'Content-Type: application/json' \
  -H 'api-key: $SIPPULSE_API_KEY' \
  -d '{
  "role": "user",
  "content": "Hello, how are you?"
}'
python
import requests
import json

url = "https://api.sippulse.ai/v1/threads/thread_67890/run"
headers = {
    "Content-Type": "application/json",
    "api-key": "SIPPULSE_API_KEY"
}
data = {
    "role": "user",
    "content": "Hello, how are you?"
}

response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.json())
javascript
const url = "https://api.sippulse.ai/v1/threads/thread_67890/run";
const headers = {
  "Content-Type": "application/json",
  "api-key": "SIPPULSE_API_KEY",
};
const data = {
  role: "user",
  content: "Hello, how are you?",
};

fetch(url, {
  method: "POST",
  headers: headers,
  body: JSON.stringify(data),
})
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error("Error:", error));

Exemplo de Resposta

json
{
  "choices": [
    {
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "I'm doing well, thank you! How can I assist you today?"
      }
    }
  ],
  "created": 1672531200,
  "model": "gpt-4-turbo",
  "usage": {
    "input_tokens": 10,
    "output_tokens": 12
  },
  "execution_time": 1.234
}

POST /threads/{id}/clear

Este endpoint reseta o histórico de mensagens da thread.

Parâmetros de Caminho

  • id (obrigatório): O ID da thread.

Exemplo de Requisição

bash
curl -X 'POST' \
  'https://api.sippulse.ai/v1/threads/thread_67890/clear' \
  -H 'api-key: $SIPPULSE_API_KEY'
python
import requests

url = "https://api.sippulse.ai/v1/threads/thread_67890/clear"
headers = {
    "api-key": "SIPPULSE_API_KEY"
}

response = requests.post(url, headers=headers)
print(response.status_code)
javascript
const url = "https://api.sippulse.ai/v1/threads/thread_67890/clear";
const headers = {
  "api-key": "SIPPULSE_API_KEY",
};

fetch(url, {
  method: "POST",
  headers: headers,
})
  .then((response) => console.log(response.status))
  .catch((error) => console.error("Error:", error));

Exemplo de Resposta

Status: 204 No Content

GET /threads/{id}

Este endpoint retorna as informações de uma thread específica.

Parâmetros de Caminho

  • id (obrigatório): O ID da thread.

Exemplo de Requisição

bash
curl -X 'GET' \
  'https://api.sippulse.ai/v1/threads/thread_67890' \
  -H 'Content-Type: application/json' \
  -H 'api-key: $SIPPULSE_API_KEY'
python
import requests

url = "https://api.sippulse.ai/v1/threads/thread_67890"
headers = {
    "Content-Type": "application/json",
    "api-key": "SIPPULSE_API_KEY"
}

response = requests.get(url, headers=headers)
print(response.json())
javascript
const url = "https://api.sippulse.ai/v1/threads/thread_67890";
const headers = {
  "Content-Type": "application/json",
  "api-key": "SIPPULSE_API_KEY",
};

fetch(url, {
  method: "GET",
  headers: headers,
})
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error("Error:", error));

Exemplo de Resposta

json
{
  "id": "thread_67890",
  "organization_id": "org_12345",
  "user_id": "user_12345",
  "agent_id": "agent_12345",
  "checkpoint": null,
  "history": [],
  "deleted_at": null,
  "created_at": "2023-01-01T00:00:00Z",
  "updated_at": "2023-01-01T00:00:00Z"
}

DELETE /threads/{id}

Este endpoint exclui uma thread específica.

Parâmetros de Caminho

  • id (obrigatório): O ID da thread.

Exemplo de Requisição

bash
curl -X 'DELETE' \
  'https://api.sippulse.ai/v1/threads/thread_67890' \
  -H 'api-key: $SIPPULSE_API_KEY'
python
import requests

url = "https://api.sippulse.ai/v1/threads/thread_67890"
headers = {
    "api-key": "SIPPULSE_API_KEY"
}

response = requests.delete(url, headers=headers)
print(response.status_code)
javascript
const url = "https://api.sippulse.ai/v1/threads/thread_67890";
const headers = {
  "api-key": "SIPPULSE_API_KEY",
};

fetch(url, {
  method: "DELETE",
  headers: headers,
})
  .then((response) => console.log(response.status))
  .catch((error) => console.error("Error:", error));

Exemplo de Resposta

Status: 204 No Content

Conclusão

Os endpoints de Threads na plataforma SipPulse AI permitem criar, interagir, limpar e gerenciar threads de agentes de forma eficiente. Utilize as informações e exemplos fornecidos para integrar essas funcionalidades em suas aplicações.