A python module that provides an easy way to use OpenAI GPT-4 (or 3.5) function calling
Project description
GPT Commands Python
A python module that provides an easy way to use OpenAI GPT-4 (or 3.5) function calling, which is basically GPT plugins in API world.
Just create a class with your custom logic:
class Game:
def get_inventory(self, character: str, max_items: int) -> List[str]:
"""
Get inventory of a character
Args:
character (str): The name of the character to get the inventory of. One of: 'Harry', 'Ron', 'Hermione'
max_items (int): The maximum number of items to return
Returns:
List[str]: The inventory of the character
"""
if character == "Harry":
return ["Wand", "Broom", "Cloak"]
elif character == "Ron":
return ["Wand", "Rat"]
elif character == "Hermione":
return ["Wand", "Cat", "Book"]
return []
def alohomora(self):
"""
Unlock the door
"""
print("[COMMAND] Alohomora!")
def expelliarmus(self, target: str):
"""
Disarm the target
Args:
target (str): The target to disarm
"""
print(f"[COMMAND] Expelliarmus {target}!")
Make sure to annotate your code with type hints and doc strings. This is what the module uses to "explain" the functions to GPT.
Then pass an instance of your class to GPTCommandsClient like so and start prompting:
manager = Game()
model = "gpt-4-0613" # "gpt-3.5-turbo-16k-0613"
async with GPTCommandsClient(model, system_prompt) as client:
while True:
prompt = input("You: ")
async for data in client.chat_stream(prompt, manager):
print(data, end="")
print()
List of currently supported types for parameters:
boolintfloatstrList[T]Dict[str, T]Optional[T]@dataclassmarked classes withJsonSchemaMixin
See example for a full example.
Installation
-
Install the package
pip install gpt-commands-python
-
Set environment variables
Copy
.env.examplefile and rename it to.env. Then set your OpenAI API key (and optionally your organization ID). Your.envfile should look someting like this:OPENAI_API_KEY=sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX OPENAI_ORGANIZATION=org-XXXXXXXXXXXXXXXXXXXXXXXXYou can also pass these values as arguments to
GPTCommandsClientconstructor or set them as environment variables.
Running the example
python -m examples.game
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gpt-commands-python-0.1.0.tar.gz.
File metadata
- Download URL: gpt-commands-python-0.1.0.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe839094c4bdf9713274157588d412a7ac774726635e91584ddc0bcc8e934fb0
|
|
| MD5 |
4109929fbe3f0fb8fa589c3c6775a0f5
|
|
| BLAKE2b-256 |
7d1baee28286ec8c7cde2079593d7238f947158d53d36282c6295be935ae25e4
|
File details
Details for the file gpt_commands_python-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gpt_commands_python-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc2554e794f9c98524b922abfff1f226bf82b3deb80c1abe48bb23efa31c8012
|
|
| MD5 |
e5ace9c5a209fd23b4276ebb7b67694f
|
|
| BLAKE2b-256 |
639a243f5a256e66653962c4953b9542da2e0d6d7e9eedfc8bcc064c9888c49d
|