Johnny Lee commited on
Commit
221cc78
·
1 Parent(s): ef56439

async fixes

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -2,12 +2,14 @@
2
  from __future__ import annotations
3
  import asyncio
4
  import datetime
5
- import pytz
6
  import logging
7
  import os
8
  from enum import Enum
9
  import json
10
  import uuid
 
 
 
11
  from pydantic import BaseModel
12
  import gspread
13
 
@@ -23,6 +25,7 @@ import tiktoken
23
 
24
  from langchain.callbacks.streaming_aiter import AsyncIteratorCallbackHandler
25
  from langchain.callbacks.tracers.run_collector import RunCollectorCallbackHandler
 
26
  from langchain.chains import ConversationChain
27
  from langsmith import Client
28
  from langchain.chat_models import ChatAnthropic, ChatOpenAI
@@ -40,6 +43,8 @@ logging.basicConfig(format="%(asctime)s %(name)s %(levelname)s:%(message)s")
40
  LOG = logging.getLogger(__name__)
41
  LOG.setLevel(logging.INFO)
42
 
 
 
43
  GPT_3_5_CONTEXT_LENGTH = 4096
44
  CLAUDE_2_CONTEXT_LENGTH = 100000 # need to use claude tokenizer
45
 
@@ -464,7 +469,7 @@ async def respond(
464
  state.chain.apredict(
465
  input=chat_input,
466
  callbacks=[callback, run_collector],
467
- )
468
  )
469
  state.history.append((chat_input, ""))
470
  run_id = None
@@ -474,7 +479,11 @@ async def respond(
474
  bot += tok
475
  state.history[-1] = (user, bot)
476
  yield state.history, state, None
477
- await run
 
 
 
 
478
  if run_collector.traced_runs and run_id is None:
479
  run_id = run_collector.traced_runs[0].id
480
  LOG.info(f"RUNID: {run_id}")
@@ -495,7 +504,7 @@ async def respond(
495
  ):
496
  url_markdown += """\n
497
  🙌 You have completed 10 exchanges with the chatbot."""
498
- yield state.history, state, url_markdown
499
  LOG.info(f"""[{request.username}] ENDING CHAIN""")
500
  LOG.debug(f"History: {state.history}")
501
  LOG.debug(f"Memory: {state.chain.memory.json()}")
@@ -519,12 +528,13 @@ async def respond(
519
  gsheet_row = [[timestamp_string, *metadata_to_gsheet, langsmith_url]]
520
  LOG.info(f"Data to GSHEET: {gsheet_row}")
521
  try:
522
- append_gsheet_rows(
523
- sheet_id=GSHEET_ID,
524
- sheet_name=TURNS_GSHEET_NAME,
525
- rows=gsheet_row,
526
- creds=GS_CREDS,
527
- )
 
528
  except Exception as exc:
529
  LOG.error(f"Failed to log entry to Google Sheet. Row {gsheet_row}")
530
  LOG.error(exc)
@@ -657,4 +667,4 @@ with gr.Blocks(
657
  chat_submit_button.click(**clear_chatbot_messages_params)
658
  input_message.submit(**clear_chatbot_messages_params)
659
 
660
- demo.queue(max_size=99, concurrency_count=25, api_open=False).launch(auth=auth)
 
2
  from __future__ import annotations
3
  import asyncio
4
  import datetime
 
5
  import logging
6
  import os
7
  from enum import Enum
8
  import json
9
  import uuid
10
+ import threading
11
+
12
+ import pytz
13
  from pydantic import BaseModel
14
  import gspread
15
 
 
25
 
26
  from langchain.callbacks.streaming_aiter import AsyncIteratorCallbackHandler
27
  from langchain.callbacks.tracers.run_collector import RunCollectorCallbackHandler
28
+ from langchain.callbacks.tracers.langchain import wait_for_all_tracers
29
  from langchain.chains import ConversationChain
30
  from langsmith import Client
31
  from langchain.chat_models import ChatAnthropic, ChatOpenAI
 
43
  LOG = logging.getLogger(__name__)
44
  LOG.setLevel(logging.INFO)
45
 
46
+ thread_lock = threading.Lock()
47
+
48
  GPT_3_5_CONTEXT_LENGTH = 4096
49
  CLAUDE_2_CONTEXT_LENGTH = 100000 # need to use claude tokenizer
50
 
 
469
  state.chain.apredict(
470
  input=chat_input,
471
  callbacks=[callback, run_collector],
472
+ ),
473
  )
474
  state.history.append((chat_input, ""))
475
  run_id = None
 
479
  bot += tok
480
  state.history[-1] = (user, bot)
481
  yield state.history, state, None
482
+ complete_response = await run
483
+ wait_for_all_tracers()
484
+ user, _ = state.history[-1]
485
+ state.history[-1] = (user, complete_response)
486
+ url_markdown = None
487
  if run_collector.traced_runs and run_id is None:
488
  run_id = run_collector.traced_runs[0].id
489
  LOG.info(f"RUNID: {run_id}")
 
504
  ):
505
  url_markdown += """\n
506
  🙌 You have completed 10 exchanges with the chatbot."""
507
+ yield state.history, state, url_markdown
508
  LOG.info(f"""[{request.username}] ENDING CHAIN""")
509
  LOG.debug(f"History: {state.history}")
510
  LOG.debug(f"Memory: {state.chain.memory.json()}")
 
528
  gsheet_row = [[timestamp_string, *metadata_to_gsheet, langsmith_url]]
529
  LOG.info(f"Data to GSHEET: {gsheet_row}")
530
  try:
531
+ with thread_lock:
532
+ append_gsheet_rows(
533
+ sheet_id=GSHEET_ID,
534
+ sheet_name=TURNS_GSHEET_NAME,
535
+ rows=gsheet_row,
536
+ creds=GS_CREDS,
537
+ )
538
  except Exception as exc:
539
  LOG.error(f"Failed to log entry to Google Sheet. Row {gsheet_row}")
540
  LOG.error(exc)
 
667
  chat_submit_button.click(**clear_chatbot_messages_params)
668
  input_message.submit(**clear_chatbot_messages_params)
669
 
670
+ demo.queue(max_size=25, concurrency_count=16, api_open=False).launch(auth=auth)