google-thinking-disable.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { describe, expect, it } from "vitest";
  2. import { getModel } from "../src/models.ts";
  3. import { streamSimple } from "../src/stream.ts";
  4. import type { Api, Context, Model, SimpleStreamOptions } from "../src/types.ts";
  5. type SimpleOptionsWithExtras = SimpleStreamOptions & Record<string, unknown>;
  6. interface RunResult {
  7. thinkingEventCount: number;
  8. thinkingCharCount: number;
  9. text: string;
  10. outputTokens: number;
  11. contentTypes: string[];
  12. }
  13. interface DisableExpectations {
  14. requestOptions?: SimpleOptionsWithExtras;
  15. minPongs?: number;
  16. maxOutputTokens?: number;
  17. }
  18. function makeContext(): Context {
  19. return {
  20. systemPrompt: "You are a precise assistant. Follow the requested output format exactly.",
  21. messages: [
  22. {
  23. role: "user",
  24. content:
  25. "Before replying, carefully solve 36863 * 5279 internally. Then reply with the word pong repeated exactly 40 times, separated by single spaces. Do not add any other text.",
  26. timestamp: Date.now(),
  27. },
  28. ],
  29. };
  30. }
  31. function countPongs(text: string): number {
  32. return text.match(/\bpong\b/gi)?.length ?? 0;
  33. }
  34. async function runWithoutReasoning<TApi extends Api>(
  35. model: Model<TApi>,
  36. options: SimpleOptionsWithExtras = {},
  37. ): Promise<RunResult> {
  38. const s = streamSimple(model, makeContext(), {
  39. maxTokens: 160,
  40. temperature: 0,
  41. ...options,
  42. });
  43. let thinkingEventCount = 0;
  44. let thinkingCharCount = 0;
  45. for await (const event of s) {
  46. if (event.type === "thinking_start" || event.type === "thinking_end") {
  47. thinkingEventCount += 1;
  48. }
  49. if (event.type === "thinking_delta") {
  50. thinkingEventCount += 1;
  51. thinkingCharCount += event.delta.length;
  52. }
  53. }
  54. const response = await s.result();
  55. expect(response.stopReason, response.errorMessage).toBe("stop");
  56. const text = response.content
  57. .filter((block) => block.type === "text")
  58. .map((block) => block.text)
  59. .join("")
  60. .trim();
  61. return {
  62. thinkingEventCount,
  63. thinkingCharCount,
  64. text,
  65. outputTokens: response.usage.output,
  66. contentTypes: response.content.map((block) => block.type),
  67. };
  68. }
  69. async function expectThinkingDisabledE2E<TApi extends Api>(model: Model<TApi>, expectations: DisableExpectations = {}) {
  70. const result = await runWithoutReasoning(model, expectations.requestOptions);
  71. expect(result.thinkingEventCount).toBe(0);
  72. expect(result.thinkingCharCount).toBe(0);
  73. expect(result.contentTypes).not.toContain("thinking");
  74. expect(countPongs(result.text)).toBeGreaterThanOrEqual(expectations.minPongs ?? 35);
  75. if (expectations.maxOutputTokens !== undefined) {
  76. expect(result.outputTokens).toBeLessThan(expectations.maxOutputTokens);
  77. }
  78. }
  79. describe.skipIf(!process.env.ANTHROPIC_API_KEY)("Anthropic thinking disable E2E", () => {
  80. it("disables thinking for budget-based reasoning models", { retry: 2, timeout: 30000 }, async () => {
  81. await expectThinkingDisabledE2E(getModel("anthropic", "claude-sonnet-4-5"), {
  82. requestOptions: { maxTokens: 320, temperature: 0 },
  83. });
  84. });
  85. it("disables thinking for adaptive reasoning models", { retry: 2, timeout: 30000 }, async () => {
  86. await expectThinkingDisabledE2E(getModel("anthropic", "claude-sonnet-4-6"), {
  87. requestOptions: { maxTokens: 320, temperature: 0 },
  88. });
  89. });
  90. });
  91. describe.skipIf(!process.env.GEMINI_API_KEY)("Google thinking disable E2E", () => {
  92. it("disables thinking for Gemini 2.5", { retry: 2, timeout: 30000 }, async () => {
  93. await expectThinkingDisabledE2E(getModel("google", "gemini-2.5-flash"));
  94. });
  95. it("disables thinking for Gemini 3.x", { retry: 2, timeout: 30000 }, async () => {
  96. await expectThinkingDisabledE2E(getModel("google", "gemini-3-flash-preview"));
  97. });
  98. it("does not error when thinking is off for Gemini 3.1 Pro", { retry: 2, timeout: 30000 }, async () => {
  99. await expectThinkingDisabledE2E(getModel("google", "gemini-3.1-pro-preview"), {
  100. requestOptions: { maxTokens: 512 },
  101. minPongs: 20,
  102. });
  103. });
  104. });
  105. describe("Google Vertex thinking disable E2E", () => {
  106. const vertexProject = process.env.GOOGLE_CLOUD_PROJECT || process.env.GCLOUD_PROJECT;
  107. const vertexLocation = process.env.GOOGLE_CLOUD_LOCATION;
  108. const vertexApiKey = process.env.GOOGLE_CLOUD_API_KEY;
  109. const vertexOptions = vertexApiKey
  110. ? ({ apiKey: vertexApiKey } satisfies SimpleOptionsWithExtras)
  111. : vertexProject && vertexLocation
  112. ? ({ project: vertexProject, location: vertexLocation } satisfies SimpleOptionsWithExtras)
  113. : undefined;
  114. it.skipIf(!vertexOptions)("disables thinking for Gemini 2.5", { retry: 2, timeout: 30000 }, async () => {
  115. await expectThinkingDisabledE2E(getModel("google-vertex", "gemini-2.5-flash"), {
  116. requestOptions: vertexOptions,
  117. });
  118. });
  119. it.skipIf(!vertexOptions)("disables thinking for Gemini 3.x", { retry: 2, timeout: 30000 }, async () => {
  120. await expectThinkingDisabledE2E(getModel("google-vertex", "gemini-3-flash-preview"), {
  121. requestOptions: vertexOptions,
  122. });
  123. });
  124. });
  125. describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI thinking disable E2E", () => {
  126. it("disables thinking for Responses reasoning models", { retry: 2, timeout: 30000 }, async () => {
  127. await expectThinkingDisabledE2E(getModel("openai", "gpt-5.4-mini"), {
  128. requestOptions: { temperature: undefined },
  129. });
  130. });
  131. });
  132. describe.skipIf(!process.env.OPENROUTER_API_KEY)("OpenRouter thinking disable E2E", () => {
  133. it("disables thinking for Qwen 3.5 reasoning models", { retry: 2, timeout: 30000 }, async () => {
  134. await expectThinkingDisabledE2E(getModel("openrouter", "qwen/qwen3.5-plus-02-15"), {
  135. maxOutputTokens: 100,
  136. });
  137. });
  138. });