Skip to content

Commit eb8e3c3

Browse files
committed
re-introduce legacy function calling
1 parent b657711 commit eb8e3c3

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.0.0]
9+
10+
- Support parallel tool calling by default in `Chat`.
11+
- Legacy support for function calling is available by passing `legacy_function_calling=True` to the `Chat` constructor.
12+
813
## [1.3.0]
914

1015
- Support tool call format from `FunctionRegistry`. Enables parallel function calling (note: not in `Chat` yet). https://github.com/rgbkrk/chatlab/pull/122

chatlab/chat.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def __init__(
7373
chat_functions: Optional[List[Callable]] = None,
7474
allow_hallucinated_python: bool = False,
7575
python_hallucination_function: Optional[PythonHallucinationFunction] = None,
76+
legacy_function_calling: bool = False,
7677
):
7778
"""Initialize a Chat with an optional initial context of messages.
7879
@@ -99,6 +100,8 @@ def __init__(
99100
self.api_key = openai_api_key
100101
self.base_url = base_url
101102

103+
self.legacy_function_calling = legacy_function_calling
104+
102105
if initial_context is None:
103106
initial_context = [] # type: ignore
104107

@@ -295,28 +298,32 @@ async def submit(self, *messages: Union[ChatCompletionMessageParam, str], stream
295298
base_url=self.base_url,
296299
)
297300

301+
chat_create_kwargs = {
302+
"model": self.model,
303+
"messages": full_messages,
304+
"temperature": kwargs.get("temperature", 0),
305+
}
306+
298307
# Due to the strict response typing based on `Literal` typing on `stream`, we have to process these
299308
# two cases separately
300309
if stream:
310+
if self.legacy_function_calling:
311+
chat_create_kwargs.update(self.function_registry.api_manifest())
312+
else:
313+
chat_create_kwargs["tools"] = self.function_registry.tools
314+
301315
streaming_response = await client.chat.completions.create(
302-
model=self.model,
303-
messages=full_messages,
304-
tools=self.function_registry.tools,
316+
**chat_create_kwargs,
305317
stream=True,
306-
temperature=kwargs.get("temperature", 0),
307318
)
308319

309320
self.append(*messages)
310321

311322
finish_reason, function_call_request, tool_arguments = await self.__process_stream(streaming_response)
312323
else:
313-
# TODO: Process tools for non stream
314324
full_response = await client.chat.completions.create(
315-
model=self.model,
316-
messages=full_messages,
317-
tools=self.function_registry.tools,
325+
**chat_create_kwargs,
318326
stream=False,
319-
temperature=kwargs.get("temperature", 0),
320327
)
321328

322329
self.append(*messages)

0 commit comments

Comments
 (0)