@k-apps-io/llm-dsl-anthropic
v0.0.2
Published
A @k-apps-io/llm-dsl Implementation for Antropic (Claude)
Readme
@k-apps-io/llm-dsl-anthropic
This is an implementation of @k-apps-io/llm-dsl for Anthropic
Installation
npm install @k-apps-io/llm-dsl-anthropicUsage
Hello World
import { CODE_BLOCK_RULE, json, Locals, stdout } from '@k-apps-io/llm-dsl';
import Anthropic from '@k-apps-io/llm-dsl-anthropic';
type ChatLocals = Locals & {
languages: string[];
};
const session = new Anthropic<ChatLocals, undefined>({
options: {
apiKey: process.env.ANTHROPIC_API_KEY!,
model: 'claude-sonnet-4-20250514',
},
settings: {
windowSize: 150000, // max is 200k
},
});
chat
.rule(CODE_BLOCK_RULE)
.prompt({
message: 'Hello!',
})
.response(({ response }) => {
return new Promise<void>((resolve, reject) => {
// here you can access the response of the LLM and do what you like
// to move onto the next stage, resolve the promise
resolve();
});
})
// send a follow up prompt to the LLM
.prompt({
message:
'send me 3 progamming languages as a JSON array e.g ```json ["lang1", "lang2", "langN"]```',
response_format: { type: 'json_object' },
})
.expect(
json(),
({ response, chat: $chat }) =>
new Promise<void>((resolve, reject) => {
// here we can access the json response and assign it to locals for later use
$chat.locals.languages = $chat.locals.$blocks as string[];
resolve();
})
)
.promptForEach(({ locals }) => {
// now we can use the languages provided by the response
return locals.languages.map((language) => {
const message = `write me a hello world progamming in ${language}`;
return { message: message };
});
})
// stream the responses to a handler, stdout() is provided OOTB as are others
.stream(stdout())
.then(() => {
console.log('done');
})
.catch((error) => {
console.error(error);
});Support
If you find this package useful and would like to support its maintenance and development, there are several ways you can contribute:
- Bug Reports and Feature Requests: If you encounter any bugs or have ideas for new features, please open an issue on GitHub. Your feedback helps improve the package for everyone.
- Spread the Word: Help others discover this package by sharing it on social media, forums, or your own networks. Every mention counts and contributes to the growth of the community around the package.
Your support is greatly appreciated! Thank you for using and contributing to this package.
