Skip to content

Commit dc56d33

Browse files
committed
migrate over to a simple tool view model
1 parent 7b18f9c commit dc56d33

15 files changed

+889
-1171
lines changed

Diff for: chatlab/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
tool_result
3434
)
3535
from .registry import FunctionRegistry
36-
from .views.markdown import Markdown
36+
from spork import Markdown
3737

3838

3939
# Deprecate Session in favor of Chat

Diff for: chatlab/builtins/files.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import aiofiles
1414

15-
from chatlab.decorators import expose_exception_to_llm
15+
from ..decorators import expose_exception_to_llm
1616

1717

1818
@expose_exception_to_llm

Diff for: chatlab/builtins/python.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from repr_llm import register_llm_formatter
88
from repr_llm.pandas import format_dataframe_for_llm, format_series_for_llm
99

10-
from chatlab.decorators import expose_exception_to_llm
10+
from ..decorators import expose_exception_to_llm
1111

1212
from ._mediatypes import pluck_richest_text, redisplay_superrich
1313

Diff for: chatlab/chat.py

+24-27
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@
2424
from openai.types.chat import ChatCompletion, ChatCompletionChunk, ChatCompletionMessageParam
2525
from pydantic import BaseModel
2626

27-
from chatlab.views.assistant_function_call import AssistantFunctionCallView
28-
2927
from ._version import __version__
30-
from .display import ChatFunctionCall
3128
from .errors import ChatLabError
3229
from .messaging import human
3330
from .registry import FunctionRegistry, PythonHallucinationFunction
34-
from .views.assistant import AssistantMessageView
31+
from .views import ToolArguments, AssistantMessageView
3532

3633
logger = logging.getLogger(__name__)
3734

@@ -145,9 +142,9 @@ async def __call__(self, *messages: Union[ChatCompletionMessageParam, str], stre
145142

146143
async def __process_stream(
147144
self, resp: AsyncStream[ChatCompletionChunk]
148-
) -> Tuple[str, Optional[AssistantFunctionCallView]]:
149-
assistant_view: AssistantMessageView = AssistantMessageView()
150-
function_view: Optional[AssistantFunctionCallView] = None
145+
) -> Tuple[str, Optional[ToolArguments]]:
146+
assistant_view: AssistantMessageView = AssistantMessageView(content="")
147+
function_view: Optional[ToolArguments] = None
151148
finish_reason = None
152149

153150
async for result in resp: # Go through the results of the stream
@@ -162,37 +159,41 @@ async def __process_stream(
162159
# Is stream choice?
163160
if choice.delta is not None:
164161
if choice.delta.content is not None:
162+
assistant_view.display_once()
165163
assistant_view.append(choice.delta.content)
166164
elif choice.delta.function_call is not None:
167165
function_call = choice.delta.function_call
168166
if function_call.name is not None:
169-
if assistant_view.in_progress():
167+
if not assistant_view.finished:
170168
# Flush out the finished assistant message
171-
message = assistant_view.flush()
169+
message = assistant_view.get_message()
172170
self.append(message)
173-
function_view = AssistantFunctionCallView(function_call.name)
171+
# IDs are for the tool calling apparatus from newer versions of the API
172+
# We will make use of it later.
173+
function_view = ToolArguments(id="TBD", name=function_call.name)
174+
function_view.display()
174175
if function_call.arguments is not None:
175176
if function_view is None:
176177
raise ValueError("Function arguments provided without function name")
177-
function_view.append(function_call.arguments)
178+
function_view.append_arguments(function_call.arguments)
178179
if choice.finish_reason is not None:
179180
finish_reason = choice.finish_reason
180181
break
181182

182183
# Wrap up the previous assistant
183184
# Note: This will also wrap up the assistant's message when it ran out of tokens
184-
if assistant_view.in_progress():
185-
message = assistant_view.flush()
185+
if not assistant_view.finished:
186+
message = assistant_view.get_message()
186187
self.append(message)
187188

188189
if finish_reason is None:
189190
raise ValueError("No finish reason provided by OpenAI")
190191

191192
return (finish_reason, function_view)
192193

193-
async def __process_full_completion(self, resp: ChatCompletion) -> Tuple[str, Optional[AssistantFunctionCallView]]:
194-
assistant_view: AssistantMessageView = AssistantMessageView()
195-
function_view: Optional[AssistantFunctionCallView] = None
194+
async def __process_full_completion(self, resp: ChatCompletion) -> Tuple[str, Optional[ToolArguments]]:
195+
assistant_view: AssistantMessageView = AssistantMessageView(content="")
196+
function_view: Optional[ToolArguments] = None
196197

197198
if len(resp.choices) == 0:
198199
logger.warning(f"Result has no choices: {resp}")
@@ -203,12 +204,13 @@ async def __process_full_completion(self, resp: ChatCompletion) -> Tuple[str, Op
203204
message = choice.message
204205

205206
if message.content is not None:
207+
assistant_view.display_once()
206208
assistant_view.append(message.content)
207-
self.append(assistant_view.flush())
209+
self.append(assistant_view.get_message())
208210
if message.function_call is not None:
209211
function_call = message.function_call
210-
function_view = AssistantFunctionCallView(function_name=function_call.name)
211-
function_view.append(function_call.arguments)
212+
function_view = ToolArguments(id="TBD", name=function_call.name, arguments=function_call.arguments)
213+
function_view.display()
212214

213215
return choice.finish_reason, function_view
214216

@@ -286,17 +288,12 @@ async def submit(self, *messages: Union[ChatCompletionMessageParam, str], stream
286288
"Function call was the stated function_call reason without having a complete function call. If you see this, report it as an issue to https://github.com/rgbkrk/chatlab/issues" # noqa: E501
287289
)
288290
# Record the attempted call from the LLM
289-
self.append(function_call_request.get_message())
291+
self.append(function_call_request.get_function_message())
290292

291-
chat_function = ChatFunctionCall(
292-
**function_call_request.finalize(),
293-
function_registry=self.function_registry,
294-
)
293+
function_called = await function_call_request.call(function_registry=self.function_registry)
295294

296-
# Make the call
297-
fn_message = await chat_function.call()
298295
# Include the response (or error) for the model
299-
self.append(fn_message)
296+
self.append(function_called.get_function_called_message())
300297

301298
# Reply back to the LLM with the result of the function call, allow it to continue
302299
await self.submit(stream=stream, **kwargs)

Diff for: chatlab/views/__init__.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""Views for ChatLab."""
22
from .assistant import AssistantMessageView
3-
from .assistant_function_call import AssistantFunctionCallView
4-
from .markdown import Markdown
3+
from .tools import ToolArguments, ToolCalled
54

65
__all__ = [
76
"AssistantMessageView",
8-
"AssistantFunctionCallView",
9-
"Markdown",
7+
"ToolArguments",
8+
"ToolCalled"
109
]

Diff for: chatlab/views/abstracts.py

-101
This file was deleted.

Diff for: chatlab/views/argument_buffer.py

-46
This file was deleted.

Diff for: chatlab/views/assistant.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
"""Views for the buffers."""
1+
from spork import Markdown
22

33
from ..messaging import assistant
4-
from .abstracts import BufferView
5-
from .markdown import Markdown
64

5+
class AssistantMessageView(Markdown):
6+
finished: bool = False
7+
has_displayed: bool = False
78

8-
class AssistantMessageView(BufferView):
9-
"""A view for the assistant's message."""
9+
def get_message(self):
10+
return assistant(content=self.content)
1011

11-
buffer: Markdown
12+
def display(self):
13+
super().display()
14+
self.has_displayed = True
15+
16+
def display_once(self):
17+
if not self.has_displayed:
18+
self.display()
1219

13-
def create_buffer(self, content: str = "") -> Markdown:
14-
"""Creates the specific buffer for the view."""
15-
return Markdown(content)
1620

17-
def get_message(self):
18-
"""Returns the crafted message."""
19-
return assistant(self.content)

Diff for: chatlab/views/assistant_function_call.py

-43
This file was deleted.

0 commit comments

Comments
 (0)