Skip to content

Commit

Permalink
POC of forwarding warnings to user
Browse files Browse the repository at this point in the history
  • Loading branch information
brimoor committed Jun 7, 2023
1 parent 0802a34 commit 8b437c1
Showing 1 changed file with 18 additions and 0 deletions.
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

0 comments on commit 8b437c1

Please sign in to comment.