A Python library for easy interaction with the GPT API, featuring typed requests and responses.
Project description
nix-gpt-client
nix-gpt-client is a simple Python library for working with the GPT API. It uses Pydantic to add input and output schemas, allowing you to manage data structures with ease.
Table of Contents
Installation
To install nix-gpt-client
, you can use pip
. Python 3.11 or higher is required.
pip install nix-gpt-client
Usage
- If you want to use typing, create Pydantic schemas for input and output data structures:
from datetime import datetime
from pydantic import BaseModel, Field
class GPTInput(BaseModel):
time: datetime = Field(description="The current time")
timezone: str = Field(description="The timezone of the current time")
target_timezone: str = Field(description="The target timezone")
class GPTOutput(BaseModel):
target_time: datetime = Field(description="The target time")
- Then create GPTClient object, set schemas, and add system message:
from nix.gptclient import GPTClient, GPTModel, GPTMessageRole, GPTSchemaType
gpt_client = GPTClient(
model=GPTModel.GPT_3_5_TURBO,
top_p=0.5,
presence_penalty=0,
frequency_penalty=0,
temperature=0)
gpt_client.set_schema(GPTInput, GPTSchemaType.input)
gpt_client.set_schema(GPTOutput, GPTSchemaType.output)
gpt_client.add_message(GPTMessageRole.system, """
Take from \'Input JSON schema\' the fields 'time', 'timezone', and 'target_timezone'.
Then generate an Output JSON schema with the field 'target_time', which represents the given 'time' converted
from 'timezone' to 'target_timezone'.
""")
- Add data like user message and call
run
method to get the response:
input_data = {
"time": datetime.now().__str__(),
"timezone": "UTC",
"target_timezone": "America/New_York"
}
gpt_client.add_message(GPTMessageRole.user, input_data)
response = json.loads(gpt_client.run())
- The response will be:
{
"target_time": "2024-05-06T13:04:38.621713-04:00"
}
Full example:
import json
from datetime import datetime
from dotenv import load_dotenv
from pydantic import BaseModel, Field
from src import GPTClient, GPTModel, GPTMessageRole, GPTSchemaType
load_dotenv()
class GPTInput(BaseModel):
time: datetime = Field(description="The current time")
timezone: str = Field(description="The timezone of the current time")
target_timezone: str = Field(description="The target timezone")
class GPTOutput(BaseModel):
target_time: datetime = Field(description="The target time")
gpt_client = GPTClient(
model=GPTModel.GPT_3_5_TURBO,
top_p=0.5,
presence_penalty=0,
frequency_penalty=0,
temperature=0)
gpt_client.set_schema(GPTInput, GPTSchemaType.input)
gpt_client.set_schema(GPTOutput, GPTSchemaType.output)
gpt_client.add_message(GPTMessageRole.system, """
Take from \'Input JSON schema\' the fields 'time', 'timezone', and 'target_timezone'.
Then generate an Output JSON schema with the field 'target_time', which represents the given 'time' converted
from 'timezone' to 'target_timezone'.
""")
input_data = {
"time": datetime.now().__str__(),
"timezone": "UTC",
"target_timezone": "America/New_York"
}
gpt_client.add_message(GPTMessageRole.user, input_data)
response = json.loads(gpt_client.run())
print(response)
License
This project is licensed under the MIT License. See the LICENSE file for more details.
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
nix_gpt_client-0.1.0.tar.gz
(4.4 kB
view details)
Built Distribution
File details
Details for the file nix_gpt_client-0.1.0.tar.gz
.
File metadata
- Download URL: nix_gpt_client-0.1.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43b3426f9ea5e03b72d979e97b346d96452b1568aa1709cc6c3c762b60d17ecc |
|
MD5 | 41592e673146a9fc1ac68ed61bd2bb26 |
|
BLAKE2b-256 | 98f3e2360a3e5c85632288c89bdaffb16cd30f7086d55053dd5adf87821227d5 |
File details
Details for the file nix_gpt_client-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: nix_gpt_client-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7179873cd1444845c1b782f0d7e46a72340da47fbcbef074269779510d87be18 |
|
MD5 | 6ca9345eed830a49446bd01aa1e76fc1 |
|
BLAKE2b-256 | 37c8d5fd25113ada1071ecdad592e72afcb7946ba8474d1db671106a7869e3b4 |