Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a /query command. #28

Merged
merged 7 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 31, 2025
commit 2c62a827bd96b5d84fa5e3bab868e5d323eae9cc
9 changes: 3 additions & 6 deletions app/discord_message_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def split_message_into_chunks(message: str, *, max_length: int) -> list[str]:
return [message[:split_index]] + split_message_into_chunks(
message[split_index:], max_length=max_length
)


def smart_split_message_into_chunks(message: str, *, max_length: int) -> list[str]:
"""Like `split_message_into_chunks`, but also considers code blocks."""
Expand All @@ -53,13 +53,10 @@ def smart_split_message_into_chunks(message: str, *, max_length: int) -> list[st
message_chunk = f"```{code_block_language}\n" + message_chunk
code_block_language = None

code_block_language = (
get_unclosed_code_block_language(message_chunk)
)
code_block_language = get_unclosed_code_block_language(message_chunk)
if code_block_language is not None:
message_chunk += "\n```"

output_messages.append(message_chunk)

return output_messages

return output_messages
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if we should use this everywhere?

2 changes: 2 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,11 @@ async def transcript(
file=discord.File(f, filename="transcript.txt"),
)


TRUNCATION_SUFFIX = " (truncated)"
TRUNCATION_SUFFIX_LENGTH = len(TRUNCATION_SUFFIX)


@command_tree.command(name=command_name("query"))
async def query(
interaction: discord.Interaction,
Expand Down
25 changes: 16 additions & 9 deletions app/usecases/ai_conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ class _GptRequestResponse(NamedTuple):
input_tokens: int
output_tokens: int

async def _make_gpt_request(message_history: list[gpt.Message], model: gpt.OpenAIModel) -> _GptRequestResponse | Error:

async def _make_gpt_request(
message_history: list[gpt.Message], model: gpt.OpenAIModel
) -> _GptRequestResponse | Error:
functions = openai_functions.get_full_openai_functions_schema()
try:
gpt_response = await gpt.send(
Expand Down Expand Up @@ -243,9 +246,11 @@ async def send_message_to_thread(
return gpt_response

# Handle code blocks which may exceed the previous message.
response_messages: list[str] = discord_message_utils.smart_split_message_into_chunks(
gpt_response.response_content,
max_length=2000,
response_messages: list[str] = (
discord_message_utils.smart_split_message_into_chunks(
gpt_response.response_content,
max_length=2000,
)
)

await thread_messages.create(
Expand Down Expand Up @@ -289,7 +294,7 @@ async def send_message_without_context(
code=ErrorCode.UNAUTHORIZED,
messages=["User is not authorised to use this bot"],
)

prompt = f"{interaction.user.name}: {message_content}"

user_messages: list[MessageContent] = [
Expand All @@ -308,11 +313,13 @@ async def send_message_without_context(
gpt_response = await _make_gpt_request(message_context, model)
if isinstance(gpt_response, Error):
return gpt_response

# TODO: Track input and output tokens here.

response_messages: list[str] = discord_message_utils.smart_split_message_into_chunks(
gpt_response.response_content,
max_length=2000,
response_messages: list[str] = (
discord_message_utils.smart_split_message_into_chunks(
gpt_response.response_content,
max_length=2000,
)
)
return SendAndReceiveResponse(response_messages=response_messages)