| 1 | import { LanguageModel } from "@effect/ai" |
| 2 | import { OpenRouterClient, OpenRouterLanguageModel } from "@effect/ai-openrouter" |
| 3 | import { FetchHttpClient } from "@effect/platform" |
| 4 | import { Config, Effect, Layer, Stream } from "effect" |
| 5 | |
| 6 | const Gpt4o = OpenRouterLanguageModel.model("openai/gpt-4o") |
| 7 | |
| 8 | const program = LanguageModel.streamText({ |
| 9 | prompt: [ |
| 10 | { role: "system", content: "You are a comedian with a penchant for groan-inducing puns" }, |
| 11 | { role: "user", content: [{ type: "text", text: "Tell me a dad joke" }] } |
| 12 | ] |
| 13 | }).pipe( |
| 14 | Stream.filter((part) => part.type === "text-delta"), |
| 15 | Stream.runForEach((part) => Effect.sync(() => process.stdout.write(part.delta))), |
| 16 | Effect.provide(Gpt4o) |
| 17 | ) |
| 18 | |
| 19 | const OpenRouter = OpenRouterClient.layerConfig({ |
| 20 | apiKey: Config.redacted("OPENROUTER_API_KEY") |
| 21 | }).pipe(Layer.provide(FetchHttpClient.layer)) |
| 22 | |
| 23 | program.pipe( |
| 24 | Effect.provide(OpenRouter), |
| 25 | Effect.runPromise |
| 26 | ) |