This repository was archived by the owner on Dec 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add support for Moonshot AI's Kimi K2 Thinking model #111
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
df290dc
feat: add support for Moonshot AI's Kimi K2 Thinking model
dragos199993 88fb8a2
fix: update Moonshot AI API base URL and enhance chunk streaming func…
dragos199993 9d30e21
Merge branch 'main' into ENG-3998_support-kimi-k2-thinking
dragos199993 1fad140
Merge branch 'main' into ENG-3998_support-kimi-k2-thinking
dragos199993 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,26 @@ | ||
| def stream_chunks(ichunk): | ||
| def stream_chunks(ichunk, max_chunks=5): | ||
| n_chunks = 0 | ||
| all_empty = True | ||
| for chunk in ichunk: | ||
| assert chunk.type == "AIMessageChunk" | ||
| assert isinstance(chunk.content, str) | ||
| all_empty &= len(chunk.content) == 0 | ||
| n_chunks += 1 | ||
| if n_chunks >= 5: | ||
| # If max_chunks is None, consume all chunks | ||
| if max_chunks is not None and n_chunks >= max_chunks: | ||
| break | ||
| assert not all_empty | ||
|
|
||
|
|
||
| async def astream_chunks(async_ichunk): | ||
| async def astream_chunks(async_ichunk, max_chunks=5): | ||
| n_chunks = 0 | ||
| all_empty = True | ||
| async for chunk in async_ichunk: | ||
| assert chunk.type == "AIMessageChunk" | ||
| assert isinstance(chunk.content, str) | ||
| all_empty &= len(chunk.content) == 0 | ||
| n_chunks += 1 | ||
| if n_chunks >= 5: | ||
| # If max_chunks is None, consume all chunks | ||
| if max_chunks is not None and n_chunks >= max_chunks: | ||
| break | ||
| assert not all_empty | ||
151 changes: 151 additions & 0 deletions
151
.../cassettes/test_moonshotai/Test_Moonshotai_LLMs.test_kimi_k2_thinking_response_model.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| interactions: | ||
| - request: | ||
| body: '{"messages": [{"role": "user", "content": "Tell me a joke"}], "llm_providers": | ||
| [{"provider": "moonshotai", "model": "kimi-k2-thinking", "is_custom": false, | ||
| "context_length": null, "input_price": null, "output_price": null, "latency": | ||
| null}], "metric": "accuracy", "max_model_depth": 1, "hash_content": true}' | ||
| headers: | ||
| Accept: | ||
| - '*/*' | ||
| Accept-Encoding: | ||
| - gzip, deflate, zstd | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '307' | ||
| User-Agent: | ||
| - Python-SDK/0.4.6 | ||
| content-type: | ||
| - application/json | ||
| method: POST | ||
| uri: https://staging-api.notdiamond.ai/v2/modelRouter/modelSelect | ||
| response: | ||
| body: | ||
| string: '{"providers":[{"provider":"moonshotai","model":"kimi-k2-thinking"}],"session_id":"a9f29142-49ef-419f-a62b-275a07f67875"}' | ||
| headers: | ||
| CF-RAY: | ||
| - 9a0a3d193801e432-OTP | ||
| Connection: | ||
| - keep-alive | ||
| Content-Type: | ||
| - application/json | ||
| Date: | ||
| - Tue, 18 Nov 2025 20:32:43 GMT | ||
| Server: | ||
| - cloudflare | ||
| Transfer-Encoding: | ||
| - chunked | ||
| alt-svc: | ||
| - h3=":443"; ma=86400 | ||
| cf-cache-status: | ||
| - DYNAMIC | ||
| content-length: | ||
| - '120' | ||
| rndr-id: | ||
| - 7c647cb2-d389-48f2 | ||
| vary: | ||
| - Accept-Encoding | ||
| x-render-origin-server: | ||
| - uvicorn | ||
| x-request-id: | ||
| - 7c647cb2-d389-48f2 | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| - request: | ||
| body: '{"messages":[{"content":"The output should be formatted as a JSON instance | ||
| that conforms to the JSON schema below.\n\nAs an example, for the schema {\"properties\": | ||
| {\"foo\": {\"title\": \"Foo\", \"description\": \"a list of strings\", \"type\": | ||
| \"array\", \"items\": {\"type\": \"string\"}}}, \"required\": [\"foo\"]}\nthe | ||
| object {\"foo\": [\"bar\", \"baz\"]} is a well-formatted instance of the schema. | ||
| The object {\"properties\": {\"foo\": [\"bar\", \"baz\"]}} is not well-formatted.\n\nHere | ||
| is the output schema:\n```\n{\"properties\": {\"setup\": {\"description\": \"question | ||
| to set up a joke\", \"title\": \"Setup\", \"type\": \"string\"}, \"punchline\": | ||
| {\"description\": \"answer to resolve the joke\", \"title\": \"Punchline\", | ||
| \"type\": \"string\"}}, \"required\": [\"setup\", \"punchline\"]}\n```\nTell | ||
| me a joke","role":"user"}],"model":"kimi-k2-thinking","max_completion_tokens":2000,"stream":false}' | ||
| headers: | ||
| accept: | ||
| - application/json | ||
| accept-encoding: | ||
| - gzip, deflate, zstd | ||
| connection: | ||
| - keep-alive | ||
| content-length: | ||
| - '912' | ||
| content-type: | ||
| - application/json | ||
| host: | ||
| - api.moonshot.ai | ||
| user-agent: | ||
| - OpenAI/Python 1.99.9 | ||
| x-stainless-arch: | ||
| - arm64 | ||
| x-stainless-async: | ||
| - 'false' | ||
| x-stainless-lang: | ||
| - python | ||
| x-stainless-os: | ||
| - MacOS | ||
| x-stainless-package-version: | ||
| - 1.99.9 | ||
| x-stainless-read-timeout: | ||
| - '120.0' | ||
| x-stainless-retry-count: | ||
| - '0' | ||
| x-stainless-runtime: | ||
| - CPython | ||
| x-stainless-runtime-version: | ||
| - 3.11.12 | ||
| method: POST | ||
| uri: https://api.moonshot.ai/v1/chat/completions | ||
| response: | ||
| body: | ||
| string: '{"id":"chatcmpl-691cd7ec3e228ab73cf751df","object":"chat.completion","created":1763497964,"model":"kimi-k2-thinking","choices":[{"index":0,"message":{"role":"assistant","content":"{\"setup\": | ||
| \"Why don''t scientists trust atoms?\", \"punchline\": \"Because they make | ||
| up everything!\"}","reasoning_content":"The user wants me to:\n1. Tell a joke\n2. | ||
| Format the output as a JSON instance that conforms to the provided schema\n3. | ||
| The schema has two required properties: \"setup\" (string) and \"punchline\" | ||
| (string)\n\nI need to create a JSON object with \"setup\" and \"punchline\" | ||
| keys. The setup should be the question part of the joke, and the punchline | ||
| should be the answer.\n\nLet me think of a good joke. I''ll use a classic | ||
| one:\nSetup: \"Why don''t scientists trust atoms?\"\nPunchline: \"Because | ||
| they make up everything!\"\n\nNow I need to format this as JSON:\n{\n \"setup\": | ||
| \"Why don''t scientists trust atoms?\",\n \"punchline\": \"Because they make | ||
| up everything!\"\n}\n\nThis matches the required schema."},"finish_reason":"stop"}],"usage":{"prompt_tokens":201,"completion_tokens":193,"total_tokens":394,"cached_tokens":201}}' | ||
| headers: | ||
| Connection: | ||
| - keep-alive | ||
| Content-Type: | ||
| - application/json; charset=utf-8 | ||
| Date: | ||
| - Tue, 18 Nov 2025 20:32:54 GMT | ||
| Msh-Context-Cache-Token-Saved: | ||
| - '201' | ||
| Msh-Gid: | ||
| - free | ||
| Msh-Org-Id: | ||
| - org-67752d4c14124a4fb97b1cb607c5492a | ||
| Msh-Project-Id: | ||
| - proj-afa6516f69f142258a1acf013213e49c | ||
| Msh-Request-Id: | ||
| - bcaa050a-c4bd-11f0-8ed1-00163e49fa2f | ||
| Msh-Trace-Mode: | ||
| - 'on' | ||
| Msh-Uid: | ||
| - d4a871m6s4t4vqocb090 | ||
| Strict-Transport-Security: | ||
| - max-age=31536000; includeSubDomains | ||
| Transfer-Encoding: | ||
| - chunked | ||
| Vary: | ||
| - Accept-Encoding | ||
| - Origin | ||
| X-Msh-Trace-Id: | ||
| - 58c39ec1c7a5f240826722ace064ce99 | ||
| content-length: | ||
| - '1136' | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to do this because this model is "thinking" out loud and it just spams the chunks