-
Notifications
You must be signed in to change notification settings - Fork 348
feat: multiturn, streaming, AI SDK and OpenAI protocol support #1107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leonardmq
wants to merge
19
commits into
main
Choose a base branch
from
leonard/kil-447-feat-stream-multiturn-ai-sdk-openai-protocols
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,392
−88
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3f83187
refactor: support multiturn (existing conversation history with task …
leonardmq 6c7cc58
refactor: use correct typing in chat formatter
leonardmq 04748e4
refactor: retrieve task_run one level up
leonardmq 57896a8
Proof of concept streaming API
scosman 3d6ced2
test: paid integration test for streaming
leonardmq 1464fb8
test: add test for session + streaming together
leonardmq dee10b3
Update libs/core/kiln_ai/adapters/model_adapters/test_litellm_adapter…
leonardmq 456088d
refactor: stream with support for AI SDK (with tool events) and OpenA…
leonardmq 0ea65b4
refactor: ai sdk events as pydantic models
leonardmq a98d886
fix: model_dump implementation and remove to_see to leave transport s…
leonardmq e989dca
fix: should reset before next round of toolcalls
leonardmq 11710f2
refactor: take in a trace instead of a task_run for session continuation
leonardmq 3ad6a27
refactor: remove ability to continue task run at api level
leonardmq 3f08ed5
Merge branch 'main' of github.com:Kiln-AI/Kiln into leonard/kil-420-a…
leonardmq 4d8e99f
refactor: wrap stream iterators to allow exposing task run at the end
leonardmq eb537ed
fix: remove autosave_runs hardcoded
leonardmq 66b3151
fix: close text when opening toolcall
leonardmq 4460580
fix: reset fully
leonardmq c516dca
Merge branch 'main' of github.com:Kiln-AI/Kiln into leonard/kil-447-f…
leonardmq 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,3 +21,5 @@ libs/server/build | |
| dist/ | ||
|
|
||
| .mcp.json | ||
|
|
||
| test_output/ | ||
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
Empty file.
60 changes: 60 additions & 0 deletions
60
libs/core/kiln_ai/adapters/litellm_utils/litellm_streaming.py
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,60 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any, AsyncIterator, Optional, Union | ||
|
|
||
| import litellm | ||
| from litellm.types.utils import ( | ||
| ModelResponse, | ||
| ModelResponseStream, | ||
| TextCompletionResponse, | ||
| ) | ||
|
|
||
|
|
||
| class StreamingCompletion: | ||
| """ | ||
| Async iterable wrapper around ``litellm.acompletion`` with streaming. | ||
|
|
||
| Yields ``ModelResponseStream`` chunks as they arrive. After iteration | ||
| completes, the assembled ``ModelResponse`` is available via the | ||
| ``.response`` property. | ||
|
|
||
| Usage:: | ||
|
|
||
| stream = StreamingCompletion(model=..., messages=...) | ||
| async for chunk in stream: | ||
| # handle chunk however you like (print, log, send over WS, …) | ||
| pass | ||
| final = stream.response # fully assembled ModelResponse | ||
| """ | ||
|
|
||
| def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
| kwargs = dict(kwargs) | ||
| kwargs.pop("stream", None) | ||
| self._args = args | ||
| self._kwargs = kwargs | ||
| self._response: Optional[Union[ModelResponse, TextCompletionResponse]] = None | ||
| self._iterated: bool = False | ||
|
|
||
| @property | ||
| def response(self) -> Optional[Union[ModelResponse, TextCompletionResponse]]: | ||
| """The final assembled response. Only available after iteration.""" | ||
| if not self._iterated: | ||
| raise RuntimeError( | ||
| "StreamingCompletion has not been iterated yet. " | ||
| "Use 'async for chunk in stream:' before accessing .response" | ||
| ) | ||
| return self._response | ||
|
|
||
| async def __aiter__(self) -> AsyncIterator[ModelResponseStream]: | ||
| self._response = None | ||
| self._iterated = False | ||
|
|
||
| chunks: list[ModelResponseStream] = [] | ||
| stream = await litellm.acompletion(*self._args, stream=True, **self._kwargs) | ||
|
|
||
| async for chunk in stream: | ||
| chunks.append(chunk) | ||
| yield chunk | ||
|
|
||
| self._response = litellm.stream_chunk_builder(chunks) | ||
| self._iterated = True |
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.
Preserve the prior trace in formatter-owned state.
initial_messages()carries the continuation history, but_messagesis still empty here, somessages()andmessage_dicts()only describe the newly appended turn. Any caller that serializes the formatter state will drop the earlier conversation.🤖 Prompt for AI Agents