Skip to content

Commit

Permalink
Instead of using the OpenAI library directly, use phidata
Browse files Browse the repository at this point in the history
  • Loading branch information
slashtechno committed Jun 1, 2024
1 parent df1507e commit bf50364
Show file tree
Hide file tree
Showing 3 changed files with 383 additions and 16 deletions.
24 changes: 13 additions & 11 deletions llmail/utils/responding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import yagmail
from imapclient import IMAPClient
from openai import OpenAI
from phi.assistant import Assistant
from phi.llm.openai.like import OpenAILike


# Uses utils/__init__.py to import from utils/logging.py and utils/cli_args.py respectively
from llmail.utils import logger, args, bot_email
Expand Down Expand Up @@ -164,9 +167,14 @@ def fetch_and_process_emails(
msg_id=msg_id,
message_id=message_id,
references_ids=references_ids,
openai=openai,
assistant=Assistant(
llm=OpenAILike(
model=args.openai_model,
api_key=openai.api_key,
base_url=args.openai_base_url,
)
),
system_prompt=system_prompt,
model=args.openai_model,
)

logger.info(f"Current number of email threads: {len(email_threads.keys())}")
Expand All @@ -180,9 +188,8 @@ def send_reply(
msg_id: int,
message_id: str,
references_ids: list[str],
openai: OpenAI,
assistant: Assistant,
system_prompt: str,
model: str,
):
"""Send a reply to the email with the specified message ID."""
# Set roles deletes the sender key so we need to store the sender before calling it
Expand All @@ -191,13 +198,8 @@ def send_reply(
if system_prompt:
thread.insert(0, {"role": "system", "content": system_prompt})
references_ids.append(message_id)
generated_response = (
openai.chat.completions.create(
model=model,
messages=thread,
)
.choices[0]
.message.content
generated_response = assistant.run(
messages=thread, stream=False
)
logger.debug(f"Generated response: {generated_response}")
yag = yagmail.SMTP(
Expand Down
Loading

0 comments on commit bf50364

Please sign in to comment.