-
Notifications
You must be signed in to change notification settings - Fork 350
refactor: support multiturn chat #1088
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
Closed
+828
−64
Closed
Changes from all commits
Commits
Show all changes
6 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 c75e7d7
Merge branch 'main' of github.com:Kiln-AI/Kiln into leonard/kil-421-a…
leonardmq e71efe6
refactor: always create a new run
leonardmq 855e451
fix: coderabbit feedback
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
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 |
|---|---|---|
|
|
@@ -3,7 +3,11 @@ | |
| from dataclasses import dataclass | ||
| from typing import Dict, Tuple | ||
|
|
||
| from kiln_ai.adapters.chat.chat_formatter import ChatFormatter, get_chat_formatter | ||
| from kiln_ai.adapters.chat.chat_formatter import ( | ||
| ChatFormatter, | ||
| MultiturnFormatter, | ||
| get_chat_formatter, | ||
| ) | ||
| from kiln_ai.adapters.ml_model_list import ( | ||
| KilnModelProvider, | ||
| StructuredOutputMode, | ||
|
|
@@ -123,14 +127,18 @@ async def invoke( | |
| self, | ||
| input: InputType, | ||
| input_source: DataSource | None = None, | ||
| existing_run: TaskRun | None = None, | ||
| ) -> TaskRun: | ||
| run_output, _ = await self.invoke_returning_run_output(input, input_source) | ||
| run_output, _ = await self.invoke_returning_run_output( | ||
| input, input_source, existing_run | ||
| ) | ||
| return run_output | ||
|
|
||
| async def _run_returning_run_output( | ||
| self, | ||
| input: InputType, | ||
| input_source: DataSource | None = None, | ||
| existing_run: TaskRun | None = None, | ||
| ) -> Tuple[TaskRun, RunOutput]: | ||
| # validate input, allowing arrays | ||
| if self.input_schema is not None: | ||
|
|
@@ -141,6 +149,15 @@ async def _run_returning_run_output( | |
| require_object=False, | ||
| ) | ||
|
|
||
| if existing_run is not None and ( | ||
| not existing_run.trace or len(existing_run.trace) == 0 | ||
| ): | ||
| raise ValueError( | ||
| "Run has no trace. Cannot continue session without conversation history." | ||
| ) | ||
|
|
||
| prior_trace = existing_run.trace if existing_run else None | ||
|
|
||
| # Format model input for model call (we save the original input in the task without formatting) | ||
| formatted_input = input | ||
| formatter_id = self.model_provider().formatter | ||
|
|
@@ -149,7 +166,7 @@ async def _run_returning_run_output( | |
| formatted_input = formatter.format_input(input) | ||
|
|
||
| # Run | ||
| run_output, usage = await self._run(formatted_input) | ||
| run_output, usage = await self._run(formatted_input, prior_trace=prior_trace) | ||
|
|
||
| # Parse | ||
| provider = self.model_provider() | ||
|
|
@@ -198,10 +215,28 @@ async def _run_returning_run_output( | |
| "Reasoning is required for this model, but no reasoning was returned." | ||
| ) | ||
|
|
||
| # Generate the run and output | ||
| run = self.generate_run( | ||
| input, input_source, parsed_output, usage, run_output.trace | ||
| ) | ||
| # Create the run and output - merge if there is an existing run | ||
| if existing_run is not None: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see comment here: #1088 (comment) I'm leaning more towards always saving a new task_run, and just setting a new parent_id field on the child? Keep it immutable. Will add a bit of work in UI, but much more robust for collisions. |
||
| merged_output = RunOutput( | ||
| output=parsed_output.output, | ||
| intermediate_outputs=parsed_output.intermediate_outputs | ||
| or run_output.intermediate_outputs, | ||
| output_logprobs=parsed_output.output_logprobs | ||
| or run_output.output_logprobs, | ||
| trace=run_output.trace, | ||
| ) | ||
| run = self.generate_run( | ||
| input, | ||
| input_source, | ||
| merged_output, | ||
| usage, | ||
| run_output.trace, | ||
| existing_run=existing_run, | ||
| ) | ||
| else: | ||
| run = self.generate_run( | ||
| input, input_source, parsed_output, usage, run_output.trace | ||
| ) | ||
|
|
||
| # Save the run if configured to do so, and we have a path to save to | ||
| if ( | ||
|
|
@@ -210,7 +245,7 @@ async def _run_returning_run_output( | |
| and self.task.path is not None | ||
| ): | ||
| run.save_to_file() | ||
| else: | ||
| elif existing_run is None: | ||
| # Clear the ID to indicate it's not persisted | ||
| run.id = None | ||
|
|
||
|
|
@@ -220,6 +255,7 @@ async def invoke_returning_run_output( | |
| self, | ||
| input: InputType, | ||
| input_source: DataSource | None = None, | ||
| existing_run: TaskRun | None = None, | ||
| ) -> Tuple[TaskRun, RunOutput]: | ||
| # Determine if this is the root agent (no existing run context) | ||
| is_root_agent = get_agent_run_id() is None | ||
|
|
@@ -229,7 +265,9 @@ async def invoke_returning_run_output( | |
| set_agent_run_id(run_id) | ||
|
|
||
| try: | ||
| return await self._run_returning_run_output(input, input_source) | ||
| return await self._run_returning_run_output( | ||
| input, input_source, existing_run | ||
| ) | ||
| finally: | ||
| if is_root_agent: | ||
| try: | ||
|
|
@@ -247,7 +285,11 @@ def adapter_name(self) -> str: | |
| pass | ||
|
|
||
| @abstractmethod | ||
| async def _run(self, input: InputType) -> Tuple[RunOutput, Usage | None]: | ||
| async def _run( | ||
| self, | ||
| input: InputType, | ||
| prior_trace: list[ChatCompletionMessageParam] | None = None, | ||
| ) -> Tuple[RunOutput, Usage | None]: | ||
| pass | ||
|
|
||
| def build_prompt(self) -> str: | ||
|
|
@@ -267,7 +309,14 @@ def build_prompt(self) -> str: | |
| include_json_instructions=add_json_instructions | ||
| ) | ||
|
|
||
| def build_chat_formatter(self, input: InputType) -> ChatFormatter: | ||
| def build_chat_formatter( | ||
| self, | ||
| input: InputType, | ||
| prior_trace: list[ChatCompletionMessageParam] | None = None, | ||
| ) -> ChatFormatter: | ||
| if prior_trace is not None: | ||
| return MultiturnFormatter(prior_trace, input) | ||
|
|
||
| if self.prompt_builder is None: | ||
| raise ValueError("Prompt builder is not available for MCP run config") | ||
| # Determine the chat strategy to use based on the prompt the user selected, the model's capabilities, and if the model was finetuned with a specific chat strategy. | ||
|
|
@@ -323,24 +372,14 @@ def generate_run( | |
| run_output: RunOutput, | ||
| usage: Usage | None = None, | ||
| trace: list[ChatCompletionMessageParam] | None = None, | ||
| existing_run: TaskRun | None = None, | ||
| ) -> TaskRun: | ||
| # Convert input and output to JSON strings if they aren't strings | ||
| input_str = ( | ||
| input if isinstance(input, str) else json.dumps(input, ensure_ascii=False) | ||
| ) | ||
| output_str = ( | ||
| json.dumps(run_output.output, ensure_ascii=False) | ||
| if isinstance(run_output.output, dict) | ||
| else run_output.output | ||
| ) | ||
|
|
||
| # If no input source is provided, use the human data source | ||
| if input_source is None: | ||
| input_source = DataSource( | ||
| type=DataSourceType.human, | ||
| properties={"created_by": Config.shared().user_id}, | ||
| ) | ||
|
|
||
| # Synthetic since an adapter, not a human, is creating this | ||
| # Special case for MCP run configs which calls a mcp tool | ||
| output_source_type = ( | ||
|
|
@@ -349,26 +388,41 @@ def generate_run( | |
| else DataSourceType.synthetic | ||
| ) | ||
|
|
||
| new_task_run = TaskRun( | ||
| new_output = TaskOutput( | ||
| output=output_str, | ||
| source=DataSource( | ||
| type=output_source_type, | ||
| properties=self._properties_for_task_output(), | ||
| run_config=self.run_config, | ||
| ), | ||
| ) | ||
|
|
||
| final_usage = usage | ||
| final_intermediate = run_output.intermediate_outputs | ||
| if existing_run is not None: | ||
| final_usage = (existing_run.usage or Usage()) + (usage or Usage()) | ||
| final_intermediate = run_output.intermediate_outputs | ||
|
|
||
| input_str = ( | ||
| input if isinstance(input, str) else json.dumps(input, ensure_ascii=False) | ||
| ) | ||
| if input_source is None: | ||
| input_source = DataSource( | ||
| type=DataSourceType.human, | ||
| properties={"created_by": Config.shared().user_id}, | ||
| ) | ||
|
|
||
| return TaskRun( | ||
| parent=self.task, | ||
| input=input_str, | ||
| input_source=input_source, | ||
| output=TaskOutput( | ||
| output=output_str, | ||
| source=DataSource( | ||
| type=output_source_type, | ||
| properties=self._properties_for_task_output(), | ||
| run_config=self.run_config, | ||
| ), | ||
| ), | ||
| intermediate_outputs=run_output.intermediate_outputs, | ||
| output=new_output, | ||
| intermediate_outputs=final_intermediate, | ||
| tags=self.base_adapter_config.default_tags or [], | ||
| usage=usage, | ||
| usage=final_usage, | ||
| trace=trace, | ||
| ) | ||
|
|
||
| return new_task_run | ||
|
|
||
| def _properties_for_task_output(self) -> Dict[str, str | int | float]: | ||
| match self.run_config.type: | ||
| case "mcp": | ||
|
|
||
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.
naming maybe one of: continue_from? prior_task_run? parent_task_run?