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

[WIP] forward rate limit warnings to user #67

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
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
18 changes: 18 additions & 0 deletions links/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
|
"""
import hashlib
import logging
import os
import re
import tiktoken
Expand Down Expand Up @@ -96,6 +97,12 @@ def stream_llm(prompt):


def _llm_thread(g, prompt):
logger = logging.getLogger()
handler = logging.StreamHandler(g)
handler.setLevel(logging.WARNING)
handler.setFormatter(logging.Formatter("%(message)s"))
logger.addHandler(handler)

try:
llm = ChatOpenAI(
openai_api_key=get_openai_key(),
Expand All @@ -104,11 +111,16 @@ def _llm_thread(g, prompt):
streaming=True,
callbacks=[StreamingHandler(g)],
)

# https://github.com/hwchase17/langchain/blob/aec642febb3daa7dbb6a19996aac2efa92bbf1bd/langchain/chat_models/openai.py#L79
logger.warning("THIS IS A WARNING")

llm.call_as_llm(prompt)
except Exception as e:
g.send(e)
finally:
g.close()
logger.removeHandler(handler)


def query_retriever(retriever, prompt_template, query):
Expand Down Expand Up @@ -221,6 +233,12 @@ def __next__(self):
def send(self, data):
self.queue.put(data)

def write(self, data):
self.queue.put(Warning(data))

def flush(self):
pass

def close(self):
self.queue.put(StopIteration)

Expand Down