AI
Overview
Use language models from your services: chat, tools, agents, search by meaning, and tests.
A large language model (LLM) such as Claude takes text and produces text. Your application can use one to summarise a support ticket, answer a customer, extract data from a document, or decide which of your functions to call. The ai package gives you the pieces, in the same style as the rest of Kest, with no extra SDK:
| Concept | In plain words | Kest |
|---|---|---|
| Model | the LLM you call over HTTP | ChatModel: AnthropicModel, OpenAiCompatibleModel |
| Tool | a function of yours the model may ask you to run (look up an order) | @AiTool |
| Agent | a loop: ask the model, run the tools it requests, ask again, until it answers | Agent |
| Embedding | text turned into numbers so similar meanings are close | EmbeddingModel, KnowledgeBase |
| Test model | a fake model with scripted replies | FakeChatModel |
Loading diagram…
Kest.run<AppModule> {
import(AiModule.forRoot {
model = AnthropicModel(apiKey = System.getenv("ANTHROPIC_API_KEY")) // or OpenAiCompatibleModel(...)
embeddings = OpenAiEmbeddings(apiKey = System.getenv("OPENAI_API_KEY"))
})
}
@Injectable class Support(private val ai: AiService) {
fun answer(q: String): String = ai.chat(q, system = "You are a polite support agent for Acme Shop.")
}AiService gives you chat(prompt, system), agent(...) (a model that can use your methods as tools) and knowledgeBase() (search by meaning).
| Page | |
|---|---|
| Models | Anthropic, OpenAI-compatible (OpenAI, Ollama, vLLM, ...) and the ChatModel interface |
| Tools | @AiTool: let a model call your methods |
| Agents | the loop that runs tools until there is an answer, with limits |
| Embeddings | search by meaning, and answering from your own documents |
| Testing | a fake model, so tests need no network and cost nothing |
Things to know before you build
- A model answers from text; it does not know your data unless you give it. Two ways to give it data: tools (it asks for what it needs) and retrieval (you search your documents first and put the best passages in the prompt).
- Models make mistakes and can be fooled by text in the data they read (prompt injection). Treat model output as untrusted input: validate it, never run it as code or SQL, and limit what tools can do.
- Cost and speed: every call costs money and takes seconds. Cache, keep prompts short, use
maxTokens, and run long work in a queue rather than inside a web request. - Keep the API key in the environment, never in code.
Everything talks to the providers over plain HTTP. Streaming responses are not built: chat returns the whole reply.