-
Notifications
You must be signed in to change notification settings - Fork 2
/
mychatgpt.py
75 lines (60 loc) · 1.96 KB
/
mychatgpt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# __ __ ____ _ _ ____ _
# | \/ |_ _ / ___| |__ __ _| |_ / ___|_ __ | |_
# | |\/| | | | | | | '_ \ / _` | __| | _| '_ \| __|
# | | | | |_| | |___| | | | (_| | |_| |_| | |_) | |_
# |_| |_|\__, |\____|_| |_|\__,_|\__|\____| .__/ \__|
# |___/ |_|
#
# by Stephan Raabe (2023)
# -----------------------------------------------------
# myChatGpt script
# modify the path to the openai.yaml
# -----------------------------------------------------
import openai
import pyperclip
import yaml
import readline
import click
from pathlib import Path
from yaml import load,CLoader as Loader
# Path to your openai.yaml in your home directory
openaipath = "/private/openai.yaml"
# openaipath = "/mychatgpt/openai.yaml"
chat_logfile = "/private/chatlog.txt"
# Get home path
home = str(Path.home())
# Load openai api key
with open(home + openaipath, "r") as ymlfile:
cfg = yaml.load(ymlfile, Loader=Loader)
# Set up the OpenAI API client
openai.api_key = cfg["openai"]["api-key"]
# Set up the model and prompt
model_engine = "text-davinci-003"
@click.command()
def sendRequest():
prompt = str(input("Hello, how can I help you? "))
print('...')
# Generate a response
completion = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
response = completion.choices[0].text
print(response)
pyperclip.copy(response)
print("...")
print("Output has been copied to the clipboard!")
c = input("Do you want to store the question and answer in the Chat Logfile (y/n)? (default: y) ")
if (c != "n"):
f = open (home + chat_logfile, "a")
f.write("Question: " + prompt)
f.write(response + "\n")
f.write(" \n")
f.close()
print("Output written to chat logfile " + chat_logfile + "!")
if __name__ == '__main__':
sendRequest()