@@ -49,12 +49,12 @@ def __init__(self, bot):
4949 description = "Generate an chatGPT completion" ,
5050 )
5151 @app_commands .describe (
52- prompt = "The prompt to pass to chatGPT: Default= \" \" " ,
52+ prompt = "The prompt to pass to chatGPT" ,
5353 role = " system | user | asssistant: Default=user" ,
5454 temp = "What sampling temperature to use. Higher values means more risks: Min=0 Max=1 Default=1" ,
5555 presence_penalty = "Number between -2.0 and 2.0. Positive values will encourage new topics: Min=-2 Max=2 Default=0" ,
5656 frequency_penalty = "Number between -2.0 and 2.0. Positive values will encourage new words: Min=-2 Max=2 Default=0" )
57- async def chatgpt (self , context : Context , prompt : str = "" , role : Roles = Roles .user , temp : float = 1.0 ,
57+ async def chatgpt (self , context : Context , prompt : str , role : Roles = Roles .user , temp : float = 1.0 ,
5858 presence_penalty : float = 0.0 , frequency_penalty : float = 0.0 ):
5959 openai .api_key = self .bot .config ["openai_key" ]
6060 model = "gpt-3.5-turbo"
@@ -63,7 +63,7 @@ async def chatgpt(self, context: Context, prompt: str = "", role: Roles = Roles.
6363 freqPen = min (max (frequency_penalty , - 2 ), 2 )
6464
6565 if context .guild .id not in self .bot .chat_messages :
66- self .bot .chat_messages [context .guild .id ] = [{ "role" : "system" , "content" : self . bot . chat_init [ context . guild . id ]}] if context . guild . id in self . bot . chat_init and self . bot . chat_init [ context . guild . id ] else [ ]
66+ self .bot .chat_messages [context .guild .id ] = []
6767 self .bot .chat_messages [context .guild .id ].append ({"role" : role .value , "content" : prompt })
6868 messages = self .bot .chat_messages [context .guild .id ]
6969
@@ -88,7 +88,7 @@ async def chatgpt(self, context: Context, prompt: str = "", role: Roles = Roles.
8888 presence_penalty = freqPen ,
8989 max_tokens = 325 if 325 <= 4096 - token_cost else token_cost
9090 )
91- await context .send (f"{ prompt } \n { response ['choices' ][0 ]['message' ]['content' ]} { warning .value } " [:2000 ])
91+ await context .send (f"{ prompt } \n \n { response ['choices' ][0 ]['message' ]['content' ]} { warning .value } " [:2000 ])
9292 self .bot .chat_messages [context .guild .id ].append (response ['choices' ][0 ]['message' ])
9393 except Exception as error :
9494 print (f"Failed to generate valid response for prompt: { prompt } \n Error: { error } " )
@@ -97,32 +97,27 @@ async def chatgpt(self, context: Context, prompt: str = "", role: Roles = Roles.
9797 )
9898
9999 @commands .hybrid_command (
100- name = "resetchat " ,
100+ name = "chatreset " ,
101101 description = "Resets the chat history for chatGPT completions" ,
102102 )
103- async def resetchat (self , context ):
104- self .bot .chat_messages [context .guild .id ] = [{ "role" : "system" , "content" : self . bot . chat_init [ context . guild . id ]}] if context . guild . id in self . bot . chat_init and self . bot . chat_init [ context . guild . id ] else [ ]
103+ async def chatreset (self , context ):
104+ self .bot .chat_messages [context .guild .id ] = []
105105 await context .send ("Chat history has been reset" )
106106
107107 @commands .hybrid_command (
108- name = "setchatinit " ,
109- description = "Set the initialization message, a guide for the AI on how to respond to future chat messages " ,
108+ name = "chatinit " ,
109+ description = "Initialize chatGPT with an instructional message " ,
110110 )
111- @app_commands .describe (message = "The init message for chatGPT completions. Omit to reset" )
112- async def setchatinit (self , context , message : str = "" ):
113- self .bot .chat_init [context .guild .id ] = message
114- if message :
115- if context .guild .id in self .bot .chat_messages :
116- if self .bot .chat_messages [context .guild .id ] and self .bot .chat_messages [context .guild .id ][0 ]["role" ] == "system" :
117- self .bot .chat_messages [context .guild .id ][0 ] = {"role" : "system" , "content" : self .bot .chat_init [context .guild .id ]}
118- else :
119- self .bot .chat_messages [context .guild .id ] = [{"role" : "system" , "content" : self .bot .chat_init [context .guild .id ]}] + self .bot .chat_messages [context .guild .id ]
120- await context .send ("Chat init message has been set" )
111+ @app_commands .describe (message = "The initialization message" )
112+ async def chatinit (self , context , message : str ):
113+ if context .guild .id in self .bot .chat_messages :
114+ if self .bot .chat_messages [context .guild .id ] and self .bot .chat_messages [context .guild .id ][0 ]["role" ] == "system" :
115+ self .bot .chat_messages [context .guild .id ][0 ] = {"role" : "system" , "content" : message }
116+ else :
117+ self .bot .chat_messages [context .guild .id ] = [{"role" : "system" , "content" : message }] + self .bot .chat_messages [context .guild .id ]
121118 else :
122- if context .guild .id in self .bot .chat_messages :
123- if self .bot .chat_messages [context .guild .id ] and self .bot .chat_messages [context .guild .id ][0 ]["role" ] == "system" :
124- self .bot .chat_messages [context .guild .id ].pop (0 )
125- await context .send ("Chat init message has been reset" )
119+ self .bot .chat_messages [context .guild .id ] = [{"role" : "system" , "content" : message }]
120+ await context .send (f"ChatGPT has been initialized with \" { message } \" " )
126121
127122async def setup (bot ):
128123 await bot .add_cog (ChatGPT (bot ))
0 commit comments