Include History for Chatbot
All checks were successful
OpenWebUI Discord Bot / Build-and-Push (push) Successful in 59s
All checks were successful
OpenWebUI Discord Bot / Build-and-Push (push) Successful in 59s
This commit is contained in:
parent
35839395f4
commit
43f40981db
@ -23,20 +23,27 @@ conversation_histories = {}
|
||||
DEFAULT_HISTORY_LIMIT = 50
|
||||
MAX_HISTORY_LIMIT = 200
|
||||
|
||||
async def get_ai_response(prompt, channel_id=None, include_history=False, history_limit=DEFAULT_HISTORY_LIMIT):
|
||||
async def get_ai_response(prompt, channel_id=None):
|
||||
try:
|
||||
messages = []
|
||||
|
||||
# If history is requested and exists for this channel, include it
|
||||
if include_history and channel_id in conversation_histories:
|
||||
messages = conversation_histories[channel_id]
|
||||
# Always include history if it exists for this channel
|
||||
if channel_id in conversation_histories:
|
||||
history = conversation_histories[channel_id][-400:] # Get last 400 messages (200 exchanges)
|
||||
|
||||
# Add the current prompt
|
||||
messages.append({"role": "user", "content": prompt})
|
||||
# Format the history as a context block
|
||||
history_text = "\n".join([
|
||||
f"{'User: ' if msg['role'] == 'user' else 'Assistant: '}{msg['content']}"
|
||||
for msg in history
|
||||
])
|
||||
|
||||
formatted_prompt = f"{prompt}\n##CONTEXT##\n{history_text}\n##ENDCONTEXT##"
|
||||
else:
|
||||
formatted_prompt = prompt
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model=os.getenv('MODEL_NAME') or "us.anthropic.claude-3-5-sonnet-20241022-v2:0",
|
||||
messages=messages,
|
||||
messages=[{"role": "user", "content": formatted_prompt}],
|
||||
temperature=0.7,
|
||||
max_tokens=500
|
||||
)
|
||||
@ -50,9 +57,9 @@ async def get_ai_response(prompt, channel_id=None, include_history=False, histor
|
||||
{"role": "assistant", "content": response.choices[0].message.content}
|
||||
])
|
||||
|
||||
# Limit history based on specified or default limit
|
||||
if len(conversation_histories[channel_id]) > history_limit * 2: # multiply by 2 because each exchange has 2 messages
|
||||
conversation_histories[channel_id] = conversation_histories[channel_id][-(history_limit * 2):]
|
||||
# Always keep last 400 messages (200 exchanges)
|
||||
if len(conversation_histories[channel_id]) > 400:
|
||||
conversation_histories[channel_id] = conversation_histories[channel_id][-400:]
|
||||
|
||||
return response.choices[0].message.content
|
||||
except Exception as e:
|
||||
@ -76,34 +83,10 @@ async def on_message(message):
|
||||
await message.channel.send("Hello! How can I help you?")
|
||||
return
|
||||
|
||||
# Check if the message includes a request for history
|
||||
include_history = False
|
||||
history_limit = DEFAULT_HISTORY_LIMIT
|
||||
|
||||
# Check for "with history", "with X history", or "X lines of chat" patterns
|
||||
import re
|
||||
if "with history" in prompt.lower() or re.search(r"\d+\s*lines of chat", prompt.lower()):
|
||||
include_history = True
|
||||
# Check for specific history limit
|
||||
if match := re.search(r"with (\d+) history", prompt.lower()):
|
||||
requested_limit = int(match.group(1))
|
||||
history_limit = min(requested_limit, MAX_HISTORY_LIMIT)
|
||||
prompt = re.sub(r"with \d+ history", "", prompt, flags=re.IGNORECASE)
|
||||
elif match := re.search(r"(\d+)\s*lines of chat", prompt.lower()):
|
||||
requested_limit = int(match.group(1))
|
||||
history_limit = min(requested_limit, MAX_HISTORY_LIMIT)
|
||||
prompt = re.sub(r"\d+\s*lines of chat", "", prompt, flags=re.IGNORECASE)
|
||||
else:
|
||||
prompt = prompt.lower().replace("with history", "")
|
||||
|
||||
prompt = prompt.strip()
|
||||
|
||||
async with message.channel.typing():
|
||||
response = await get_ai_response(
|
||||
prompt,
|
||||
channel_id=str(message.channel.id),
|
||||
include_history=include_history,
|
||||
history_limit=history_limit
|
||||
channel_id=str(message.channel.id)
|
||||
)
|
||||
|
||||
if len(response) > 2000:
|
||||
@ -115,15 +98,6 @@ async def on_message(message):
|
||||
|
||||
await bot.process_commands(message)
|
||||
|
||||
@bot.command(name='clearhistory')
|
||||
async def clear_history(ctx):
|
||||
channel_id = str(ctx.channel.id)
|
||||
if channel_id in conversation_histories:
|
||||
conversation_histories[channel_id] = []
|
||||
await ctx.send("Conversation history has been cleared.")
|
||||
else:
|
||||
await ctx.send("No conversation history exists for this channel.")
|
||||
|
||||
def main():
|
||||
# Get the Discord token from environment variables
|
||||
discord_token = os.getenv('DISCORD_TOKEN')
|
||||
|
Loading…
Reference in New Issue
Block a user