Skip to content

Instantly share code, notes, and snippets.

@matthew01lokiet
Last active May 12, 2024 09:41
Show Gist options
  • Save matthew01lokiet/cb4d7ff523ac21357559df8b06cc8a72 to your computer and use it in GitHub Desktop.
Save matthew01lokiet/cb4d7ff523ac21357559df8b06cc8a72 to your computer and use it in GitHub Desktop.
ChatGPT Simple Client

ChatGPT Simple Client

Motivation behind it

  • I wanted to use gpt-4 without usage limit, which is being applied when using website.
  • I wanted to conveniently use gpt-3.5 and gpt-4 at the same time to ask questions according to their level of complexity.

Requirements

Usage

  • Create folder for script.
  • Put chatgpt_client.py and Pipfile inside folder.
  • Run pipenv shell inside folder.
  • Run pipenv install --skip-lock inside folder.
  • Run pipenv run python chatgpt_client.py - (use --help argument or read script instruction output below).
Usage: chatgpt_client.py [OPTIONS]

Options:
  -gak, --gpt-api-key TEXT        ChatGPT api key without exceeded quota - 
                                  provided either through cli argument or 
                                  environmental variable.
                                  [env var: GPT_API_KEY; required]
  -pi, --project-id TEXT          Project id associated with your API Key.
                                  [required]
  -oi, --organization-id TEXT     Organization id associated with your project
                                  id.  [required]
  -gm, --gpt-model [gpt-3.5-turbo|gpt-3.5-turbo-instruct|gpt-4|gpt-4-32k]
                                  Version of ChatGPT model to use.  [default:
                                  gpt-3.5-turbo]
  --help                          Show this message and exit.
from typing import Final
import click
from openai import OpenAI
from rich.console import Console
from rich.markdown import Markdown
from rich.padding import Padding
from rich.prompt import Prompt
class ChatGPTModels:
GPT_3_5_TURBO: Final[str] = "gpt-3.5-turbo"
GPT_3_5_TURBO_INSTRUCT: Final[str] = "gpt-3.5-turbo-instruct"
GPT_4: Final[str] = "gpt-4"
GPT_4_32K: Final[str] = "gpt-4-32k"
@staticmethod
def values() -> list[str]:
return [
ChatGPTModels.GPT_3_5_TURBO, ChatGPTModels.GPT_3_5_TURBO_INSTRUCT,
ChatGPTModels.GPT_4, ChatGPTModels.GPT_4_32K
]
@click.command()
@click.option(
"-gak", "--gpt-api-key", envvar="GPT_API_KEY", show_envvar=True, required=True, type=str,
help="ChatGPT api key without exceeded quota - provided either through cli argument or environmental variable."
)
@click.option(
"-pi", "--project-id", required=True, type=str, help="Project id associated with your API Key."
)
@click.option(
"-oi", "--organization-id", required=True, type=str, help="Organization id associated with your project id."
)
@click.option(
"-gm", "--gpt-model", default=ChatGPTModels.GPT_3_5_TURBO, show_default=True, required=False,
type=click.Choice(ChatGPTModels.values()), help="Version of ChatGPT model to use."
)
def start_gpt_chat(gpt_api_key: str, organization_id: str, project_id: str, gpt_model: str):
client = OpenAI(organization=organization_id, project=project_id, api_key=gpt_api_key)
space_padding: int = 2
console = Console()
while True:
question_prompt = f"Ask {gpt_model} a question"
print((' ' * space_padding) + ('-' * len(question_prompt)))
print()
user_question = Prompt.ask(f" [bold red]{question_prompt}[/bold red]")
if user_question == 'q' or user_question == 'quit':
break
generated_answer = client.chat.completions.create(
messages=[{"role": "user", "content": user_question}], model=gpt_model
).choices[0].message.content
print()
console.print(Padding(Markdown(generated_answer), pad=(0, 0, 0, space_padding)))
print()
if __name__ == '__main__':
start_gpt_chat()
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
openai = "~=1.0"
click = "~=8.0"
rich = "~=13.0"
[dev-packages]
[requires]
python_version = "3.12"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment