mirror of
https://github.com/humanlayer/12-factor-agents.git
synced 2025-08-20 18:59:53 +03:00
wip
This commit is contained in:
154
workshops/2025-05-17/baml_client/async_client.ts
Normal file
154
workshops/2025-05-17/baml_client/async_client.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
import type { BamlRuntime, FunctionResult, BamlCtxManager, ClientRegistry, Image, Audio, Collector } from "@boundaryml/baml"
|
||||
import { toBamlError, BamlStream, type HTTPRequest } from "@boundaryml/baml"
|
||||
import type { Checked, Check, RecursivePartialNull as MovedRecursivePartialNull } from "./types"
|
||||
import type { partial_types } from "./partial_types"
|
||||
import type * as types from "./types"
|
||||
import type {DoneForNow} from "./types"
|
||||
import type TypeBuilder from "./type_builder"
|
||||
import { AsyncHttpRequest, AsyncHttpStreamRequest } from "./async_request"
|
||||
import { LlmResponseParser, LlmStreamParser } from "./parser"
|
||||
import { DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX, DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME } from "./globals"
|
||||
|
||||
/**
|
||||
* @deprecated Use RecursivePartialNull from 'baml_client/types' instead.
|
||||
*/
|
||||
export type RecursivePartialNull<T> = MovedRecursivePartialNull<T>
|
||||
|
||||
type BamlCallOptions = {
|
||||
tb?: TypeBuilder
|
||||
clientRegistry?: ClientRegistry
|
||||
collector?: Collector | Collector[]
|
||||
}
|
||||
|
||||
export class BamlAsyncClient {
|
||||
private runtime: BamlRuntime
|
||||
private ctxManager: BamlCtxManager
|
||||
private streamClient: BamlStreamClient
|
||||
private httpRequest: AsyncHttpRequest
|
||||
private httpStreamRequest: AsyncHttpStreamRequest
|
||||
private llmResponseParser: LlmResponseParser
|
||||
private llmStreamParser: LlmStreamParser
|
||||
private bamlOptions: BamlCallOptions
|
||||
|
||||
constructor(runtime: BamlRuntime, ctxManager: BamlCtxManager, bamlOptions?: BamlCallOptions) {
|
||||
this.runtime = runtime
|
||||
this.ctxManager = ctxManager
|
||||
this.streamClient = new BamlStreamClient(runtime, ctxManager, bamlOptions)
|
||||
this.httpRequest = new AsyncHttpRequest(runtime, ctxManager)
|
||||
this.httpStreamRequest = new AsyncHttpStreamRequest(runtime, ctxManager)
|
||||
this.llmResponseParser = new LlmResponseParser(runtime, ctxManager)
|
||||
this.llmStreamParser = new LlmStreamParser(runtime, ctxManager)
|
||||
this.bamlOptions = bamlOptions || {}
|
||||
}
|
||||
|
||||
withOptions(bamlOptions: BamlCallOptions) {
|
||||
return new BamlAsyncClient(this.runtime, this.ctxManager, bamlOptions)
|
||||
}
|
||||
|
||||
get stream() {
|
||||
return this.streamClient
|
||||
}
|
||||
|
||||
get request() {
|
||||
return this.httpRequest
|
||||
}
|
||||
|
||||
get streamRequest() {
|
||||
return this.httpStreamRequest
|
||||
}
|
||||
|
||||
get parse() {
|
||||
return this.llmResponseParser
|
||||
}
|
||||
|
||||
get parseStream() {
|
||||
return this.llmStreamParser
|
||||
}
|
||||
|
||||
|
||||
async DetermineNextStep(
|
||||
thread: string,
|
||||
__baml_options__?: BamlCallOptions
|
||||
): Promise<DoneForNow> {
|
||||
try {
|
||||
const options = { ...this.bamlOptions, ...(__baml_options__ || {}) }
|
||||
const collector = options.collector ? (Array.isArray(options.collector) ? options.collector : [options.collector]) : [];
|
||||
const raw = await this.runtime.callFunction(
|
||||
"DetermineNextStep",
|
||||
{
|
||||
"thread": thread
|
||||
},
|
||||
this.ctxManager.cloneContext(),
|
||||
options.tb?.__tb(),
|
||||
options.clientRegistry,
|
||||
collector,
|
||||
)
|
||||
return raw.parsed(false) as DoneForNow
|
||||
} catch (error) {
|
||||
throw toBamlError(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class BamlStreamClient {
|
||||
private runtime: BamlRuntime
|
||||
private ctxManager: BamlCtxManager
|
||||
private bamlOptions: BamlCallOptions
|
||||
|
||||
constructor(runtime: BamlRuntime, ctxManager: BamlCtxManager, bamlOptions?: BamlCallOptions) {
|
||||
this.runtime = runtime
|
||||
this.ctxManager = ctxManager
|
||||
this.bamlOptions = bamlOptions || {}
|
||||
}
|
||||
|
||||
|
||||
DetermineNextStep(
|
||||
thread: string,
|
||||
__baml_options__?: { tb?: TypeBuilder, clientRegistry?: ClientRegistry, collector?: Collector | Collector[] }
|
||||
): BamlStream<partial_types.DoneForNow, DoneForNow> {
|
||||
try {
|
||||
const options = { ...this.bamlOptions, ...(__baml_options__ || {}) }
|
||||
const collector = options.collector ? (Array.isArray(options.collector) ? options.collector : [options.collector]) : [];
|
||||
const raw = this.runtime.streamFunction(
|
||||
"DetermineNextStep",
|
||||
{
|
||||
"thread": thread
|
||||
},
|
||||
undefined,
|
||||
this.ctxManager.cloneContext(),
|
||||
options.tb?.__tb(),
|
||||
options.clientRegistry,
|
||||
collector,
|
||||
)
|
||||
return new BamlStream<partial_types.DoneForNow, DoneForNow>(
|
||||
raw,
|
||||
(a): partial_types.DoneForNow => a,
|
||||
(a): DoneForNow => a,
|
||||
this.ctxManager.cloneContext(),
|
||||
)
|
||||
} catch (error) {
|
||||
throw toBamlError(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const b = new BamlAsyncClient(DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME, DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX)
|
||||
80
workshops/2025-05-17/baml_client/async_request.ts
Normal file
80
workshops/2025-05-17/baml_client/async_request.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
import type { BamlRuntime, BamlCtxManager, ClientRegistry, Image, Audio } from "@boundaryml/baml"
|
||||
import { toBamlError, HTTPRequest } from "@boundaryml/baml"
|
||||
import type { Checked, Check } from "./types"
|
||||
import type * as types from "./types"
|
||||
import type {DoneForNow} from "./types"
|
||||
import type TypeBuilder from "./type_builder"
|
||||
|
||||
type BamlCallOptions = {
|
||||
tb?: TypeBuilder
|
||||
clientRegistry?: ClientRegistry
|
||||
}
|
||||
|
||||
export class AsyncHttpRequest {
|
||||
constructor(private runtime: BamlRuntime, private ctxManager: BamlCtxManager) {}
|
||||
|
||||
|
||||
async DetermineNextStep(
|
||||
thread: string,
|
||||
__baml_options__?: BamlCallOptions
|
||||
): Promise<HTTPRequest> {
|
||||
try {
|
||||
return await this.runtime.buildRequest(
|
||||
"DetermineNextStep",
|
||||
{
|
||||
"thread": thread
|
||||
},
|
||||
this.ctxManager.cloneContext(),
|
||||
__baml_options__?.tb?.__tb(),
|
||||
__baml_options__?.clientRegistry,
|
||||
false,
|
||||
)
|
||||
} catch (error) {
|
||||
throw toBamlError(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class AsyncHttpStreamRequest {
|
||||
constructor(private runtime: BamlRuntime, private ctxManager: BamlCtxManager) {}
|
||||
|
||||
|
||||
async DetermineNextStep(
|
||||
thread: string,
|
||||
__baml_options__?: BamlCallOptions
|
||||
): Promise<HTTPRequest> {
|
||||
try {
|
||||
return await this.runtime.buildRequest(
|
||||
"DetermineNextStep",
|
||||
{
|
||||
"thread": thread
|
||||
},
|
||||
this.ctxManager.cloneContext(),
|
||||
__baml_options__?.tb?.__tb(),
|
||||
__baml_options__?.clientRegistry,
|
||||
true,
|
||||
)
|
||||
} catch (error) {
|
||||
throw toBamlError(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
19
workshops/2025-05-17/baml_client/config.ts
Normal file
19
workshops/2025-05-17/baml_client/config.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
export { setLogLevel, getLogLevel, setLogJson } from "@boundaryml/baml/logging";
|
||||
export { resetBamlEnvVars } from "./globals";
|
||||
67
workshops/2025-05-17/baml_client/globals.ts
Normal file
67
workshops/2025-05-17/baml_client/globals.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
import { BamlRuntime, BamlCtxManager } from '@boundaryml/baml'
|
||||
import { getBamlFiles } from './inlinedbaml'
|
||||
|
||||
|
||||
export const DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME = BamlRuntime.fromFiles(
|
||||
'baml_src',
|
||||
getBamlFiles(),
|
||||
process.env
|
||||
)
|
||||
export const DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX = new BamlCtxManager(DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME)
|
||||
|
||||
|
||||
export function resetBamlEnvVars(envVars: Record<string, string | undefined>) {
|
||||
if (DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX.allowResets()) {
|
||||
const envVarsToReset = Object.fromEntries(Object.entries(envVars).filter((kv): kv is [string, string] => kv[1] !== undefined));
|
||||
DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME.reset('baml_src', getBamlFiles(), envVarsToReset)
|
||||
DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX.reset()
|
||||
} else {
|
||||
throw new Error('BamlError: Cannot reset BAML environment variables while there are active BAML contexts.')
|
||||
}
|
||||
}
|
||||
|
||||
const patchedLoad = (originalFn: any) => (...args: any[]) => {
|
||||
const result = originalFn(...args);
|
||||
try {
|
||||
// Dont fail if env vars fail to reset
|
||||
resetBamlEnvVars(process.env);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
try {
|
||||
const dotenv = require('dotenv');
|
||||
// Monkeypatch load function to call resetBamlEnvVars after execution
|
||||
|
||||
|
||||
// Apply the patch
|
||||
dotenv.config = patchedLoad(dotenv.config);
|
||||
dotenv.configDotenv = patchedLoad(dotenv.configDotenv);
|
||||
dotenv.populate = patchedLoad(dotenv.populate);
|
||||
} catch (error) {
|
||||
// dotenv is not installed, so we do nothing
|
||||
}
|
||||
|
||||
// also patch process.loadEnvFile
|
||||
if (process.loadEnvFile) {
|
||||
process.loadEnvFile = patchedLoad(process.loadEnvFile);
|
||||
}
|
||||
47
workshops/2025-05-17/baml_client/index.ts
Normal file
47
workshops/2025-05-17/baml_client/index.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
/**
|
||||
* If this import fails, you may need to upgrade @boundaryml/baml.
|
||||
*
|
||||
* Please upgrade @boundaryml/baml to 0.88.0.
|
||||
*
|
||||
* $ npm install @boundaryml/baml@0.88.0
|
||||
* $ yarn add @boundaryml/baml@0.88.0
|
||||
* $ pnpm add @boundaryml/baml@0.88.0
|
||||
*
|
||||
* If nothing else works, please ask for help:
|
||||
*
|
||||
* https://github.com/boundaryml/baml/issues
|
||||
* https://boundaryml.com/discord
|
||||
*
|
||||
**/
|
||||
import { ThrowIfVersionMismatch } from "@boundaryml/baml";
|
||||
|
||||
export const version = "0.88.0";
|
||||
|
||||
ThrowIfVersionMismatch(version);
|
||||
|
||||
|
||||
|
||||
export { b } from "./async_client"
|
||||
|
||||
export * from "./types"
|
||||
export type { partial_types } from "./partial_types"
|
||||
export * from "./tracing"
|
||||
export { resetBamlEnvVars } from "./globals"
|
||||
export { BamlClientHttpError, BamlValidationError, BamlClientFinishReasonError } from "@boundaryml/baml"
|
||||
26
workshops/2025-05-17/baml_client/inlinedbaml.ts
Normal file
26
workshops/2025-05-17/baml_client/inlinedbaml.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
const fileMap = {
|
||||
|
||||
"agent.baml": "class DoneForNow {\n intent \"done_for_now\"\n message string \n}\n\nclient<llm> Qwen3 {\n provider \"openai-generic\"\n options {\n base_url \"https://model-4w7jrl6w.api.baseten.co/environments/production/sync/v1\"\n api_key \"QQksI6f4.mFAZX9D0DevBQMUWs2OV8gcfzMUinN9N\"\n }\n}\n\nfunction DetermineNextStep(\n thread: string \n) -> DoneForNow {\n client Qwen3\n\n prompt #\"\n {{ _.role(\"system\") }}\n /nothink\n\n You are a helpful assistant that can help with tasks.\n\n {{ _.role(\"user\") }}\n\n You are working on the following thread:\n\n {{ thread }}\n\n What should the next step be?\n\n {{ ctx.output_format }}\n \"#\n}\n\ntest HelloWorld {\n functions [DetermineNextStep]\n args {\n thread #\"\n {\n \"type\": \"user_input\",\n \"data\": \"hello!\"\n }\n \"#\n }\n}",
|
||||
"clients.baml": "// Learn more about clients at https://docs.boundaryml.com/docs/snippets/clients/overview\n\nclient<llm> CustomGPT4o {\n provider openai\n options {\n model \"gpt-4o\"\n api_key env.OPENAI_API_KEY\n }\n}\n\nclient<llm> CustomGPT4oMini {\n provider openai\n retry_policy Exponential\n options {\n model \"gpt-4o-mini\"\n api_key env.OPENAI_API_KEY\n }\n}\n\nclient<llm> CustomSonnet {\n provider anthropic\n options {\n model \"claude-3-5-sonnet-20241022\"\n api_key env.ANTHROPIC_API_KEY\n }\n}\n\n\nclient<llm> CustomHaiku {\n provider anthropic\n retry_policy Constant\n options {\n model \"claude-3-haiku-20240307\"\n api_key env.ANTHROPIC_API_KEY\n }\n}\n\n// https://docs.boundaryml.com/docs/snippets/clients/round-robin\nclient<llm> CustomFast {\n provider round-robin\n options {\n // This will alternate between the two clients\n strategy [CustomGPT4oMini, CustomHaiku]\n }\n}\n\n// https://docs.boundaryml.com/docs/snippets/clients/fallback\nclient<llm> OpenaiFallback {\n provider fallback\n options {\n // This will try the clients in order until one succeeds\n strategy [CustomGPT4oMini, CustomGPT4oMini]\n }\n}\n\n// https://docs.boundaryml.com/docs/snippets/clients/retry\nretry_policy Constant {\n max_retries 3\n // Strategy is optional\n strategy {\n type constant_delay\n delay_ms 200\n }\n}\n\nretry_policy Exponential {\n max_retries 2\n // Strategy is optional\n strategy {\n type exponential_backoff\n delay_ms 300\n multiplier 1.5\n max_delay_ms 10000\n }\n}",
|
||||
"generators.baml": "// This helps use auto generate libraries you can use in the language of\n// your choice. You can have multiple generators if you use multiple languages.\n// Just ensure that the output_dir is different for each generator.\ngenerator target {\n // Valid values: \"python/pydantic\", \"typescript\", \"ruby/sorbet\", \"rest/openapi\"\n output_type \"typescript\"\n\n // Where the generated code will be saved (relative to baml_src/)\n output_dir \"../\"\n\n // The version of the BAML package you have installed (e.g. same version as your baml-py or @boundaryml/baml).\n // The BAML VSCode extension version should also match this version.\n version \"0.88.0\"\n\n // Valid values: \"sync\", \"async\"\n // This controls what `b.FunctionName()` will be (sync or async).\n default_client_mode async\n}\n",
|
||||
}
|
||||
export const getBamlFiles = () => {
|
||||
return fileMap;
|
||||
}
|
||||
72
workshops/2025-05-17/baml_client/parser.ts
Normal file
72
workshops/2025-05-17/baml_client/parser.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
import type { BamlRuntime, BamlCtxManager, ClientRegistry, Image, Audio, Collector } from "@boundaryml/baml"
|
||||
import { toBamlError } from "@boundaryml/baml"
|
||||
import type { Checked, Check } from "./types"
|
||||
import type { partial_types } from "./partial_types"
|
||||
import type * as types from "./types"
|
||||
import type {DoneForNow} from "./types"
|
||||
import type TypeBuilder from "./type_builder"
|
||||
|
||||
export class LlmResponseParser {
|
||||
constructor(private runtime: BamlRuntime, private ctxManager: BamlCtxManager) {}
|
||||
|
||||
|
||||
DetermineNextStep(
|
||||
llmResponse: string,
|
||||
__baml_options__?: { tb?: TypeBuilder, clientRegistry?: ClientRegistry }
|
||||
): DoneForNow {
|
||||
try {
|
||||
return this.runtime.parseLlmResponse(
|
||||
"DetermineNextStep",
|
||||
llmResponse,
|
||||
false,
|
||||
this.ctxManager.cloneContext(),
|
||||
__baml_options__?.tb?.__tb(),
|
||||
__baml_options__?.clientRegistry,
|
||||
) as DoneForNow
|
||||
} catch (error) {
|
||||
throw toBamlError(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class LlmStreamParser {
|
||||
constructor(private runtime: BamlRuntime, private ctxManager: BamlCtxManager) {}
|
||||
|
||||
|
||||
DetermineNextStep(
|
||||
llmResponse: string,
|
||||
__baml_options__?: { tb?: TypeBuilder, clientRegistry?: ClientRegistry }
|
||||
): partial_types.DoneForNow {
|
||||
try {
|
||||
return this.runtime.parseLlmResponse(
|
||||
"DetermineNextStep",
|
||||
llmResponse,
|
||||
true,
|
||||
this.ctxManager.cloneContext(),
|
||||
__baml_options__?.tb?.__tb(),
|
||||
__baml_options__?.clientRegistry,
|
||||
) as partial_types.DoneForNow
|
||||
} catch (error) {
|
||||
throw toBamlError(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
42
workshops/2025-05-17/baml_client/partial_types.ts
Normal file
42
workshops/2025-05-17/baml_client/partial_types.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
import type { Image, Audio } from "@boundaryml/baml"
|
||||
import type { Checked, Check } from "./types"
|
||||
import type { DoneForNow } from "./types"
|
||||
import type * as types from "./types"
|
||||
|
||||
/******************************************************************************
|
||||
*
|
||||
* These types are used for streaming, for when an instance of a type
|
||||
* is still being built up and any of its fields is not yet fully available.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
export interface StreamState<T> {
|
||||
value: T
|
||||
state: "Pending" | "Incomplete" | "Complete"
|
||||
}
|
||||
|
||||
export namespace partial_types {
|
||||
|
||||
export interface DoneForNow {
|
||||
intent: "done_for_now"
|
||||
message?: (string | null)
|
||||
}
|
||||
|
||||
}
|
||||
113
workshops/2025-05-17/baml_client/sync_client.ts
Normal file
113
workshops/2025-05-17/baml_client/sync_client.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
import type { BamlRuntime, FunctionResult, BamlCtxManager, Image, Audio, ClientRegistry, Collector } from "@boundaryml/baml"
|
||||
import { toBamlError, type HTTPRequest } from "@boundaryml/baml"
|
||||
import type { Checked, Check, RecursivePartialNull as MovedRecursivePartialNull } from "./types"
|
||||
import type * as types from "./types"
|
||||
import type {DoneForNow} from "./types"
|
||||
import type TypeBuilder from "./type_builder"
|
||||
import { HttpRequest, HttpStreamRequest } from "./sync_request"
|
||||
import { LlmResponseParser, LlmStreamParser } from "./parser"
|
||||
import { DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX, DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME } from "./globals"
|
||||
|
||||
/**
|
||||
* @deprecated Use RecursivePartialNull from 'baml_client/types' instead.
|
||||
* Example:
|
||||
* ```ts
|
||||
* import { RecursivePartialNull } from './baml_client/types'
|
||||
* ```
|
||||
*/
|
||||
export type RecursivePartialNull<T> = MovedRecursivePartialNull<T>;
|
||||
|
||||
type BamlCallOptions = {
|
||||
tb?: TypeBuilder
|
||||
clientRegistry?: ClientRegistry
|
||||
collector?: Collector | Collector[]
|
||||
}
|
||||
|
||||
export class BamlSyncClient {
|
||||
private httpRequest: HttpRequest
|
||||
private httpStreamRequest: HttpStreamRequest
|
||||
private llmResponseParser: LlmResponseParser
|
||||
private llmStreamParser: LlmStreamParser
|
||||
private bamlOptions: BamlCallOptions
|
||||
|
||||
constructor(private runtime: BamlRuntime, private ctxManager: BamlCtxManager, private bamlOptions?: BamlCallOptions) {
|
||||
this.httpRequest = new HttpRequest(runtime, ctxManager)
|
||||
this.httpStreamRequest = new HttpStreamRequest(runtime, ctxManager)
|
||||
this.llmResponseParser = new LlmResponseParser(runtime, ctxManager)
|
||||
this.llmStreamParser = new LlmStreamParser(runtime, ctxManager)
|
||||
this.bamlOptions = bamlOptions || {}
|
||||
}
|
||||
|
||||
withOptions(bamlOptions: BamlCallOptions) {
|
||||
return new BamlSyncClient(this.runtime, this.ctxManager, bamlOptions)
|
||||
}
|
||||
|
||||
/*
|
||||
* @deprecated NOT IMPLEMENTED as streaming must by async. We
|
||||
* are not providing an async version as we want to reserve the
|
||||
* right to provide a sync version in the future.
|
||||
*/
|
||||
get stream() {
|
||||
throw new Error("stream is not available in BamlSyncClient. Use `import { b } from 'baml_client/async_client")
|
||||
}
|
||||
|
||||
get request() {
|
||||
return this.httpRequest
|
||||
}
|
||||
|
||||
get streamRequest() {
|
||||
return this.httpStreamRequest
|
||||
}
|
||||
|
||||
get parse() {
|
||||
return this.llmResponseParser
|
||||
}
|
||||
|
||||
get parseStream() {
|
||||
return this.llmStreamParser
|
||||
}
|
||||
|
||||
|
||||
DetermineNextStep(
|
||||
thread: string,
|
||||
__baml_options__?: BamlCallOptions
|
||||
): DoneForNow {
|
||||
try {
|
||||
const options = { ...this.bamlOptions, ...(__baml_options__ || {}) }
|
||||
const collector = options.collector ? (Array.isArray(options.collector) ? options.collector : [options.collector]) : [];
|
||||
const raw = this.runtime.callFunctionSync(
|
||||
"DetermineNextStep",
|
||||
{
|
||||
"thread": thread
|
||||
},
|
||||
this.ctxManager.cloneContext(),
|
||||
options.tb?.__tb(),
|
||||
options.clientRegistry,
|
||||
collector,
|
||||
)
|
||||
return raw.parsed(false) as DoneForNow
|
||||
} catch (error: any) {
|
||||
throw toBamlError(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const b = new BamlSyncClient(DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME, DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX)
|
||||
80
workshops/2025-05-17/baml_client/sync_request.ts
Normal file
80
workshops/2025-05-17/baml_client/sync_request.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
import type { BamlRuntime, BamlCtxManager, ClientRegistry, Image, Audio } from "@boundaryml/baml"
|
||||
import { toBamlError, HTTPRequest } from "@boundaryml/baml"
|
||||
import type { Checked, Check } from "./types"
|
||||
import type * as types from "./types"
|
||||
import type {DoneForNow} from "./types"
|
||||
import type TypeBuilder from "./type_builder"
|
||||
|
||||
type BamlCallOptions = {
|
||||
tb?: TypeBuilder
|
||||
clientRegistry?: ClientRegistry
|
||||
}
|
||||
|
||||
export class HttpRequest {
|
||||
constructor(private runtime: BamlRuntime, private ctxManager: BamlCtxManager) {}
|
||||
|
||||
|
||||
DetermineNextStep(
|
||||
thread: string,
|
||||
__baml_options__?: BamlCallOptions
|
||||
): HTTPRequest {
|
||||
try {
|
||||
return this.runtime.buildRequestSync(
|
||||
"DetermineNextStep",
|
||||
{
|
||||
"thread": thread
|
||||
},
|
||||
this.ctxManager.cloneContext(),
|
||||
__baml_options__?.tb?.__tb(),
|
||||
__baml_options__?.clientRegistry,
|
||||
false,
|
||||
)
|
||||
} catch (error) {
|
||||
throw toBamlError(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class HttpStreamRequest {
|
||||
constructor(private runtime: BamlRuntime, private ctxManager: BamlCtxManager) {}
|
||||
|
||||
|
||||
DetermineNextStep(
|
||||
thread: string,
|
||||
__baml_options__?: BamlCallOptions
|
||||
): HTTPRequest {
|
||||
try {
|
||||
return this.runtime.buildRequestSync(
|
||||
"DetermineNextStep",
|
||||
{
|
||||
"thread": thread
|
||||
},
|
||||
this.ctxManager.cloneContext(),
|
||||
__baml_options__?.tb?.__tb(),
|
||||
__baml_options__?.clientRegistry,
|
||||
true,
|
||||
)
|
||||
} catch (error) {
|
||||
throw toBamlError(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
33
workshops/2025-05-17/baml_client/tracing.ts
Normal file
33
workshops/2025-05-17/baml_client/tracing.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
import type { BamlLogEvent } from '@boundaryml/baml';
|
||||
import { DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX } from './globals';
|
||||
|
||||
const traceAsync =
|
||||
DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX.traceFnAsync.bind(DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX)
|
||||
const traceSync =
|
||||
DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX.traceFnSync.bind(DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX)
|
||||
const setTags =
|
||||
DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX.upsertTags.bind(DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX)
|
||||
const flush = () => {
|
||||
DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX.flush.bind(DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX)()
|
||||
}
|
||||
const onLogEvent = (callback: undefined | ((event: BamlLogEvent) => void)) =>
|
||||
DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX.onLogEvent(callback)
|
||||
|
||||
export { traceAsync, traceSync, setTags, flush, onLogEvent }
|
||||
106
workshops/2025-05-17/baml_client/type_builder.ts
Normal file
106
workshops/2025-05-17/baml_client/type_builder.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
import { FieldType } from '@boundaryml/baml/native'
|
||||
import { TypeBuilder as _TypeBuilder, EnumBuilder, EnumViewer, ClassBuilder, ClassViewer } from '@boundaryml/baml/type_builder'
|
||||
import { DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME } from "./globals"
|
||||
|
||||
export default class TypeBuilder {
|
||||
private tb: _TypeBuilder;
|
||||
|
||||
DoneForNow: ClassViewer<'DoneForNow', "intent" | "message">;
|
||||
|
||||
|
||||
|
||||
constructor() {
|
||||
this.tb = new _TypeBuilder({
|
||||
classes: new Set([
|
||||
"DoneForNow",
|
||||
]),
|
||||
enums: new Set([
|
||||
|
||||
]),
|
||||
runtime: DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME
|
||||
});
|
||||
|
||||
this.DoneForNow = this.tb.classViewer("DoneForNow", [
|
||||
"intent","message",
|
||||
]);
|
||||
|
||||
|
||||
}
|
||||
|
||||
__tb() {
|
||||
return this.tb._tb();
|
||||
}
|
||||
|
||||
string(): FieldType {
|
||||
return this.tb.string()
|
||||
}
|
||||
|
||||
literalString(value: string): FieldType {
|
||||
return this.tb.literalString(value)
|
||||
}
|
||||
|
||||
literalInt(value: number): FieldType {
|
||||
return this.tb.literalInt(value)
|
||||
}
|
||||
|
||||
literalBool(value: boolean): FieldType {
|
||||
return this.tb.literalBool(value)
|
||||
}
|
||||
|
||||
int(): FieldType {
|
||||
return this.tb.int()
|
||||
}
|
||||
|
||||
float(): FieldType {
|
||||
return this.tb.float()
|
||||
}
|
||||
|
||||
bool(): FieldType {
|
||||
return this.tb.bool()
|
||||
}
|
||||
|
||||
list(type: FieldType): FieldType {
|
||||
return this.tb.list(type)
|
||||
}
|
||||
|
||||
null(): FieldType {
|
||||
return this.tb.null()
|
||||
}
|
||||
|
||||
map(key: FieldType, value: FieldType): FieldType {
|
||||
return this.tb.map(key, value)
|
||||
}
|
||||
|
||||
union(types: FieldType[]): FieldType {
|
||||
return this.tb.union(types)
|
||||
}
|
||||
|
||||
addClass<Name extends string>(name: Name): ClassBuilder<Name> {
|
||||
return this.tb.addClass(name);
|
||||
}
|
||||
|
||||
addEnum<Name extends string>(name: Name): EnumBuilder<Name> {
|
||||
return this.tb.addEnum(name);
|
||||
}
|
||||
|
||||
addBaml(baml: string): void {
|
||||
this.tb.addBaml(baml);
|
||||
}
|
||||
}
|
||||
53
workshops/2025-05-17/baml_client/types.ts
Normal file
53
workshops/2025-05-17/baml_client/types.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
/*************************************************************************************************
|
||||
|
||||
Welcome to Baml! To use this generated code, please run one of the following:
|
||||
|
||||
$ npm install @boundaryml/baml
|
||||
$ yarn add @boundaryml/baml
|
||||
$ pnpm add @boundaryml/baml
|
||||
|
||||
*************************************************************************************************/
|
||||
|
||||
// This file was generated by BAML: do not edit it. Instead, edit the BAML
|
||||
// files and re-generate this code.
|
||||
//
|
||||
/* eslint-disable */
|
||||
// tslint:disable
|
||||
// @ts-nocheck
|
||||
// biome-ignore format: autogenerated code
|
||||
import type { Image, Audio } from "@boundaryml/baml"
|
||||
|
||||
/**
|
||||
* Recursively partial type that can be null.
|
||||
*
|
||||
* @deprecated Use types from the `partial_types` namespace instead, which provides type-safe partial implementations
|
||||
* @template T The type to make recursively partial.
|
||||
*/
|
||||
export type RecursivePartialNull<T> = T extends object
|
||||
? { [P in keyof T]?: RecursivePartialNull<T[P]> }
|
||||
: T | null;
|
||||
|
||||
export interface Checked<T,CheckName extends string = string> {
|
||||
value: T,
|
||||
checks: Record<CheckName, Check>,
|
||||
}
|
||||
|
||||
|
||||
export interface Check {
|
||||
name: string,
|
||||
expr: string
|
||||
status: "succeeded" | "failed"
|
||||
}
|
||||
|
||||
export function all_succeeded<CheckName extends string>(checks: Record<CheckName, Check>): boolean {
|
||||
return get_checks(checks).every(check => check.status === "succeeded")
|
||||
}
|
||||
|
||||
export function get_checks<CheckName extends string>(checks: Record<CheckName, Check>): Check[] {
|
||||
return Object.values(checks)
|
||||
}
|
||||
export interface DoneForNow {
|
||||
intent: "done_for_now"
|
||||
message: string
|
||||
|
||||
}
|
||||
49
workshops/2025-05-17/baml_src/agent.baml
Normal file
49
workshops/2025-05-17/baml_src/agent.baml
Normal file
@@ -0,0 +1,49 @@
|
||||
class DoneForNow {
|
||||
intent "done_for_now"
|
||||
message string
|
||||
}
|
||||
|
||||
client<llm> Qwen3 {
|
||||
provider "openai-generic"
|
||||
options {
|
||||
base_url "https://model-4w7jrl6w.api.baseten.co/environments/production/sync/v1"
|
||||
api_key env.BASETEN_API_KEY
|
||||
}
|
||||
}
|
||||
|
||||
function DetermineNextStep(
|
||||
thread: string
|
||||
) -> DoneForNow {
|
||||
client Qwen3
|
||||
|
||||
// use /nothink for now because the thinking tokens screw with baml
|
||||
prompt #"
|
||||
{{ _.role("system") }}
|
||||
|
||||
/nothink
|
||||
|
||||
You are a helpful assistant that can help with tasks.
|
||||
|
||||
{{ _.role("user") }}
|
||||
|
||||
You are working on the following thread:
|
||||
|
||||
{{ thread }}
|
||||
|
||||
What should the next step be?
|
||||
|
||||
{{ ctx.output_format }}
|
||||
"#
|
||||
}
|
||||
|
||||
test HelloWorld {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "hello!"
|
||||
}
|
||||
"#
|
||||
}
|
||||
}
|
||||
75
workshops/2025-05-17/baml_src/clients.baml
Normal file
75
workshops/2025-05-17/baml_src/clients.baml
Normal file
@@ -0,0 +1,75 @@
|
||||
// Learn more about clients at https://docs.boundaryml.com/docs/snippets/clients/overview
|
||||
|
||||
client<llm> CustomGPT4o {
|
||||
provider openai
|
||||
options {
|
||||
model "gpt-4o"
|
||||
api_key env.OPENAI_API_KEY
|
||||
}
|
||||
}
|
||||
|
||||
client<llm> CustomGPT4oMini {
|
||||
provider openai
|
||||
retry_policy Exponential
|
||||
options {
|
||||
model "gpt-4o-mini"
|
||||
api_key env.OPENAI_API_KEY
|
||||
}
|
||||
}
|
||||
|
||||
client<llm> CustomSonnet {
|
||||
provider anthropic
|
||||
options {
|
||||
model "claude-3-5-sonnet-20241022"
|
||||
api_key env.ANTHROPIC_API_KEY
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
client<llm> CustomHaiku {
|
||||
provider anthropic
|
||||
retry_policy Constant
|
||||
options {
|
||||
model "claude-3-haiku-20240307"
|
||||
api_key env.ANTHROPIC_API_KEY
|
||||
}
|
||||
}
|
||||
|
||||
// https://docs.boundaryml.com/docs/snippets/clients/round-robin
|
||||
client<llm> CustomFast {
|
||||
provider round-robin
|
||||
options {
|
||||
// This will alternate between the two clients
|
||||
strategy [CustomGPT4oMini, CustomHaiku]
|
||||
}
|
||||
}
|
||||
|
||||
// https://docs.boundaryml.com/docs/snippets/clients/fallback
|
||||
client<llm> OpenaiFallback {
|
||||
provider fallback
|
||||
options {
|
||||
// This will try the clients in order until one succeeds
|
||||
strategy [CustomGPT4oMini, CustomGPT4oMini]
|
||||
}
|
||||
}
|
||||
|
||||
// https://docs.boundaryml.com/docs/snippets/clients/retry
|
||||
retry_policy Constant {
|
||||
max_retries 3
|
||||
// Strategy is optional
|
||||
strategy {
|
||||
type constant_delay
|
||||
delay_ms 200
|
||||
}
|
||||
}
|
||||
|
||||
retry_policy Exponential {
|
||||
max_retries 2
|
||||
// Strategy is optional
|
||||
strategy {
|
||||
type exponential_backoff
|
||||
delay_ms 300
|
||||
multiplier 1.5
|
||||
max_delay_ms 10000
|
||||
}
|
||||
}
|
||||
18
workshops/2025-05-17/baml_src/generators.baml
Normal file
18
workshops/2025-05-17/baml_src/generators.baml
Normal file
@@ -0,0 +1,18 @@
|
||||
// This helps use auto generate libraries you can use in the language of
|
||||
// your choice. You can have multiple generators if you use multiple languages.
|
||||
// Just ensure that the output_dir is different for each generator.
|
||||
generator target {
|
||||
// Valid values: "python/pydantic", "typescript", "ruby/sorbet", "rest/openapi"
|
||||
output_type "typescript"
|
||||
|
||||
// Where the generated code will be saved (relative to baml_src/)
|
||||
output_dir "../"
|
||||
|
||||
// The version of the BAML package you have installed (e.g. same version as your baml-py or @boundaryml/baml).
|
||||
// The BAML VSCode extension version should also match this version.
|
||||
version "0.88.0"
|
||||
|
||||
// Valid values: "sync", "async"
|
||||
// This controls what `b.FunctionName()` will be (sync or async).
|
||||
default_client_mode async
|
||||
}
|
||||
2433
workshops/2025-05-17/package-lock.json
generated
Normal file
2433
workshops/2025-05-17/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
20
workshops/2025-05-17/package.json
Normal file
20
workshops/2025-05-17/package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "my-agent",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "tsx src/index.ts",
|
||||
"build": "tsc"
|
||||
},
|
||||
"dependencies": {
|
||||
"@boundaryml/baml": "^0.88.0",
|
||||
"tsx": "^4.15.0",
|
||||
"typescript": "^5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.0.0",
|
||||
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
||||
"@typescript-eslint/parser": "^6.0.0",
|
||||
"eslint": "^8.0.0"
|
||||
}
|
||||
}
|
||||
32
workshops/2025-05-17/src/agent.ts
Normal file
32
workshops/2025-05-17/src/agent.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { b } from "../baml_client";
|
||||
|
||||
// tool call or a respond to human tool
|
||||
type AgentResponse = Awaited<ReturnType<typeof b.DetermineNextStep>>;
|
||||
|
||||
export interface Event {
|
||||
type: string
|
||||
data: any;
|
||||
}
|
||||
|
||||
export class Thread {
|
||||
events: Event[] = [];
|
||||
|
||||
constructor(events: Event[]) {
|
||||
this.events = events;
|
||||
}
|
||||
|
||||
serializeForLLM() {
|
||||
// can change this to whatever custom serialization you want to do, XML, etc
|
||||
// e.g. https://github.com/got-agents/agents/blob/59ebbfa236fc376618f16ee08eb0f3bf7b698892/linear-assistant-ts/src/agent.ts#L66-L105
|
||||
return JSON.stringify(this.events);
|
||||
}
|
||||
}
|
||||
|
||||
// right now this just runs one turn with the LLM, but
|
||||
// we'll update this function to handle all the agent logic
|
||||
export async function agentLoop(thread: Thread): Promise<AgentResponse> {
|
||||
const nextStep = await b.DetermineNextStep(thread.serializeForLLM());
|
||||
return nextStep;
|
||||
}
|
||||
|
||||
|
||||
23
workshops/2025-05-17/src/cli.ts
Normal file
23
workshops/2025-05-17/src/cli.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
// cli.ts lets you invoke the agent loop from the command line
|
||||
|
||||
import { agentLoop, Thread, Event } from "./agent";
|
||||
|
||||
export async function cli() {
|
||||
// Get command line arguments, skipping the first two (node and script name)
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
if (args.length === 0) {
|
||||
console.error("Error: Please provide a message as a command line argument");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Join all arguments into a single message
|
||||
const message = args.join(" ");
|
||||
|
||||
// Create a new thread with the user's message as the initial event
|
||||
const thread = new Thread([{ type: "user_input", data: message }]);
|
||||
|
||||
// Run the agent loop with the thread
|
||||
const result = await agentLoop(thread);
|
||||
console.log(result);
|
||||
}
|
||||
11
workshops/2025-05-17/src/index.ts
Normal file
11
workshops/2025-05-17/src/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { cli } from "./cli"
|
||||
|
||||
async function hello(): Promise<void> {
|
||||
console.log('hello, world!')
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await cli()
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
24
workshops/2025-05-17/tsconfig.json
Normal file
24
workshops/2025-05-17/tsconfig.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2017",
|
||||
"lib": ["esnext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"noEmit": true,
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "preserve",
|
||||
"incremental": true,
|
||||
"plugins": [],
|
||||
"paths": {
|
||||
"@/*": ["./*"]
|
||||
}
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
||||
"exclude": ["node_modules", "walkthrough"]
|
||||
}
|
||||
|
||||
111
workshops/2025-05-17/walkthrough.yaml
Normal file
111
workshops/2025-05-17/walkthrough.yaml
Normal file
@@ -0,0 +1,111 @@
|
||||
title: "Building the 12-factor agent template from scratch"
|
||||
text: "Steps to start from a bare TS repo and build up a 12-factor agent. This walkthrough will guide you through creating a TypeScript agent that follows the 12-factor methodology."
|
||||
|
||||
targets:
|
||||
- markdown: "./build/walkthrough.md"
|
||||
onChange:
|
||||
diff: true
|
||||
cp: true
|
||||
newFiles:
|
||||
cat: false
|
||||
cp: true
|
||||
- folders:
|
||||
path: "./build/sections"
|
||||
skip:
|
||||
- "cleanup"
|
||||
final:
|
||||
dirName: "final"
|
||||
|
||||
sections:
|
||||
- name: cleanup
|
||||
title: "Cleanup"
|
||||
text: "Make sure you're starting from a clean slate"
|
||||
steps:
|
||||
- text: "Clean up existing files"
|
||||
command: |
|
||||
rm -rf baml_src/ && rm -rf src/
|
||||
|
||||
- name: hello-world
|
||||
title: "Chapter 0 - Hello World"
|
||||
text: "Let's start with a basic TypeScript setup and a hello world program."
|
||||
steps:
|
||||
- text: "Copy initial package.json"
|
||||
file: {src: ./walkthrough/00-package.json, dest: package.json}
|
||||
- text: "Install dependencies"
|
||||
command: |
|
||||
npm install
|
||||
incremental: true
|
||||
- text: "Copy tsconfig.json"
|
||||
file: {src: ./walkthrough/00-tsconfig.json, dest: tsconfig.json}
|
||||
- text: "add .gitignore"
|
||||
file: {src: ./walkthrough/00-.gitignore, dest: .gitignore}
|
||||
- text: "Create src folder"
|
||||
dir: {create: true, path: src}
|
||||
- text: "Add a simple hello world index.ts"
|
||||
file: {src: ./walkthrough/00-index.ts, dest: src/index.ts}
|
||||
- text: "Run it to verify"
|
||||
command: |
|
||||
npx tsx src/index.ts
|
||||
results:
|
||||
- text: "You should see:"
|
||||
code: |
|
||||
hello, world!
|
||||
|
||||
- name: cli-and-agent
|
||||
title: "Chapter 1 - CLI and Agent Loop"
|
||||
text: "Now let's add BAML and create our first agent with a CLI interface."
|
||||
steps:
|
||||
- text: "Install BAML"
|
||||
command: |
|
||||
npm i baml
|
||||
incremental: true
|
||||
- text: "Initialize BAML"
|
||||
command: |
|
||||
npx baml-cli init
|
||||
incremental: true
|
||||
- text: "Remove default resume.baml"
|
||||
command: |
|
||||
rm baml_src/resume.baml
|
||||
incremental: true
|
||||
- text: "Add our starter agent"
|
||||
file: {src: ./walkthrough/01-agent.baml, dest: baml_src/agent.baml}
|
||||
- text: "Generate BAML client code"
|
||||
command: |
|
||||
npx baml-cli generate
|
||||
incremental: true
|
||||
- text: "Enable BAML logging for development"
|
||||
command: |
|
||||
export BAML_LOG=debug
|
||||
- text: "Add the CLI interface"
|
||||
file: {src: ./walkthrough/01-cli.ts, dest: src/cli.ts}
|
||||
- text: "Update index.ts to use the CLI"
|
||||
file: {src: ./walkthrough/01-index.ts, dest: src/index.ts}
|
||||
- text: "Add the agent implementation"
|
||||
file: {src: ./walkthrough/01-agent.ts, dest: src/agent.ts}
|
||||
- text: "Try it out"
|
||||
command: |
|
||||
npx tsx src/index.ts hello
|
||||
|
||||
- name: calculator-tools
|
||||
title: "Chapter 2 - Add Calculator Tools"
|
||||
text: |
|
||||
Let's add some calculator tools to our agent.
|
||||
|
||||
We'll start by adding a tool definition, then update the agent to use it, and finally try it out.
|
||||
steps:
|
||||
- text: "Add calculator tools definition"
|
||||
file: {src: ./walkthrough/02-tool_calculator.baml, dest: baml_src/tool_calculator.baml}
|
||||
- text: "Update agent to use calculator tools"
|
||||
file: {src: ./walkthrough/02-agent.baml, dest: baml_src/agent.baml}
|
||||
- text: "Generate updated BAML client"
|
||||
command: |
|
||||
npx baml-cli generate
|
||||
incremental: true
|
||||
- text: "Try out the calculator"
|
||||
command: |
|
||||
npx tsx src/index.ts 'can you add 3 and 4'
|
||||
results:
|
||||
- text: "You should see: output like"
|
||||
code: |
|
||||
{ "intent": "add", "a": 3, "b": 4}
|
||||
- text: note that there won't be any result here, yet, we're just illustrating
|
||||
2
workshops/2025-05-17/walkthrough/00-.gitignore
Normal file
2
workshops/2025-05-17/walkthrough/00-.gitignore
Normal file
@@ -0,0 +1,2 @@
|
||||
baml_client/
|
||||
node_modules/
|
||||
9
workshops/2025-05-17/walkthrough/00-index.ts
Normal file
9
workshops/2025-05-17/walkthrough/00-index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
async function hello(): Promise<void> {
|
||||
console.log('hello, world!')
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await hello()
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
20
workshops/2025-05-17/walkthrough/00-package.json
Normal file
20
workshops/2025-05-17/walkthrough/00-package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "my-agent",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "tsx src/index.ts",
|
||||
"build": "tsc"
|
||||
},
|
||||
"dependencies": {
|
||||
"tsx": "^4.15.0",
|
||||
"typescript": "^5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.0.0",
|
||||
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
||||
"@typescript-eslint/parser": "^6.0.0",
|
||||
"eslint": "^8.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
24
workshops/2025-05-17/walkthrough/00-tsconfig.json
Normal file
24
workshops/2025-05-17/walkthrough/00-tsconfig.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2017",
|
||||
"lib": ["esnext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"noEmit": true,
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "preserve",
|
||||
"incremental": true,
|
||||
"plugins": [],
|
||||
"paths": {
|
||||
"@/*": ["./*"]
|
||||
}
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
||||
"exclude": ["node_modules", "walkthrough"]
|
||||
}
|
||||
|
||||
46
workshops/2025-05-17/walkthrough/01-agent.baml
Normal file
46
workshops/2025-05-17/walkthrough/01-agent.baml
Normal file
@@ -0,0 +1,46 @@
|
||||
class DoneForNow {
|
||||
intent "done_for_now"
|
||||
message string
|
||||
}
|
||||
|
||||
client<llm> Qwen3 {
|
||||
provider "openai-generic"
|
||||
options {
|
||||
base_url "https://model-4w7jrl6w.api.baseten.co/environments/production/predict"
|
||||
api_key "QQksI6f4.mFAZX9D0DevBQMUWs2OV8gcfzMUinN9N"
|
||||
}
|
||||
}
|
||||
|
||||
function DetermineNextStep(
|
||||
thread: string
|
||||
) -> DoneForNow {
|
||||
client Qwen3
|
||||
|
||||
prompt #"
|
||||
{{ _.role("system") }}
|
||||
|
||||
You are a helpful assistant that can help with tasks.
|
||||
|
||||
{{ _.role("user") }}
|
||||
|
||||
You are working on the following thread:
|
||||
|
||||
{{ thread }}
|
||||
|
||||
What should the next step be?
|
||||
|
||||
{{ ctx.output_format }}
|
||||
"#
|
||||
}
|
||||
|
||||
test HelloWorld {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "hello!"
|
||||
}
|
||||
"#
|
||||
}
|
||||
}
|
||||
32
workshops/2025-05-17/walkthrough/01-agent.ts
Normal file
32
workshops/2025-05-17/walkthrough/01-agent.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { b } from "../baml_client";
|
||||
|
||||
// tool call or a respond to human tool
|
||||
type AgentResponse = Awaited<ReturnType<typeof b.DetermineNextStep>>;
|
||||
|
||||
export interface Event {
|
||||
type: string
|
||||
data: any;
|
||||
}
|
||||
|
||||
export class Thread {
|
||||
events: Event[] = [];
|
||||
|
||||
constructor(events: Event[]) {
|
||||
this.events = events;
|
||||
}
|
||||
|
||||
serializeForLLM() {
|
||||
// can change this to whatever custom serialization you want to do, XML, etc
|
||||
// e.g. https://github.com/got-agents/agents/blob/59ebbfa236fc376618f16ee08eb0f3bf7b698892/linear-assistant-ts/src/agent.ts#L66-L105
|
||||
return JSON.stringify(this.events);
|
||||
}
|
||||
}
|
||||
|
||||
// right now this just runs one turn with the LLM, but
|
||||
// we'll update this function to handle all the agent logic
|
||||
export async function agentLoop(thread: Thread): Promise<AgentResponse> {
|
||||
const nextStep = await b.DetermineNextStep(thread.serializeForLLM());
|
||||
return nextStep;
|
||||
}
|
||||
|
||||
|
||||
23
workshops/2025-05-17/walkthrough/01-cli.ts
Normal file
23
workshops/2025-05-17/walkthrough/01-cli.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
// cli.ts lets you invoke the agent loop from the command line
|
||||
|
||||
import { agentLoop, Thread, Event } from "./agent";
|
||||
|
||||
export async function cli() {
|
||||
// Get command line arguments, skipping the first two (node and script name)
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
if (args.length === 0) {
|
||||
console.error("Error: Please provide a message as a command line argument");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Join all arguments into a single message
|
||||
const message = args.join(" ");
|
||||
|
||||
// Create a new thread with the user's message as the initial event
|
||||
const thread = new Thread([{ type: "user_input", data: message }]);
|
||||
|
||||
// Run the agent loop with the thread
|
||||
const result = await agentLoop(thread);
|
||||
console.log(result);
|
||||
}
|
||||
11
workshops/2025-05-17/walkthrough/01-index.ts
Normal file
11
workshops/2025-05-17/walkthrough/01-index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { cli } from "./cli"
|
||||
|
||||
async function hello(): Promise<void> {
|
||||
console.log('hello, world!')
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await cli()
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
38
workshops/2025-05-17/walkthrough/02-agent.baml
Normal file
38
workshops/2025-05-17/walkthrough/02-agent.baml
Normal file
@@ -0,0 +1,38 @@
|
||||
class DoneForNow {
|
||||
intent "done_for_now"
|
||||
message string
|
||||
}
|
||||
|
||||
function DetermineNextStep(
|
||||
thread: string
|
||||
) -> CalculatorTools | DoneForNow {
|
||||
client "openai/gpt-4o"
|
||||
|
||||
prompt #"
|
||||
{{ _.role("system") }}
|
||||
|
||||
You are a helpful assistant that can help with tasks.
|
||||
|
||||
{{ _.role("user") }}
|
||||
|
||||
You are working on the following thread:
|
||||
|
||||
{{ thread }}
|
||||
|
||||
What should the next step be?
|
||||
|
||||
{{ ctx.output_format }}
|
||||
"#
|
||||
}
|
||||
|
||||
test HelloWorld {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "hello!"
|
||||
}
|
||||
"#
|
||||
}
|
||||
}
|
||||
27
workshops/2025-05-17/walkthrough/02-tool_calculator.baml
Normal file
27
workshops/2025-05-17/walkthrough/02-tool_calculator.baml
Normal file
@@ -0,0 +1,27 @@
|
||||
type CalculatorTools = AddTool | SubtractTool | MultiplyTool | DivideTool
|
||||
|
||||
|
||||
class AddTool {
|
||||
intent "add"
|
||||
a int | float
|
||||
b int | float
|
||||
}
|
||||
|
||||
class SubtractTool {
|
||||
intent "subtract"
|
||||
a int | float
|
||||
b int | float
|
||||
}
|
||||
|
||||
class MultiplyTool {
|
||||
intent "multiply"
|
||||
a int | float
|
||||
b int | float
|
||||
}
|
||||
|
||||
class DivideTool {
|
||||
intent "divide"
|
||||
a int | float
|
||||
b int | float
|
||||
}
|
||||
|
||||
55
workshops/2025-05-17/walkthrough/03-agent.ts
Normal file
55
workshops/2025-05-17/walkthrough/03-agent.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { b } from "../baml_client";
|
||||
|
||||
// tool call or a respond to human tool
|
||||
type AgentResponse = Awaited<ReturnType<typeof b.DetermineNextStep>>;
|
||||
|
||||
export interface Event {
|
||||
type: string
|
||||
data: any;
|
||||
}
|
||||
|
||||
export class Thread {
|
||||
events: Event[] = [];
|
||||
|
||||
constructor(events: Event[]) {
|
||||
this.events = events;
|
||||
}
|
||||
|
||||
serializeForLLM() {
|
||||
// can change this to whatever custom serialization you want to do, XML, etc
|
||||
// e.g. https://github.com/got-agents/agents/blob/59ebbfa236fc376618f16ee08eb0f3bf7b698892/linear-assistant-ts/src/agent.ts#L66-L105
|
||||
return JSON.stringify(this.events);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
export async function agentLoop(thread: Thread): Promise<string> {
|
||||
|
||||
while (true) {
|
||||
const nextStep = await b.DetermineNextStep(thread.serializeForLLM());
|
||||
console.log("nextStep", nextStep);
|
||||
|
||||
switch (nextStep.intent) {
|
||||
case "done_for_now":
|
||||
// response to human, return the next step object
|
||||
return nextStep.message;
|
||||
case "add":
|
||||
thread.events.push({
|
||||
"type": "tool_call",
|
||||
"data": nextStep
|
||||
});
|
||||
const result = nextStep.a + nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
continue;
|
||||
default:
|
||||
throw new Error(`Unknown intent: ${nextStep.intent}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
86
workshops/2025-05-17/walkthrough/03b-agent.ts
Normal file
86
workshops/2025-05-17/walkthrough/03b-agent.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { AddTool, SubtractTool, DivideTool, MultiplyTool, b } from "../baml_client";
|
||||
|
||||
export interface Event {
|
||||
type: string
|
||||
data: any;
|
||||
}
|
||||
|
||||
export class Thread {
|
||||
events: Event[] = [];
|
||||
|
||||
constructor(events: Event[]) {
|
||||
this.events = events;
|
||||
}
|
||||
|
||||
serializeForLLM() {
|
||||
// can change this to whatever custom serialization you want to do, XML, etc
|
||||
// e.g. https://github.com/got-agents/agents/blob/59ebbfa236fc376618f16ee08eb0f3bf7b698892/linear-assistant-ts/src/agent.ts#L66-L105
|
||||
return JSON.stringify(this.events);
|
||||
}
|
||||
}
|
||||
|
||||
export type CalculatorTool = AddTool | SubtractTool | MultiplyTool | DivideTool;
|
||||
|
||||
export async function handleNextStep(nextStep: CalculatorTool, thread: Thread): Promise<Thread> {
|
||||
let result: number;
|
||||
switch (nextStep.intent) {
|
||||
case "add":
|
||||
result = nextStep.a + nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "subtract":
|
||||
result = nextStep.a - nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "multiply":
|
||||
result = nextStep.a * nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "divide":
|
||||
result = nextStep.a / nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
}
|
||||
}
|
||||
|
||||
export async function agentLoop(thread: Thread): Promise<string> {
|
||||
|
||||
while (true) {
|
||||
const nextStep = await b.DetermineNextStep(thread.serializeForLLM());
|
||||
console.log("nextStep", nextStep);
|
||||
|
||||
thread.events.push({
|
||||
"type": "tool_call",
|
||||
"data": nextStep
|
||||
});
|
||||
|
||||
switch (nextStep.intent) {
|
||||
case "done_for_now":
|
||||
// response to human, return the next step object
|
||||
return nextStep.message;
|
||||
case "add":
|
||||
case "subtract":
|
||||
case "multiply":
|
||||
case "divide":
|
||||
thread = await handleNextStep(nextStep, thread);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
51
workshops/2025-05-17/walkthrough/04-agent.baml
Normal file
51
workshops/2025-05-17/walkthrough/04-agent.baml
Normal file
@@ -0,0 +1,51 @@
|
||||
class DoneForNow {
|
||||
intent "done_for_now"
|
||||
message string
|
||||
}
|
||||
|
||||
function DetermineNextStep(
|
||||
thread: string
|
||||
) -> CalculatorTools | DoneForNow {
|
||||
client "openai/gpt-4o"
|
||||
|
||||
prompt #"
|
||||
{{ _.role("system") }}
|
||||
|
||||
You are a helpful assistant that can help with tasks.
|
||||
|
||||
{{ _.role("user") }}
|
||||
|
||||
You are working on the following thread:
|
||||
|
||||
{{ thread }}
|
||||
|
||||
What should the next step be?
|
||||
|
||||
{{ ctx.output_format }}
|
||||
"#
|
||||
}
|
||||
|
||||
test HelloWorld {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "hello!"
|
||||
}
|
||||
"#
|
||||
}
|
||||
}
|
||||
|
||||
test MathOperation {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "can you multiply 3 and 4?"
|
||||
}
|
||||
"#
|
||||
}
|
||||
}
|
||||
|
||||
53
workshops/2025-05-17/walkthrough/04b-agent.baml
Normal file
53
workshops/2025-05-17/walkthrough/04b-agent.baml
Normal file
@@ -0,0 +1,53 @@
|
||||
class DoneForNow {
|
||||
intent "done_for_now"
|
||||
message string
|
||||
}
|
||||
|
||||
function DetermineNextStep(
|
||||
thread: string
|
||||
) -> CalculatorTools | DoneForNow {
|
||||
client "openai/gpt-4o"
|
||||
|
||||
prompt #"
|
||||
{{ _.role("system") }}
|
||||
|
||||
You are a helpful assistant that can help with tasks.
|
||||
|
||||
{{ _.role("user") }}
|
||||
|
||||
You are working on the following thread:
|
||||
|
||||
{{ thread }}
|
||||
|
||||
What should the next step be?
|
||||
|
||||
{{ ctx.output_format }}
|
||||
"#
|
||||
}
|
||||
|
||||
test HelloWorld {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "hello!"
|
||||
}
|
||||
"#
|
||||
}
|
||||
@@assert(hello, {{this.intent == "done_for_now"}})
|
||||
}
|
||||
|
||||
test MathOperation {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "can you multiply 3 and 4?"
|
||||
}
|
||||
"#
|
||||
}
|
||||
@@assert(math_operation, {{this.intent == "multiply"}})
|
||||
}
|
||||
|
||||
105
workshops/2025-05-17/walkthrough/04c-agent.baml
Normal file
105
workshops/2025-05-17/walkthrough/04c-agent.baml
Normal file
@@ -0,0 +1,105 @@
|
||||
class DoneForNow {
|
||||
intent "done_for_now"
|
||||
message string
|
||||
}
|
||||
|
||||
function DetermineNextStep(
|
||||
thread: string
|
||||
) -> CalculatorTools | DoneForNow {
|
||||
client "openai/gpt-4o"
|
||||
|
||||
prompt #"
|
||||
{{ _.role("system") }}
|
||||
|
||||
You are a helpful assistant that can help with tasks.
|
||||
|
||||
{{ _.role("user") }}
|
||||
|
||||
You are working on the following thread:
|
||||
|
||||
{{ thread }}
|
||||
|
||||
What should the next step be?
|
||||
|
||||
{{ ctx.output_format }}
|
||||
"#
|
||||
}
|
||||
|
||||
test HelloWorld {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "hello!"
|
||||
}
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "done_for_now"}})
|
||||
}
|
||||
|
||||
test MathOperation {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "can you multiply 3 and 4?"
|
||||
}
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "multiply"}})
|
||||
}
|
||||
|
||||
test LongMath {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
[
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "can you multiply 3 and 4, then divide the result by 2 and then add 12 to that result?"
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "multiply",
|
||||
"a": 3,
|
||||
"b": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 12
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "divide",
|
||||
"a": 12,
|
||||
"b": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 6
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "add",
|
||||
"a": 6,
|
||||
"b": 12
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 18
|
||||
}
|
||||
]
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "done_for_now"}})
|
||||
@@assert(answer, {{"18" in this.message}})
|
||||
}
|
||||
|
||||
117
workshops/2025-05-17/walkthrough/05-agent.baml
Normal file
117
workshops/2025-05-17/walkthrough/05-agent.baml
Normal file
@@ -0,0 +1,117 @@
|
||||
// human tools are async requests to a human
|
||||
type HumanTools = ClarificationRequest | DoneForNow
|
||||
|
||||
class ClarificationRequest {
|
||||
intent "request_more_information" @description("you can request more information from me")
|
||||
message string
|
||||
}
|
||||
|
||||
class DoneForNow {
|
||||
intent "done_for_now"
|
||||
|
||||
message string @description(#"
|
||||
message to send to the user about the work that was done.
|
||||
"#)
|
||||
}
|
||||
|
||||
function DetermineNextStep(
|
||||
thread: string
|
||||
) -> HumanTools | CalculatorTools {
|
||||
client "openai/gpt-4o"
|
||||
|
||||
prompt #"
|
||||
{{ _.role("system") }}
|
||||
|
||||
You are a helpful assistant that can help with tasks.
|
||||
|
||||
{{ _.role("user") }}
|
||||
|
||||
You are working on the following thread:
|
||||
|
||||
{{ thread }}
|
||||
|
||||
What should the next step be?
|
||||
|
||||
{{ ctx.output_format }}
|
||||
"#
|
||||
}
|
||||
|
||||
test HelloWorld {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "hello!"
|
||||
}
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "done_for_now"}})
|
||||
}
|
||||
|
||||
test MathOperation {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "can you multiply 3 and 4?"
|
||||
}
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "multiply"}})
|
||||
}
|
||||
|
||||
test LongMath {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
[
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "can you multiply 3 and 4, then divide the result by 2 and then add 12 to that result?"
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "multiply",
|
||||
"a": 3,
|
||||
"b": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 12
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "divide",
|
||||
"a": 12,
|
||||
"b": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 6
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "add",
|
||||
"a": 6,
|
||||
"b": 12
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 18
|
||||
}
|
||||
]
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "done_for_now"}})
|
||||
@@assert(answer, {{"18" in this.message}})
|
||||
}
|
||||
|
||||
|
||||
87
workshops/2025-05-17/walkthrough/05-agent.ts
Normal file
87
workshops/2025-05-17/walkthrough/05-agent.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { AddTool, SubtractTool, DivideTool, MultiplyTool, b } from "../baml_client";
|
||||
|
||||
export interface Event {
|
||||
type: string
|
||||
data: any;
|
||||
}
|
||||
|
||||
export class Thread {
|
||||
events: Event[] = [];
|
||||
|
||||
constructor(events: Event[]) {
|
||||
this.events = events;
|
||||
}
|
||||
|
||||
serializeForLLM() {
|
||||
// can change this to whatever custom serialization you want to do, XML, etc
|
||||
// e.g. https://github.com/got-agents/agents/blob/59ebbfa236fc376618f16ee08eb0f3bf7b698892/linear-assistant-ts/src/agent.ts#L66-L105
|
||||
return JSON.stringify(this.events);
|
||||
}
|
||||
}
|
||||
|
||||
export type CalculatorTool = AddTool | SubtractTool | MultiplyTool | DivideTool;
|
||||
|
||||
export async function handleNextStep(nextStep: CalculatorTool, thread: Thread): Promise<Thread> {
|
||||
let result: number;
|
||||
switch (nextStep.intent) {
|
||||
case "add":
|
||||
result = nextStep.a + nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "subtract":
|
||||
result = nextStep.a - nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "multiply":
|
||||
result = nextStep.a * nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "divide":
|
||||
result = nextStep.a / nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
}
|
||||
}
|
||||
|
||||
export async function agentLoop(thread: Thread): Promise<Thread> {
|
||||
|
||||
while (true) {
|
||||
const nextStep = await b.DetermineNextStep(thread.serializeForLLM());
|
||||
console.log("nextStep", nextStep);
|
||||
|
||||
thread.events.push({
|
||||
"type": "tool_call",
|
||||
"data": nextStep
|
||||
});
|
||||
|
||||
switch (nextStep.intent) {
|
||||
case "done_for_now":
|
||||
case "request_more_information":
|
||||
// response to human, return the thread
|
||||
return thread;
|
||||
case "add":
|
||||
case "subtract":
|
||||
case "multiply":
|
||||
case "divide":
|
||||
thread = await handleNextStep(nextStep, thread);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
50
workshops/2025-05-17/walkthrough/05-cli.ts
Normal file
50
workshops/2025-05-17/walkthrough/05-cli.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
// cli.ts lets you invoke the agent loop from the command line
|
||||
|
||||
import { agentLoop, Thread, Event } from "../src/agent";
|
||||
|
||||
|
||||
|
||||
export async function cli() {
|
||||
// Get command line arguments, skipping the first two (node and script name)
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
if (args.length === 0) {
|
||||
console.error("Error: Please provide a message as a command line argument");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Join all arguments into a single message
|
||||
const message = args.join(" ");
|
||||
|
||||
// Create a new thread with the user's message as the initial event
|
||||
const thread = new Thread([{ type: "user_input", data: message }]);
|
||||
|
||||
// Run the agent loop with the thread
|
||||
const result = await agentLoop(thread);
|
||||
let lastEvent = result.events.slice(-1)[0];
|
||||
|
||||
while (lastEvent.data.intent === "request_more_information") {
|
||||
const message = await askHuman(lastEvent.data.message);
|
||||
thread.events.push({ type: "human_response", data: message });
|
||||
const result = await agentLoop(thread);
|
||||
lastEvent = result.events.slice(-1)[0];
|
||||
}
|
||||
|
||||
// print the final result
|
||||
// optional - you could loop here too
|
||||
console.log(lastEvent.data.message);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
async function askHuman(message: string) {
|
||||
const readline = require('readline').createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
return new Promise((resolve) => {
|
||||
readline.question(`${message}\n> `, (answer: string) => {
|
||||
resolve(answer);
|
||||
});
|
||||
});
|
||||
}
|
||||
146
workshops/2025-05-17/walkthrough/05b-agent.baml
Normal file
146
workshops/2025-05-17/walkthrough/05b-agent.baml
Normal file
@@ -0,0 +1,146 @@
|
||||
// human tools are async requests to a human
|
||||
type HumanTools = ClarificationRequest | DoneForNow
|
||||
|
||||
class ClarificationRequest {
|
||||
intent "request_more_information" @description("you can request more information from me")
|
||||
message string
|
||||
}
|
||||
|
||||
class DoneForNow {
|
||||
intent "done_for_now"
|
||||
|
||||
message string @description(#"
|
||||
message to send to the user about the work that was done.
|
||||
"#)
|
||||
}
|
||||
|
||||
function DetermineNextStep(
|
||||
thread: string
|
||||
) -> HumanTools | CalculatorTools {
|
||||
client "openai/gpt-4o"
|
||||
|
||||
prompt #"
|
||||
{{ _.role("system") }}
|
||||
|
||||
You are a helpful assistant that can help with tasks.
|
||||
|
||||
{{ _.role("user") }}
|
||||
|
||||
You are working on the following thread:
|
||||
|
||||
{{ thread }}
|
||||
|
||||
What should the next step be?
|
||||
|
||||
{{ ctx.output_format }}
|
||||
"#
|
||||
}
|
||||
|
||||
test HelloWorld {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "hello!"
|
||||
}
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "done_for_now"}})
|
||||
}
|
||||
|
||||
test MathOperation {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "can you multiply 3 and 4?"
|
||||
}
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "multiply"}})
|
||||
}
|
||||
|
||||
test LongMath {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
[
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "can you multiply 3 and 4, then divide the result by 2 and then add 12 to that result?"
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "multiply",
|
||||
"a": 3,
|
||||
"b": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 12
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "divide",
|
||||
"a": 12,
|
||||
"b": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 6
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "add",
|
||||
"a": 6,
|
||||
"b": 12
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 18
|
||||
}
|
||||
]
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "done_for_now"}})
|
||||
@@assert(answer, {{"18" in this.message}})
|
||||
}
|
||||
|
||||
|
||||
|
||||
test MathOperationWithClarification {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
[{"type":"user_input","data":"can you multiply 3 and feee9ff10"}]
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "request_more_information"}})
|
||||
}
|
||||
|
||||
test MathOperationPostClarification {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
[
|
||||
{"type":"user_input","data":"can you multiply 3 and FD*(#F&& ?"},
|
||||
{"type":"tool_call","data":{"intent":"request_more_information","message":"It seems like there was a typo or mistake in your request. Could you please clarify or provide the correct numbers you would like to multiply?"}},
|
||||
{"type":"human_response","data":"lets try 12 instead"},
|
||||
]
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "multiply"}})
|
||||
@@assert(a, {{this.b == 12}})
|
||||
@@assert(b, {{this.a == 3}})
|
||||
}
|
||||
|
||||
|
||||
|
||||
146
workshops/2025-05-17/walkthrough/05c-agent.baml
Normal file
146
workshops/2025-05-17/walkthrough/05c-agent.baml
Normal file
@@ -0,0 +1,146 @@
|
||||
// human tools are async requests to a human
|
||||
type HumanTools = ClarificationRequest | DoneForNow
|
||||
|
||||
class ClarificationRequest {
|
||||
intent "request_more_information" @description("you can request more information from me")
|
||||
message string
|
||||
}
|
||||
|
||||
class DoneForNow {
|
||||
intent "done_for_now"
|
||||
|
||||
message string @description(#"
|
||||
message to send to the user about the work that was done.
|
||||
"#)
|
||||
}
|
||||
|
||||
function DetermineNextStep(
|
||||
thread: string
|
||||
) -> HumanTools | CalculatorTools {
|
||||
client "openai/gpt-4o"
|
||||
|
||||
prompt #"
|
||||
{{ _.role("system") }}
|
||||
|
||||
You are a helpful assistant that can help with tasks.
|
||||
|
||||
{{ _.role("user") }}
|
||||
|
||||
You are working on the following thread:
|
||||
|
||||
{{ thread }}
|
||||
|
||||
What should the next step be?
|
||||
|
||||
{{ ctx.output_format }}
|
||||
"#
|
||||
}
|
||||
|
||||
test HelloWorld {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "hello!"
|
||||
}
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "request_more_information"}})
|
||||
}
|
||||
|
||||
test MathOperation {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "can you multiply 3 and 4?"
|
||||
}
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "multiply"}})
|
||||
}
|
||||
|
||||
test LongMath {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
[
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "can you multiply 3 and 4, then divide the result by 2 and then add 12 to that result?"
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "multiply",
|
||||
"a": 3,
|
||||
"b": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 12
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "divide",
|
||||
"a": 12,
|
||||
"b": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 6
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "add",
|
||||
"a": 6,
|
||||
"b": 12
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 18
|
||||
}
|
||||
]
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "done_for_now"}})
|
||||
@@assert(answer, {{"18" in this.message}})
|
||||
}
|
||||
|
||||
|
||||
|
||||
test MathOperationWithClarification {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
[{"type":"user_input","data":"can you multiply 3 and feee9ff10"}]
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "request_more_information"}})
|
||||
}
|
||||
|
||||
test MathOperationPostClarification {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
[
|
||||
{"type":"user_input","data":"can you multiply 3 and FD*(#F&& ?"},
|
||||
{"type":"tool_call","data":{"intent":"request_more_information","message":"It seems like there was a typo or mistake in your request. Could you please clarify or provide the correct numbers you would like to multiply?"}},
|
||||
{"type":"human_response","data":"lets try 12 instead"},
|
||||
]
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "multiply"}})
|
||||
@@assert(a, {{this.b == 12}})
|
||||
@@assert(b, {{this.a == 3}})
|
||||
}
|
||||
|
||||
|
||||
|
||||
152
workshops/2025-05-17/walkthrough/06-agent.baml
Normal file
152
workshops/2025-05-17/walkthrough/06-agent.baml
Normal file
@@ -0,0 +1,152 @@
|
||||
// human tools are async requests to a human
|
||||
type HumanTools = ClarificationRequest | DoneForNow
|
||||
|
||||
class ClarificationRequest {
|
||||
intent "request_more_information" @description("you can request more information from me")
|
||||
message string
|
||||
}
|
||||
|
||||
class DoneForNow {
|
||||
intent "done_for_now"
|
||||
|
||||
message string @description(#"
|
||||
message to send to the user about the work that was done.
|
||||
"#)
|
||||
}
|
||||
|
||||
function DetermineNextStep(
|
||||
thread: string
|
||||
) -> HumanTools | CalculatorTools {
|
||||
client "openai/gpt-4o"
|
||||
|
||||
prompt #"
|
||||
{{ _.role("system") }}
|
||||
|
||||
You are a helpful assistant that can help with tasks.
|
||||
|
||||
{{ _.role("user") }}
|
||||
|
||||
You are working on the following thread:
|
||||
|
||||
{{ thread }}
|
||||
|
||||
What should the next step be?
|
||||
|
||||
{{ ctx.output_format }}
|
||||
|
||||
Always think about what to do next first, like:
|
||||
|
||||
- ...
|
||||
- ...
|
||||
- ...
|
||||
|
||||
{...} // schema
|
||||
"#
|
||||
}
|
||||
|
||||
test HelloWorld {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "hello!"
|
||||
}
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "request_more_information"}})
|
||||
}
|
||||
|
||||
test MathOperation {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "can you multiply 3 and 4?"
|
||||
}
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "multiply"}})
|
||||
}
|
||||
|
||||
test LongMath {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
[
|
||||
{
|
||||
"type": "user_input",
|
||||
"data": "can you multiply 3 and 4, then divide the result by 2 and then add 12 to that result?"
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "multiply",
|
||||
"a": 3,
|
||||
"b": 4
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 12
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "divide",
|
||||
"a": 12,
|
||||
"b": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 6
|
||||
},
|
||||
{
|
||||
"type": "tool_call",
|
||||
"data": {
|
||||
"intent": "add",
|
||||
"a": 6,
|
||||
"b": 12
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "tool_response",
|
||||
"data": 18
|
||||
}
|
||||
]
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "done_for_now"}})
|
||||
@@assert(answer, {{"18" in this.message}})
|
||||
}
|
||||
|
||||
|
||||
|
||||
test MathOperationWithClarification {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
[{"type":"user_input","data":"can you multiply 3 and feee9ff10"}]
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "request_more_information"}})
|
||||
}
|
||||
|
||||
test MathOperationPostClarification {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
[
|
||||
{"type":"user_input","data":"can you multiply 3 and FD*(#F&& ?"},
|
||||
{"type":"tool_call","data":{"intent":"request_more_information","message":"It seems like there was a typo or mistake in your request. Could you please clarify or provide the correct numbers you would like to multiply?"}},
|
||||
{"type":"human_response","data":"lets try 12 instead"},
|
||||
]
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "multiply"}})
|
||||
@@assert(a, {{this.b == 12}})
|
||||
@@assert(b, {{this.a == 3}})
|
||||
}
|
||||
|
||||
87
workshops/2025-05-17/walkthrough/07-agent.ts
Normal file
87
workshops/2025-05-17/walkthrough/07-agent.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { AddTool, SubtractTool, DivideTool, MultiplyTool, b } from "../baml_client";
|
||||
|
||||
export interface Event {
|
||||
type: string
|
||||
data: any;
|
||||
}
|
||||
|
||||
export class Thread {
|
||||
events: Event[] = [];
|
||||
|
||||
constructor(events: Event[]) {
|
||||
this.events = events;
|
||||
}
|
||||
|
||||
serializeForLLM() {
|
||||
// can change this to whatever custom serialization you want to do, XML, etc
|
||||
// e.g. https://github.com/got-agents/agents/blob/59ebbfa236fc376618f16ee08eb0f3bf7b698892/linear-assistant-ts/src/agent.ts#L66-L105
|
||||
return JSON.stringify(this.events, null, 2);
|
||||
}
|
||||
}
|
||||
|
||||
export type CalculatorTool = AddTool | SubtractTool | MultiplyTool | DivideTool;
|
||||
|
||||
export async function handleNextStep(nextStep: CalculatorTool, thread: Thread): Promise<Thread> {
|
||||
let result: number;
|
||||
switch (nextStep.intent) {
|
||||
case "add":
|
||||
result = nextStep.a + nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "subtract":
|
||||
result = nextStep.a - nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "multiply":
|
||||
result = nextStep.a * nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "divide":
|
||||
result = nextStep.a / nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
}
|
||||
}
|
||||
|
||||
export async function agentLoop(thread: Thread): Promise<Thread> {
|
||||
|
||||
while (true) {
|
||||
const nextStep = await b.DetermineNextStep(thread.serializeForLLM());
|
||||
console.log("nextStep", nextStep);
|
||||
|
||||
thread.events.push({
|
||||
"type": "tool_call",
|
||||
"data": nextStep
|
||||
});
|
||||
|
||||
switch (nextStep.intent) {
|
||||
case "done_for_now":
|
||||
case "request_more_information":
|
||||
// response to human, return the thread
|
||||
return thread;
|
||||
case "add":
|
||||
case "subtract":
|
||||
case "multiply":
|
||||
case "divide":
|
||||
thread = await handleNextStep(nextStep, thread);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
99
workshops/2025-05-17/walkthrough/07b-agent.ts
Normal file
99
workshops/2025-05-17/walkthrough/07b-agent.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { AddTool, SubtractTool, DivideTool, MultiplyTool, b } from "../baml_client";
|
||||
|
||||
export interface Event {
|
||||
type: string
|
||||
data: any;
|
||||
}
|
||||
|
||||
export class Thread {
|
||||
events: Event[] = [];
|
||||
|
||||
constructor(events: Event[]) {
|
||||
this.events = events;
|
||||
}
|
||||
|
||||
serializeForLLM() {
|
||||
return this.events.map(e => this.serializeOneEvent(e)).join("\n");
|
||||
}
|
||||
|
||||
trimLeadingWhitespace(s: string) {
|
||||
return s.replace(/^[ \t]+/gm, '');
|
||||
}
|
||||
|
||||
serializeOneEvent(e: Event) {
|
||||
return this.trimLeadingWhitespace(`
|
||||
<${e.data?.intent || e.type}>
|
||||
${
|
||||
typeof e.data !== 'object' ? e.data :
|
||||
Object.keys(e.data).filter(k => k !== 'intent').map(k => `${k}: ${e.data[k]}`).join("\n")}
|
||||
</${e.data?.intent || e.type}>
|
||||
`)
|
||||
}
|
||||
}
|
||||
|
||||
export type CalculatorTool = AddTool | SubtractTool | MultiplyTool | DivideTool;
|
||||
|
||||
export async function handleNextStep(nextStep: CalculatorTool, thread: Thread): Promise<Thread> {
|
||||
let result: number;
|
||||
switch (nextStep.intent) {
|
||||
case "add":
|
||||
result = nextStep.a + nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "subtract":
|
||||
result = nextStep.a - nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "multiply":
|
||||
result = nextStep.a * nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "divide":
|
||||
result = nextStep.a / nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
}
|
||||
}
|
||||
|
||||
export async function agentLoop(thread: Thread): Promise<Thread> {
|
||||
|
||||
while (true) {
|
||||
const nextStep = await b.DetermineNextStep(thread.serializeForLLM());
|
||||
console.log("nextStep", nextStep);
|
||||
|
||||
thread.events.push({
|
||||
"type": "tool_call",
|
||||
"data": nextStep
|
||||
});
|
||||
|
||||
switch (nextStep.intent) {
|
||||
case "done_for_now":
|
||||
case "request_more_information":
|
||||
// response to human, return the thread
|
||||
return thread;
|
||||
case "add":
|
||||
case "subtract":
|
||||
case "multiply":
|
||||
case "divide":
|
||||
thread = await handleNextStep(nextStep, thread);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
153
workshops/2025-05-17/walkthrough/07c-agent.baml
Normal file
153
workshops/2025-05-17/walkthrough/07c-agent.baml
Normal file
@@ -0,0 +1,153 @@
|
||||
// human tools are async requests to a human
|
||||
type HumanTools = ClarificationRequest | DoneForNow
|
||||
|
||||
class ClarificationRequest {
|
||||
intent "request_more_information" @description("you can request more information from me")
|
||||
message string
|
||||
}
|
||||
|
||||
class DoneForNow {
|
||||
intent "done_for_now"
|
||||
|
||||
message string @description(#"
|
||||
message to send to the user about the work that was done.
|
||||
"#)
|
||||
}
|
||||
|
||||
function DetermineNextStep(
|
||||
thread: string
|
||||
) -> HumanTools | CalculatorTools {
|
||||
client "openai/gpt-4o"
|
||||
|
||||
prompt #"
|
||||
{{ _.role("system") }}
|
||||
|
||||
You are a helpful assistant that can help with tasks.
|
||||
|
||||
{{ _.role("user") }}
|
||||
|
||||
You are working on the following thread:
|
||||
|
||||
{{ thread }}
|
||||
|
||||
What should the next step be?
|
||||
|
||||
{{ ctx.output_format }}
|
||||
|
||||
Always think about what to do next first, like:
|
||||
|
||||
- ...
|
||||
- ...
|
||||
- ...
|
||||
|
||||
{...} // schema
|
||||
"#
|
||||
}
|
||||
|
||||
test HelloWorld {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
<user_input>
|
||||
hello!
|
||||
</user_input>
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "request_more_information"}})
|
||||
}
|
||||
|
||||
test MathOperation {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
<user_input>
|
||||
can you multiply 3 and 4?
|
||||
</user_input>
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "multiply"}})
|
||||
}
|
||||
|
||||
test LongMath {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
<user_input>
|
||||
can you multiply 3 and 4, then divide the result by 2 and then add 12 to that result?
|
||||
</user_input>
|
||||
|
||||
|
||||
<multiply>
|
||||
a: 3
|
||||
b: 4
|
||||
</multiply>
|
||||
|
||||
|
||||
<tool_response>
|
||||
12
|
||||
</tool_response>
|
||||
|
||||
|
||||
<divide>
|
||||
a: 12
|
||||
b: 2
|
||||
</divide>
|
||||
|
||||
|
||||
<tool_response>
|
||||
6
|
||||
</tool_response>
|
||||
|
||||
|
||||
<add>
|
||||
a: 6
|
||||
b: 12
|
||||
</add>
|
||||
|
||||
|
||||
<tool_response>
|
||||
18
|
||||
</tool_response>
|
||||
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "done_for_now"}})
|
||||
@@assert(answer, {{"18" in this.message}})
|
||||
}
|
||||
|
||||
|
||||
|
||||
test MathOperationWithClarification {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
<user_input>
|
||||
can you multiply 3 and fe1iiaff10
|
||||
</user_input>
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "request_more_information"}})
|
||||
}
|
||||
|
||||
test MathOperationPostClarification {
|
||||
functions [DetermineNextStep]
|
||||
args {
|
||||
thread #"
|
||||
<user_input>
|
||||
can you multiply 3 and FD*(#F&& ?
|
||||
</user_input>
|
||||
|
||||
<request_more_information>
|
||||
message: It seems like there was a typo or mistake in your request. Could you please clarify or provide the correct numbers you would like to multiply?
|
||||
</request_more_information>
|
||||
|
||||
<human_response>
|
||||
lets try 12 instead
|
||||
</human_response>
|
||||
"#
|
||||
}
|
||||
@@assert(intent, {{this.intent == "multiply"}})
|
||||
@@assert(b, {{this.a == 3}})
|
||||
@@assert(a, {{this.b == 12}})
|
||||
}
|
||||
|
||||
28
workshops/2025-05-17/walkthrough/08-server.ts
Normal file
28
workshops/2025-05-17/walkthrough/08-server.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import express from 'express';
|
||||
import { Thread, agentLoop } from '../src/agent';
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
// POST /thread - Start new thread
|
||||
app.post('/thread', async (req, res) => {
|
||||
const thread = new Thread([{
|
||||
type: "user_input",
|
||||
data: req.body.message
|
||||
}]);
|
||||
const result = await agentLoop(thread);
|
||||
res.json(result);
|
||||
});
|
||||
|
||||
// GET /thread/:id - Get thread status
|
||||
app.get('/thread/:id', (req, res) => {
|
||||
// optional - add state
|
||||
res.status(404).json({ error: "Not implemented yet" });
|
||||
});
|
||||
|
||||
const port = process.env.PORT || 3000;
|
||||
app.listen(port, () => {
|
||||
console.log(`Server running on port ${port}`);
|
||||
});
|
||||
|
||||
export { app };
|
||||
71
workshops/2025-05-17/walkthrough/09-server.ts
Normal file
71
workshops/2025-05-17/walkthrough/09-server.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import express from 'express';
|
||||
import { Thread, agentLoop } from '../src/agent';
|
||||
import { ThreadStore } from '../src/state';
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
const store = new ThreadStore();
|
||||
|
||||
// POST /thread - Start new thread
|
||||
app.post('/thread', async (req, res) => {
|
||||
const thread = new Thread([{
|
||||
type: "user_input",
|
||||
data: req.body.message
|
||||
}]);
|
||||
|
||||
const threadId = store.create(thread);
|
||||
const result = await agentLoop(thread);
|
||||
|
||||
// If clarification is needed, include the response URL
|
||||
const lastEvent = result.events[result.events.length - 1];
|
||||
if (lastEvent.data.intent === 'request_more_information') {
|
||||
lastEvent.data.response_url = `/thread/${threadId}/response`;
|
||||
}
|
||||
|
||||
store.update(threadId, result);
|
||||
res.json({
|
||||
thread_id: threadId,
|
||||
...result
|
||||
});
|
||||
});
|
||||
|
||||
// GET /thread/:id - Get thread status
|
||||
app.get('/thread/:id', (req, res) => {
|
||||
const thread = store.get(req.params.id);
|
||||
if (!thread) {
|
||||
return res.status(404).json({ error: "Thread not found" });
|
||||
}
|
||||
res.json(thread);
|
||||
});
|
||||
|
||||
// POST /thread/:id/response - Handle clarification response
|
||||
app.post('/thread/:id/response', async (req, res) => {
|
||||
const thread = store.get(req.params.id);
|
||||
if (!thread) {
|
||||
return res.status(404).json({ error: "Thread not found" });
|
||||
}
|
||||
|
||||
thread.events.push({
|
||||
type: "human_response",
|
||||
data: req.body.message
|
||||
});
|
||||
|
||||
const result = await agentLoop(thread);
|
||||
|
||||
// If another clarification is needed, include the response URL
|
||||
const lastEvent = result.events[result.events.length - 1];
|
||||
if (lastEvent.data.intent === 'request_more_information') {
|
||||
lastEvent.data.response_url = `/thread/${req.params.id}/response`;
|
||||
}
|
||||
|
||||
store.update(req.params.id, result);
|
||||
res.json(result);
|
||||
});
|
||||
|
||||
const port = process.env.PORT || 3000;
|
||||
app.listen(port, () => {
|
||||
console.log(`Server running on port ${port}`);
|
||||
});
|
||||
|
||||
export { app };
|
||||
23
workshops/2025-05-17/walkthrough/09-state.ts
Normal file
23
workshops/2025-05-17/walkthrough/09-state.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import crypto from 'crypto';
|
||||
import { Thread } from '../src/agent';
|
||||
|
||||
|
||||
// you can replace this with any simple state management,
|
||||
// e.g. redis, sqlite, postgres, etc
|
||||
export class ThreadStore {
|
||||
private threads: Map<string, Thread> = new Map();
|
||||
|
||||
create(thread: Thread): string {
|
||||
const id = crypto.randomUUID();
|
||||
this.threads.set(id, thread);
|
||||
return id;
|
||||
}
|
||||
|
||||
get(id: string): Thread | undefined {
|
||||
return this.threads.get(id);
|
||||
}
|
||||
|
||||
update(id: string, thread: Thread): void {
|
||||
this.threads.set(id, thread);
|
||||
}
|
||||
}
|
||||
111
workshops/2025-05-17/walkthrough/10-agent.ts
Normal file
111
workshops/2025-05-17/walkthrough/10-agent.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { AddTool, SubtractTool, DivideTool, MultiplyTool, b } from "../baml_client";
|
||||
|
||||
export interface Event {
|
||||
type: string
|
||||
data: any;
|
||||
}
|
||||
|
||||
export class Thread {
|
||||
events: Event[] = [];
|
||||
|
||||
constructor(events: Event[]) {
|
||||
this.events = events;
|
||||
}
|
||||
|
||||
serializeForLLM() {
|
||||
return this.events.map(e => this.serializeOneEvent(e)).join("\n");
|
||||
}
|
||||
|
||||
trimLeadingWhitespace(s: string) {
|
||||
return s.replace(/^[ \t]+/gm, '');
|
||||
}
|
||||
|
||||
serializeOneEvent(e: Event) {
|
||||
return this.trimLeadingWhitespace(`
|
||||
<${e.data?.intent || e.type}>
|
||||
${
|
||||
typeof e.data !== 'object' ? e.data :
|
||||
Object.keys(e.data).filter(k => k !== 'intent').map(k => `${k}: ${e.data[k]}`).join("\n")}
|
||||
</${e.data?.intent || e.type}>
|
||||
`)
|
||||
}
|
||||
|
||||
awaitingHumanResponse(): boolean {
|
||||
const lastEvent = this.events[this.events.length - 1];
|
||||
return ['request_more_information', 'done_for_now'].includes(lastEvent.data.intent);
|
||||
}
|
||||
|
||||
awaitingHumanApproval(): boolean {
|
||||
const lastEvent = this.events[this.events.length - 1];
|
||||
return lastEvent.data.intent === 'divide';
|
||||
}
|
||||
}
|
||||
|
||||
export type CalculatorTool = AddTool | SubtractTool | MultiplyTool | DivideTool;
|
||||
|
||||
export async function handleNextStep(nextStep: CalculatorTool, thread: Thread): Promise<Thread> {
|
||||
let result: number;
|
||||
switch (nextStep.intent) {
|
||||
case "add":
|
||||
result = nextStep.a + nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "subtract":
|
||||
result = nextStep.a - nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "multiply":
|
||||
result = nextStep.a * nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "divide":
|
||||
result = nextStep.a / nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
}
|
||||
}
|
||||
|
||||
export async function agentLoop(thread: Thread): Promise<Thread> {
|
||||
|
||||
while (true) {
|
||||
const nextStep = await b.DetermineNextStep(thread.serializeForLLM());
|
||||
console.log("nextStep", nextStep);
|
||||
|
||||
thread.events.push({
|
||||
"type": "tool_call",
|
||||
"data": nextStep
|
||||
});
|
||||
|
||||
switch (nextStep.intent) {
|
||||
case "done_for_now":
|
||||
case "request_more_information":
|
||||
// response to human, return the thread
|
||||
return thread;
|
||||
case "divide":
|
||||
// divide is scary, return it for human approval
|
||||
return thread;
|
||||
case "add":
|
||||
case "subtract":
|
||||
case "multiply":
|
||||
thread = await handleNextStep(nextStep, thread);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
111
workshops/2025-05-17/walkthrough/10-server.ts
Normal file
111
workshops/2025-05-17/walkthrough/10-server.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import express from 'express';
|
||||
import { Thread, agentLoop, handleNextStep } from '../src/agent';
|
||||
import { ThreadStore } from '../src/state';
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
const store = new ThreadStore();
|
||||
|
||||
// POST /thread - Start new thread
|
||||
app.post('/thread', async (req, res) => {
|
||||
const thread = new Thread([{
|
||||
type: "user_input",
|
||||
data: req.body.message
|
||||
}]);
|
||||
|
||||
const threadId = store.create(thread);
|
||||
const newThread = await agentLoop(thread);
|
||||
|
||||
store.update(threadId, newThread);
|
||||
|
||||
const lastEvent = newThread.events[newThread.events.length - 1];
|
||||
// If we exited the loop, include the response URL so the client can
|
||||
// push a new message onto the thread
|
||||
lastEvent.data.response_url = `/thread/${threadId}/response`;
|
||||
|
||||
console.log("returning last event from endpoint", lastEvent);
|
||||
|
||||
res.json({
|
||||
thread_id: threadId,
|
||||
...newThread
|
||||
});
|
||||
});
|
||||
|
||||
// GET /thread/:id - Get thread status
|
||||
app.get('/thread/:id', (req, res) => {
|
||||
const thread = store.get(req.params.id);
|
||||
if (!thread) {
|
||||
return res.status(404).json({ error: "Thread not found" });
|
||||
}
|
||||
res.json(thread);
|
||||
});
|
||||
|
||||
|
||||
type ApprovalPayload = {
|
||||
type: "approval";
|
||||
approved: boolean;
|
||||
comment?: string;
|
||||
}
|
||||
|
||||
type ResponsePayload = {
|
||||
type: "response";
|
||||
response: string;
|
||||
}
|
||||
|
||||
type Payload = ApprovalPayload | ResponsePayload;
|
||||
|
||||
// POST /thread/:id/response - Handle clarification response
|
||||
app.post('/thread/:id/response', async (req, res) => {
|
||||
let thread = store.get(req.params.id);
|
||||
if (!thread) {
|
||||
return res.status(404).json({ error: "Thread not found" });
|
||||
}
|
||||
|
||||
const body: Payload = req.body;
|
||||
|
||||
let lastEvent = thread.events[thread.events.length - 1];
|
||||
|
||||
if (thread.awaitingHumanResponse() && body.type === 'response') {
|
||||
thread.events.push({
|
||||
type: "human_response",
|
||||
data: body.response
|
||||
});
|
||||
} else if (thread.awaitingHumanApproval() && body.type === 'approval' && !body.approved) {
|
||||
// push feedback onto the thread
|
||||
thread.events.push({
|
||||
type: "tool_response",
|
||||
data: `user denied the operation with feedback: "${body.comment}"`
|
||||
});
|
||||
} else if (thread.awaitingHumanApproval() && body.type === 'approval' && body.approved) {
|
||||
// approved, run the tool, pushing results onto the thread
|
||||
await handleNextStep(lastEvent.data, thread);
|
||||
} else {
|
||||
res.status(400).json({
|
||||
error: "Invalid request: " + body.type,
|
||||
awaitingHumanResponse: thread.awaitingHumanResponse(),
|
||||
awaitingHumanApproval: thread.awaitingHumanApproval()
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// loop until stop event
|
||||
const result = await agentLoop(thread);
|
||||
|
||||
store.update(req.params.id, result);
|
||||
|
||||
lastEvent = result.events[result.events.length - 1];
|
||||
lastEvent.data.response_url = `/thread/${req.params.id}/response`;
|
||||
|
||||
console.log("returning last event from endpoint", lastEvent);
|
||||
|
||||
res.json(result);
|
||||
});
|
||||
|
||||
const port = process.env.PORT || 3000;
|
||||
app.listen(port, () => {
|
||||
console.log(`Server running on port ${port}`);
|
||||
});
|
||||
|
||||
export { app };
|
||||
111
workshops/2025-05-17/walkthrough/11-agent.ts
Normal file
111
workshops/2025-05-17/walkthrough/11-agent.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { AddTool, SubtractTool, DivideTool, MultiplyTool, b } from "../baml_client";
|
||||
|
||||
export interface Event {
|
||||
type: string
|
||||
data: any;
|
||||
}
|
||||
|
||||
export class Thread {
|
||||
events: Event[] = [];
|
||||
|
||||
constructor(events: Event[]) {
|
||||
this.events = events;
|
||||
}
|
||||
|
||||
serializeForLLM() {
|
||||
return this.events.map(e => this.serializeOneEvent(e)).join("\n");
|
||||
}
|
||||
|
||||
trimLeadingWhitespace(s: string) {
|
||||
return s.replace(/^[ \t]+/gm, '');
|
||||
}
|
||||
|
||||
serializeOneEvent(e: Event) {
|
||||
return this.trimLeadingWhitespace(`
|
||||
<${e.data?.intent || e.type}>
|
||||
${
|
||||
typeof e.data !== 'object' ? e.data :
|
||||
Object.keys(e.data).filter(k => k !== 'intent').map(k => `${k}: ${e.data[k]}`).join("\n")}
|
||||
</${e.data?.intent || e.type}>
|
||||
`)
|
||||
}
|
||||
|
||||
awaitingHumanResponse(): boolean {
|
||||
const lastEvent = this.events[this.events.length - 1];
|
||||
return ['request_more_information', 'done_for_now'].includes(lastEvent.data.intent);
|
||||
}
|
||||
|
||||
awaitingHumanApproval(): boolean {
|
||||
const lastEvent = this.events[this.events.length - 1];
|
||||
return lastEvent.data.intent === 'divide';
|
||||
}
|
||||
}
|
||||
|
||||
export type CalculatorTool = AddTool | SubtractTool | MultiplyTool | DivideTool;
|
||||
|
||||
export async function handleNextStep(nextStep: CalculatorTool, thread: Thread): Promise<Thread> {
|
||||
let result: number;
|
||||
switch (nextStep.intent) {
|
||||
case "add":
|
||||
result = nextStep.a + nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "subtract":
|
||||
result = nextStep.a - nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "multiply":
|
||||
result = nextStep.a * nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
case "divide":
|
||||
result = nextStep.a / nextStep.b;
|
||||
console.log("tool_response", result);
|
||||
thread.events.push({
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
});
|
||||
return thread;
|
||||
}
|
||||
}
|
||||
|
||||
export async function agentLoop(thread: Thread): Promise<Thread> {
|
||||
|
||||
while (true) {
|
||||
const nextStep = await b.DetermineNextStep(thread.serializeForLLM());
|
||||
console.log("nextStep", nextStep);
|
||||
|
||||
thread.events.push({
|
||||
"type": "tool_call",
|
||||
"data": nextStep
|
||||
});
|
||||
|
||||
switch (nextStep.intent) {
|
||||
case "done_for_now":
|
||||
case "request_more_information":
|
||||
// response to human, return the next step object
|
||||
return thread;
|
||||
case "divide":
|
||||
// divide is scary, return it for human approval
|
||||
return thread;
|
||||
case "add":
|
||||
case "subtract":
|
||||
case "multiply":
|
||||
thread = await handleNextStep(nextStep, thread);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
120
workshops/2025-05-17/walkthrough/11-cli.ts
Normal file
120
workshops/2025-05-17/walkthrough/11-cli.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
// cli.ts lets you invoke the agent loop from the command line
|
||||
|
||||
import { humanlayer } from "humanlayer";
|
||||
import { agentLoop, Thread, Event } from "../src/agent";
|
||||
|
||||
export async function cli() {
|
||||
// Get command line arguments, skipping the first two (node and script name)
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
if (args.length === 0) {
|
||||
console.error("Error: Please provide a message as a command line argument");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Join all arguments into a single message
|
||||
const message = args.join(" ");
|
||||
|
||||
// Create a new thread with the user's message as the initial event
|
||||
const thread = new Thread([{ type: "user_input", data: message }]);
|
||||
|
||||
// Run the agent loop with the thread
|
||||
let newThread = await agentLoop(thread);
|
||||
let lastEvent = newThread.events.slice(-1)[0];
|
||||
|
||||
let needsResponse =
|
||||
newThread.awaitingHumanResponse() ||
|
||||
newThread.awaitingHumanApproval();
|
||||
|
||||
while (needsResponse) {
|
||||
lastEvent = newThread.events.slice(-1)[0];
|
||||
const responseEvent = await askHuman(lastEvent);
|
||||
thread.events.push(responseEvent);
|
||||
newThread = await agentLoop(thread);
|
||||
// determine if we should loop or if we're done
|
||||
needsResponse = newThread.awaitingHumanResponse()
|
||||
|| newThread.awaitingHumanApproval();
|
||||
}
|
||||
|
||||
// print the final result
|
||||
// optional - you could loop here too
|
||||
console.log(lastEvent.data.message);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
async function askHuman(lastEvent: Event): Promise<Event> {
|
||||
if (process.env.HUMANLAYER_API_KEY) {
|
||||
return await askHumanEmail(lastEvent);
|
||||
} else {
|
||||
return await askHumanCLI(lastEvent.data.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function askHumanCLI(message: string): Promise<Event> {
|
||||
const readline = require('readline').createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
return new Promise((resolve) => {
|
||||
readline.question(`${message}\n> `, (answer: string) => {
|
||||
resolve({ type: "human_response", data: answer });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function askHumanEmail(lastEvent: Event): Promise<Event> {
|
||||
if (!process.env.HUMANLAYER_EMAIL) {
|
||||
throw new Error("missing or invalid parameters: HUMANLAYER_EMAIL");
|
||||
}
|
||||
const hl = humanlayer({ //reads apiKey from env
|
||||
// name of this agent
|
||||
runId: "cli-agent",
|
||||
contactChannel: {
|
||||
// agent should request permission via email
|
||||
email: {
|
||||
address: process.env.HUMANLAYER_EMAIL,
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (lastEvent.data.intent === "request_more_information") {
|
||||
const response = await hl.fetchHumanResponse({
|
||||
spec: {
|
||||
msg: lastEvent.data.message
|
||||
}
|
||||
})
|
||||
return {
|
||||
"type": "tool_response",
|
||||
"data": response
|
||||
}
|
||||
}
|
||||
|
||||
if (lastEvent.data.intent === "divide") {
|
||||
// fetch approval synchronously
|
||||
const response = await hl.fetchHumanApproval({
|
||||
spec: {
|
||||
fn: "divide",
|
||||
kwargs: {
|
||||
a: lastEvent.data.a,
|
||||
b: lastEvent.data.b
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (response.approved) {
|
||||
const result = lastEvent.data.a / lastEvent.data.b;
|
||||
console.log("tool_response", result);
|
||||
return {
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
"type": "tool_response",
|
||||
"data": `user denied operation ${lastEvent.data.intent}`
|
||||
};
|
||||
}
|
||||
}
|
||||
throw new Error(`unknown tool: ${lastEvent.data.intent}`)
|
||||
}
|
||||
BIN
workshops/2025-05-17/walkthrough/11-email-reject.png
Normal file
BIN
workshops/2025-05-17/walkthrough/11-email-reject.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 162 KiB |
128
workshops/2025-05-17/walkthrough/11b-cli.ts
Normal file
128
workshops/2025-05-17/walkthrough/11b-cli.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
// cli.ts lets you invoke the agent loop from the command line
|
||||
|
||||
import { humanlayer } from "humanlayer";
|
||||
import { agentLoop, Thread, Event } from "../src/agent";
|
||||
|
||||
export async function cli() {
|
||||
// Get command line arguments, skipping the first two (node and script name)
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
if (args.length === 0) {
|
||||
console.error("Error: Please provide a message as a command line argument");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Join all arguments into a single message
|
||||
const message = args.join(" ");
|
||||
|
||||
// Create a new thread with the user's message as the initial event
|
||||
const thread = new Thread([{ type: "user_input", data: message }]);
|
||||
|
||||
// Run the agent loop with the thread
|
||||
let newThread = await agentLoop(thread);
|
||||
let lastEvent = newThread.events.slice(-1)[0];
|
||||
|
||||
let needsResponse =
|
||||
newThread.awaitingHumanResponse() ||
|
||||
newThread.awaitingHumanApproval();
|
||||
|
||||
while (needsResponse) {
|
||||
lastEvent = newThread.events.slice(-1)[0];
|
||||
const responseEvent = await askHuman(lastEvent);
|
||||
thread.events.push(responseEvent);
|
||||
newThread = await agentLoop(thread);
|
||||
// determine if we should loop or if we're done
|
||||
needsResponse = newThread.awaitingHumanResponse()
|
||||
|| newThread.awaitingHumanApproval();
|
||||
}
|
||||
|
||||
// print the final result
|
||||
// optional - you could loop here too
|
||||
console.log(lastEvent.data.message);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
async function askHuman(lastEvent: Event): Promise<Event> {
|
||||
if (process.env.HUMANLAYER_API_KEY) {
|
||||
return await askHumanEmail(lastEvent);
|
||||
} else {
|
||||
return await askHumanCLI(lastEvent.data.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function askHumanCLI(message: string): Promise<Event> {
|
||||
const readline = require('readline').createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
return new Promise((resolve) => {
|
||||
readline.question(`${message}\n> `, (answer: string) => {
|
||||
resolve({ type: "human_response", data: answer });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function askHumanEmail(lastEvent: Event): Promise<Event> {
|
||||
if (!process.env.HUMANLAYER_EMAIL) {
|
||||
throw new Error("missing or invalid parameters: HUMANLAYER_EMAIL");
|
||||
}
|
||||
const hl = humanlayer({ //reads apiKey from env
|
||||
// name of this agent
|
||||
runId: "cli-agent",
|
||||
contactChannel: {
|
||||
// agent should request permission via email
|
||||
email: {
|
||||
address: process.env.HUMANLAYER_EMAIL,
|
||||
// custom email body - jinja
|
||||
template: `
|
||||
agent {{ event.run_id }} is requesting approval for {{event.spec.fn}}
|
||||
with args: {{event.spec.kwargs}}
|
||||
<br><br>
|
||||
reply to this email to approve
|
||||
`
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (lastEvent.data.intent === "request_more_information") {
|
||||
const response = await hl.fetchHumanResponse({
|
||||
spec: {
|
||||
msg: lastEvent.data.message
|
||||
}
|
||||
})
|
||||
return {
|
||||
"type": "tool_response",
|
||||
"data": response
|
||||
}
|
||||
}
|
||||
|
||||
if (lastEvent.data.intent === "divide") {
|
||||
// fetch approval synchronously
|
||||
const response = await hl.fetchHumanApproval({
|
||||
spec: {
|
||||
fn: "divide",
|
||||
kwargs: {
|
||||
a: lastEvent.data.a,
|
||||
b: lastEvent.data.b
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (response.approved) {
|
||||
const result = lastEvent.data.a / lastEvent.data.b;
|
||||
console.log("tool_response", result);
|
||||
return {
|
||||
"type": "tool_response",
|
||||
"data": result
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
"type": "tool_response",
|
||||
"data": `user denied operation ${lastEvent.data.intent}
|
||||
with feedback: ${response.comment}`
|
||||
};
|
||||
}
|
||||
}
|
||||
throw new Error(`unknown tool: ${lastEvent.data.intent}`)
|
||||
}
|
||||
140
workshops/2025-05-17/walkthrough/12-server.ts
Normal file
140
workshops/2025-05-17/walkthrough/12-server.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import express from 'express';
|
||||
import { Thread, agentLoop, handleNextStep } from '../src/agent';
|
||||
import { ThreadStore } from '../src/state';
|
||||
import { V1Beta2EmailEventReceived } from 'humanlayer';
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
const store = new ThreadStore();
|
||||
|
||||
// POST /thread - Start new thread
|
||||
app.post('/thread', async (req, res) => {
|
||||
const thread = new Thread([{
|
||||
type: "user_input",
|
||||
data: req.body.message
|
||||
}]);
|
||||
|
||||
const threadId = store.create(thread);
|
||||
const newThread = await agentLoop(thread);
|
||||
|
||||
store.update(threadId, newThread);
|
||||
|
||||
const lastEvent = newThread.events[newThread.events.length - 1];
|
||||
// If we exited the loop, include the response URL so the client can
|
||||
// push a new message onto the thread
|
||||
lastEvent.data.response_url = `/thread/${threadId}/response`;
|
||||
|
||||
res.json({
|
||||
thread_id: threadId,
|
||||
...newThread
|
||||
});
|
||||
});
|
||||
|
||||
// GET /thread/:id - Get thread status
|
||||
app.get('/thread/:id', (req, res) => {
|
||||
const thread = store.get(req.params.id);
|
||||
if (!thread) {
|
||||
return res.status(404).json({ error: "Thread not found" });
|
||||
}
|
||||
res.json(thread);
|
||||
});
|
||||
|
||||
|
||||
type ApprovalPayload = {
|
||||
type: "approval";
|
||||
approved: boolean;
|
||||
comment?: string;
|
||||
}
|
||||
|
||||
type ResponsePayload = {
|
||||
type: "response";
|
||||
response: string;
|
||||
}
|
||||
|
||||
type Payload = ApprovalPayload | ResponsePayload;
|
||||
|
||||
// POST /thread/:id/response - Handle clarification response
|
||||
app.post('/thread/:id/response', async (req, res) => {
|
||||
const thread = store.get(req.params.id);
|
||||
if (!thread) {
|
||||
return res.status(404).json({ error: "Thread not found" });
|
||||
}
|
||||
|
||||
const body: Payload = req.body;
|
||||
|
||||
let lastEvent = thread.events[thread.events.length - 1];
|
||||
|
||||
if (thread.awaitingHumanResponse() && body.type === 'response') {
|
||||
thread.events.push({
|
||||
type: "human_response",
|
||||
data: body.response
|
||||
});
|
||||
} else if (thread.awaitingHumanApproval() && body.type === 'approval' && !body.approved) {
|
||||
// push feedback onto the thread
|
||||
thread.events.push({
|
||||
type: "tool_response",
|
||||
data: `user denied the operation with feedback: "${body.comment}"`
|
||||
});
|
||||
} else if (thread.awaitingHumanApproval() && body.type === 'approval' && !body.approved) {
|
||||
// approved, run the tool, pushing results onto the thread
|
||||
await handleNextStep(lastEvent, thread);
|
||||
} else {
|
||||
res.status(400).json({
|
||||
error: "Invalid request: " + body.type,
|
||||
awaitingHumanResponse: thread.awaitingHumanResponse(),
|
||||
awaitingHumanApproval: thread.awaitingHumanApproval()
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// loop until stop event
|
||||
const result = await agentLoop(thread);
|
||||
|
||||
store.update(req.params.id, result);
|
||||
|
||||
lastEvent = result.events[result.events.length - 1];
|
||||
lastEvent.data.response_url = `/thread/${req.params.id}/response`;
|
||||
|
||||
res.json(result);
|
||||
});
|
||||
|
||||
|
||||
app.post('/webhook', async (req, res) => {
|
||||
//todo verify webhook
|
||||
const payload: V1Beta2EmailEventReceived = req.body
|
||||
|
||||
const { subject, body, to_address, from_address} = payload.event;
|
||||
|
||||
const thread = new Thread([{
|
||||
type: "user_input",
|
||||
data: {
|
||||
subject,
|
||||
body,
|
||||
to_address,
|
||||
from_address,
|
||||
}
|
||||
}]);
|
||||
|
||||
const threadId = store.create(thread);
|
||||
const newThread = await agentLoop(thread);
|
||||
|
||||
store.update(threadId, newThread);
|
||||
|
||||
const lastEvent = newThread.events[newThread.events.length - 1];
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// don't return any content, we sent the next step to a human
|
||||
res.json({ status: "ok" });
|
||||
})
|
||||
|
||||
const port = process.env.PORT || 3000;
|
||||
app.listen(port, () => {
|
||||
console.log(`Server running on port ${port}`);
|
||||
});
|
||||
|
||||
export { app };
|
||||
Reference in New Issue
Block a user