Skip to content

Commit

Permalink
feat(chat): new cli command for continuous chat (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
AumitLeon authored Mar 5, 2023
1 parent 2a9b154 commit c4dbaba
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,31 @@

A simple python CLI wrapper for the [ChatGPT API](https://platform.openai.com/docs/guides/chat/introduction).

Installation:
## Installation
```
$ pip install chatgpt-api-cli
```

Setup:
## Setup
```
$ chatgpt init "your openai API key"
```

Usage:
## Usage

### Single prompts:
```
$ chatgpt prompt "Explain the current state of the economy as if you're Christopher Moltisanti from the Sopranos"
"Listen up, Tony. The economy? Fuggedaboudit. It's in the shitter. The recession's got us all by the balls, and businesses are falling like dead flies. The GDP's down, unemployment's up - it's a real shitshow. People are struggling just to make ends meet, while the fat cats on Wall Street keep raking in the dough. And we all know who's responsible for this mess - the damn politicians and their crooked deals. I'm telling you, Tone, this whole system is screwed up, and it's getting worse every day. It's enough to make a guy wanna start his own damn business, just so he can make a buck without getting screwed over by the man."
```

### Continuous chat:
```
$ chatgpt chat
To exit, type `exit` or press CTRL+C
You: why did the chicken cross the road?
ChatGPT: As an AI language model, I don't have access to the context as to why the chicken might cross the road. However, the most common answer to that age-old question has been "to get to the other side."
```
24 changes: 22 additions & 2 deletions chatgpt_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from pathlib import Path
from chatgpt_cli.services.chatgpt import query_chatgpt
from chatgpt_cli.services.chatgpt import ChatGPTResponse
from rich.console import Console

console = Console()

app = typer.Typer()

Expand All @@ -26,14 +28,32 @@ def init(
with config_path.open("w", encoding="utf-8") as f:
f.write(f"OPENAI_API_KEY={open_api_key}")

print("Done!")
console.print("Done!", style="bold green")


@app.command()
def prompt(prompt: str = typer.Argument(..., help="Prompt for ChatGPT")):
"""Query ChatGPT with a prompt."""
response: ChatGPTResponse = query_chatgpt(prompt=prompt)
print(response.choices[0].message.content)
message = response.choices[0].message.content.strip()
console.print(f"\n{message}", style="bold cyan")


@app.command()
def chat():
"""Chat with ChatGPT."""
console.print("To exit, type `exit` or press CTRL+C", style="bold yellow")
while True:
prompt = typer.prompt("\nYou")
if prompt.lower() in ("exit", "quit"):
exit = typer.confirm("Are you sure you want to exit?")
if exit:
console.print("Goodbye!", style="bold green")
return
else:
response: ChatGPTResponse = query_chatgpt(prompt=prompt)
message = response.choices[0].message.content.strip()
console.print(f"ChatGPT: {message}", style="bold cyan")


def main():
Expand Down
2 changes: 1 addition & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ authors = [
dependencies = [
"openai>=0.27.0",
"typer[all]>=0.7.0",
"pydantic[dotenv]>=1.10.5"
"pydantic[dotenv]>=1.10.5",
"rich>=12.6.0",
]
requires-python = ">=3.11"
license = {text = "MIT"}
Expand Down

0 comments on commit c4dbaba

Please sign in to comment.