-
Notifications
You must be signed in to change notification settings - Fork 0
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
POC: AI #65
base: main
Are you sure you want to change the base?
POC: AI #65
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from discord import app_commands | ||
import discord | ||
from modules.ai.llm import get_response, list_models | ||
from utils.splitter import split_text | ||
|
||
def register_commands( | ||
tree: discord.app_commands.CommandTree, | ||
guilds: list[discord.Object], | ||
): | ||
@tree.command( | ||
name="llm", | ||
description="Ask the AI a question", | ||
guilds=guilds, | ||
) | ||
@app_commands.describe(question="The question you want to ask the AI") | ||
async def llm(interaction: discord.Interaction, question: str, model: str = ""): | ||
await interaction.response.defer() | ||
try: | ||
response = get_response(question, model) | ||
split_response = split_text(response) | ||
for chunk in split_response: | ||
await interaction.followup.send(chunk) | ||
except Exception as e: | ||
await interaction.followup.send(f"An error occurred: {e}") | ||
|
||
@tree.command( | ||
name="llm_models", | ||
description="List available models", | ||
guilds=guilds, | ||
) | ||
async def llm_models(interaction: discord.Interaction): | ||
await interaction.response.defer() | ||
try: | ||
models = list_models() | ||
await interaction.followup.send("\n".join(models)) | ||
except Exception as e: | ||
await interaction.followup.send(f"An error occurred: {e}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will need to fix integration tests for this lol (monkeypatch away |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import requests | ||
from openai import OpenAI | ||
from openai.types.chat import ChatCompletion | ||
import os | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
openai = OpenAI( | ||
base_url=os.getenv("OPENAI_BASE_URL"), | ||
api_key=os.getenv("OPENAI_API_KEY") | ||
) | ||
|
||
def list_models() -> list[str]: | ||
return [model.id for model in openai.models.list().data] | ||
|
||
def get_response(prompt: str, model: str = "") -> str: | ||
completion: ChatCompletion = openai.chat.completions.create( | ||
model=model if model else os.getenv("DEFAULT_LLM_MODEL"), | ||
messages=[ | ||
{"role": "user", "content": prompt} | ||
], | ||
stream=False | ||
) | ||
return completion.choices[0].message.content | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs error handling pain (a try catch maybe) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try this one-liner Or you could use |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
|
||
def split_text(text: str, max_length: int = 2000) -> list[str]: | ||
chunks = [] | ||
current_chunk = "" | ||
|
||
for line in text.splitlines(keepends=True): # keepends=True keeps the newline characters | ||
if len(current_chunk) + len(line) <= max_length: | ||
current_chunk += line | ||
else: | ||
chunks.append(current_chunk) | ||
current_chunk = line | ||
|
||
if current_chunk: # Append any remaining text | ||
chunks.append(current_chunk) | ||
|
||
return chunks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choice
s