Skip to content

Commit

Permalink
chat(): bugfix token counting, rename _chat_conversation_size to `_…
Browse files Browse the repository at this point in the history
…chat_tokens_count`
  • Loading branch information
deedy5 committed Jul 24, 2024
1 parent ee0d9d9 commit 8e0a0ff
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion duckduckgo_search/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def chat(load, proxy, multiline, timeout, model):
client._chat_messages = cache.get("messages", [])

while True:
print(f"{'-'*78}\nYou[{model=} contextsize={client._chat_conversation_size}]: ", end="")
print(f"{'-'*78}\nYou[{model=} tokens={client._chat_tokens_count}]: ", end="")
if multiline:
print(f"""[multiline, send message: ctrl+{"Z" if sys.platform == "win32" else "D"}]""")
user_input = sys.stdin.read()
Expand Down
8 changes: 5 additions & 3 deletions duckduckgo_search/duckduckgo_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def __init__(
verify=False,
)
self._exception_event = Event()
self._chat_conversation_size = 0
self._chat_messages: List[Dict[str, str]] = []
self._chat_tokens_count = 0
self._chat_vqd: str = ""

def __enter__(self) -> "DDGS":
Expand Down Expand Up @@ -155,6 +155,7 @@ def chat(self, keywords: str, model: str = "gpt-3.5", timeout: int = 20) -> str:
self._chat_vqd = resp.headers.get("x-vqd-4", "")

self._chat_messages.append({"role": "user", "content": keywords})
self._chat_tokens_count += len(keywords) // 4 if len(keywords) >= 4 else 1 # approximate number of tokens

json_data = {
"model": models[model],
Expand Down Expand Up @@ -182,11 +183,12 @@ def chat(self, keywords: str, model: str = "gpt-3.5", timeout: int = 20) -> str:
else RatelimitException(err_message)
)
raise DuckDuckGoSearchException(err_message)
results.append(x.get("message", ""))
elif message := x.get("message"):
results.append(message)
result = "".join(results)

self._chat_messages.append({"role": "assistant", "content": result})
self._chat_conversation_size = sum(len(d["content"]) for d in self._chat_messages)
self._chat_tokens_count += len(results)
return result

def text(
Expand Down

0 comments on commit 8e0a0ff

Please sign in to comment.