tool.types.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Effect, Schema } from "effect"
  2. import { LLM, LLMRequest, ToolRuntime, toDefinitions } from "../src"
  3. import * as OpenAIChat from "../src/protocols/openai-chat"
  4. import { Auth } from "../src/route"
  5. import { Tool } from "../src/tool"
  6. const request = LLM.request({
  7. model: OpenAIChat.route.with({ auth: Auth.bearer("fixture") }).model({ id: "gpt-4o-mini" }),
  8. prompt: "Use the tool.",
  9. })
  10. const executable = Tool.make({
  11. description: "Get weather.",
  12. parameters: Schema.Struct({ city: Schema.String }),
  13. success: Schema.Struct({ forecast: Schema.String }),
  14. execute: (input) => Effect.succeed({ forecast: input.city }),
  15. })
  16. const schemaOnly = Tool.make({
  17. description: "Get weather.",
  18. parameters: Schema.Struct({ city: Schema.String }),
  19. success: Schema.Struct({ forecast: Schema.String }),
  20. })
  21. Tool.make({
  22. description: "Encode success before projection.",
  23. parameters: Schema.Struct({ city: Schema.String }),
  24. success: Schema.Struct({ forecast: Schema.NumberFromString }),
  25. execute: () => Effect.succeed({ forecast: 1 }),
  26. toModelOutput: ({ id, parameters, output }) => [
  27. { type: "text", text: `${id}:${parameters.city}:${output.forecast}` },
  28. ],
  29. })
  30. LLM.stream(request)
  31. LLM.generate(LLMRequest.update(request, { tools: toDefinitions({ schemaOnly }) }))
  32. ToolRuntime.dispatch({ executable }, { type: "tool-call", id: "call_1", name: "executable", input: { city: "Paris" } })
  33. // @ts-expect-error High-level tool orchestration overloads are intentionally not supported.
  34. LLM.stream({ request, tools: { schemaOnly } })