Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Plugin Chat #1090

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion installer/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ pdf2image
python-nmap
yeelight
haversine
FlightRadarAPI
FlightRadarAPI
gpt-2-simple
80 changes: 80 additions & 0 deletions jarviscli/plugins/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from plugin import plugin
from plugin import complete
import gpt_2_simple as gpt2
import os

@complete("chat")
@plugin("chat")
def chat(jarvis, s):
"""
Uses GPT-2 to give a response based on the users prompt

Parameters:
jarvis (obj): Jarvis assistant object
s (str): Prompt entered by the user

Returns:
None

Example Usage:
chat What is the best programming language?
"""

# Check for user input
if not s:
print("Please input a prompt. Usage: chat [prompt]")
return

# List of available GPT-2 models
MODELS = ['124M', '355M', '774M', '1558M']

# Check if the model directory already exists
if os.path.exists('models'):
print('GPT-2 models already installed.')
else:
# If the directory doesn't exist, create it
os.makedirs('models')

# Prompt the user to select a model to install
print('Available models:')
for model in MODELS:
print(f'- {model}')
model_choice = input('Enter the name of the model you want to install (the larger the models the better the responses): ')

# Check that the selected model is valid
if model_choice not in MODELS:
print('Invalid model choice.')
else:
# Download the selected model
if not os.path.isdir(os.path.join("models", model_choice)):
print(f"Downloading {model_choice} model...")
gpt2.download_gpt2(model_name=model_choice)

# Check models directory for already installed models
model_name = ""

for model in MODELS:
model_dir = os.path.join(os.getcwd(), 'models', model)
if os.path.exists(model_dir):
print(f'The {model} model is installed.')
model_name = model
break

if not 'models':
print('None of the specified models are installed. Exiting.')
exit()

# Start GPT-2 session with the installed model
sess = gpt2.start_tf_sess()
gpt2.load_gpt2(sess, model_name=model_name)

# Generate response
response = gpt2.generate(sess, model_name=model_name, prefix=s, length=100, temperature=0.7, nsamples=1, batch_size=1, return_as_list=True)[0]

# Print out GPT-2's response
print('\n')
jarvis.say(response)
print('\n')

# Reset GPT-2's session
gpt2.reset_session(sess)