Skip to main content

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

  1. 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")
  1. 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'.
""")
  1. 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())
  1. 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.

Release files for nix-gpt-client 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nix-gpt-client 0.2.0
File Size Uploaded
nix_gpt_client-0.2.0.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nix-gpt-client 0.2.0
File Interpreter ABI Platform
nix_gpt_client-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.3 kB

Release files / nix_gpt_client-0.2.0.tar.gz

Download URL nix_gpt_client-0.2.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
09d826b0105e7e5507fb6546b7fdeb53711a10a8f9194e2138e093872dbcaa34
BLAKE2b-256 checksum
How to use checksums
a2f7d8c15735f1b3659fbf0bc066767a33b91318af1b42a7140d6824f77f0ebd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.11.8

Release files / nix_gpt_client-0.2.0-py3-none-any.whl

Download URL nix_gpt_client-0.2.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
071bfe3b08b12798609b9d507df6aeea0dd6e165778e87723e8de841298dad1e
BLAKE2b-256 checksum
How to use checksums
3952df21aa4ce7287b7fb069dace7facc69621fcdb2cc3c5c1a49b897670db0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.11.8

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page