02-custom-model.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Custom Model Selection
  3. *
  4. * Shows how to select a specific model and thinking level.
  5. */
  6. import { getModel } from "@earendil-works/pi-ai";
  7. import { AuthStorage, createAgentSession, ModelRegistry } from "@earendil-works/pi-coding-agent";
  8. // Set up auth storage and model registry
  9. const authStorage = AuthStorage.create();
  10. const modelRegistry = ModelRegistry.create(authStorage);
  11. // Option 1: Find a specific built-in model by provider/id
  12. const opus = getModel("anthropic", "claude-opus-4-5");
  13. if (opus) {
  14. console.log(`Found model: ${opus.provider}/${opus.id}`);
  15. }
  16. // Option 2: Find model via registry (includes custom models from models.json)
  17. const customModel = modelRegistry.find("my-provider", "my-model");
  18. if (customModel) {
  19. console.log(`Found custom model: ${customModel.provider}/${customModel.id}`);
  20. }
  21. // Option 3: Pick from available models (have valid API keys)
  22. const available = await modelRegistry.getAvailable();
  23. console.log(
  24. "Available models:",
  25. available.map((m) => `${m.provider}/${m.id}`),
  26. );
  27. if (available.length > 0) {
  28. const { session } = await createAgentSession({
  29. model: available[0],
  30. thinkingLevel: "medium", // off, low, medium, high
  31. authStorage,
  32. modelRegistry,
  33. });
  34. try {
  35. session.subscribe((event) => {
  36. if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
  37. process.stdout.write(event.assistantMessageEvent.delta);
  38. }
  39. });
  40. await session.prompt("Say hello in one sentence.");
  41. console.log();
  42. } finally {
  43. session.dispose();
  44. }
  45. }