Source code for sherpa_ai.prompt
import time
from typing import Any, Callable, List
from langchain_core.messages import (
AIMessage,
BaseMessage,
HumanMessage,
SystemMessage,
)
from langchain_core.prompts import BaseChatPromptTemplate
from langchain_core.vectorstores import VectorStoreRetriever
from loguru import logger
from pydantic import BaseModel
from sherpa_ai.prompt_generator import get_prompt
from sherpa_ai.tools import BaseTool
[docs]
class SlackBotPrompt(BaseChatPromptTemplate):
"""A chat prompt template for a Slack bot.
This class extends BaseChatPromptTemplate to provide a specialized prompt template
for Slack bots. It handles message formatting, token counting, and chat history
processing.
Attributes:
ai_name (str): The name of the AI assistant.
ai_role (str): The role of the AI assistant.
tools (List[BaseTool]): List of tools available to the AI.
token_counter (Callable[[str], int]): Function to count tokens in a string.
send_token_limit (int): Maximum number of tokens to send in a message.
Defaults to 4196.
Example:
>>> from sherpa_ai.prompt import SlackBotPrompt
>>> prompt = SlackBotPrompt(
... ai_name="Assistant",
... ai_role="Helper",
... tools=[],
... token_counter=lambda x: len(x.split())
... )
>>> messages = prompt.format_messages(task="Hello", user_input="Hi")
>>> print(len(messages))
3
"""
ai_name: str
ai_role: str
tools: List[BaseTool]
token_counter: Callable[[str], int]
send_token_limit: int = 4196
[docs]
def construct_base_prompt(self):
"""Construct the base prompt for the AI assistant.
This method generates the foundational system prompt that defines the AI's
identity, role, and available tools. It combines the AI's name with the
prompt generated from available tools.
Returns:
str: The complete base prompt string.
Example:
>>> from sherpa_ai.prompt import SlackBotPrompt
>>> prompt = SlackBotPrompt(
... ai_name="Assistant",
... ai_role="Helper",
... tools=[],
... token_counter=lambda x: len(x.split())
... )
>>> base = prompt.construct_base_prompt()
>>> print(base.startswith("You are"))
True
"""
full_prompt = f"You are a friendly assistent bot called"
f" {self.ai_name}\n\n"
full_prompt += f"\n\n{get_prompt(self.tools)}"
logger.debug(full_prompt)
return full_prompt
[docs]
def process_chat_history(self, messages: List[dict]) -> List[BaseMessage]:
"""Process raw chat history into formatted message objects.
This method converts raw chat messages into appropriate message objects
(AIMessage or HumanMessage) and handles message formatting.
Args:
messages (List[dict]): List of raw chat messages, each containing:
- type (str): Message type ("message" or "text")
- user (str): User identifier
- text (str): Message content
Returns:
List[BaseMessage]: List of processed message objects.
Example:
>>> prompt = SlackBotPrompt(
... ai_name="Assistant",
... ai_role="Helper",
... tools=[],
... token_counter=lambda x: len(x.split())
... )
>>> raw_messages = [
... {"type": "message", "user": "user1", "text": "Hello"},
... {"type": "message", "user": "assistant", "text": "Hi"}
... ]
>>> processed = prompt.process_chat_history(raw_messages)
>>> print(len(processed))
2
"""
results = []
for message in messages:
logger.debug(message)
if message["type"] != "message" and message["type"] != "text":
continue
message_cls = AIMessage if message["user"] == self.ai_id else HumanMessage
# replace the at in the message with the name of the bot
text = message["text"].replace(
f"@{self.ai_id}", f"@{self.ai_name}")
results.append(message_cls(content=text))
return results