Spaces:
Runtime error
Runtime error
File size: 2,801 Bytes
37bd4dd e7c1910 37bd4dd 7d0c63c 37bd4dd e7c1910 0ff15a5 7d0c63c 1dda07c 0ff15a5 37bd4dd 0ff15a5 4099f5c 37bd4dd 0ff15a5 7d0c63c 4099f5c 37bd4dd 0ff15a5 da9f335 4099f5c e7c1910 0ff15a5 37bd4dd 7d0c63c 4099f5c 7d0c63c 4099f5c | 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 | 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 getResponseForChatHistory(self, chatHistory, userInput):
"""
Assume incoming chat History as [("user message", "bot message"), ("user message", "bot message")]
and systemp prompt is fixed.
"""
messages = [self.messages[0]]
for history in chatHistory:
userMessage, botMessage = history
messages.append({"role":"user", "content":userMessage})
messages.append({"role":"assistant", "content":botMessage})
messages = self.getRecentContextOnly(messages)
messages.append({"role":"user", "content":userInput})
try:
completion = self.client.chat.completions.create(
model=self.model,
messages=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
return gptResponse
def getResponseForUserInput(self, userInput):
#send only recent history to gpt.
self.messages = self.getRecentContextOnly(self.messages)
userMessage = {"role":"user", "content":userInput}
self.messages.append(userMessage)
print(self.messages, "messages being sent to gpt for completion.")
print(help(self.client.chat.completions.create),'\n\n\n')
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, messages):
#take only systemp prompt and recent self.contextHistoryLen user input and self.contextHistoryLen assistant messages
if len(messages)<2*self.contextHistoryLen+1:
return messages[:]
return [messages[0]] + messages[-2*self.contextHistoryLen:] |