Skip to content

Commit

Permalink
Local AI via GPT4Local
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSoftDiamond committed Jul 10, 2024
1 parent 41c4603 commit cd86460
Showing 1 changed file with 67 additions and 3 deletions.
70 changes: 67 additions & 3 deletions main_userContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@
from elevenlabs import *
from elevenlabs.client import ElevenLabs


# We only will import local AI handling if the user wishes to. The user must install requirements-localai.txt to use this feature. Otherwise it will fail and yield an error.
# This will require you to follow instructions from https://github.com/xtekky/gpt4local for installation.
if settings.AIMode.lower()=="local":
from gpt4localmain.g4l.local import *
from gpt4localmain.g4l import stubs, typing
engine = LocalEngine(gpu_layers=int(settings.localAI_gpulayers), cores=int(settings.localAI_CPUCores))


last_message_time_bitsMessages = 0
last_message_time_RawMessages = 0
last_message_time_keywords = 0
REDEEM_ID = settings.redeemID
CONVERSATION_LIMIT = int(settings.CONVERSATION_LIMIT)
AINAME_FIXED=settings.AINAME+":"

Version = "1.4.1" #Do not touch this line. It is used for version checking.
Version = "1.5.0" #Do not touch this line. It is used for version checking.

class Bot(commands.Bot):

Expand Down Expand Up @@ -243,6 +252,7 @@ def generate_speech(self, response, user_context, CONVERSATION_LIMIT, copiedmess
ssml_text = '<speak>'
response_counter = 0
mark_array = []

for s in response.split(' '):
ssml_text += f'<mark name="{response_counter}"/>{s}'
mark_array.append(s)
Expand Down Expand Up @@ -777,10 +787,64 @@ async def userRunsEvent(self, message, pfilter, copiedmessage, currentTimeBits,
user_context.append({ 'role': 'system', 'content': self.context_string }) #Readd context string from default
user_context.append({ 'role': 'user', 'content': theusername+" said: "+content })
response = gpt3_completion(user_context) #Retry the question after readding context

print(AINAME_FIXED , response)

# Copied for text chat response reasons below
textresponse = response

if settings.AIMode.lower() == "local":
if message.author.name not in Bot.conversations:
Bot.conversations[message.author.name] = []
user_context = Bot.conversations[message.author.name]
if settings.useUserPrompt:
#Runs only if Per User Prompt setting enabled in settings.py
usernamefield = message.author.name
userpromptsfolder = os.path.join(os.path.dirname(__file__), "customprompts/userprompts")
thisUserPrompt = os.path.join(userpromptsfolder, f"{usernamefield}_prompt.txt")
print("Expecting User File @ "+thisUserPrompt+"\n")
if os.path.exists(thisUserPrompt):
#File found case (User Prompt Mode)
print("File found. Using "+thisUserPrompt+" context\n")
thisUserString = open_file(thisUserPrompt)
user_context.append({ 'role': 'user', 'content': thisUserString })
else:
#File not found case (Default context)
print("File not found. Using default user context instead.")
user_context.append({ 'role': 'user', 'content': self.context_string })
else:
#Runs if Per User Prompt Mode is disabled in settings.py
user_context.append({ 'role': 'user', 'content': self.context_string })
user_context = Bot.conversations[message.author.name]

# Copied for text chat response reasons below
print(user_context)

content = message.content.encode(encoding='ASCII',errors='ignore').decode()
user_context.append({ 'role': 'user', 'content': theusername+" said: "+content })

try:
response = engine.chat.completions.create(model=settings.localAI_ModelName, messages=user_context)
except Exception as e:
Bot.conversations[message.author.name] = [] #Wipe User Messages if token limit reached
user_context = Bot.conversations[message.author.name] #Redeclare user context
if settings.useUserPrompt:
usernamefield = message.author.name
userpromptsfolder = os.path.join(os.path.dirname(__file__), "customprompts/userprompts")
thisUserPrompt = os.path.join(userpromptsfolder, f"{usernamefield}_prompt.txt")
if os.path.exists(thisUserPrompt):
thisUserString = open_file(thisUserPrompt)
user_context.append({ 'role': 'user', 'content': thisUserString }) #Readd context string from file
else:
user_context.append({ 'role': 'user', 'content': self.context_string }) #Readd context string from default
else:
user_context.append({ 'role': 'user', 'content': self.context_string }) #Readd context string from default
user_context.append({ 'role': 'user', 'content': theusername+" said: "+content })
response = engine.chat.completions.create(model=settings.localAI_ModelName, messages=user_context) #Retry the question after readding context

response = response.choices[0].message.content
print(AINAME_FIXED , response)

# Copied for text chat response reasons below
textresponse = response

await self.run_methods_concurrently(textresponse, response, user_context, CONVERSATION_LIMIT, copiedmessage, message.author.name)
Expand Down

0 comments on commit cd86460

Please sign in to comment.