Skip to content

Commit

Permalink
Bump version to 4.2.0 and update dependencies; add methods for trunca…
Browse files Browse the repository at this point in the history
…ted chat history
  • Loading branch information
schleising committed Nov 17, 2024
1 parent e565ce7 commit 5bb02ad
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 33 deletions.
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "simple-openai"
version = "4.1.1"
version = "4.2.0"
description = "Simple OpenAI API wrapper"
readme = "README.md"
authors = [{ name = "Stephen Schleising", email = "[email protected]" }]
Expand All @@ -18,6 +18,8 @@ classifiers = [
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Topic :: Software Development :: Libraries",
"Topic :: Software Development :: Libraries :: Python Modules"
]
Expand Down
55 changes: 28 additions & 27 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
aiohappyeyeballs==2.4.0
aiohttp==3.10.5
aiohappyeyeballs==2.4.3
aiohttp==3.11.2
aiosignal==1.3.1
annotated-types==0.7.0
async-timeout==4.0.3
async-timeout==5.0.1
attrs==24.2.0
babel==2.16.0
certifi==2024.7.4
cffi==1.17.0
charset-normalizer==3.3.2
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.4.0
click==8.1.7
colorama==0.4.6
cryptography==43.0.0
frozenlist==1.4.1
cryptography==43.0.3
frozenlist==1.5.0
ghp-import==2.1.0
griffe==1.2.0
idna==3.8
griffe==1.5.1
idna==3.10
Jinja2==3.1.4
Markdown==3.7
MarkupSafe==2.1.5
MarkupSafe==3.0.2
mergedeep==1.3.4
mkdocs==1.6.0
mkdocs-autorefs==1.1.0
mkdocs==1.6.1
mkdocs-autorefs==1.2.0
mkdocs-get-deps==0.2.0
mkdocs-material==9.5.33
mkdocs-material==9.5.44
mkdocs-material-extensions==1.3.1
mkdocstrings==0.25.2
mkdocstrings-python==1.10.8
multidict==6.0.5
packaging==24.1
mkdocstrings==0.27.0
mkdocstrings-python==1.12.2
multidict==6.1.0
packaging==24.2
paginate==0.5.7
pathspec==0.12.1
platformdirs==4.2.2
platformdirs==4.3.6
propcache==0.2.0
pycparser==2.22
pydantic==2.8.2
pydantic_core==2.20.1
pydantic==2.9.2
pydantic_core==2.23.4
Pygments==2.18.0
pymdown-extensions==10.9
pymdown-extensions==10.12
python-dateutil==2.9.0.post0
PyYAML==6.0.2
pyyaml_env_tag==0.1
regex==2024.7.24
regex==2024.11.6
requests==2.32.3
simple-openai==4.1.0
simple-openai==4.1.1
six==1.16.0
typing_extensions==4.12.2
urllib3==2.2.2
watchdog==5.0.0
yarl==1.9.4
urllib3==2.2.3
watchdog==6.0.0
yarl==1.17.1
16 changes: 16 additions & 0 deletions src/simple_openai/async_simple_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from pathlib import Path
from typing import Callable

import aiohttp

from . import constants
Expand Down Expand Up @@ -348,3 +349,18 @@ def get_chat_history(self, chat_id: str) -> str:

# Return the chat history
return chat_history

def get_truncated_chat_history(self, chat_id: str) -> str:
"""Get the truncated chat history, limited to the last 4,000 characters
Args:
chat_id (str): The ID of the chat
Returns:
str: The truncated chat history
"""
# Get the chat history
chat_history = self._chat.get_truncated_chat(chat_id)

# Return the chat history
return chat_history
31 changes: 27 additions & 4 deletions src/simple_openai/chat_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,37 @@ def get_chat(self, chat_id: str = DEFAULT_CHAT_ID) -> str:
# If the chat ID is not in the messages, create a new deque
if chat_id not in self._messages:
return ""

# Get the chat
chat = self._messages[chat_id]

# Parse the most recent 10 chat messages to a string with each name and message on a new line
chat_str = "\n".join(
[f"{message.name}: {message.content}" for message in list(chat)[-10:]]
)
chat_str = "\n".join([f"{message.name}: {message.content}" for message in chat])

# Return the chat
return chat_str

def get_truncated_chat(self, chat_id: str = DEFAULT_CHAT_ID) -> str:
"""Get the truncated chat, limited to the last 4,000 characters
Args:
chat_id (str, optional): The ID of the chat to get. Defaults to DEFAULT_CHAT_ID.
Returns:
str: The truncated chat
"""
# If the chat ID is not in the messages, create a new deque
if chat_id not in self._messages:
return ""

# Get the chat
chat = self._messages[chat_id]

# Parse the most recent 10 chat messages to a string with each name and message on a new line
chat_str = "\n".join([f"{message.name}: {message.content}" for message in chat])

# Get the last 4,000 characters of the chat
chat_str = chat_str[-4000:]

# Return the chat
return chat_str
16 changes: 15 additions & 1 deletion src/simple_openai/simple_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ def get_image_url(self, prompt: str, style: str = "vivid") -> SimpleOpenaiRespon
# Return the response
return response


def get_chat_history(self, chat_id: str) -> str:
"""Get the chat history
Expand All @@ -329,3 +328,18 @@ def get_chat_history(self, chat_id: str) -> str:

# Return the chat history
return chat_history

def get_truncated_chat_history(self, chat_id: str) -> str:
"""Get the truncated chat history, limited to the last 4,000 characters
Args:
chat_id (str): The ID of the chat
Returns:
str: The truncated chat history
"""
# Get the chat history
chat_history = self._chat.get_truncated_chat(chat_id)

# Return the chat history
return chat_history

0 comments on commit 5bb02ad

Please sign in to comment.