Spanicin/agentic-ai
0
1import gradio as gr2import logging, os, sys, threading, time3 4from agent_langchain import agent_langchain5#from agent_llamaindex import agent_llamaindex6from openai import OpenAI7from trace import trace_wandb8 9from dotenv import load_dotenv, find_dotenv10_ = load_dotenv(find_dotenv())11 12lock = threading.Lock()13 14AGENT_OFF = "Off"15AGENT_LANGCHAIN = "LangChain"16AGENT_LLAMAINDEX = "LlamaIndex"17 18config = {19 "model": "gpt-4o-mini",20 "temperature": 021}22 23logging.basicConfig(stream = sys.stdout, level = logging.DEBUG)24logging.getLogger().addHandler(logging.StreamHandler(stream = sys.stdout))25 26def invoke(openai_api_key, prompt, agent_option):27 if not openai_api_key:28 raise gr.Error("OpenAI API Key is required.")29 if not prompt:30 raise gr.Error("Prompt is required.")31 if not agent_option:32 raise gr.Error("Use Agent is required.")33 34 with lock:35 os.environ["OPENAI_API_KEY"] = openai_api_key36 37 completion = ""38 result = ""39 callback = ""40 err_msg = ""41 42 try:43 start_time_ms = round(time.time() * 1000)44 45 if (agent_option == AGENT_LANGCHAIN):46 completion, callback = agent_langchain(47 config,48 prompt49 )50 51 result = completion["output"]52 #elif (agent_option == AGENT_LLAMAINDEX):53 # result = agent_llamaindex(54 # config,55 # prompt56 # )57 else:58 client = OpenAI()59 60 completion = client.chat.completions.create(61 messages = [{"role": "user", "content": prompt}],62 model = config["model"],63 temperature = config["temperature"]64 )65 66 callback = completion.usage67 result = completion.choices[0].message.content68 except Exception as e:69 err_msg = e70 71 raise gr.Error(e)72 finally:73 end_time_ms = round(time.time() * 1000)74 75 trace_wandb(76 config,77 agent_option,78 prompt, 79 completion, 80 result,81 callback,82 err_msg, 83 start_time_ms, 84 end_time_ms85 )86 87 del os.environ["OPENAI_API_KEY"]88 89 #print("===")90 #print(result)91 #print("===")92 93 return result94 95gr.close_all()96 97demo = gr.Interface(98 fn = invoke, 99 inputs = [gr.Textbox(label = "OpenAI API Key", type = "password", lines = 1),100 gr.Textbox(label = "Prompt", lines = 1, 101 value = "How does current weather in San Francisco and Paris compare in metric and imperial system? Answer in JSON format and include today's date."),102 gr.Radio([AGENT_OFF, AGENT_LANGCHAIN, AGENT_LLAMAINDEX], label = "Use Agent", value = AGENT_LANGCHAIN)],103 outputs = [gr.Markdown(label = "Completion", value=os.environ["OUTPUT"])],104 title = "Agentic Reasoning Application",105 description = os.environ["DESCRIPTION"]106)107 108demo.launch()