Skip to main content

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

  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.

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.3.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

nix_gpt_client-0.1.3-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file nix_gpt_client-0.1.3.tar.gz.

File metadata

  • Download URL: nix_gpt_client-0.1.3.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.8

File hashes

Hashes for nix_gpt_client-0.1.3.tar.gz
Algorithm Hash digest
SHA256 77e5e8c04e3c5f62b654189fcc6ac5a3e5d6329bb372ad2929955d7168b60e95
MD5 f6985529f4ec7de59af8f7df3b050f68
BLAKE2b-256 e121d4a27c1fbe7af3df6f8b5885198c4489dce26e5d2b18b7c604cb309e12ca

See more details on using hashes here.

File details

Details for the file nix_gpt_client-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for nix_gpt_client-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e5dc29ee67d72f01532417caa429555b3b685e209bd4ed3394a3a5c69f67c69f
MD5 0049adb53e4d05c3f169c38b7d6e3405
BLAKE2b-256 34ba5c2e242889afb385a1fab3413f36c20ba1740ca00773d1ef544127fff9b7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page