dlaima commited on
Commit
3b64660
·
verified ·
1 Parent(s): 4c4f363

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -1,11 +1,13 @@
 
1
  import gradio as gr
2
- from langchain.agents import AgentExecutor
3
  from langchain.chat_models import ChatOpenAI
4
  from langchain.memory import ConversationBufferMemory
5
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
6
  from langchain.tools import Tool
7
 
8
  # Define the tool
 
9
  def create_your_own(query: str) -> str:
10
  """This function can do whatever you would like once you fill it in"""
11
  return query[::-1]
@@ -28,25 +30,28 @@ tools = [
28
  class cbfs:
29
 
30
  def __init__(self, tools):
31
- self.panels = []
32
- self.functions = [tool.func for tool in tools]
33
- self.model = ChatOpenAI(temperature=0)
34
  self.memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history")
35
  self.prompt = ChatPromptTemplate.from_messages([
36
- ("system", "You are helpful but sassy assistant"),
37
  MessagesPlaceholder(variable_name="chat_history"),
38
  ("user", "{input}"),
39
  MessagesPlaceholder(variable_name="agent_scratchpad")
40
  ])
41
- self.chain = self.model.bind_tools(tools) # Connecting the tools to the model
42
- self.qa = AgentExecutor(agent=self.chain, tools=tools, verbose=False, memory=self.memory)
 
 
 
 
 
 
43
 
44
  def convchain(self, query):
45
  if not query:
46
  return "Please enter a query."
47
- result = self.qa.invoke({"input": query})
48
- self.answer = result['output']
49
- return self.answer
50
 
51
  # Create an instance of the agent
52
  cb = cbfs(tools)
@@ -60,7 +65,9 @@ with gr.Blocks() as demo:
60
  with gr.Row():
61
  inp = gr.Textbox(placeholder="Enter text here…", label="User Input")
62
  output = gr.Textbox(placeholder="Response...", label="ChatBot Output", interactive=False)
63
- inp.submit(process_query, inputs=inp, outputs=output)
64
 
 
 
65
  demo.launch(share=True)
66
 
 
 
1
+ import os
2
  import gradio as gr
3
+ from langchain.agents import initialize_agent
4
  from langchain.chat_models import ChatOpenAI
5
  from langchain.memory import ConversationBufferMemory
6
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
7
  from langchain.tools import Tool
8
 
9
  # Define the tool
10
+
11
  def create_your_own(query: str) -> str:
12
  """This function can do whatever you would like once you fill it in"""
13
  return query[::-1]
 
30
  class cbfs:
31
 
32
  def __init__(self, tools):
33
+ self.model = ChatOpenAI(temperature=0, openai_api_key=os.getenv("OPENAI_API_KEY"))
 
 
34
  self.memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history")
35
  self.prompt = ChatPromptTemplate.from_messages([
36
+ ("system", "You are a helpful but sassy assistant"),
37
  MessagesPlaceholder(variable_name="chat_history"),
38
  ("user", "{input}"),
39
  MessagesPlaceholder(variable_name="agent_scratchpad")
40
  ])
41
+
42
+ self.chain = initialize_agent(
43
+ tools=tools,
44
+ llm=self.model,
45
+ agent="chat-conversational-react-description",
46
+ memory=self.memory,
47
+ verbose=True
48
+ )
49
 
50
  def convchain(self, query):
51
  if not query:
52
  return "Please enter a query."
53
+ result = self.chain.invoke({"input": query})
54
+ return result.get('output_text', "No response generated.")
 
55
 
56
  # Create an instance of the agent
57
  cb = cbfs(tools)
 
65
  with gr.Row():
66
  inp = gr.Textbox(placeholder="Enter text here…", label="User Input")
67
  output = gr.Textbox(placeholder="Response...", label="ChatBot Output", interactive=False)
 
68
 
69
+ inp.submit(process_query, inputs=inp, outputs=output)
70
+
71
  demo.launch(share=True)
72
 
73
+