Automatically creating functions that LLMs can use.
Project description
chat2func
chat2func
automatically generates JSON schemas from Python, allowing ChatGPT to talk to your code.
Installation
# Clone the repo
git clone git@github.com:ttumiel/chat2func.git
cd chat2func
pip install -e .
Quick Start
Annotate your function with @json_schema
from chat2func import json_schema
@json_schema
def my_function(x: float, y: float) -> bool:
"""This is a sample function.
Args:
x: The first float.
y: Another float.
"""
return x > y
After this, my_function
will have an additional .json
attribute containing its JSON schema.
print(my_function.json)
{'description': 'This is a sample function.',
'name': 'my_function',
'parameters': {'properties': {'x': {'description': 'The first float.', 'type': 'number'},
'y': {'description': 'Another float.', 'type': 'number'}},
'required': ['x', 'y'],
'type': 'object'}}
Using Custom Classes
json_schema
works with classes or dataclasses too. Set descriptions=False
to not generate object descriptions from docstrings.
@json_schema(descriptions=False)
@dataclass
class Data:
a: int = 0
print(Data.json)
{'name': 'Data',
'parameters': {'type': 'object', 'properties': {'a': {'type': 'integer'}}}}
Calling Functions with JSON Arguments
function_call
provides additional functionality for calling functions with JSON arguments. It automatically converts JSON arguments to Python objects and returns the result as JSON. It validates the JSON, raising FunctionCallError
if something is unexpected.
import json
from chat2func import function_call, collect_functions
def plusplus(x: float, y: float) -> float:
"Add two floats."
return x + y
# Specify the available functions.
# You can also use `collect_functions` to collect all functions within a scope.
functions = {"plusplus": plusplus}
# Arguments are passed as a JSON string.
arguments = json.dumps({"x": 1.0, "y": 2.0})
result = function_call("plusplus", arguments, functions)
print(result) # 3.0
# We can optionally validate the function arguments too. Defaults to on.
arguments = json.dumps({"x": "a", "y": 2.0})
result = function_call("plusplus", arguments, functions)
# FunctionCallError: Function call failed. 1 validation error for plusplus
Creating a ChatGPT Plugin
You can easily create and demo a ChatGPT plugin using the included server. First, install the additional server requirements using pip install -e .[server]
. Then, define the functions you want ChatGPT to be able to use, expose them with the server and connect to them from ChatGPT. Visit http://localhost:3333/ to see the available functions.
from chat2func.server import FunctionServer
def addition(x: float, y: float) -> float:
"Add two floats."
return x + y
server = FunctionServer({"addition": addition})
server.run()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file chat2func-0.1.1.tar.gz
.
File metadata
- Download URL: chat2func-0.1.1.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7916cdc4f4a56aa66912735a16db61119db7749394327b9e7373b4b4a59ebc55 |
|
MD5 | 8a938483723dafd61296f707b6c6d737 |
|
BLAKE2b-256 | 60364b52741b7d389b446fc8eaf9358d6cd10b288773bd77833daa1dbd932aed |
File details
Details for the file chat2func-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: chat2func-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 026687609b1855f6a878e3bfde2f89f43620e41f62d83d80dbfd4bd39befeb4f |
|
MD5 | 80e6832ae53890fd39f640989ab55c6a |
|
BLAKE2b-256 | 2c2eb9c790fe874a6aa2ff78b29612a8d66b721154db80156a661d5bc03f4607 |