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

Improve error handling #3

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
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
32 changes: 19 additions & 13 deletions alttexter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import mimetypes
import os
import time
from typing import List
from typing import List, Optional, Tuple

from langchain import callbacks
from langchain.callbacks.tracers.langchain import wait_for_all_tracers
Expand All @@ -15,7 +15,8 @@
from schema import AlttexterResponse, ImageAltText


def alttexter(input_text: str, images: dict, image_urls: List[str]) -> List[ImageAltText]:
def alttexter(input_text: str, images: dict, image_urls: List[str]) -> Tuple[List[ImageAltText], Optional[str]]:

"""
Processes input text and images to generate alt text and title attributes.

Expand Down Expand Up @@ -99,27 +100,32 @@ def alttexter(input_text: str, images: dict, image_urls: List[str]) -> List[Imag
tracing_enabled = os.getenv("LANGCHAIN_TRACING_V2", "").lower() == "true"
if tracing_enabled:
client = Client()
with callbacks.collect_runs() as cb:
try:
try:
with callbacks.collect_runs() as cb:
alttexts = llm.invoke(messages.format_messages())
finally:

# Ensure that all tracers complete their execution
wait_for_all_tracers()

if alttexts:
try:
if alttexts:
# Get public URL for run
run_id = cb.traced_runs[0].id
time.sleep(2)
client.share_run(run_id)
run_url = client.read_run_shared_link(run_id)
except Exception as e:
logging.error(f"LangSmith API error: {str(e)}")
except Exception as e:
logging.error(f"Error during LLM invocation with tracing: {str(e)}")
else:
try:
alttexts = llm(messages.format_messages())
alttexts = llm.invoke(messages.format_messages())
except Exception as e:
logging.error(f"Error during LLM invocation without tracing: {str(e)}")

if alttexts:
try:
alttexts_parsed = parser.parse(alttexts.content)
return alttexts_parsed, run_url
except Exception as e:
raise Exception(f"Error during LLM invocation without tracing: {e}")
logging.error(f"Error parsing LLM response: {str(e)}")

alttexts_parsed = parser.parse(alttexts.content) if alttexts else None
return alttexts_parsed, run_url
return None, run_url
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ def alttexter_text(
if (is_valid_notebook(text)):
text = remove_outputs_from_notebook(text)

# Send to LLM
alttexts_response, run_url = alttexter(text, images, image_urls)
if alttexts_response is None:
raise Exception("Failed to generate alt texts. Please try again later.")

return ExtendedAlttexterResponse(images=alttexts_response.images, run_url=run_url)

Expand Down
Loading