llm_generic / app.py
uijnbh's picture
Invoke lancghain_openai
f02d208
Raw History Blame Contribute Delete
2.12 kB
from langchain_openai import ChatOpenAI
from langchain.schema import AIMessage, HumanMessage
import openai
import gradio as gr
import datetime
import json
from pathlib import Path
llm = ChatOpenAI(model_name='gpt-4-turbo-preview')
counter_file_path = Path("process_counter.json")
daily_API_limit=50
def read_counter_file():
if counter_file_path.exists():
with open(counter_file_path, "r") as file:
return json.load(file)
else:
return {"count": 0, "last_run_date": str(datetime.date.today())}
def write_counter_file(data):
with open(counter_file_path, "w") as file:
json.dump(data, file)
def reset_counter_if_needed(data):
last_run_date = datetime.datetime.strptime(data["last_run_date"], "%Y-%m-%d").date()
today = datetime.date.today()
if last_run_date < today:
data["count"] = 0
data["last_run_date"] = str(today)
return data
def check_daily_limit(data, daily_limit=daily_API_limit):
if data["count"] >= daily_limit:
return True # Daily limit reached
else:
return False # Daily limit not reached
def check_daily_limit_reached():
data = read_counter_file()
data = reset_counter_if_needed(data)
daily_limit_reached = check_daily_limit(data)
if not daily_limit_reached:
data["count"] += 1 # Increment the process run count
daily_limit_reached = check_daily_limit(data) # Re-check after incrementing
write_counter_file(data) # Save the updated count and date
return daily_limit_reached
def predict(message, history):
history_langchain_format = []
for human, ai in history:
history_langchain_format.append(HumanMessage(content=human))
history_langchain_format.append(AIMessage(content=ai))
history_langchain_format.append(HumanMessage(content=message))
daily_limit_reached=check_daily_limit_reached()
if daily_limit_reached:
return("Daily limit reached. Please try again tomorrow.")
else:
gpt_response = llm(history_langchain_format)
return gpt_response.content
gr.ChatInterface(predict).launch()