Klu Actions
Actions are the core of Klu and working with Actions will be the bulk of your development work. Klu Actions encapsulate everything needed to run the same prompt generation at scale. This section provides examples of common Action interactions, as well as detailed API information.
Klu Actions power content generation using dynamic variables, business operations workflows, content analysis and summarization, internal context-based question answering, customer feedback analysis, sentiment or intent analysis, and topic and category classification, and more. Klu unlocks your imagination.
Action model
Actions need an input to run the generation. Depending on your Action, your input is either a string, or key-value pairs with variable name as the key and variable input as the value. Actions return a response with the generated text, however you can also stream the response or run the Action asynchronously. Actions also return a feedback URL that you can use to provide Feedback on the generation.
Streamed Actions return a stream URL that you can use to stream the response token-by-token.
Asynchronous Actions return a Generation GUID that you can use to retrieve the generated output.
Action Examples
Here are common examples for how to use Klu Actions in your applications.
Run Action
Get your Action GUID from Klu Studio. You can also use the API to list your Actions.
Run Action
curl --location --request POST 'https://api.klu.ai/v1/actions/' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
"action": "6942f034-6872-4eed-9bb3-7e3bf18e5743",
"cache": true,
"input": "Write a sentence about Machine Learning."
}'
Save User Feedback on Action Generation
Klu Actions return a Feedback URL feedback_url
containing the data point GUID that you can use to provide Feedback on the generation. The SDK only needs the GUID to save the feedback.
Save Feedback
curl --location --request PUT 'https://api.klu.ai/v1/data/69420c8bz-22z2-446d-af2e-e525b39dafda' \
--header 'Authorization: Bearer YOUR_TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
"correction": "Machine Learning, a category of artificial intelligence, is a process where computers analyze data to learn patterns or behaviors, equipping them with the capability to make decisions independently, without someone having to write specific instructions for each task.",
"rating": 2,
"issue": "Vague",
}'
Action with Variables
Klu Actions support {{dynamic variables}}
that you can use in prompt generations.
Here is an example prompt template with variables:
Prompt Template
======
System Message: You are a helpful writing assistant.
======
Review this content and make the following changes:
{{task}}
If no changes, do not output anything and explain why. If there are changes,
explain in a table what you changed and why.
======
{{content}}
Run Action with Variables
curl --location --request POST 'https://api.klu.ai/v1/actions/' \
--header 'Authorization: Bearer YOUR_TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
"action": "6942f034-6872-4eed-9bb3-7e3bf18e5743",
"input": {
"content": "Machine Learning.",
"task": "Correct any grammar or spelling mistakes."
}
}'
Action with Context Filter
Utilize the filter
parameter to narrow down to specific Context documents by matching input with the document's metadata. Klu enables you to filter by one or many document attributes. This is beneficial when you want to reuse an Action across different documents in the same Context library, such as documents belonging to different tenants, users, or products. Klu Studio automatically creates file-based metadata for each document or URL. You can also add custom metadata to each document via the API.
Run Action with Filtered Context
curl --location --request POST 'https://api.klu.ai/v1/actions/' \
--header 'Authorization: Bearer YOUR_TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
"action": "6942f034-6872-4eed-9bb3-7e3bf18e5743",
"metadata_filter": {
"user-id": "00379-28829999a",
"tenant-id": "aa882-bb873"
},
"input": {
"topic": "Machine Learning.",
"task": "Write a help center article"
}
}'
Run Action with Session Memory
Klu Actions support session
memory that you can use to store and retrieve data across multiple prompt generations. This is useful for conversational agents, where you want to retain the context of the conversation. This is a 2-step process: first create a new session, then use the session GUID in subsequent Action generations.
You can also specify an optional external user ID extUserId
when creating a session. This external user ID can then be used to connect the activity across multiple generations and sessions in other analytics or observability systems.
Create Session
curl --location --request POST 'https://api.klu.ai/v1/sessions/' \
--header 'Authorization: Bearer YOUR_TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
"action": "6942f034-6872-4eed-9bb3-7e3bf18e5743",
"extUserId": "[email protected]",
"name": "A conversation about X.com with Elon Musk",
}'
Run Action in Session
curl --location --request POST 'https://api.klu.ai/v1/actions/' \
--header 'Authorization: Bearer YOUR_TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
"action": "6942f034-6872-4eed-9bb3-7e3bf18e5743",
"input": {
"topic": "Machine Learning.",
"task": "Help me build my first ML feature"
},
"session": "3b7c1d28-8f21-4b6a-a9d2-1a7b9c2d6a57",
}'
Run Experiment
Klu Experiments allow for A/B testing of two Actions via one endpoint. This is useful for comparing the performance of two Actions on the same input, but with different configurations (Prompt, Model Provider, Model, Model Config, Context, etc). If the Actions have two different sets of variables you must provide all inputs in the request.
Run A/B Generation
curl --location --request POST 'https://api.klu.ai/v1/experiments/dbcc25d4-722c-433d-a1ea-548250badf8d/prompt' \
--header 'Authorization: Bearer YOUR_TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
"input": {
"topic": "Machine Learning.",
"task": "Help me build my first ML feature"
}
}'
Run Action
Detailed documentation for the Actions endpoint. The Action GUID and input is required to run an Action, all other attributes are optional.
Required attributes
- Name
action
- Type
- string
- Description
Action GUID to run.
- Name
input
- Type
- string / object
- Description
Input string (no variables) or key-value variable object to pass to Action.
Optional attributes
- Name
streaming
- Type
- boolean
- Description
Enable streaming response.
- Name
async_mode
- Type
- boolean
- Description
Enable async response.
- Name
cache
- Type
- boolean
- Description
Use cached response if available.
- Name
session
- Type
- string
- Description
Assign a session to the Action generation.
- Name
metadata_filter
- Type
- object
- Description
Filter by Metadata to retrieve specific Context used by Action.
- Name
experiment
- Type
- string
- Description
Pass the experiment GUID to run the action in a specific experiment. Only recommended if you are manually controlling experiment traffic.
Request
curl --location --request POST 'https://api.klu.ai/v1/actions/' \
--header 'Authorization: Bearer YOUR_TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
"action": "6942f034-6872-4eed-9bb3-7e3bf18e5743",
"input": "Write a sentence about Machine Learning."
}'
Response
{
"msg":"Machine learning is a branch of artificial intelligence that allows computer systems to learn and improve from experience without being explicitly programmed.",
"feedback_url":"https://api.klu.ai/v1/data/123e4567-e89b-12d3-a456-426655440000",
"streaming":false
}