QueryHelper / gptManager.py
anumaurya114exp's picture
send only recent context history
7d0c63c
raw
history blame
1.79 kB
import re
from persistStorage import saveLog
class ChatgptManager:
def __init__(self, openAIClient, model="gpt-3.5-turbo-1106", tokenLimit=8000, throwError=False, contextHistoryLen=3):
self.client = openAIClient
self.tokenLimit = tokenLimit
self.model = model
self.throwError = throwError
self.messages = []
self.contextHistoryLen = contextHistoryLen #sending previous n user inputs and their assistant messages.
def setSystemPrompt(self, systemPrompt):
systemMessage = {"role":"system", "content":systemPrompt}
if len(self.messages)==0:
self.messages = [systemMessage]
else:
del self.messages[0]
self.messages.insert(0, systemMessage)
def getResponseForUserInput(self, userInput):
#send only recent history to gpt.
self.messages = self.getRecentContextOnly()
userMessage = {"role":"user", "content":userInput}
self.messages.append(userMessage)
print(self.messages, "messages being sent to gpt for completion.")
try:
completion = self.client.chat.completions.create(
model=self.model,
messages=self.messages,
temperature=0,
)
gptResponse = completion.choices[0].message.content
except Exception as e:
if not self.throwError:
errorText = "Error while connecting with gpt " + str(e)[:100] + "..."
return errorText
self.messages.append({"role": "assistant", "content": gptResponse})
return gptResponse
def getRecentContextOnly(self):
#take only systemp prompt and recent self.contextHistoryLen user input and self.contextHistoryLen assistant messages
if len(self.messages)<2*self.contextHistoryLen+1:
return self.messages[:]
return [self.messages[0]] + self.messages[-2*self.contextHistoryLen:]