Skip to content

Commit

Permalink
Merge pull request #122 from rgbkrk/parallel-tool-calling
Browse files Browse the repository at this point in the history
provide tools for multiple function calling
  • Loading branch information
rgbkrk authored Jan 16, 2024
2 parents 1909bd4 + a6d7374 commit 93b2a11
Show file tree
Hide file tree
Showing 6 changed files with 499 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.3.0]

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

## [1.2.1]

- Drop Noteable builtin
Expand Down
2 changes: 2 additions & 0 deletions chatlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
narrate,
system,
user,
tool_result
)
from .registry import FunctionRegistry
from .views.markdown import Markdown
Expand Down Expand Up @@ -85,6 +86,7 @@ def __init__(self, *args, **kwargs):
"assistant",
"assistant_function_call",
"function_result",
"tool_result",
"models",
"Session",
"Chat",
Expand Down
18 changes: 17 additions & 1 deletion chatlab/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from typing import Optional

from openai.types.chat import ChatCompletionMessageParam
from openai.types.chat import ChatCompletionMessageParam, ChatCompletionToolMessageParam


def assistant(content: str) -> ChatCompletionMessageParam:
Expand Down Expand Up @@ -100,6 +100,22 @@ def function_result(name: str, content: str) -> ChatCompletionMessageParam:
}


def tool_result(tool_call_id: str, content: str) -> ChatCompletionToolMessageParam:
"""Create a tool result message.
Args:
tool_call_id: The ID of the tool call.
content: The content of the message.
Returns:
A dictionary representing a tool result message.
"""
return {
"role": "tool",
"content": content,
"tool_call_id": tool_call_id,
}

# Aliases
narrate = system
human = user
Expand Down
4 changes: 4 additions & 0 deletions chatlab/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ def api_manifest(self, function_call_option: FunctionCall = "auto") -> APIManife
"function_call": function_call_option,
}

@property
def tools(self):
return [{"type": "function", "function": adapt_function_definition(f)} for f in self.__schemas.values()]

async def call(self, name: str, arguments: Optional[str] = None) -> Any:
"""Call a function by name with the given parameters."""
if name is None:
Expand Down
Loading

0 comments on commit 93b2a11

Please sign in to comment.