|
5 | 5 | from jupyterlab_chat.models import Message
|
6 | 6 | from litellm import acompletion
|
7 | 7 |
|
| 8 | +from ...litellm_utils import StreamResult, ToolCallOutput |
8 | 9 | from ..base_persona import BasePersona, PersonaDefaults
|
9 | 10 | from ..persona_manager import SYSTEM_USERNAME
|
10 | 11 | from .prompt_template import (
|
@@ -69,30 +70,78 @@ async def process_message(self, message: Message) -> None:
|
69 | 70 | "tool_calls": tool_calls_json
|
70 | 71 | })
|
71 | 72 |
|
72 |
| - # Show tool call requests to YChat (not synced with `messages`) |
73 |
| - if len(tool_calls_json): |
74 |
| - self.ychat.update_message(Message( |
75 |
| - id=result.id, |
76 |
| - body=f"\n\n```\n{json.dumps(tool_calls_json, indent=2)}\n```\n", |
77 |
| - sender=self.id, |
78 |
| - time=time.time(), |
79 |
| - raw_time=False |
80 |
| - ), append=True) |
| 73 | + # Render tool calls in new message |
| 74 | + if len(result.tool_call_list): |
| 75 | + self.render_tool_calls(result) |
81 | 76 |
|
82 | 77 | # Run tools and append outputs to `messages`
|
83 | 78 | tool_call_outputs = await self.run_tools(result.tool_call_list)
|
84 | 79 | messages.extend(tool_call_outputs)
|
85 | 80 |
|
86 |
| - # Add tool call outputs to YChat (not synced with `messages`) |
| 81 | + # Render tool call outputs in new message |
87 | 82 | if tool_call_outputs:
|
88 |
| - self.ychat.update_message(Message( |
89 |
| - id=result.id, |
90 |
| - body=f"\n\n```\n{json.dumps(tool_call_outputs, indent=2)}\n```\n", |
91 |
| - sender=self.id, |
92 |
| - time=time.time(), |
93 |
| - raw_time=False |
94 |
| - ), append=True) |
| 83 | + self.render_tool_call_outputs( |
| 84 | + message_id=result.id, |
| 85 | + tool_call_outputs=tool_call_outputs |
| 86 | + ) |
95 | 87 |
|
| 88 | + def render_tool_calls(self, stream_result: StreamResult): |
| 89 | + """ |
| 90 | + Renders tool calls by appending the tool calls to a message. |
| 91 | + """ |
| 92 | + message_id = stream_result.id |
| 93 | + tool_call_list = stream_result.tool_call_list |
| 94 | + |
| 95 | + for tool_call in tool_call_list.resolve(): |
| 96 | + id = tool_call.id |
| 97 | + index = tool_call.index |
| 98 | + type_val = tool_call.type |
| 99 | + function = tool_call.function.model_dump_json() |
| 100 | + # We have to HTML-escape double quotes in the JSON string. |
| 101 | + function = function.replace('"', """) |
| 102 | + |
| 103 | + self.ychat.update_message(Message( |
| 104 | + id=message_id, |
| 105 | + body=f'\n\n<jai-tool-call id="{id}" type="{type_val}" index={index} function="{function}"></jai-tool-call>\n', |
| 106 | + sender=self.id, |
| 107 | + time=time.time(), |
| 108 | + raw_time=False |
| 109 | + ), append=True) |
| 110 | + |
| 111 | + |
| 112 | + def render_tool_call_outputs(self, message_id: str, tool_call_outputs: list[dict]): |
| 113 | + # TODO |
| 114 | + # self.ychat.update_message(Message( |
| 115 | + # id=message_id, |
| 116 | + # body=f"\n\n```\n{json.dumps(tool_call_outputs, indent=2)}\n```\n", |
| 117 | + # sender=self.id, |
| 118 | + # time=time.time(), |
| 119 | + # raw_time=False |
| 120 | + # ), append=True) |
| 121 | + |
| 122 | + # Updates the content of the last message directly |
| 123 | + message = self.ychat.get_message(message_id) |
| 124 | + body = message.body |
| 125 | + for output in tool_call_outputs: |
| 126 | + if not output['content']: |
| 127 | + output['content'] = "" |
| 128 | + output = ToolCallOutput(**output) |
| 129 | + tool_id = output.tool_call_id |
| 130 | + tool_output = output.model_dump_json() |
| 131 | + tool_output = tool_output.replace('"', '"') |
| 132 | + body = body.replace( |
| 133 | + f'<jai-tool-call id="{tool_id}"', |
| 134 | + f'<jai-tool-call id="{tool_id}" output="{tool_output}"', |
| 135 | + ) |
| 136 | + |
| 137 | + self.log.error(body) |
| 138 | + self.ychat.update_message(Message( |
| 139 | + id=message.id, |
| 140 | + time=time.time(), |
| 141 | + body=body, |
| 142 | + sender=self.id, |
| 143 | + raw_time=False |
| 144 | + )) |
96 | 145 |
|
97 | 146 |
|
98 | 147 | def get_context_as_messages(
|
|
0 commit comments