Slop.Client (slop v0.1.0)

View Source

A client for consuming SLOP (Simple Language Open Protocol) APIs.

This module provides functions for interacting with SLOP servers, making it easy to send requests to the standard SLOP endpoints.

Usage

# Initialize a client
client = Slop.Client.new("https://my-slop-server.com")

# Get server information
{:ok, info} = Slop.Client.info(client)

# Send a chat message
{:ok, response} = Slop.Client.chat(client, [
  %{role: "user", content: "Hello, how are you?"}
])

# List available tools
{:ok, tools} = Slop.Client.list_tools(client)

# Execute a tool
{:ok, result} = Slop.Client.execute_tool(client, "calculator", %{
  expression: "2 + 2"
})

Summary

Functions

Sends a chat message to the SLOP server.

Executes a tool on the SLOP server.

Gets a specific resource from the SLOP server.

Gets information about the SLOP server.

Gets resources from the SLOP server.

Lists the tools available on the SLOP server.

Creates a new SLOP client.

Searches for resources on the SLOP server.

Types

t()

@type t() :: %Slop.Client{
  base_url: String.t(),
  headers: [{String.t(), String.t()}],
  http_client: module()
}

Functions

chat(client, messages)

@spec chat(t(), [map()]) :: {:ok, map()} | {:error, term()}

Sends a chat message to the SLOP server.

Examples

{:ok, response} = Slop.Client.chat(client, [
  %{role: "user", content: "Hello, how are you?"}
])

# response = %{
#   "response" => "I'm doing well, thank you for asking!",
#   "messages" => [
#     %{"role" => "user", "content" => "Hello, how are you?"},
#     %{"role" => "assistant", "content" => "I'm doing well, thank you for asking!"}
#   ]
# }

execute_tool(client, tool_id, params)

@spec execute_tool(t(), String.t(), map()) :: {:ok, map()} | {:error, term()}

Executes a tool on the SLOP server.

Examples

{:ok, result} = Slop.Client.execute_tool(client, "calculator", %{
  expression: "2 + 2"
})

# result = %{
#   "id" => "calculator",
#   "result" => "The result is 4"
# }

get_resource(client, resource_id)

@spec get_resource(t(), String.t()) :: {:ok, map()} | {:error, term()}

Gets a specific resource from the SLOP server.

Examples

{:ok, %{"resource" => resource}} = Slop.Client.get_resource(client, "article-1")

# resource = %{
#   "id" => "article-1",
#   "title" => "Article 1",
#   "content" => "...",
#   ...
# }

info(client)

@spec info(t()) :: {:ok, map()} | {:error, term()}

Gets information about the SLOP server.

Examples

{:ok, info} = Slop.Client.info(client)

# info = %{
#   "name" => "Example SLOP Server",
#   "version" => "1.0.0",
#   "endpoints" => ["chat", "info"],
#   ...
# }

list_resources(client)

@spec list_resources(t()) :: {:ok, map()} | {:error, term()}

Gets resources from the SLOP server.

Examples

{:ok, %{"resources" => resources}} = Slop.Client.list_resources(client)

# resources = [
#   %{
#     "id" => "article-1",
#     "title" => "Article 1",
#     ...
#   },
#   ...
# ]

list_tools(client)

@spec list_tools(t()) :: {:ok, map()} | {:error, term()}

Lists the tools available on the SLOP server.

Examples

{:ok, %{"tools" => tools}} = Slop.Client.list_tools(client)

# tools = [
#   %{
#     "id" => "calculator",
#     "name" => "Calculator",
#     "description" => "Perform mathematical calculations",
#     ...
#   },
#   ...
# ]

new(base_url, opts \\ [])

@spec new(
  String.t(),
  keyword()
) :: t()

Creates a new SLOP client.

Options

  • :headers - Additional headers to include in requests
  • :http_client - The HTTP client module to use (default: HTTPoison)

Examples

# Basic client
client = Slop.Client.new("https://my-slop-server.com")

# Client with authentication
client = Slop.Client.new("https://my-slop-server.com",
  headers: [{"Authorization", "Bearer my-token"}]
)

search_resources(client, query)

@spec search_resources(t(), String.t()) :: {:ok, map()} | {:error, term()}

Searches for resources on the SLOP server.

Examples

{:ok, %{"results" => results}} = Slop.Client.search_resources(client, "example")

# results = [
#   %{
#     "id" => "article-1",
#     "title" => "Example Article",
#     "score" => 0.9,
#     ...
#   },
#   ...
# ]