Class: Braintrust::API::Functions
- Inherits:
-
Object
- Object
- Braintrust::API::Functions
- Defined in:
- lib/braintrust/api/functions.rb
Overview
Functions API namespace Provides methods for creating, invoking, and managing remote functions (prompts)
Constant Summary collapse
- TYPE_LLM =
"llm"- TYPE_PROMPT =
"prompt"- TYPE_SCORER =
"scorer"- TYPE_TASK =
"task"- TYPE_TOOL =
"tool"
Instance Method Summary collapse
-
#create(project_name:, slug:, function_data:, prompt_data: nil, name: nil, description: nil, function_type: nil, function_schema: nil) ⇒ Hash
Create or register a function (idempotent) POST /v1/function This method is idempotent - if a function with the same slug already exists in the project, it will return the existing function unmodified.
-
#create_llm(project_name:, slug:, prompt_data:, name: nil, description: nil, function_schema: nil) ⇒ Hash
Create an LLM function LLM functions are prompt-based functions categorized as LLM type.
-
#create_scorer(project_name:, slug:, prompt_data:, name: nil, description: nil, function_schema: nil) ⇒ Hash
Create a scorer function Scorers evaluate task outputs and return scores (typically 0-1).
-
#create_task(project_name:, slug:, prompt_data:, name: nil, description: nil, function_schema: nil) ⇒ Hash
Create a task function Tasks are general-purpose prompt functions.
-
#create_tool(project_name:, slug:, prompt_data:, name: nil, description: nil, function_schema: nil) ⇒ Hash
Create a tool function Tools are functions that LLMs can call during execution.
-
#delete(id:) ⇒ Hash
Delete a function by ID DELETE /v1/function/id.
-
#get(id:, version: nil) ⇒ Hash
Get a function by ID (includes full prompt_data) GET /v1/function/id.
-
#initialize(api) ⇒ Functions
constructor
A new instance of Functions.
-
#invoke(id:, input:) ⇒ Object
Invoke a function by ID with input POST /v1/function/id/invoke.
-
#list(project_name: nil, project_id: nil, function_name: nil, slug: nil, limit: nil) ⇒ Hash
List functions with optional filters GET /v1/function?project_name=X&…
Constructor Details
#initialize(api) ⇒ Functions
Returns a new instance of Functions.
20 21 22 23 |
# File 'lib/braintrust/api/functions.rb', line 20 def initialize(api) @api = api @state = api.state end |
Instance Method Details
#create(project_name:, slug:, function_data:, prompt_data: nil, name: nil, description: nil, function_type: nil, function_schema: nil) ⇒ Hash
Create or register a function (idempotent) POST /v1/function This method is idempotent - if a function with the same slug already exists in the project, it will return the existing function unmodified. Unlike datasets, the response does not include a “found_existing” field.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/braintrust/api/functions.rb', line 60 def create(project_name:, slug:, function_data:, prompt_data: nil, name: nil, description: nil, function_type: nil, function_schema: nil) # Look up project ID projects_result = http_get("/v1/project", {"project_name" => project_name}) project = projects_result["objects"]&.first raise Error, "Project '#{project_name}' not found" unless project project_id = project["id"] payload = { project_id: project_id, slug: slug, name: name || slug, # Name is required, default to slug function_data: function_data } payload[:prompt_data] = prompt_data if prompt_data payload[:description] = description if description payload[:function_type] = function_type if function_type payload[:function_schema] = function_schema if function_schema http_post_json("/v1/function", payload) end |
#create_llm(project_name:, slug:, prompt_data:, name: nil, description: nil, function_schema: nil) ⇒ Hash
Create an LLM function LLM functions are prompt-based functions categorized as LLM type.
188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/braintrust/api/functions.rb', line 188 def create_llm(project_name:, slug:, prompt_data:, name: nil, description: nil, function_schema: nil) validate_prompt_data!(prompt_data) create( project_name: project_name, slug: slug, function_data: {type: TYPE_PROMPT}, prompt_data: prompt_data, name: name, description: description, function_type: TYPE_LLM, function_schema: function_schema ) end |
#create_scorer(project_name:, slug:, prompt_data:, name: nil, description: nil, function_schema: nil) ⇒ Hash
Create a scorer function Scorers evaluate task outputs and return scores (typically 0-1).
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/braintrust/api/functions.rb', line 142 def create_scorer(project_name:, slug:, prompt_data:, name: nil, description: nil, function_schema: nil) validate_prompt_data!(prompt_data) create( project_name: project_name, slug: slug, function_data: {type: TYPE_PROMPT}, prompt_data: prompt_data, name: name, description: description, function_type: TYPE_SCORER, function_schema: function_schema ) end |
#create_task(project_name:, slug:, prompt_data:, name: nil, description: nil, function_schema: nil) ⇒ Hash
Create a task function Tasks are general-purpose prompt functions.
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/braintrust/api/functions.rb', line 165 def create_task(project_name:, slug:, prompt_data:, name: nil, description: nil, function_schema: nil) validate_prompt_data!(prompt_data) create( project_name: project_name, slug: slug, function_data: {type: TYPE_PROMPT}, prompt_data: prompt_data, name: name, description: description, function_type: TYPE_TASK, function_schema: function_schema ) end |
#create_tool(project_name:, slug:, prompt_data:, name: nil, description: nil, function_schema: nil) ⇒ Hash
Create a tool function Tools are functions that LLMs can call during execution.
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/braintrust/api/functions.rb', line 119 def create_tool(project_name:, slug:, prompt_data:, name: nil, description: nil, function_schema: nil) validate_prompt_data!(prompt_data) create( project_name: project_name, slug: slug, function_data: {type: TYPE_PROMPT}, prompt_data: prompt_data, name: name, description: description, function_type: TYPE_TOOL, function_schema: function_schema ) end |
#delete(id:) ⇒ Hash
Delete a function by ID DELETE /v1/function/id
106 107 108 |
# File 'lib/braintrust/api/functions.rb', line 106 def delete(id:) http_delete("/v1/function/#{id}") end |
#get(id:, version: nil) ⇒ Hash
Get a function by ID (includes full prompt_data) GET /v1/function/id
96 97 98 99 100 |
# File 'lib/braintrust/api/functions.rb', line 96 def get(id:, version: nil) params = {} params["version"] = version if version http_get("/v1/function/#{id}", params) end |
#invoke(id:, input:) ⇒ Object
Invoke a function by ID with input POST /v1/function/id/invoke
86 87 88 89 |
# File 'lib/braintrust/api/functions.rb', line 86 def invoke(id:, input:) payload = {input: input} http_post_json("/v1/function/#{id}/invoke", payload) end |
#list(project_name: nil, project_id: nil, function_name: nil, slug: nil, limit: nil) ⇒ Hash
List functions with optional filters GET /v1/function?project_name=X&…
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/braintrust/api/functions.rb', line 33 def list(project_name: nil, project_id: nil, function_name: nil, slug: nil, limit: nil) params = {} params["project_name"] = project_name if project_name params["project_id"] = project_id if project_id params["function_name"] = function_name if function_name params["slug"] = slug if slug params["limit"] = limit if limit http_get("/v1/function", params) end |